Exemplo n.º 1
0
        public static Dictionary <I, T> LoadIndex <I, T>(
            string path,
            IIndexInfo <I> indexInfo,
            out List <EntityIndex <T> > entities
            ) where T : class, ISerializable
        {
            var map = new Dictionary <I, T>();

            object[] ctorArgs = new object[1];

            var indexType = indexInfo.TypeName;

            string indexPath = Path.Combine(path, indexType, $"{indexType}.idx");
            string typesPath = Path.Combine(path, indexType, $"{indexType}.tdb");

            entities = new List <EntityIndex <T> >();

            if (!File.Exists(indexPath) || !File.Exists(typesPath))
            {
                return(map);
            }

            using FileStream idx = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryReader idxReader = new BinaryReader(idx);

            using FileStream tdb = new FileStream(typesPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryReader tdbReader = new BinaryReader(tdb);

            List <Tuple <ConstructorInfo, string> > types = ReadTypes <I>(tdbReader);

            var count = idxReader.ReadInt32();

            for (int i = 0; i < count; ++i)
            {
                var typeID = idxReader.ReadInt32();
                var number = idxReader.ReadUInt32();
                var pos    = idxReader.ReadInt64();
                var length = idxReader.ReadInt32();

                Tuple <ConstructorInfo, string> objs = types[typeID];

                if (objs == null)
                {
                    continue;
                }

                T t;
                ConstructorInfo ctor = objs.Item1;
                I indexer            = indexInfo.CreateIndex(number);

                ctorArgs[0] = indexer;
                t           = ctor.Invoke(ctorArgs) as T;

                if (t != null)
                {
                    entities.Add(new EntityIndex <T>(t, typeID, pos, length));
                    map[indexer] = t;
                }
            }

            tdbReader.Close();
            idxReader.Close();

            return(map);
        }
Exemplo n.º 2
0
        public static Dictionary <I, T> LoadIndex <I, T>(
            string path,
            IIndexInfo <I> indexInfo,
            out List <EntityIndex <T> > entities
            ) where T : class, ISerializable
        {
            var map = new Dictionary <I, T>();

            object[] ctorArgs = new object[1];

            var indexType = indexInfo.TypeName;

            string indexPath = Path.Combine(path, indexType, $"{indexType}.idx");
            string typesPath = Path.Combine(path, indexType, $"{indexType}.tdb");

            entities = new List <EntityIndex <T> >();

            if (!File.Exists(indexPath) || !File.Exists(typesPath))
            {
                return(map);
            }

            using FileStream idx = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryReader idxReader = new BinaryReader(idx);

            using FileStream tdb = new FileStream(typesPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryReader tdbReader = new BinaryReader(tdb);

            List <Tuple <ConstructorInfo, string> > types = ReadTypes <I>(tdbReader);

            int count;
            var version = idxReader.ReadInt32();

            // Handle non-versioned (version 0).
            if (version > _idxVersion || idx.Length - 4 - version * 20 == 0)
            {
                count   = version;
                version = 0;
            }
            else
            {
                count = idxReader.ReadInt32();
            }

            var now = DateTime.UtcNow;

            for (int i = 0; i < count; ++i)
            {
                var typeID         = idxReader.ReadInt32();
                var serial         = idxReader.ReadUInt32();
                var created        = version == 0 ? now : new DateTime(idxReader.ReadInt64(), DateTimeKind.Utc);
                var lastSerialized = version == 0 ? DateTime.MinValue : new DateTime(idxReader.ReadInt64(), DateTimeKind.Utc);
                var pos            = idxReader.ReadInt64();
                var length         = idxReader.ReadInt32();

                Tuple <ConstructorInfo, string> objs = types[typeID];

                if (objs == null)
                {
                    continue;
                }

                ConstructorInfo ctor = objs.Item1;
                I indexer            = indexInfo.CreateIndex(serial);

                ctorArgs[0] = indexer;

                if (ctor.Invoke(ctorArgs) is T t)
                {
                    t.Created        = created;
                    t.LastSerialized = lastSerialized;
                    entities.Add(new EntityIndex <T>(t, typeID, pos, length));
                    map[indexer] = t;
                }
            }

            tdbReader.Close();
            idxReader.Close();

            return(map);
        }