Exemple #1
0
        public DataFile(dbModel model)
        {
            Opened = false;

            m_mapPort   = MemoryMappedFile.Create(MapProtection.PageReadWrite, 4, model.Name);
            m_bytes     = new Dictionary <int, byte[]>();
            m_cacheSR   = new Dictionary <SearchRequest, SearchResult>(new SearchRequest.EqualityComparer());
            m_lockFile  = new ReaderWriterLockSlim();
            m_lockCache = new ReaderWriterLockSlim();

            m_FileName = model.Name + ".df";
            m_FilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), m_FileName);

            http_Init(model);

            try
            {
                if (File.Exists(m_FilePath))
                {
                    FileInfo fi = new FileInfo(m_FilePath);
                    m_FileSize = (int)fi.Length;
                    m_mapFile  = MemoryMappedFile.Create(m_FilePath, MapProtection.PageReadWrite, m_FileSize);
                    if (m_FileSize > m_HeaderSize)
                    {
                        byte[] buf = new byte[m_FileSize];
                        using (Stream view = m_mapFile.MapView(MapAccess.FileMapRead, 0, m_FileSize))
                            view.Read(buf, 0, m_FileSize);

                        if (bindHeader(buf) == false)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    m_Capacity = m_BlobGrowSize;
                    m_FileSize = m_HeaderSize + (m_Capacity * m_BlobSizeMax) + 1;
                    m_mapFile  = MemoryMappedFile.Create(m_FilePath, MapProtection.PageReadWrite, m_FileSize);

                    //string fs = _model.Name + ";" + string.Join(",", _model.Fields.Select(x => ((int)x.Type).ToString() + x.Name).ToArray());
                    writeHeaderBlank(model);
                }

                Opened = true;
                if (m_listItems == null)
                {
                    m_listItems = (IList)typeof(List <>).MakeGenericType(m_typeDynamic).GetConstructor(Type.EmptyTypes).Invoke(null);
                }
            }
            catch
            {
            }
        }
Exemple #2
0
        private void http_Init(dbModel model)
        {
            TcpListener l = new TcpListener(IPAddress.Loopback, 0);

            l.Start();
            Port = ((IPEndPoint)l.LocalEndpoint).Port;
            l.Stop();

            using (Stream view = m_mapPort.MapView(MapAccess.FileMapWrite, 0, 4))
                view.Write(BitConverter.GetBytes(Port), 0, 4);

            listener = new HttpListener();
            listener.Prefixes.Add("http://*:" + Port.ToString() + "/");

            listener.Start();
            listener.BeginGetContext(ProcessRequest, listener);
        }
Exemple #3
0
        private Type buildTypeDynamic(dbModel m)
        {
            if (m == null || string.IsNullOrEmpty(m.Name) || m.Fields == null || m.Fields.Length == 0)
            {
                return(null);
            }

            Type type = DynamicExpression.CreateClass(m.Fields);
            //DynamicProperty[] at = new DynamicProperty[]
            //{
            //    new DynamicProperty("Name", typeof(string)),
            //    new DynamicProperty("Birthday", typeof(DateTime))
            //};
            //object obj = Activator.CreateInstance(type);
            //t.GetProperty("Name").SetValue(obj, "Albert", null);
            //t.GetProperty("Birthday").SetValue(obj, new DateTime(1879, 3, 14), null);

            var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
            // Obtain all serializable types having no explicit proto contract
            var serializableTypes = Assembly.GetExecutingAssembly()
                                    .GetTypes()
                                    .Where(t => t.IsSerializable && !Attribute.IsDefined(t, typeof(ProtoContractAttribute)));

            var metaType = model.Add(type, false);

            metaType.AsReferenceDefault = true;
            metaType.UseConstructor     = false;

            // Add contract for all the serializable fields
            var serializableFields = type
                                     .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                     .Where(fi => !Attribute.IsDefined(fi, typeof(NonSerializedAttribute)))
                                     .OrderBy(fi => fi.Name) // it's important to keep the same fields order in all the AppDomains
                                     .Select((fi, k) => new { info = fi, index = k });

            foreach (var field in serializableFields)
            {
                var metaField = metaType.AddField(field.index + 1, field.info.Name);
                metaField.AsReference = !field.info.FieldType.IsValueType;       // cyclic references support
                metaField.DynamicType = field.info.FieldType == typeof(object);  // any type support
            }
            // Compile model in place for better performance, .Compile() can be used if all types are known beforehand
            model.CompileInPlace();

            return(type);
        }
Exemple #4
0
        private void writeHeaderBlank(dbModel m)
        {
            ////////////////////////////////////////////////
            // HEADER = [4 bytes blob len] + [8 byte ID] + [4 bytes Capacity] + [4 bytes Count] + [980 byte fields] = 1,000

            // [4 bytes blob LEN]
            byte[] _byteBlobLEN = BitConverter.GetBytes(m_BlobLEN);

            // [8 byte ID]
            m_FileID = long.Parse(DateTime.Now.ToString("yyMMddHHmmssfff"));
            byte[] _byteFileID = BitConverter.GetBytes(m_FileID).ToArray();

            // [4 bytes Capacity]
            byte[] _byteCapacity = BitConverter.GetBytes(m_Capacity);

            // [4 bytes Count]
            byte[] _byteCount = BitConverter.GetBytes(m_Count);

            // [980 byte fields]
            byte[] bm;
            using (var ms = new MemoryStream())
            {
                new JsonSerializer().Serialize(new BsonWriter(ms), m);
                bm = ms.ToArray();
            }
            byte[] bm7 = SevenZip.Compression.LZMA.SevenZipHelper.Compress(bm);

            List <byte> _byteFields = new List <byte>();

            _byteFields.AddRange(BitConverter.GetBytes(bm7.Length));
            _byteFields.AddRange(bm7);
            _byteFields.AddRange(new byte[980 - _byteFields.Count]);
            m_typeDynamic = buildTypeDynamic(m);

            List <byte> ls = new List <byte>();

            ls.AddRange(_byteBlobLEN);
            ls.AddRange(_byteFileID);
            ls.AddRange(_byteCapacity);
            ls.AddRange(_byteCount);
            ls.AddRange(_byteFields);

            using (Stream view = m_mapFile.MapView(MapAccess.FileMapWrite, 0, ls.Count))
                view.Write(ls.ToArray(), 0, ls.Count);
        }