Пример #1
0
        /// <summary>
        /// Serializes this tags index to the given stream.
        /// </summary>
        /// <param name="stream">The stream to write to. Writing will start at position 0.</param>
        public long Serialize(System.IO.Stream stream)
        { // serialize the tags and strings index.
            var size          = _tagsIndex.Serialize(stream);
            var limitedStream = new LimitedStream(stream);

            return(_stringIndex.Serialize(limitedStream) + size);
        }
Пример #2
0
        /// <summary>
        /// Deserializes the vertices
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="graph"></param>
        /// <param name="size"></param>
        protected virtual void DeserializeVertices(LimitedStream stream, long size, DynamicGraphRouterDataSource <TEdgeData> graph)
        {
            RuntimeTypeModel typeModel = RuntimeTypeModel.Create();

            typeModel.Add(typeof(SerializableVertex), true);

            long position = stream.Position;
            uint vertex   = 0;

            while (stream.Position - position < size)
            { // keep reading vertices until the appriated number of bytes have been read.
                var vertices = typeModel.DeserializeWithSize(stream, null, typeof(SerializableVertex[])) as SerializableVertex[];
                if (vertices != null)
                { // there are a vertices.
                    for (int idx = 0; idx < vertices.Length; idx++)
                    {
                        if (vertices[idx] != null)
                        { // there is a vertex.
                            graph.AddVertex(vertices[idx].Latitude, vertices[idx].Longitude);
                        }
                        vertex++;
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Deserializes a tags index from the given stream.
        /// </summary>
        public static AttributesIndex Deserialize(System.IO.Stream stream, bool copy   = false,
                                                  AttributesIndexMode defaultIndexMode = AttributesIndexMode.ReverseStringIndexKeysOnly)
        {
            // read version byte.
            long position = 1;
            var  version  = stream.ReadByte();

            int type = 0;

            if (version < 2)
            { // unversioned version.
                type = (byte)version;
            }
            else
            { // versioned.
                // read the index mode.
                var indexModeByte = stream.ReadByte();
                position++;
                defaultIndexMode = (AttributesIndexMode)indexModeByte;

                // read the type.
                type = stream.ReadByte();
                position++;
            }

            // read the actual data.
            long size;

            if (type == 0)
            { // regular index.
                var tagsIndex = Index <int[]> .CreateFromWithSize(stream, out size, !copy);

                position += size + 8;
                stream.Seek(position, System.IO.SeekOrigin.Begin);
                var limitedStream = new LimitedStream(stream);
                var stringIndex   = Index <string> .CreateFromWithSize(limitedStream, out size, !copy);

                position += size + 8;
                stream.Seek(position, System.IO.SeekOrigin.Begin);
                return(new AttributesIndex(defaultIndexMode, stringIndex, tagsIndex));
            }
            else
            { // increase one index.
                var tagsIndex = Index <int[]> .CreateFromWithSize(stream, out size, !copy);

                position += size + 8;
                stream.Seek(position, System.IO.SeekOrigin.Begin);
                var limitedStream = new LimitedStream(stream);
                var stringIndex   = Index <string> .CreateFromWithSize(limitedStream, out size, !copy);

                position += size + 8;
                stream.Seek(position, System.IO.SeekOrigin.Begin);
                var indexLengthBytes = new byte[8];
                stream.Read(indexLengthBytes, 0, 8);
                var indexLength = BitConverter.ToInt64(indexLengthBytes, 0);
                var index       = Context.ArrayFactory.CreateMemoryBackedArray <uint>(indexLength);
                index.CopyFrom(stream);
                return(new AttributesIndex(defaultIndexMode, stringIndex, tagsIndex, index));
            }
        }
        public ResultModel ConvertFile(ConversionDto conversionDto)
        {
            var fileNames = _storageRepository.ParseNames(conversionDto.sessionId);

            if (fileNames == null)
            {
                return(ResultModel.Error(fileNames.Id, "Invalid session id", 400));
            }
            return(OpConvert.Measure(fileNames.Id, () =>
            {
                FileFormat fmt;
                if (conversionDto.outputType != null && formats.TryGetValue(conversionDto.outputType, out fmt))
                {
                    SceneImportContext importContext = CreateLoadContext(fileNames);
                    //User uploaded an unsupported file format.
                    if (importContext.SourceFormat == null)
                    {
                        _logger.LogError("Failed to detect file type from file {0}", fileNames.Id);
                        return ResultModel.Error(fileNames.Id, "Unsupported input file", 400);
                    }

                    Scene scene;
                    try
                    {
                        scene = importContext.LoadScene();
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "Failed to open input file {0}", fileNames.Id);
                        OperationFailed(e);
                        return ResultModel.Error(fileNames.Id, "Internal server error", 500);
                    }
                    var originalFile = importContext.MainFile;
                    try
                    {
                        var fileName = fileNames[OutputFile];
                        using (var stream = LimitedStream.CreateFile(fileName, MaximumOutputSize))
                        {
                            scene.Save(stream, fmt);
                        }
                    }
                    catch (Exception e)
                    {
                        var msg = "Internal server error";
                        if (e is ExportException)
                        {
                            msg = e.Message;
                        }
                        _logger.LogError(e, "Failed to save converted file to {0}", fileNames[SourceFile]);
                        OperationFailed(e);
                        return ResultModel.Error(fileNames.Id, msg, 500);
                    }
                    var newFileName = Path.ChangeExtension(originalFile, fmt.Extension);
                    importContext.MetaData.OutputFile = newFileName;
                    SetMetaData(fileNames, importContext.MetaData);
                    return ResultModel.Ok(true);
                }
                return ResultModel.Error(fileNames.Id, "Output type not found", 400);
            }));
        }
Пример #5
0
        /// <summary>
        /// Does the v2 deserialization.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="lazy"></param>
        /// <returns></returns>
        protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize(
            LimitedStream stream, bool lazy)
        {
            var intBytes = new byte[4];

            stream.Read(intBytes, 0, 4);
            int startOfBlocks = BitConverter.ToInt32(intBytes, 0);

            // deserialize regions index.
            stream.Read(intBytes, 0, 4);
            int sizeRegionIndex     = BitConverter.ToInt32(intBytes, 0);
            var chVertexRegionIndex = (CHVertexRegionIndex)_runtimeTypeModel.Deserialize(
                new CappedStream(stream, stream.Position, sizeRegionIndex), null,
                typeof(CHVertexRegionIndex));

            // deserialize blocks index.
            stream.Seek(startOfBlocks, SeekOrigin.Begin);
            stream.Read(intBytes, 0, 4);
            int sizeBlockIndex = BitConverter.ToInt32(intBytes, 0);
            var chBlockIndex   = (CHBlockIndex)_runtimeTypeModel.Deserialize(
                new CappedStream(stream, stream.Position, sizeBlockIndex), null,
                typeof(CHBlockIndex));

            return(new CHEdgeDataDataSource(stream, this, sizeRegionIndex + 8,
                                            chVertexRegionIndex, RegionZoom, startOfBlocks + sizeBlockIndex + 4, chBlockIndex, BlockVertexSize));
        }
Пример #6
0
 public static byte[] ReadMax(this Stream stream, Size limit, Size?estimatedSize = null)
 {
     using (LimitedStream limitedStream = new LimitedStream(stream, limit, leaveOpen: true))
     {
         return(limitedStream.ReadToEnd(estimatedSize));
     }
 }
Пример #7
0
 public static async Task <byte[]> ReadMaxAsync(this Stream stream, Size limit, Size?estimatedSize = null)
 {
     using (LimitedStream limitedStream = new LimitedStream(stream, limit, leaveOpen: true))
     {
         return(await limitedStream.ReadToEndAsync(estimatedSize));
     }
 }
Пример #8
0
        /// <summary>
        /// Fills the given simple scene with objects inside the given view and for the given zoomFactor.
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="view"></param>
        /// <param name="zoomFactor"></param>
        public void Get(Scene2DSimple scene, View2D view, float zoomFactor)
        {
            lock (_stream) {             // make sure there is only synchonous access to the stream.
                // check what is in the non-simplified scene.
                IScene2DPrimitivesSource simpleSource = this.GetNonSimplifiedStream();
                simpleSource.Get(scene, view, zoomFactor);

                // check what index this zoomfactor is for.
                int idx = this.SearchForScene(zoomFactor);
                if (idx >= 0)                                              // the index was found.
                {
                    if (!_loadedScenes.TryGetValue(idx, out simpleSource)) // the scene was not found.
                    {
                        long position = _index.SceneIndexes [idx];
                        _stream.Seek(position, SeekOrigin.Begin);
                        LimitedStream stream = new LimitedStream(_stream);
                        //Scene2DRTreeSerializer serializer = new Scene2DRTreeSerializer(true);
                        //simpleSource = new Scene2DPrimitivesSource(serializer.Deserialize(stream));
                        OsmSharp.UI.Renderer.Scene.Storage.Styled.Scene2DStyledSerializer serializer =
                            new Styled.Scene2DStyledSerializer();
                        simpleSource = serializer.Deserialize(stream, true);
                        _loadedScenes.Add(idx, simpleSource);
                    }
                    simpleSource.Get(scene, view, zoomFactor);

                    OsmSharp.Logging.Log.TraceEvent("Scene2DLayeredSource", System.Diagnostics.TraceEventType.Verbose,
                                                    string.Format("Deserialized from scene at zoom {0} and idx {1} synchronized.", zoomFactor, idx));
                }
            }
        }
Пример #9
0
        private void ReadResponseBody(ByteStreamReader reader, HttpResponseMessage response)
        {
            HttpContent content = null;

            if (response.Headers.TransferEncodingChunked.GetValueOrDefault(false))
            {
                // read the body with chunked transfer encoding
                var remainingStream = reader.GetRemainingStream();
                var chunkedStream   = new ReadsFromChunksStream(remainingStream);
                content = new StreamContent(chunkedStream);
            }
            else if (response.Content.Headers.ContentLength.HasValue)
            {
                // read the body with a content-length
                var remainingStream = reader.GetRemainingStream();
                var limitedStream   = new LimitedStream(remainingStream, response.Content.Headers.ContentLength.Value);
                content = new StreamContent(limitedStream);
            }
            else
            {
                // TODO: should we immediately close the connection in this case?
            }

            if (content != null)
            {
                // copy over the content headers
                foreach (var header in response.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }

                response.Content = content;
            }
        }
Пример #10
0
        /// <summary>
        /// Does the v1 deserialization.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="lazy"></param>
        /// <param name="vehicles"></param>
        /// <returns></returns>
        protected override IBasicRouterDataSource <TEdgeData> DoDeserialize(
            LimitedStream stream, bool lazy, IEnumerable <string> vehicles)
        {
            ITagsCollectionIndex tagsCollectionIndex       = this.CreateTagsCollectionIndex();
            DynamicGraphRouterDataSource <TEdgeData> graph = this.CreateGraph(tagsCollectionIndex);

            // deserialize vertices.
            var sizeBytes = new byte[8];

            stream.Read(sizeBytes, 0, 8);
            var position = stream.Position;
            var size     = BitConverter.ToInt64(sizeBytes, 0);

            this.DeserializeVertices(stream, size, graph);
            stream.Seek(position + size, System.IO.SeekOrigin.Begin);

            // deserialize edges.
            stream.Read(sizeBytes, 0, 8);
            position = stream.Position;
            size     = BitConverter.ToInt64(sizeBytes, 0);
            this.DeserializeEdges(stream, size, graph);
            stream.Seek(position + size, System.IO.SeekOrigin.Begin);

            // deserialize tags.
            stream.Read(sizeBytes, 0, 8);
            position = stream.Position;
            size     = BitConverter.ToInt64(sizeBytes, 0);
            this.DeserializeTags(stream, size, tagsCollectionIndex);
            stream.Seek(position + size, System.IO.SeekOrigin.Begin);

            return(graph);
        }
Пример #11
0
        /// <summary>
        /// Does the v2 deserialization.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="lazy"></param>
        /// <param name="vehicles"></param>
        /// <returns></returns>
        protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize(
            LimitedStream stream, bool lazy, IEnumerable <string> vehicles)
        {
            var intBytes = new byte[4];

            stream.Read(intBytes, 0, 4);
            int startOfBlocks = BitConverter.ToInt32(intBytes, 0);

            stream.Read(intBytes, 0, 4);
            int startOfTags = BitConverter.ToInt32(intBytes, 0);

            stream.Read(intBytes, 0, 4);
            int sizeRegionIndex = BitConverter.ToInt32(intBytes, 0);

            // deserialize regions index.
            var chVertexRegionIndex = (CHVertexRegionIndex)_runtimeTypeModel.Deserialize(
                new CappedStream(stream, stream.Position, sizeRegionIndex), null,
                typeof(CHVertexRegionIndex));

            // deserialize blocks index.
            stream.Seek(startOfBlocks, SeekOrigin.Begin);
            stream.Read(intBytes, 0, 4);
            int sizeBlockIndex = BitConverter.ToInt32(intBytes, 0);
            var chBlockIndex   = (CHBlockIndex)_runtimeTypeModel.Deserialize(
                new CappedStream(stream, stream.Position, sizeBlockIndex), null,
                typeof(CHBlockIndex));

            // deserialize tags.
            stream.Seek(startOfTags, SeekOrigin.Begin);
            ITagsCollectionIndexReadonly tagsIndex = TagIndexSerializer.DeserializeBlocks(stream);

            return(new v2.CHEdgeDataDataSource(stream, this, vehicles, sizeRegionIndex + 12,
                                               chVertexRegionIndex, _regionZoom, startOfBlocks + sizeBlockIndex + 4, chBlockIndex, (uint)_blockVertexSize,
                                               tagsIndex));
        }
        /// <summary>
        /// Serializes the given graph.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="graph"></param>
        /// <param name="metaTags"></param>
        public void Serialize(Stream stream, RouterDataSource <TEdgeData> graph, TagsCollectionBase metaTags)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }

            // write the header.
            this.WriteVersionHeader(stream);

            // write the vehicles.
            this.WriteVehicleProfiles(stream, graph.GetSupportedProfiles());

            // write the meta-data.
            this.WriteMeta(stream, metaTags);

            // wrap the stream.
            var routingSerializerStream = new LimitedStream(stream);

            // do the version-specific serialization.
            this.DoSerialize(routingSerializerStream, graph);
        }
        /// <summary>
        /// Deserializes all edges.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="size"></param>
        /// <param name="graph"></param>
        protected override void DeserializeEdges(LimitedStream stream, long size, DynamicGraphRouterDataSource <LiveEdge> graph)
        {
            var typeModel = RuntimeTypeModel.Create();

            typeModel.Add(typeof(SerializableEdge), true);
            typeModel.Add(typeof(GeoCoordinateSimple), true);

            long position = stream.Position;

            while (stream.Position < position + size)
            { // keep looping until the appropriate number of bytes have been read.
                var serializableEdges = typeModel.DeserializeWithSize(stream, null, typeof(SerializableEdge[])) as SerializableEdge[];
                for (int idx = 0; idx < serializableEdges.Length; idx++)
                {
                    ICoordinateCollection coordinateCollection = null;
                    if (serializableEdges[idx].Coordinates != null)
                    {
                        coordinateCollection = new CoordinateArrayCollection <GeoCoordinateSimple>(serializableEdges[idx].Coordinates);
                    }
                    graph.AddEdge(serializableEdges[idx].FromId, serializableEdges[idx].ToId,
                                  new LiveEdge()
                    {
                        Distance = serializableEdges[idx].Distance,
                        Value    = serializableEdges[idx].Value
                    }, coordinateCollection);
                }
            }
        }
        /// <summary>
        /// Deserializes the given stream into a routable graph.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="lazy"></param>
        /// <param name="metaTags"></param>
        /// <returns></returns>
        public RouterDataSource <TEdgeData> Deserialize(Stream stream, out TagsCollectionBase metaTags, bool lazy = true)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // make sure the stream seeks to the beginning.
            stream.Seek(0, SeekOrigin.Begin);

            if (this.CanDeSerialize(stream))
            {
                // read/verify the current version header.
                this.ReadAndValidateHeader(stream);

                // read vehicle profiles.
                var vehicles = this.ReadVehicleProfiles(stream);

                // deserialize meta data.
                metaTags = this.ReadMeta(stream);

                // wrap the stream.
                var routingSerializerStream = new LimitedStream(stream);

                // do the actual version-specific deserialization.
                return(this.DoDeserialize(routingSerializerStream, lazy, vehicles));
            }
            throw new ArgumentOutOfRangeException("stream", "Cannot deserialize the given stream, version unsupported or content unrecognized!");
        }
