Пример #1
0
        public IMatch NextMatch()
        {
            if (consumed)
            {
                return(null);
            }

            var target = Occurrence.Empty;
            var i      = 0;

            matches[i] = null;
            while (i < queries.Length)
            {
                var newTarget = Occurrence.Empty;

                while (matches[i] == null || matches[i].Right < target)
                {
                    var m = queries[i].NextMatch();
                    matches[i] = m;
                    if (m == null)
                    {
                        consumed = true;
                        return(null);
                    }

                    newTarget = Occurrence.O(
                        Math.Max(target.DocumentId, m.Right.DocumentId),
                        Math.Max(target.FieldId, m.Right.FieldId),
                        0);
                }


                if (target < newTarget)
                {
                    target = newTarget;
                    i      = 0;
                }
                else if (i == 0)
                {
                    i++;
                }
                else if (matches[i - 1].Right.TokenId + 1 == matches[i].Left.TokenId)
                {
                    i++;
                }
                else
                {
                    matches[i] = null;
                }
            }

            return(new SequenceMatch(matches));
        }
Пример #2
0
        private void AddTerms(ulong id, IEnumerable <string> terms)
        {
            ulong tokenId = 0;

            foreach (var term in terms)
            {
                List <Occurrence> postingList;
                if (!temp.TryGetValue(term, out postingList))
                {
                    postingList = new List <Occurrence>();
                    temp.Add(term, postingList);
                }
                postingList.Add(Occurrence.O(id, 1, ++tokenId));
            }
        }
        private void AddTerms(ulong id, ulong fieldId, IEnumerable <ScopedToken> terms)
        {
            var   positions = new List <TextPosition>();
            ulong tokenId   = 0;

            foreach (var token in terms)
            {
                var term = token.AsString();
                if (!temp.TryGetValue(term, out var postingList))
                {
                    postingList = new List <Occurrence>();
                    temp.Add(term, postingList);
                }

                positions.Add(TextPosition.P(token.CharOffset, token.Length));
                postingList.Add(Occurrence.O(id, fieldId, ++tokenId));
            }

            AddDocVector(id, fieldId, positions);
        }
        private static void ParseBufferTo(byte[] buffer, IList <Occurrence> occurrences)
        {
            var numbers = GroupVarint.Decode(buffer);

            var o = new Occurrence((ulong)numbers[0],
                                   (ulong)numbers[1],
                                   (ulong)numbers[2]);

            occurrences.Add(o);
            int i = 3;

            while (i < numbers.Count)
            {
                uint deltaSelector = (uint)numbers[i];
                ++i;
                while (deltaSelector > 0)
                {
                    int delta = (int)(deltaSelector & 0b00000011);
                    deltaSelector >>= 2;

                    if (i + delta > numbers.Count)
                    {
                        throw new Exception("Attempt to read above data");
                    }

                    switch (delta)
                    {
                    case 0:
                    {
                        throw new Exception("Zero delta is not used, see comments in DeltaWriter");
                    }

                    case 1:
                    {
                        o  = Occurrence.O(o.DocumentId, o.FieldId, o.TokenId + (ulong)numbers[i]);
                        i += 1;
                        occurrences.Add(o);
                        break;
                    }

                    case 2:
                    {
                        o  = Occurrence.O(o.DocumentId, o.FieldId + (ulong)numbers[i], (ulong)numbers[i + 1]);
                        i += 2;
                        occurrences.Add(o);
                        break;
                    }

                    case 3:
                    {
                        o  = Occurrence.O(o.DocumentId + (ulong)numbers[i], (ulong)numbers[i + 1], (ulong)numbers[i + 2]);
                        i += 3;
                        occurrences.Add(o);
                        break;
                    }

                    default:
                    {
                        throw new Exception("Something wrong");
                    }
                    }
                }
            }
        }