Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new vehicle cache.
 /// </summary>
 public VehicleCache(Vehicle[] vehicles)
 {
     _cache        = new Dictionary <uint, WhitelistAndFlags>();
     _vehicles     = vehicles;
     _edgeProfiles = new AttributesIndex(AttributesIndexMode.IncreaseOne
                                         | AttributesIndexMode.ReverseAll);
 }
Ejemplo n.º 2
0
        public static bool CompareOpposite(AttributesIndex tags, uint tags1, uint tags2)
        {
            TagsCollectionBase tags3 = tags.Get(tags1);
            TagsCollectionBase tags4 = tags.Get(tags2);
            bool?nullable1           = Vehicle.Car.IsOneWay(tags3);
            bool?nullable2           = Vehicle.Car.IsOneWay(tags4);

            if (nullable1.HasValue && nullable2.HasValue && nullable1.Value == nullable2.Value)
            {
                return(false);
            }
            foreach (Tag tag in tags3)
            {
                if (tag.Key != "oneway" && !tags4.ContainsKeyValue(tag.Key, tag.Value))
                {
                    return(false);
                }
            }
            foreach (Tag tag in tags4)
            {
                if (tag.Key != "oneway" && !tags3.ContainsKeyValue(tag.Key, tag.Value))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        /// <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>();
        }
Ejemplo n.º 4
0
        public static RouterDb Deserialize(Stream stream, RouterDbProfile profile)
        {
            int num1 = stream.ReadByte();

            if (num1 != 1)
            {
                throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", (object)num1));
            }
            byte[] numArray = new byte[16];
            stream.Read(numArray, 0, 16);
            Guid guid = new Guid(numArray);

            string[]           strArray           = stream.ReadWithSizeStringArray();
            TagsCollectionBase tagsCollectionBase = stream.ReadWithSizeTagsCollection();
            int                num2             = stream.ReadByte();
            AttributesIndex    attributesIndex1 = AttributesIndex.Deserialize((Stream) new LimitedStream(stream), true);
            AttributesIndex    attributesIndex2 = AttributesIndex.Deserialize((Stream) new LimitedStream(stream), true);
            RoutingNetwork     network          = RoutingNetwork.Deserialize(stream, profile == null ? (RoutingNetworkProfile)null : profile.RoutingNetworkProfile);
            AttributesIndex    profiles         = attributesIndex1;
            AttributesIndex    meta             = attributesIndex2;
            TagsCollectionBase dbMeta           = tagsCollectionBase;

            string[] supportedProfiles = strArray;
            RouterDb routerDb          = new RouterDb(guid, network, profiles, meta, dbMeta, supportedProfiles);

            for (int index1 = 0; index1 < num2; ++index1)
            {
                string            index2            = stream.ReadWithSizeString();
                DirectedMetaGraph directedMetaGraph = DirectedMetaGraph.Deserialize(stream, profile == null ? (DirectedMetaGraphProfile)null : profile.DirectedMetaGraphProfile);
                routerDb._contracted[index2] = directedMetaGraph;
            }
            return(routerDb);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Compares two highway tag collections and check if they mean the same.
        /// </summary>
        /// <returns></returns>
        public static bool Compare(AttributesIndex tags, uint tags1, uint tags2)
        {
            var tagsCollection1 = tags.Get(tags1);
            var tagsCollection2 = tags.Get(tags2);

            var oneway1 = Vehicle.Car.IsOneWay(tagsCollection1);
            var oneway2 = Vehicle.Car.IsOneWay(tagsCollection2);

            if (oneway1 != null && oneway2 != null &&
                oneway1.Value != oneway2.Value)
            { // both have values but opposite ones.
                return(false);
            }
            foreach (var tag1 in tagsCollection1)
            {
                if (tag1.Key != "oneway")
                {
                    if (!tagsCollection2.Contains(tag1.Key, tag1.Value))
                    {
                        return(false);
                    }
                }
            }
            foreach (var tag2 in tagsCollection2)
            {
                if (tag2.Key != "oneway")
                {
                    if (!tagsCollection1.Contains(tag2.Key, tag2.Value))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new shortcuts db.
        /// </summary>
        public ShortcutsDb(Profile profile)
        {
            _dbMeta  = new AttributeCollection();
            _profile = profile;

            _stopsMeta     = new AttributesIndex(AttributesIndexMode.ReverseAll);
            _stops         = Context.ArrayFactory.CreateMemoryBackedArray <uint>(100);
            _shortcutsMeta = new AttributesIndex(AttributesIndexMode.ReverseAll);
            _shortcuts     = Context.ArrayFactory.CreateMemoryBackedArray <uint>(100);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new shortcuts db.
        /// </summary>
        public ShortcutsDb(Profile profile)
        {
            _dbMeta  = new AttributeCollection();
            _profile = profile;

            _stopsMeta     = new AttributesIndex(AttributesIndexMode.ReverseAll);
            _stops         = new MemoryArray <uint>(100);
            _shortcutsMeta = new AttributesIndex(AttributesIndexMode.ReverseAll);
            _shortcuts     = new MemoryArray <uint>(100);
        }
Ejemplo n.º 8
0
 public RouterDb(MemoryMap map, float maxEdgeDistance = 5000f)
 {
     this._network           = new RoutingNetwork(map, RoutingNetworkProfile.NoCache, maxEdgeDistance);
     this._edgeProfiles      = new AttributesIndex(AttributesIndexMode.ReverseAll | AttributesIndexMode.IncreaseOne);
     this._meta              = new AttributesIndex(map, AttributesIndexMode.ReverseStringIndexKeysOnly);
     this._dbMeta            = (TagsCollectionBase) new TagsCollection();
     this._supportedProfiles = new HashSet <string>();
     this._contracted        = new Dictionary <string, DirectedMetaGraph>();
     this._guid              = Guid.NewGuid();
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a new transit db.
 /// </summary>
 public TransitDb()
 {
     _agencyAttributes = new AttributesIndex(AttributesIndexMode.ReverseStringIndex);
     _connectionsDb    = new ConnectionsDb();
     _stopsDb          = new StopsDb();
     _stopAttributes   = new AttributesIndex(AttributesIndexMode.ReverseStringIndex);
     _tripsDb          = new TripsDb();
     _tripAttributes   = new AttributesIndex(AttributesIndexMode.ReverseStringIndex);
     _transfersDbs     = new Dictionary <string, TransfersDb>();
     _schedulesDb      = new SchedulesDb();
     _shapesDb         = new ShapesDb();
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Deserializes from stream.
        /// </summary>
        public static TransitDb Deserialize(Stream stream)
        {
            var version = stream.ReadByte();

            if (version > 2)
            {
                throw new Exception("Cannot deserialize db, version # doesn't match.");
            }

            // read agencies attributes.
            var agencyAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // read connections db.
            var connectionsDb = ConnectionsDb.Deserialize(stream);

            // read schedules db.
            var schedulesDb = SchedulesDb.Deserialize(stream);

            // read stop attributes.
            var stopAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // read stop db.
            var stopsDb = StopsDb.Deserialize(stream);

            // read transfer db's.
            var transferDbsCount = stream.ReadByte();
            var transferDbs      = new Dictionary <string, TransfersDb>();

            for (var i = 0; i < transferDbsCount; i++)
            {
                var profileName = stream.ReadWithSizeString();
                var transferDb  = TransfersDb.Deserialize(stream);

                transferDbs.Add(profileName, transferDb);
            }

            // read trip attributes.
            var tripAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // write trips db.
            var tripsDb = TripsDb.Deserialize(stream);

            ShapesDb shapesDb = null;

            if (version != 1)
            { // write shapes db.
                shapesDb = ShapesDb.Deserialize(stream);
            }

            return(new TransitDb(agencyAttributes, connectionsDb, schedulesDb, stopAttributes, stopsDb, transferDbs,
                                 tripAttributes, tripsDb, shapesDb));
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Creates a new transit db.
 /// </summary>
 private TransitDb(AttributesIndex agencyAttributes, ConnectionsDb connectionsDb, SchedulesDb schedulesDb, AttributesIndex stopAttributes,
                   StopsDb stopsDb, Dictionary <string, TransfersDb> transferDbs, AttributesIndex tripAttributes, TripsDb tripsDb, ShapesDb shapesDb)
 {
     _agencyAttributes = agencyAttributes;
     _connectionsDb    = connectionsDb;
     _schedulesDb      = schedulesDb;
     _stopAttributes   = stopAttributes;
     _stopsDb          = stopsDb;
     _transfersDbs     = transferDbs;
     _tripAttributes   = tripAttributes;
     _tripsDb          = tripsDb;
     _shapesDb         = shapesDb;
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Searches all trips.
        /// </summary>
        public static HashSet <uint> SearchAll(this TripsDb tripsDb, AttributesIndex tripAttributes, Func <IAttributeCollection, bool> filter)
        {
            var ids = new HashSet <uint>();

            var enumerator = tripsDb.GetEnumerator();

            while (enumerator.MoveUntil(tripAttributes, filter))
            {
                ids.Add(enumerator.Id);
            }

            return(ids);
        }
Ejemplo n.º 13
0
 public RouterDb(RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, TagsCollectionBase dbMeta, params Profile[] supportedProfiles)
 {
     this._network           = network;
     this._edgeProfiles      = profiles;
     this._meta              = meta;
     this._dbMeta            = dbMeta;
     this._supportedProfiles = new HashSet <string>();
     foreach (Profile supportedProfile in supportedProfiles)
     {
         this._supportedProfiles.Add(supportedProfile.Name);
     }
     this._contracted = new Dictionary <string, DirectedMetaGraph>();
     this._guid       = Guid.NewGuid();
 }
Ejemplo n.º 14
0
 private RouterDb(Guid guid, RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, TagsCollectionBase dbMeta, string[] supportedProfiles)
 {
     this._guid              = guid;
     this._network           = network;
     this._edgeProfiles      = profiles;
     this._meta              = meta;
     this._dbMeta            = dbMeta;
     this._supportedProfiles = new HashSet <string>();
     foreach (string supportedProfile in supportedProfiles)
     {
         this._supportedProfiles.Add(supportedProfile);
     }
     this._contracted = new Dictionary <string, DirectedMetaGraph>();
 }
Ejemplo n.º 15
0
        /// <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.
            var version = stream.ReadByte();

            if (version != 1 && version != 2)
            {
                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 supportedProfiles = stream.ReadWithSizeStringArray();
            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 profiles        = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var meta            = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var network         = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile);

            // create router db.
            var routerDb = new RouterDb(guid, network, profiles, meta, metaDb, supportedProfiles);

            // 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;
            }
            return(routerDb);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Creates a new shortcuts db.
        /// </summary>
        private ShortcutsDb(Profile profile, IAttributeCollection dbMeta, AttributesIndex stopsMeta, ArrayBase <uint> stops,
                            AttributesIndex shortcutsMeta, ArrayBase <uint> shortcuts)
        {
            _dbMeta  = dbMeta;
            _profile = profile;

            _stops         = stops;
            _stopsMeta     = stopsMeta;
            _shortcuts     = shortcuts;
            _shortcutsMeta = shortcutsMeta;

            _shortcutsPointer = (uint)_shortcuts.Length;
            _stopsPointer     = (uint)_stops.Length;
        }
Ejemplo n.º 17
0
        public void TestCreate()
        {
            var index = new AttributesIndex();

            Assert.IsFalse(index.IsReadonly);

            using (var map = new MemoryMapStream())
            {
                index = new AttributesIndex(map);
                Assert.IsFalse(index.IsReadonly);

                index = new AttributesIndex(map, AttributesIndexMode.IncreaseOne);
                Assert.IsFalse(index.IsReadonly);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Creates a new router database.
        /// </summary>
        public RouterDb(float maxEdgeDistance = Constants.DefaultMaxEdgeDistance)
        {
            _network      = new RoutingNetwork(new Graphs.Geometric.GeometricGraph(1), maxEdgeDistance);
            _edgeProfiles = new AttributesIndex(AttributesIndexMode.IncreaseOne
                                                | AttributesIndexMode.ReverseAll);
            _meta   = new AttributesIndex(AttributesIndexMode.ReverseStringIndexKeysOnly);
            _dbMeta = new AttributeCollection();

            _supportedProfiles = new HashSet <string>();
            _contracted        = new Dictionary <string, ContractedDb>();
            _restrictionDbs    = new Dictionary <string, RestrictionsDb>();
            _shortcutsDbs      = new Dictionary <string, ShortcutsDb>();

            _guid = Guid.NewGuid();
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Creates a new router database.
        /// </summary>
        public RouterDb(MemoryMap map, RoutingNetworkProfile profile, float maxEdgeDistance = Constants.DefaultMaxEdgeDistance)
        {
            _network      = new RoutingNetwork(map, profile, maxEdgeDistance);
            _edgeProfiles = new AttributesIndex(map, AttributesIndexMode.IncreaseOne |
                                                AttributesIndexMode.ReverseCollectionIndex | AttributesIndexMode.ReverseStringIndex);
            _meta   = new AttributesIndex(map);
            _dbMeta = new AttributeCollection();

            _supportedProfiles = new HashSet <string>();
            _contracted        = new Dictionary <string, ContractedDb>();
            _restrictionDbs    = new Dictionary <string, RestrictionsDb>();
            _shortcutsDbs      = new Dictionary <string, ShortcutsDb>();

            _guid = Guid.NewGuid();
        }
Ejemplo n.º 20
0
        /// <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);
            _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();
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Creates a new router database.
        /// </summary>
        private RouterDb(Guid guid, RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, IAttributeCollection dbMeta,
                         string[] supportedProfiles)
        {
            _guid         = guid;
            _network      = network;
            _edgeProfiles = profiles;
            _meta         = meta;
            _dbMeta       = dbMeta;

            _supportedProfiles = new HashSet <string>();
            foreach (var supportedProfile in supportedProfiles)
            {
                _supportedProfiles.Add(supportedProfile);
            }
            _contracted     = new Dictionary <string, ContractedDb>();
            _restrictionDbs = new Dictionary <string, RestrictionsDb>();
            _shortcutsDbs   = new Dictionary <string, ShortcutsDb>();
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Deserializes a shortcuts db and leaves the stream position at the end of the shortcut db data.
        /// </summary>
        public static ShortcutsDb Deserialize(Stream stream)
        {
            var version = stream.ReadByte();

            if (version != 1)
            {
                throw new Exception(string.Format("Cannot deserialize shortcuts db: Invalid version #: {0}. Try upgrading Itinero or rebuild routing file with older version.", version));
            }

            // read profile name.
            var     profileName = stream.ReadWithSizeString();
            Profile profile;

            if (!Profile.TryGet(profileName, out profile))
            {
                throw new Exception(string.Format("Cannot deserialize shortcuts db: Profile not found: {0}. Make sure to register all vehicle profile before deserializing.", profileName));
            }

            // read meta-data.
            var metaDb = stream.ReadWithSizeAttributesCollection();

            // read stops and shortcuts data sizes.
            var bytes = new byte[4];

            stream.Read(bytes, 0, 4);
            var stopsPointer = BitConverter.ToUInt32(bytes, 0);

            stream.Read(bytes, 0, 4);
            var shortcutsPointer = BitConverter.ToUInt32(bytes, 0);

            // read stops meta and data.
            var stopsMeta = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var stops     = new MemoryArray <uint>(stopsPointer);

            stops.CopyFrom(stream);

            // read shortcuts meta and data.
            var shortcutsMeta = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var shortcuts     = new MemoryArray <uint>(shortcutsPointer);

            shortcuts.CopyFrom(stream);

            return(new ShortcutsDb(profile, metaDb, stopsMeta, stops, shortcutsMeta, shortcuts));
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Creates a new router database.
        /// </summary>
        public RouterDb(RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, IAttributeCollection dbMeta,
                        params Profiles.Profile[] supportedProfiles)
        {
            _network      = network;
            _edgeProfiles = profiles;
            _meta         = meta;
            _dbMeta       = dbMeta;

            _supportedProfiles = new HashSet <string>();
            foreach (var supportedProfile in supportedProfiles)
            {
                _supportedProfiles.Add(supportedProfile.Name);
            }
            _contracted     = new Dictionary <string, ContractedDb>();
            _restrictionDbs = new Dictionary <string, RestrictionsDb>();
            _shortcutsDbs   = new Dictionary <string, ShortcutsDb>();

            _guid = Guid.NewGuid();
        }
Ejemplo n.º 24
0
        public void TestWritableAfterDeserialization()
        {
            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);

                var id4 = index.Add(new Attribute("key3", "value4"));

                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);

                attributes = index.Get(id4);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key3", attributes.First().Key);
                Assert.AreEqual("value4", attributes.First().Value);
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Moves the enumerator until the trip that satisfies the filter.
        /// </summary>
        public static bool MoveUntil(this TripsDb.Enumerator enumerator, AttributesIndex tripAttributes, Func <IAttributeCollection, bool> filter)
        {
            if (enumerator == null)
            {
                throw new ArgumentNullException(nameof(enumerator));
            }
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            while (enumerator.MoveNext())
            {
                var meta = tripAttributes.Get(enumerator.MetaId);

                if (filter(meta))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 26
0
        public void TestAdd()
        {
            var index = new AttributesIndex();

            Assert.AreEqual(0, index.Add(null));
            Assert.AreEqual(1, index.Add(new AttributeCollection()));
            Assert.AreEqual(2, index.Add(new Attribute("key1", "value1")));  // adds 9 bytes to index's index, 1 byte for size and two 4-byte points to string-table.
            Assert.AreEqual(2, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(11, index.Add(new Attribute("key2", "value1"))); // adds another 9 bytes to index's index, 1 byte for size and two 4-byte points to string-table.
            Assert.AreEqual(20, index.Add(new Attribute("key2", "value2"))); // adds another 9 bytes to index's index, 1 byte for size and two 4-byte points to string-table.

            index = new AttributesIndex(AttributesIndexMode.IncreaseOne | AttributesIndexMode.ReverseStringIndex |
                                        AttributesIndexMode.ReverseCollectionIndex);

            Assert.AreEqual(0, index.Add(null));
            Assert.AreEqual(1, index.Add(new AttributeCollection()));
            Assert.AreEqual(2, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(2, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(3, index.Add(new Attribute("key2", "value1")));
            Assert.AreEqual(4, index.Add(new Attribute("key2", "value2")));

            index = new AttributesIndex(AttributesIndexMode.ReverseStringIndexKeysOnly);

            Assert.AreEqual(0, index.Add(null));
            Assert.AreEqual(1, index.Add(new AttributeCollection()));
            Assert.AreEqual(2, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(11, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(20, index.Add(new Attribute("key2", "value1")));
            Assert.AreEqual(29, index.Add(new Attribute("key2", "value2")));

            index = new AttributesIndex(AttributesIndexMode.None);

            Assert.AreEqual(0, index.Add(null));
            Assert.AreEqual(1, index.Add(new AttributeCollection()));
            Assert.AreEqual(2, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(11, index.Add(new Attribute("key1", "value1")));
            Assert.AreEqual(20, index.Add(new Attribute("key2", "value1")));
            Assert.AreEqual(29, index.Add(new Attribute("key2", "value2")));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Creates a new router database.
        /// </summary>
        public RouterDb(RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, IAttributeCollection dbMeta,
                        params Profiles.Vehicle[] supportedVehicles)
        {
            _network      = network;
            _edgeProfiles = profiles;
            _meta         = meta;
            _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>();

            _guid = Guid.NewGuid();
        }
Ejemplo n.º 28
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));
                    }
                }
            }
        }
Ejemplo n.º 29
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);
            }
        }
Ejemplo n.º 30
0
        public void TestGet()
        {
            var index = new AttributesIndex();

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

            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);

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

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

            for (var i = 0; i < 1000; i++)
            {
                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 = index.Add(attributes);
                refIndex[id] = attributes;
            }

            foreach (var refAttribute in refIndex)
            {
                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));
                }
            }

            index = new AttributesIndex(AttributesIndexMode.IncreaseOne);

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

            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;
            index    = new AttributesIndex(AttributesIndexMode.IncreaseOne);
            refIndex = new Dictionary <uint, IAttributeCollection>();
            for (var i = 0; i < 1000; i++)
            {
                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 = index.Add(attributes);
                refIndex[id] = attributes;
            }

            foreach (var refAttribute in refIndex)
            {
                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));
                }
            }
        }