Example #1
0
    public override void Serialize(MelDB db, bool indexOnly, bool removeUnusedFiles = true,
                                   Func <string, int, int, bool> progress           = null)
    {
        if (progress != null && progress("XML serialization: 1/3 sorting DB", 0, 1))
        {
            return;
        }
        db.Sort();
        XmlSerializer melDBSerializer = new XmlSerializer(typeof(MelDB));

        if (progress != null && progress("XML serialization: 2/3 serializing index", 0, 1))
        {
            return;
        }
        using (FileStream fDB = File.Create(GetIndexPath()))
        {
            melDBSerializer.Serialize(fDB, db);
        }

        if (!indexOnly)
        {
            XmlSerializer particleDataSerializer = new XmlSerializer(typeof(ParticleInfo.ParticleInfoData));
            int           partsCount             = db.particles.Count;

            for (int i = 0; i < partsCount; i++)
            {
                var p = db.particles[i];
                if (p.data == null)
                {
                    continue;
                }

                if (progress != null && progress("XML serialization: 3/3 serializing particles", i, partsCount))
                {
                    return;
                }

                string fullPath = GetParticlePath(p);
                p.data.metaInfo = p;

                // create directory
                string dirPath = Path.GetDirectoryName(fullPath);
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }

                using (FileStream f = File.Create(fullPath))
                {
                    particleDataSerializer.Serialize(f, p.data);
                }
                p.data.metaInfo = null;
            }
        }

        if (removeUnusedFiles)
        {
            RemoveUnusedFiles();
        }
    }
Example #2
0
    public override void Serialize(MelDB db, bool indexOnly, bool removeUnusedFiles = true,
                                   Func <string, int, int, bool> progress           = null)
    {
        if (indexOnly)
        {
            throw new Exception("Trying to binary serialize DB with index only");
        }

        var bucketPrefix = Application.dataPath + DBPath + BucketFilename;
        var dir          = Path.GetDirectoryName(bucketPrefix);

        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        if (progress != null && progress("Binary Serialization: 1/4 preparing buckets", 0, 1))
        {
            return;
        }
        var streams = new BinaryWriter[BUCKETS];

        for (int i = 0; i < BUCKETS; i++)
        {
            var s = File.Create(bucketPrefix + i.ToString("000") + ".bytes");
            streams[i] = new BinaryWriter(s, encoding);
        }
        var particles      = db.particles;
        int particlesCount = particles.Count;

        // first serialize all particles into buckets to get buckets location indexes
        for (int i = particlesCount - 1; i >= 0; i--)
        {
            if (progress != null && progress("Binary Serialization: 2/4 cleaning and serializing particles", particlesCount - i, particlesCount))
            {
                return;
            }
            var p = particles[i];
            p.Init();
            if (p.data == null | !IncludeInBuild(p))
            {
                particles.RemoveAt(i);
                continue;
            }

            var buck   = GetParticleBuckedIndex(p);
            var writer = streams[buck];
            WriteParticleData(writer, p);
        }
        for (int i = 0; i < BUCKETS; i++)
        {
            streams[i].Close();
        }
        particlesCount = particles.Count;
        if (progress != null && progress("Binary Serialization: 3/4 sorting DB", 0, 1))
        {
            return;
        }
        db.Sort();

        dir = Path.GetDirectoryName(GetIndexPath());
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }

        // now we can serialize index with bucked indexes
        using (FileStream fDB = File.Create(GetIndexPath()))
        {
            var bw = new BinaryWriter(fDB, encoding);
            bw.Write(particlesCount);
            for (int i = 0; i < particlesCount; i++)
            {
                if (progress != null && progress("Binary Serialization: 4/4 serializing index", i, particlesCount))
                {
                    return;
                }
                var p     = particles[i];
                var flags = p.flags;
                bw.Write(p.atomsHash);
                bw.Write(p.structureHash);
                bw.Write(p.structureHashExact);
                bw.Write((ushort)p.binaryDataLocationInBucket);
                bw.Write(p.id);
                bw.Write(p.name);
                bw.Write((ushort)p.flags);
                if ((flags & ParticleInfo.ParticleFlags.HasChemicalFormula) > 0)
                {
                    bw.Write(p.chemicalFormula);
                }
                if ((flags & ParticleInfo.ParticleFlags.HasParticleCharge) > 0)
                {
                    bw.Write((sbyte)p.charge);
                }
                bw.Write((byte)p.CASes.Count);
                for (int cas = 0; cas < p.CASes.Count; cas++)
                {
                    bw.Write(p.CASes[cas]);
                }
            }

            for (int i = 0; i < particlesCount; i++)
            {
                bw.Write(db.__sortedIds[i]);
            }
            //for (int i = 0; i < particlesCount; i++)
            //    bw.Write(db.__sortedStructureHashes[i]);
            //for (int i = 0; i < particlesCount; i++)
            //    bw.Write(db.__sortedStructureHashesExact[i]);
            for (int i = 0; i < particlesCount; i++)
            {
                bw.Write(db.__sortedAtomsHashes[i]);
            }
            bw.Close();
        }

        Debug.LogFormat("MelDB serialized with {0} particles into {1} buckets\nFlags: {2}",
                        particlesCount, BUCKETS, _include);
    }