/// <summary> /// Serializes a tags collection to a byte array and addes the size in the first 4 bytes. /// </summary> /// <param name="collection"></param> /// <param name="stream"></param> /// <returns></returns> public void SerializeWithSize(TagsCollectionBase collection, Stream stream) { RuntimeTypeModel typeModel = TypeModel.Create(); typeModel.Add(typeof(Tag), true); var tagsList = new List <Tag>(collection); typeModel.SerializeWithSize(stream, tagsList); }
/// <summary> /// Serializes the meta-data. /// </summary> /// <param name="stream"></param> /// <param name="tagsCollectionIndex"></param> protected virtual void SerializeTags(LimitedStream stream, ITagsCollectionIndexReadonly tagsCollectionIndex) { RuntimeTypeModel typeModel = RuntimeTypeModel.Create(); typeModel.Add(typeof(SerializableTag), true); // write tags collection-count. var countBytes = BitConverter.GetBytes(tagsCollectionIndex.Max); stream.Write(countBytes, 0, 4); int blockSize = 1000; var tagsQueue = new List <SerializableTag>(); // serialize tags collections one-by-one. for (uint idx = 0; idx < tagsCollectionIndex.Max; idx++) { // serialize objects one-by-one. var tagsCollection = tagsCollectionIndex.Get(idx); foreach (var tag in tagsCollection) { tagsQueue.Add(new SerializableTag() { CollectionId = idx, Key = tag.Key, Value = tag.Value }); if (tagsQueue.Count == blockSize) { // time to serialize. typeModel.SerializeWithSize(stream, tagsQueue.ToArray()); tagsQueue.Clear(); } } } tagsQueue.Add(new SerializableTag() { CollectionId = int.MaxValue, Key = string.Empty, Value = string.Empty }); typeModel.SerializeWithSize(stream, tagsQueue.ToArray()); }
/// <summary> /// Serializes the vertices /// </summary> /// <param name="stream"></param> /// <param name="graph"></param> protected virtual void SerializeVertices(LimitedStream stream, DynamicGraphRouterDataSource <TEdgeData> graph) { RuntimeTypeModel typeModel = RuntimeTypeModel.Create(); typeModel.Add(typeof(SerializableVertex), true); int blockSize = 1000; var vertices = new SerializableVertex[blockSize]; uint vertex = 1; float latitude, longitude; while (vertex <= graph.VertexCount) { // adjust array size if needed. if (vertices.Length > graph.VertexCount - vertex) { // shrink array. vertices = new SerializableVertex[graph.VertexCount - vertex + 1]; } // build block. for (uint idx = 0; idx < vertices.Length; idx++) { uint current = vertex + idx; if (vertex <= graph.VertexCount && graph.GetVertex(current, out latitude, out longitude)) { // vertex in the graph. if (vertices[idx] == null) { // make sure there is a vertex. vertices[idx] = new SerializableVertex(); } vertices[idx].Latitude = latitude; vertices[idx].Longitude = longitude; } else { // vertex not in the graph. throw new Exception("Cannot serialize non-existing vertices!"); } } // serialize. typeModel.SerializeWithSize(stream, vertices); // move to the next vertex. vertex = (uint)(vertex + blockSize); } }