예제 #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
        /// <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);
        }