Пример #1
0
        /// <summary>
        /// Serializes this shortcuts db to the given stream and returns the # of bytes written.
        /// </summary>
        public long Serialize(Stream stream)
        {
            // trim data structures.
            _shortcuts.Resize(_shortcutsPointer);
            _stops.Resize(_stopsPointer);

            // write version #.
            long size = 1;

            stream.WriteByte(1);

            // write profile name.
            size += stream.WriteWithSize(_profile.FullName);

            // serialize the db-meta.
            size += _dbMeta.WriteWithSize(stream);

            // write the stops count and the shortcuts count.
            var bytes = BitConverter.GetBytes(_stopsPointer);

            stream.Write(bytes, 0, 4);
            size += 4;
            bytes = BitConverter.GetBytes(_shortcutsPointer);
            stream.Write(bytes, 0, 4);
            size += 4;

            // write stops meta and data.
            size += _stopsMeta.Serialize(stream);
            size += _stops.CopyTo(stream);

            // write shortcut meta and data.
            size += _shortcutsMeta.Serialize(stream);
            size += _shortcuts.CopyTo(stream);

            return(size);
        }
Пример #2
0
        public void TestDeserialize()
        {
            var refIndex = new AttributesIndex();

            var id1 = refIndex.Add(new Attribute("key1", "value1"));
            var id2 = refIndex.Add(new Attribute("key2", "value1"));
            var id3 = refIndex.Add(new Attribute("key2", "value2"));

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                var attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);
            }

            var random = new System.Random(116542346);
            var keys   = 100;
            var values = 100000;

            refIndex = new AttributesIndex();
            var refIndexRef = new Dictionary <uint, IAttributeCollection>();

            for (var i = 0; i < 1000; i++)
            {
                var attributes = new AttributeCollection();
                for (var j = 0; j < random.Next(8); j++)
                {
                    attributes.AddOrReplace(new Attribute(
                                                string.Format("k{0}", random.Next(keys)),
                                                string.Format("v{0}", random.Next(values))));
                }

                var id = refIndex.Add(attributes);
                refIndexRef[id] = attributes;
            }

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }
            }

            refIndex = new AttributesIndex(AttributesIndexMode.IncreaseOne);

            id1 = refIndex.Add(new Attribute("key1", "value1"));
            id2 = refIndex.Add(new Attribute("key2", "value1"));
            id3 = refIndex.Add(new Attribute("key2", "value2"));

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                var attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);
            }

            random      = new System.Random(116542346);
            keys        = 100;
            values      = 100000;
            refIndex    = new AttributesIndex(AttributesIndexMode.IncreaseOne);
            refIndexRef = new Dictionary <uint, IAttributeCollection>();
            for (var i = 0; i < 1000; i++)
            {
                var attributes = new AttributeCollection();
                for (var j = 0; j < random.Next(8); j++)
                {
                    attributes.AddOrReplace(new Attribute(
                                                string.Format("k{0}", random.Next(keys)),
                                                string.Format("v{0}", random.Next(values))));
                }

                var id = refIndex.Add(attributes);
                refIndexRef[id] = attributes;
            }

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }
            }
        }