Пример #15
0
        public void TestWriteWithinThenAboveLimit()
        {
            var innerStream = new MemoryStream();
            var sut         = new LimitedStream(innerStream, 10);

            sut.Write(new byte[5] {
                1, 2, 3, 4, 5
            }, 0, 5);

            var actual5 = new byte[5];

            innerStream.Seek(0, SeekOrigin.Begin);
            innerStream.Read(actual5, 0, 5);

            Assert.Equal((byte)1, actual5[0]);
            Assert.Equal((byte)5, actual5[4]);

            sut.Write(new byte[5] {
                6, 7, 8, 9, 10
            }, 0, 5);

            var actual10 = new byte[10];

            innerStream.Seek(0, SeekOrigin.Begin);
            innerStream.Read(actual10, 0, 10);

            Assert.Equal((byte)1, actual10[0]);
            Assert.Equal((byte)10, actual10[9]);

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.Write(new byte[1] {
                11
            }, 0, 1));
        }
Пример #16
0
        public void TestWriteWithinThenAboveLimit()
        {
            MemoryStream  innerStream = new MemoryStream();
            LimitedStream sut         = new LimitedStream(innerStream, 10);

            sut.Write(new byte[5] {
                (byte)1, (byte)2, (byte)3, (byte)4, (byte)5
            }, 0, 5);

            byte[] actual5 = new byte[5];
            innerStream.Seek(0, SeekOrigin.Begin);
            innerStream.Read(actual5, 0, 5);

            Assert.AreEqual((byte)1, actual5[0]);
            Assert.AreEqual((byte)5, actual5[4]);

            sut.Write(new byte[5] {
                (byte)6, (byte)7, (byte)8, (byte)9, (byte)10
            }, 0, 5);

            byte[] actual10 = new byte[10];
            innerStream.Seek(0, SeekOrigin.Begin);
            innerStream.Read(actual10, 0, 10);

            Assert.AreEqual((byte)1, actual10[0]);
            Assert.AreEqual((byte)10, actual10[9]);

            sut.Write(new byte[1] {
                (byte)11
            }, 0, 1);
        }
