Esempio n. 1
0
            public Trie <T> SearchTrie(Trie <T> current, string term)
            {
                for (int i = 0; i < term.Length; i++)
                {
                    if (CurrentIndex < current.Key.Length)
                    {
                        // if(current.Key[currentIndex] != term[i])
                        if (CharEqualsAt(current.Key[CurrentIndex], term[i]) == false)
                        {
                            if (current.Key[CurrentIndex] == '$')
                            {
                                Match.MatchLength = i;
                                Value             = current.Value;
                                return(current);
                            }
                            return(current);
                        }
                        CurrentIndex++;
                        continue;
                    }
                    // end of node, need to search children
                    if (current.Children == null)
                    {
                        return(null);
                    }

                    var maybe = current.GetNodeAt(term[i]);
                    if (maybe == null)
                    {
                        maybe = current.GetNodeAt('*');
                        if (maybe != null)
                        {
                            Match.CaptureStart = i;
                            for (; i < term.Length; i++)
                            {
                                if (term[i] == '/')
                                {
                                    break;
                                }
                            }
                            Match.CaptureLength = i - Match.CaptureStart;
                            i--;
                        }
                        else
                        {
                            maybe = current.GetNodeAt('$');
                            if (maybe != null)
                            {
                                CurrentIndex      = 0;
                                Match.MatchLength = i;
                                Value             = maybe.Value;
                                return(maybe);
                            }
                        }
                    }
                    current      = maybe;
                    CurrentIndex = 1;
                    if (current == null)
                    {
                        return(null);
                    }
                }
                Match.MatchLength = term.Length;
                return(current);
            }
Esempio n. 2
0
        public RequestRouter(Dictionary <string, RouteInformation> routes, RavenServer ravenServer)
        {
            _trie = Trie <RouteInformation> .Build(routes);

            _ravenServer = ravenServer;
        }
Esempio n. 3
0
        private static void Build(Trie <T> current, Dictionary <string, T> source, string[] sortedKeys, int matchStart, int start, int count)
        {
            if (count == 1)
            {
                // just one entry, build the trie node
                current.Key = sortedKeys[start].Substring(matchStart, sortedKeys[start].Length - matchStart);
                if (HandleStarRoute(current, source, sortedKeys, matchStart, start, count))
                {
                    return;
                }

                current.Value = source[sortedKeys[start]];
                return;
            }
            current.Children = new Trie <T> [127];
            var minKey        = sortedKeys[start];
            var maxKey        = sortedKeys[start + count - 1];
            var matchingIndex = matchStart;

            for (int i = matchingIndex; i < Math.Min(minKey.Length, maxKey.Length); i++)
            {
                if (minKey[i] == maxKey[i])
                {
                    continue;
                }
                matchingIndex = i;
                break;
            }


            if (maxKey.StartsWith(minKey))
            {
                current.Value = source[minKey];
                current.Key   = minKey.Substring(matchStart, minKey.Length - matchStart);
                AddChild(current, source, sortedKeys, minKey.Length, start + 1, count - 1);
                return;
            }

            current.Key = minKey.Substring(matchStart, matchingIndex - matchStart);
            if (HandleStarRoute(current, source, sortedKeys, matchStart, start, count))
            {
                return;
            }
            var childStart = start;
            var childCount = 1;

            while (childStart + childCount < start + count)
            {
                var nextKey = sortedKeys[childStart + childCount];
                if (matchingIndex < nextKey.Length && CharEqualsAt(nextKey[matchingIndex], minKey[matchingIndex]))
                {
                    childCount++;
                    continue;
                }
                minKey = nextKey;
                AddChild(current, source, sortedKeys, matchingIndex, childStart, childCount);
                childStart += childCount;
                childCount  = 1;
            }
            AddChild(current, source, sortedKeys, matchingIndex, childStart, childCount);
        }