Пример #1
0
        public ulong GetCounterValue(Counter counter, Span <byte> index)
        {
            using var _ = RocksDbEncoder.EncodeCounter(counter, index, out var key);
            var result = Database.Get(key);

            return(result != null?BinaryPrimitives.ReadUInt64LittleEndian(result) : 0);
        }
Пример #2
0
        internal void IncrementCounterValue(WriteBatch batch, Counter counter, Span <byte> index)
        {
            using var _ = RocksDbEncoder.EncodeCounter(counter, index, out var key);

            byte[] newValueBytes = new byte[8];
            BinaryPrimitives.WriteUInt64LittleEndian(newValueBytes, 1);
            // Todo: Allocation
            batch.Merge(key.ToArray(), (ulong)key.Length, newValueBytes, (ulong)newValueBytes.Length);
        }
Пример #3
0
        public unsafe bool SeekCeil(ReadOnlyMemory <byte> data)
        {
            using var _ = RocksDbEncoder.EncodeInvertedIndexTerm(data.Span, out var bytes);
            fixed(byte *b = &MemoryMarshal.GetReference(bytes))
            {
                RocksDbIterator.Seek(b, (ulong)bytes.Length);
            }

            UpdateIteratorState();
            return(RocksIteratorValid);
        }
Пример #4
0
 internal RocksDbDocsIdSetEnumerator(RocksDbStorage storage, int termId, int?propertyId)
 {
     Storage          = storage;
     TermId           = termId;
     PropertyId       = propertyId;
     RocksDbIterator  = storage.Database.NewIterator();
     RocksIteratorKey = Array.Empty <byte>();
     Prefix           = RocksDbEncoder.EncodeInvertedIndexPostingPrefix(termId, propertyId);
     // Todo: Create encoder instead of slicing or create a special method
     Cost = propertyId != null
         ? (long)Storage.GetCounterValue(RocksDbStorage.Counter.TermFrequencyByTermAndProperty, Prefix.AsSpan().Slice(1))
         : (long)Storage.GetCounterValue(RocksDbStorage.Counter.TermFrequencyByTerm, Prefix.AsSpan().Slice(1));
 }
Пример #5
0
        protected void AddInvertedIndexTerm(WriteBatch batch, ReadOnlySpan <byte> property, ReadOnlySpan <byte> term,
                                            int docId)
        {
            var propertyId = AliasStore.GetOrCreate(property);
            var termId     = AliasStore.GetOrCreate(term);

            using (RocksDbEncoder.EncodeInt32(termId, out var termIdBytes))
            {
                using (RocksDbEncoder.EncodeInvertedIndexTerm(term, out var array))
                {
                    batch.Put(array, termIdBytes);
                    AddInvertedIndexPosting(batch, propertyId, termId, docId);
                }
            }
        }
Пример #6
0
        protected void AddInvertedIndexPosting(WriteBatch batch, int propertyId, int termId, int docId)
        {
            using var posting1 = RocksDbEncoder.EncodeInvertedIndexPostingWithProperty(termId, propertyId, docId, out var span1);
            using var posting2 = RocksDbEncoder.EncodeInvertedIndexPosting(termId, docId, out var span2);

            batch.Put(span1, ReadOnlySpan <byte> .Empty);
            IncrementCounterValue(batch, Counter.TermFrequencyByTermAndProperty, span1.Slice(1, 8));

            batch.Put(span2, ReadOnlySpan <byte> .Empty);
            //Todo: Slow
            if (Database.Get(span2) == null)
            {
                IncrementCounterValue(batch, Counter.TermFrequencyByTerm, span2.Slice(1, 4));
            }
        }
Пример #7
0
        public bool Advance(int target)
        {
            if (!PropertyId.HasValue)
            {
                using var _ = RocksDbEncoder.EncodeInvertedIndexPosting(TermId, target, out var prefix);
                RocksDbIterator.Seek(prefix.ToArray());
            }
            else
            {
                using var _ = RocksDbEncoder.EncodeInvertedIndexPostingWithProperty(TermId, PropertyId.Value, target, out var prefix);
                RocksDbIterator.Seek(prefix.ToArray());
            }

            Positioned = true;
            UpdateIteratorState();
            return(RocksIteratorValid);
        }
Пример #8
0
        protected void UpdateIteratorState()
        {
            var iteratorValid = RocksDbIterator.Valid();

            if (!iteratorValid)
            {
                RocksIteratorValid = false;
                return;
            }

            // Todo: Allocating, stackalloc here if rocksdb support span
            RocksIteratorKey   = RocksDbIterator.Key();
            RocksIteratorValid = RocksIteratorKey.AsSpan().StartsWith(Prefix.AsSpan());
            if (RocksIteratorValid)
            {
                CurrentDocId = RocksDbEncoder.ReadPostingDocId(RocksIteratorKey.AsSpan());
            }
        }