Пример #17
0
        public void TestWriteAboveLimit()
        {
            var innerStream = new MemoryStream();
            var sut         = new LimitedStream(innerStream, 3);

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.Write(new byte[5] {
                1, 2, 3, 4, 5
            }, 0, 5));
        }
Пример #18
0
        public void TestWriteAboveLimitWithByteLengthShorterThanCount()
        {
            var innerStream = new MemoryStream();
            var sut         = new LimitedStream(innerStream, 3);

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.Write(new byte[5] {
                1, 2, 3, 4, 5
            }, 1, 13));
        }
Пример #19
0
        public void TestWriteAboveLimitWithByteLengthShorterThanCount()
        {
            MemoryStream  innerStream = new MemoryStream();
            LimitedStream sut         = new LimitedStream(innerStream, 3);

            sut.Write(new byte[5] {
                (byte)1, (byte)2, (byte)3, (byte)4, (byte)5
            }, 1, 13);
        }
Пример #20
0
        public void TestWriteAboveLimit()
        {
            MemoryStream  innerStream = new MemoryStream();
            LimitedStream sut         = new LimitedStream(innerStream, 3);

            sut.Write(new byte[5] {
                (byte)1, (byte)2, (byte)3, (byte)4, (byte)5
            }, 0, 5);
        }
