/// <summary> /// Creates a new router database. /// </summary> private RouterDb(Guid guid, RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, MappedAttributesIndex metaVertex, MetaCollectionDb vertexData, IAttributeCollection dbMeta, Profiles.Vehicle[] supportedVehicles) { _guid = guid; _network = network; _edgeProfiles = profiles; _meta = meta; _metaVertex = metaVertex; _vertexData = vertexData; _dbMeta = dbMeta; _supportedVehicles = new Dictionary <string, Vehicle>(); _supportedProfiles = new Dictionary <string, Profile>(); foreach (var vehicle in supportedVehicles) { _supportedVehicles[vehicle.Name.ToLowerInvariant()] = vehicle; foreach (var profile in vehicle.GetProfiles()) { _supportedProfiles[profile.FullName.ToLowerInvariant()] = profile; } } _contracted = new Dictionary <string, ContractedDb>(); _restrictionDbs = new Dictionary <string, RestrictionsDb>(); _shortcutsDbs = new Dictionary <string, ShortcutsDb>(); }
public void TestCreate() { var db = new MetaCollectionDb(); MetaCollection <int> col; Assert.IsFalse(db.TryGet <int>("not-there", out col)); }
public void TestAddGet() { var db = new MetaCollectionDb(); var intCol = db.AddInt32("int"); intCol[1023] = 1; var dblCol = db.AddDouble("double"); dblCol[1023] = 1; Assert.IsTrue(db.TryGet <int>("int", out intCol)); Assert.IsTrue(db.TryGet <double>("double", out dblCol)); }
/// <summary> /// Creates a new router database. /// </summary> public RouterDb(MemoryMap map, float maxEdgeDistance = Constants.DefaultMaxEdgeDistance) { _network = new RoutingNetwork(map, RoutingNetworkProfile.NoCache, maxEdgeDistance); _edgeProfiles = new AttributesIndex(AttributesIndexMode.IncreaseOne | AttributesIndexMode.ReverseAll); _meta = new AttributesIndex(map, AttributesIndexMode.ReverseStringIndexKeysOnly); _metaVertex = new MappedAttributesIndex(); _vertexData = new MetaCollectionDb(); _dbMeta = new AttributeCollection(); _supportedVehicles = new Dictionary <string, Vehicle>(); _supportedProfiles = new Dictionary <string, Profile>(); _contracted = new Dictionary <string, ContractedDb>(); _restrictionDbs = new Dictionary <string, RestrictionsDb>(); _shortcutsDbs = new Dictionary <string, ShortcutsDb>(); _guid = Guid.NewGuid(); }
public void TestSerializeDeserialize() { var db = new MetaCollectionDb(); var intCol = db.AddInt32("int"); var dblCol = db.AddDouble("double"); var size = 15213; for (uint i = 0; i < size; i++) { intCol[i] = (int)(i * 2); } for (uint i = 0; i < size; i++) { dblCol[i] = i * .1; } using (var stream = new MemoryStream()) { db.Serialize(stream); stream.Seek(0, SeekOrigin.Begin); db = MetaCollectionDb.Deserialize(stream, null); } Assert.IsTrue(db.TryGet <int>("int", out intCol)); Assert.IsTrue(db.TryGet <double>("double", out dblCol)); for (uint i = 0; i < intCol.Count; i++) { Assert.AreEqual((int)i * 2, intCol[i]); } for (uint i = 0; i < dblCol.Count; i++) { Assert.AreEqual(i * .1, dblCol[i]); } }
/// <summary> /// Deserializes a database from the given stream. /// </summary> public static RouterDb Deserialize(Stream stream, RouterDbProfile profile) { // deserialize all basic data. // version1: OsmSharp.Routing state of layout. // version2: Added ShortcutsDbs. // version3: Add advanced profile serialization. // version4: Added missing restriction dbs. // version5: Added new dual edge-based contracted graph. // version6: Added vertex meta-data. var version = stream.ReadByte(); if (version != 1 && version != 2 && version != 3 && version != 4 && version != 5 && version != 6) { throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", version)); } var guidBytes = new byte[16]; stream.Read(guidBytes, 0, 16); var guid = new Guid(guidBytes); var supportedVehicleInstances = new List <Vehicle>(); if (version <= 2) { // just contains vehicle names. var supportedVehicles = stream.ReadWithSizeStringArray(); foreach (var vehicleName in supportedVehicles) { Profile vehicleProfile; if (Profile.TryGet(vehicleName, out vehicleProfile)) { supportedVehicleInstances.Add(vehicleProfile.Parent); } else { Itinero.Logging.Logger.Log("RouterDb", Logging.TraceEventType.Warning, "Vehicle with name {0} was not found, register all vehicle profiles before deserializing the router db.", vehicleName); } } } else { // contains the full vehicles. var lengthBytes = new byte[4]; stream.Read(lengthBytes, 0, 4); var size = BitConverter.ToInt32(lengthBytes, 0); for (var i = 0; i < size; i++) { var vehicle = Vehicle.Deserialize(stream); supportedVehicleInstances.Add(vehicle); vehicle.Register(); } } var metaDb = stream.ReadWithSizeAttributesCollection(); var shorcutsCount = (int)0; if (version >= 2) { // when version < 1 there are no shortcuts and thus no shortcut count. shorcutsCount = stream.ReadByte(); } var contractedCount = stream.ReadByte(); var restrictionDbCount = 0; if (version >= 4) { restrictionDbCount = stream.ReadByte(); } var profiles = AttributesIndex.Deserialize(new LimitedStream(stream), true); var meta = AttributesIndex.Deserialize(new LimitedStream(stream), true); MappedAttributesIndex metaVertex = null; MetaCollectionDb vertexData = null; if (version >= 6) { metaVertex = MappedAttributesIndex.Deserialize(new LimitedStream(stream), profile == null ? null : profile.VertexMetaProfile); vertexData = MetaCollectionDb.Deserialize(new LimitedStream(stream), profile == null ? null : profile.VertexDataProfile); } var network = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile); // create router db. var routerDb = new RouterDb(guid, network, profiles, meta, metaVertex, vertexData, metaDb, supportedVehicleInstances.ToArray()); // read all shortcut dbs. for (var i = 0; i < shorcutsCount; i++) { var shortcutsName = stream.ReadWithSizeString(); var shorcutsDb = ShortcutsDb.Deserialize(stream); routerDb._shortcutsDbs[shortcutsName] = shorcutsDb; } // read all contracted versions. for (var i = 0; i < contractedCount; i++) { var profileName = stream.ReadWithSizeString(); var contracted = ContractedDb.Deserialize(stream, profile == null ? null : profile.ContractedDbProfile); routerDb._contracted[profileName] = contracted; } // read all restriction dbs. for (var i = 0; i < restrictionDbCount; i++) { var restrictionDbName = stream.ReadWithSizeString(); var restrictionDb = RestrictionsDb.Deserialize(stream, profile == null ? null : profile.RestrictionDbProfile); routerDb._restrictionDbs[restrictionDbName] = restrictionDb; } return(routerDb); }