Exemplo n.º 1
0
 public TValue this[TKey key]
 {
     get
     {
         var keyBytes = KeyToByteArray(key);
         _keyValueTrProtector.Start();
         _keyValueTr.SetKeyPrefix(_prefix);
         var found = _keyValueTr.FindExactKey(keyBytes);
         if (!found)
         {
             throw new ArgumentException("Key not found in Dictionary");
         }
         var valueBytes = _keyValueTr.GetValueAsByteArray();
         return(ByteArrayToValue(valueBytes));
     }
     set
     {
         var keyBytes   = KeyToByteArray(key);
         var valueBytes = ValueToByteArray(value);
         _keyValueTrProtector.Start();
         _keyValueTr.SetKeyPrefix(_prefix);
         if (_keyValueTr.CreateOrUpdateKeyValue(keyBytes, valueBytes))
         {
             _modificationCounter++;
             NotifyAdded();
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Reads and inserts all key value pairs into current prefix from stream
 /// </summary>
 /// <param name="transaction">transaction where to import all data</param>
 /// <param name="stream">where to read it from</param>
 public static void Import(IKeyValueDBTransaction transaction, Stream stream)
 {
     if (transaction == null) throw new ArgumentNullException(nameof(transaction));
     if (stream == null) throw new ArgumentNullException(nameof(stream));
     if (!stream.CanRead) throw new ArgumentException("stream must be readable", nameof(stream));
     var tempbuf = new byte[4096];
     var tempbuf2 = new byte[4096];
     if (stream.Read(tempbuf, 0, 16) != 16) throw new EndOfStreamException();
     if (tempbuf[0] != 'B' || tempbuf[1] != 'T' || tempbuf[2] != 'D' || tempbuf[3] != 'B' || tempbuf[4] != 'E' || tempbuf[5] != 'X' || tempbuf[6] != 'P' || tempbuf[7] != '2')
     {
         throw new BTDBException("Invalid header (it should Start with BTDBEXP2)");
     }
     var keyValuePairs = PackUnpack.UnpackInt64LE(tempbuf, 8);
     if (keyValuePairs < 0) throw new BTDBException("Negative number of key value pairs");
     for (var kv = 0; kv < keyValuePairs; kv++)
     {
         if (stream.Read(tempbuf, 0, 4) != 4) throw new EndOfStreamException();
         var keySize = PackUnpack.UnpackInt32LE(tempbuf, 0);
         if (keySize < 0) throw new BTDBException("Negative key size");
         if (keySize > tempbuf.Length) tempbuf = new byte[keySize];
         if (stream.Read(tempbuf, 0, keySize) != keySize) throw new EndOfStreamException();
         if (stream.Read(tempbuf2, 0, 4) != 4) throw new EndOfStreamException();
         var valueSize = PackUnpack.UnpackInt32LE(tempbuf2, 0);
         if (valueSize < 0) throw new BTDBException("Negative value size");
         if (valueSize > tempbuf2.Length) tempbuf2 = new byte[valueSize]; 
         if (stream.Read(tempbuf2, 0, valueSize) != valueSize) throw new EndOfStreamException();
         transaction.CreateOrUpdateKeyValue(ByteBuffer.NewSync(tempbuf,0,keySize),ByteBuffer.NewSync(tempbuf2,0,valueSize));
     }
 }
Exemplo n.º 3
0
 public static bool CreateKey(this IKeyValueDBTransaction transaction, byte[] keyBuf)
 {
     if (FindExactKey(transaction, keyBuf))
     {
         return(false);
     }
     return(transaction.CreateOrUpdateKeyValue(ByteBuffer.NewSync(keyBuf), ByteBuffer.NewEmpty()));
 }
Exemplo n.º 4
0
        void StoreObject(object o)
        {
            var type = o.GetType();

            if (!type.IsClass)
            {
                throw new BTDBException("You can store only classes, not " + type.ToSimpleName());
            }
            var tableInfo = _owner.TablesInfo.FindByType(type);

            IfNeededPersistTableInfo(tableInfo);
            DBObjectMetadata metadata = null;

            if (_objSmallMetadata != null)
            {
                _objSmallMetadata.TryGetValue(o, out metadata);
            }
            else if (_objBigMetadata != null)
            {
                _objBigMetadata.TryGetValue(o, out metadata);
            }
            if (metadata == null)
            {
                throw new BTDBException("Metadata for object not found");
            }
            if (metadata.State == DBObjectState.Deleted)
            {
                return;
            }
            var writer = new ByteBufferWriter();

            writer.WriteVUInt32(tableInfo.Id);
            writer.WriteVUInt32(tableInfo.ClientTypeVersion);
            tableInfo.Saver(this, metadata, writer, o);
            if (tableInfo.IsSingletonOid(metadata.Id))
            {
                tableInfo.CacheSingletonContent(_transactionNumber + 1, null);
            }
            _keyValueTrProtector.Start();
            _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
            _keyValueTr.CreateOrUpdateKeyValue(BuildKeyFromOid(metadata.Id), writer.Data.ToByteArray());
        }
Exemplo n.º 5
0
 internal void CommitLastDictId(ulong newLastDictId, IKeyValueDBTransaction tr)
 {
     if (_lastDictId != newLastDictId)
     {
         tr.SetKeyPrefix(null);
         var w = new ByteBufferWriter();
         w.WriteVUInt64(newLastDictId);
         tr.CreateOrUpdateKeyValue(LastDictIdKey, w.Data.ToByteArray());
         _lastDictId = newLastDictId;
     }
 }
Exemplo n.º 6
0
 internal void CommitLastDictId(ulong newLastDictId, IKeyValueDBTransaction tr)
 {
     if (_lastDictId != newLastDictId)
     {
         tr.SetKeyPrefix(null);
         var w = new ByteBufferWriter();
         w.WriteVUInt64(newLastDictId);
         tr.CreateOrUpdateKeyValue(LastDictIdKey, w.Data.ToByteArray());
         _lastDictId = newLastDictId;
     }
 }
Exemplo n.º 7
0
        public bool Add(TKey key)
        {
            var keyBytes = KeyToByteArray(key);

            _modificationCounter++;
            var created = _keyValueTr.CreateOrUpdateKeyValue(keyBytes, new ReadOnlySpan <byte>());

            if (created)
            {
                NotifyAdded();
            }
            return(created);
        }
Exemplo n.º 8
0
        public bool Add(TKey key)
        {
            var keyBytes = KeyToByteArray(key);

            _keyValueTrProtector.Start();
            _modificationCounter++;
            _keyValueTr.SetKeyPrefix(_prefix);
            var created = _keyValueTr.CreateOrUpdateKeyValue(keyBytes, ByteBuffer.NewEmpty());

            if (created)
            {
                NotifyAdded();
            }
            return(created);
        }
Exemplo n.º 9
0
        public bool Insert(T obj)
        {
            Debug.Assert(typeof(T) == obj.GetType(), AssertNotDerivedTypesMsg);

            var keyBytes   = KeyBytes(obj);
            var valueBytes = ValueBytes(obj);

            ResetKeyPrefix();

            if (_kvtr.Find(keyBytes) == FindResult.Exact)
            {
                return(false);
            }

            _kvtr.CreateOrUpdateKeyValue(keyBytes, valueBytes);

            if (_hasSecondaryIndexes)
            {
                AddIntoSecondaryIndexes(obj);
            }

            _modificationCounter.MarkModification();
            return(true);
        }
Exemplo n.º 10
0
 public bool CreateOrUpdateKeyValue(byte[] keyBuf, int keyOfs, int keyLen, byte[] valueBuf, int valueOfs, int valueLen)
 {
     lock (_log)
     {
         _log.WriteUInt8((byte)KVReplayOperation.CreateOrUpdateKeyValue);
         _log.WriteVUInt32(TrIndex);
         _log.WriteVInt32(keyLen);
         _log.WriteVInt32(keyOfs);
         _log.WriteBlock(keyBuf, keyOfs, keyLen);
         _log.WriteVInt32(valueLen);
         _log.WriteVInt32(valueOfs);
         _log.WriteBlock(valueBuf, valueOfs, valueLen);
         _log.FlushBuffer();
     }
     return(_tr.CreateOrUpdateKeyValue(keyBuf, keyOfs, keyLen, valueBuf, valueOfs, valueLen));
 }
Exemplo n.º 11
0
        void StoreSingletonOid(uint tableId, ulong oid)
        {
            bool taken = false;

            try
            {
                _keyValueTrProtector.Start(ref taken);
                _keyValueTr.SetKeyPrefix(ObjectDB.TableSingletonsPrefix);
                _keyValueTr.CreateOrUpdateKeyValue(BuildKeyFromOid(tableId), BuildKeyFromOid(oid));
            }
            finally
            {
                if (taken)
                {
                    _keyValueTrProtector.Stop();
                }
            }
        }
 public bool CreateOrUpdateKeyValue(ByteBuffer key, ByteBuffer value)
 {
     return(_keyValueDBTransaction.CreateOrUpdateKeyValue(key, value));
 }
Exemplo n.º 13
0
        /// <summary>
        /// Reads and inserts all key value pairs into current prefix from stream
        /// </summary>
        /// <param name="transaction">transaction where to import all data</param>
        /// <param name="stream">where to read it from</param>
        public static void Import(IKeyValueDBTransaction transaction, Stream stream)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("stream must be readable", nameof(stream));
            }
            var tempbuf  = new byte[4096];
            var tempbuf2 = new byte[4096];

            if (stream.Read(tempbuf, 0, 16) != 16)
            {
                throw new EndOfStreamException();
            }
            if (tempbuf[0] != 'B' || tempbuf[1] != 'T' || tempbuf[2] != 'D' || tempbuf[3] != 'B' || tempbuf[4] != 'E' || tempbuf[5] != 'X' || tempbuf[6] != 'P' || tempbuf[7] != '2')
            {
                throw new BTDBException("Invalid header (it should Start with BTDBEXP2)");
            }
            var keyValuePairs = PackUnpack.UnpackInt64LE(tempbuf, 8);

            if (keyValuePairs < 0)
            {
                throw new BTDBException("Negative number of key value pairs");
            }
            for (var kv = 0; kv < keyValuePairs; kv++)
            {
                if (stream.Read(tempbuf, 0, 4) != 4)
                {
                    throw new EndOfStreamException();
                }
                var keySize = PackUnpack.UnpackInt32LE(tempbuf, 0);
                if (keySize < 0)
                {
                    throw new BTDBException("Negative key size");
                }
                if (keySize > tempbuf.Length)
                {
                    tempbuf = new byte[keySize];
                }
                if (stream.Read(tempbuf, 0, keySize) != keySize)
                {
                    throw new EndOfStreamException();
                }
                if (stream.Read(tempbuf2, 0, 4) != 4)
                {
                    throw new EndOfStreamException();
                }
                var valueSize = PackUnpack.UnpackInt32LE(tempbuf2, 0);
                if (valueSize < 0)
                {
                    throw new BTDBException("Negative value size");
                }
                if (valueSize > tempbuf2.Length)
                {
                    tempbuf2 = new byte[valueSize];
                }
                if (stream.Read(tempbuf2, 0, valueSize) != valueSize)
                {
                    throw new EndOfStreamException();
                }
                transaction.CreateOrUpdateKeyValue(tempbuf.AsSpan(0, keySize), tempbuf2.AsSpan(0, valueSize));
            }
            if (stream.Read(tempbuf, 0, 8) == 8)
            {
                transaction.SetCommitUlong(PackUnpack.UnpackUInt64LE(tempbuf, 0));
                if (stream.Read(tempbuf, 0, 4) == 4)
                {
                    var ulongCount = PackUnpack.UnpackUInt32LE(tempbuf, 0);
                    for (var i = 0u; i < ulongCount; i++)
                    {
                        if (stream.Read(tempbuf, 0, 8) != 8)
                        {
                            throw new EndOfStreamException();
                        }
                        transaction.SetUlong(i, PackUnpack.UnpackUInt64LE(tempbuf, 0));
                    }
                }
            }
        }
Exemplo n.º 14
0
 public static bool CreateOrUpdateKeyValue(this IKeyValueDBTransaction transaction, byte[] keyBuf, byte[] valueBuf)
 {
     return(transaction.CreateOrUpdateKeyValue(keyBuf, 0, keyBuf.Length, valueBuf, 0, valueBuf.Length));
 }
Exemplo n.º 15
0
 public static bool CreateOrUpdateKeyValue(this IKeyValueDBTransaction transaction, byte[] keyBuf, byte[] valueBuf)
 {
     return(transaction.CreateOrUpdateKeyValue(ByteBuffer.NewSync(keyBuf), ByteBuffer.NewSync(valueBuf)));
 }