Пример #21
0
        protected HttpResponseMessage StreamResult(string filename, Stream resultContent)
        {
            var response = new HttpResponseMessage
            {
                Headers =
                {
                    TransferEncodingChunked = false
                }
            };
            long length;
            ContentRangeHeaderValue contentRange = null;

            if (Request.Headers.Range != null)
            {
                if (Request.Headers.Range.Ranges.Count != 1)
                {
                    throw new InvalidOperationException("Can't handle multiple range values");
                }
                var range = Request.Headers.Range.Ranges.First();
                var from  = range.From ?? 0;
                var to    = range.To ?? resultContent.Length;

                length = (to - from);

                // "to" in Content-Range points on the last byte. In other words the set is: <from..to>  not <from..to)
                if (from < to)
                {
                    contentRange  = new ContentRangeHeaderValue(from, to - 1, resultContent.Length);
                    resultContent = new LimitedStream(resultContent, from, to);
                }
                else
                {
                    contentRange  = new ContentRangeHeaderValue(0);
                    resultContent = Stream.Null;
                }
            }
            else
            {
                length = resultContent.Length;
            }

            response.Content = new StreamContent(resultContent)
            {
                Headers =
                {
                    ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName       = filename
                    },
                    // ContentLength = length,
                    ContentRange       = contentRange,
                }
            };

            return(response);
        }
