Ejemplo n.º 1
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));
 }
Ejemplo n.º 2
0
        public int GetOrCreate(ReadOnlySpan <byte> value)
        {
            var size = 1 + value.Length;

            //Todo: faire pool avec taille exacte
            using var array = MemoryPool <byte> .Shared.Rent(size);

            var valueBytes = array.Memory.Slice(0, size).Span;

            valueBytes[0] = (byte)RocksDbStorage.StoreType.Alias;
            value.CopyTo(valueBytes.Slice(1));

            var exising = Storage.Database.Get(valueBytes);

            if (exising != null)
            {
                return(BinaryPrimitives.ReadInt32BigEndian(exising));
            }

            lock (this)
            {
                exising = Storage.Database.Get(valueBytes);
                if (exising != null)
                {
                    return(BinaryPrimitives.ReadInt32BigEndian(exising));
                }

                WriteBatch b = new();
                Storage.IncrementCounterValue(b, RocksDbStorage.Counter.LastAlias, Span <byte> .Empty);
                Storage.Database.Write(b);

                Span <byte> idBytes = stackalloc byte[4];
                var         id      = (int)Storage.GetCounterValue(RocksDbStorage.Counter.LastAlias, Span <byte> .Empty);
                BinaryPrimitives.WriteInt32BigEndian(idBytes, id);


                Storage.Database.Put(valueBytes, idBytes);
                return(id);
            }
        }