Пример #3
0
        public void TestSerialize()
        {
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex();

                Assert.AreEqual(17, index.Serialize(memoryStream));
                Assert.AreEqual(17, memoryStream.Position);
            }
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex();
                index.Add(new Attribute("1", "2"));

                Assert.AreEqual(32, index.Serialize(memoryStream));
                Assert.AreEqual(32, memoryStream.Position);
            }
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex();
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("1", "2"));

                Assert.AreEqual(32, index.Serialize(memoryStream));
                Assert.AreEqual(32, memoryStream.Position);
            }
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex();
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("2", "1"));

                Assert.AreEqual(41, index.Serialize(memoryStream));
                Assert.AreEqual(41, memoryStream.Position);
            }


            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex(AttributesIndexMode.IncreaseOne);

                Assert.AreEqual(16 + 9, index.Serialize(memoryStream));
                Assert.AreEqual(16 + 9, memoryStream.Position);
            }
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex(AttributesIndexMode.IncreaseOne);
                index.Add(new Attribute("1", "2"));

                Assert.AreEqual(31 + 1 + 8 + 4, index.Serialize(memoryStream));
                Assert.AreEqual(31 + 1 + 8 + 4, memoryStream.Position);
            }
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex(AttributesIndexMode.IncreaseOne | AttributesIndexMode.ReverseAll);
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("1", "2"));

                Assert.AreEqual(31 + 1 + 8 + 4, index.Serialize(memoryStream));
                Assert.AreEqual(31 + 1 + 8 + 4, memoryStream.Position);
            }
            using (var memoryStream = new MemoryStream())
            {
                var index = new AttributesIndex(AttributesIndexMode.IncreaseOne | AttributesIndexMode.ReverseAll);
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("1", "2"));
                index.Add(new Attribute("2", "1"));

                Assert.AreEqual(40 + 1 + 8 + 4 * 2, index.Serialize(memoryStream));
                Assert.AreEqual(40 + 1 + 8 + 4 * 2, memoryStream.Position);
            }
        }
Пример #4
0
        /// <summary>
        /// Writes this database to the given stream.
        /// </summary>
        public long Serialize(Stream stream, bool toReadonly)
        {
            var position = stream.Position;

            // write version #.
            // version1: OsmSharp.Routing state of layout.
            // version2: Added ShortcutsDbs.
            // version3: Add advanced profile serialization.
            // version4: Added missing restriction dbs.
            long size = 1;

            stream.WriteByte(4);

            // write guid.
            stream.Write(_guid.ToByteArray(), 0, 16);
            size += 16;

            // serialize supported profiles.
            var lengthBytes = BitConverter.GetBytes(_supportedVehicles.Count);

            size += 4;
            stream.Write(lengthBytes, 0, 4);
            foreach (var vehicle in _supportedVehicles)
            {
                size += vehicle.Value.Serialize(stream);
            }

            // serialize the db-meta.
            size += _dbMeta.WriteWithSize(stream);

            // serialize the # of shortcutsdbs profiles.
            if (_shortcutsDbs.Count > byte.MaxValue)
            {
                throw new Exception("Cannot serialize a router db with more than 255 shortcut dbs.");
            }
            stream.WriteByte((byte)_shortcutsDbs.Count);
            size += 1;

            // serialize the # of contracted profiles.
            if (_contracted.Count > byte.MaxValue)
            {
                throw new Exception("Cannot serialize a router db with more than 255 contracted graphs.");
            }
            stream.WriteByte((byte)_contracted.Count);
            size += 1;

            // serialize the # of restriction dbs.
            if (_restrictionDbs.Count > byte.MaxValue)
            {
                throw new Exception("Cannot serialize a router db with more than 255 restriction dbs.");
            }
            stream.WriteByte((byte)_restrictionDbs.Count);
            size += 1;

            // serialize edge profiles.
            size += _edgeProfiles.Serialize(new LimitedStream(stream));
            stream.Seek(position + size, System.IO.SeekOrigin.Begin);

            // serialize meta-data.
            size += _meta.Serialize(new LimitedStream(stream));
            stream.Seek(position + size, System.IO.SeekOrigin.Begin);

            // serialize network.
            size += _network.Serialize(new LimitedStream(stream));
            stream.Seek(position + size, System.IO.SeekOrigin.Begin);

            // serialize all shortcut dbs.
            foreach (var shortcutsDb in _shortcutsDbs)
            {
                size += stream.WriteWithSize(shortcutsDb.Key);
                size += shortcutsDb.Value.Serialize(
                    new LimitedStream(stream));
            }

            // serialize all contracted networks.
            foreach (var contracted in _contracted)
            {
                size += stream.WriteWithSize(contracted.Key);
                size += contracted.Value.Serialize(
                    new LimitedStream(stream), toReadonly);
            }

            // serialize all restriction dbs.
            foreach (var restrictionDb in _restrictionDbs)
            {
                size += stream.WriteWithSize(restrictionDb.Key);
                size += restrictionDb.Value.Serialize(stream);
            }

            return(size);
        }