Пример #22
0
 /// <summary>
 /// Deserializes to the level of primitive blocks (PrimitiveBlock)
 /// </summary>
 private void ReadFilePdb()
 {
     using (var file = File.OpenRead(_pathFilePdb))
     {
         int length, blockCount = 0;
         while (Serializer.TryReadLengthPrefix(file, PrefixStyle.Fixed32, out length))
         {
             length = LowLevelReader.IntLittleEndianToBigEndian((uint)length);
             BlobHeader header;
             using (var tmp = new LimitedStream(file, length))
             {
                 try
                 {
                     header = Serializer.Deserialize <BlobHeader>(tmp);
                 }
                 catch (Exception e)
                 {
                     throw new FileLoadException("Invalid file format .pdb", e);
                 }
             }
             Blob blob;
             using (var tmp = new LimitedStream(file, header.datasize))
             {
                 blob = Serializer.Deserialize <Blob>(tmp);
             }
             if (blob.zlib_data == null)
             {
                 throw new NotSupportedException("I'm only handling zlib here!");
             }
             HeaderBlock    headerBlock;
             PrimitiveBlock primitiveBlock;
             using (var ms = new MemoryStream(blob.zlib_data))
                 using (var zlib = new ZLibStream(ms))
                 {
                     if (header.type == "OSMHeader")
                     {
                         headerBlock = Serializer.Deserialize <HeaderBlock>(zlib);
                     }
                     if (header.type == "OSMData")
                     {
                         primitiveBlock = Serializer.Deserialize <PrimitiveBlock>(zlib);
                         if (primitiveBlock != null)
                         {
                             ReadPrimitiveBlock(primitiveBlock);
                         }
                     }
                 }
             blockCount++;
         }
         if (_tagsValues.Rows.Count > 0)
         {
             this.ImportDataTableInDb(ref _tagsValues, new GetTable(GetTableTagsValue));
         }
     }
 }
Пример #23
0
        /// <summary>
        /// Serializes all edges.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="graph"></param>
        protected override void SerializeEdges(LimitedStream stream, DynamicGraphRouterDataSource <LiveEdge> graph)
        {
            var typeModel = RuntimeTypeModel.Create();

            typeModel.Add(typeof(SerializableEdge), true);
            typeModel.Add(typeof(GeoCoordinateSimple), true);

            int blockSize = 1000;
            var arcsQueue = new List <SerializableEdge>(blockSize);

            uint vertex = 0;

            while (vertex < graph.VertexCount)
            { // keep looping and serialize all vertices.
                var arcs = graph.GetEdges(vertex);
                if (arcs != null)
                { // serialize the arcs, but serialize them only once.
                    // choose only those arcs that start at a vertex smaller than the target.
                    for (int idx = 0; idx < arcs.Length; idx++)
                    {
                        if (arcs[idx].Key > vertex)
                        {
                            GeoCoordinateSimple[] coordinates;
                            if (!graph.GetEdgeShape(vertex, arcs[idx].Key, out coordinates))
                            {
                                coordinates = null;
                            }
                            arcsQueue.Add(new SerializableEdge()
                            {
                                Distance    = arcs[idx].Value.Distance,
                                FromId      = vertex,
                                ToId        = arcs[idx].Key,
                                Value       = arcs[idx].Value.Value,
                                Coordinates = coordinates
                            });

                            if (arcsQueue.Count == blockSize)
                            { // execute serialization.
                                typeModel.SerializeWithSize(stream, arcsQueue.ToArray());
                                arcsQueue.Clear();
                            }
                        }
                    }

                    // serialize.
                    vertex++;
                }
            }

            if (arcsQueue.Count > 0)
            { // execute serialization.
                typeModel.SerializeWithSize(stream, arcsQueue.ToArray());
                arcsQueue.Clear();
            }
        }
Пример #24
0
        public void TestCopyToAboveLimit()
        {
            var innerStream = new MemoryStream();
            var sut         = new LimitedStream(innerStream, 3);

            var sourceStream = new MemoryStream(new byte[5] {
                1, 2, 3, 4, 5
            });

            Assert.Throws <ArgumentOutOfRangeException>(() => sourceStream.CopyTo(sut));
        }
Пример #25
0
        public void TestCopyToAboveLimit()
        {
            MemoryStream  innerStream = new MemoryStream();
            LimitedStream sut         = new LimitedStream(innerStream, 3);

            MemoryStream sourceStream = new MemoryStream(new byte[5] {
                (byte)1, (byte)2, (byte)3, (byte)4, (byte)5
            });

            sourceStream.CopyTo(sut);
        }
Пример #26
0
        /// <summary>
        /// Deserializes a tags index from the given stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static TagsIndex Deserialize(System.IO.Stream stream)
        {
            long size;
            var  tagsIndex = MemoryMappedIndex <int[]> .Deserialize(stream, MemoryMappedDelegates.ReadFromIntArray, MemoryMappedDelegates.WriteToIntArray, false, out size);

            stream.Seek(size, System.IO.SeekOrigin.Begin);
            var limitedStream = new LimitedStream(stream);
            var stringIndex   = MemoryMappedIndex <string> .Deserialize(limitedStream, MemoryMappedDelegates.ReadFromString, MemoryMappedDelegates.WriteToString, false);

            return(new TagsIndex(stringIndex, tagsIndex));
        }
Пример #27
0
        public async Task Limits_Asynchronously()
        {
            // ARRANGE
            var innerStream = new MemoryStream(Encoding.ASCII.GetBytes("foobar"));
            var stream      = new LimitedStream(innerStream, 3);

            // ACT
            var actual = await new StreamReader(stream, Encoding.ASCII).ReadToEndAsync();

            // ASSERT
            actual.Should().Be("foo");
        }
Пример #28
0
        public async Task TestReadAsync()
        {
            Size limitedSize = new Size(2);

            using (FileStream fileStream = readFileInfo.OpenRead())
                using (LimitedStream limitedStream = new LimitedStream(fileStream, limitedSize))
                {
                    Assert.AreNotEqual(limitedSize.Bytes, fileStream.Length);
                    Assert.AreEqual(limitedSize.Bytes, (await limitedStream.ReadToEndAsync()).LongLength);
                    Assert.AreEqual(0, (await limitedStream.ReadToEndAsync()).LongLength);
                }
        }
Пример #29
0
        /// <summary>
        /// Serialize/deserialize index.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        private ITagsCollectionIndexReadonly SerializeDeserializeLimitedStream(ITagsCollectionIndex index, int position)
        {
            MemoryStream memoryStream = new MemoryStream();

            memoryStream.Seek(position, SeekOrigin.Begin);

            LimitedStream stream = new LimitedStream(memoryStream);

            TagIndexSerializer.Serialize(stream, index);
            stream.Seek(0, SeekOrigin.Begin);

            return(TagIndexSerializer.Deserialize(stream));
        }
Пример #30
0
        /// <summary>
        /// Serialize/deserialize index.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="blockSize"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        private ITagsIndexReadonly SerializeDeserializeBlockLimitedStream(ITagsIndex index, uint blockSize, int position)
        {
            MemoryStream memoryStream = new MemoryStream();

            memoryStream.Seek(position, SeekOrigin.Begin);

            LimitedStream stream = new LimitedStream(memoryStream);

            stream.Seek(position, SeekOrigin.Begin);
            TagIndexSerializer.SerializeBlocks(stream, index, blockSize);

            stream.Seek(position, SeekOrigin.Begin);
            return(TagIndexSerializer.DeserializeBlocks(stream));
        }