Example #1
0
        ByteBuffer Vuint2ByteBuffer(ulong v)
        {
            var ofs = 0;

            PackUnpack.PackVUInt(_tempBytes, ref ofs, v);
            return(ByteBuffer.NewSync(_tempBytes, 0, ofs));
        }
Example #2
0
        void CheckKeySequence(int keys)
        {
            var sw  = Stopwatch.StartNew();
            var key = new byte[8];

            using (var fileCollection = CreateTestFileCollection())
            {
                using (IKeyValueDB db = CreateKeyValueDB(fileCollection, new NoCompressionStrategy()))
                {
                    using (var tr = db.StartTransaction())
                    {
                        for (int i = 0; i < keys; i++)
                        {
                            int o = 0;
                            PackUnpack.PackVUInt(key, ref o, (uint)i);
                            tr.Find(ByteBuffer.NewSync(key, 0, o));
                        }

                        tr.Commit();
                    }
                }
            }

            Console.WriteLine("CheckSequence:" + sw.Elapsed.TotalMilliseconds);
        }
Example #3
0
        public void InternalDelete(object obj)
        {
            var o     = (IDBObject)obj;
            var oid   = o.Oid;
            var taken = false;

            try
            {
                _keyValueTrProtector.Start(ref taken);
                _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                var key = new byte[PackUnpack.LengthVUInt(oid)];
                int ofs = 0;
                PackUnpack.PackVUInt(key, ref ofs, oid);
                if (_keyValueTr.FindExactKey(key))
                {
                    _keyValueTr.EraseCurrent();
                }
            }
            finally
            {
                if (taken)
                {
                    _keyValueTrProtector.Stop();
                }
            }
            _objCache.TryRemove(oid);
            _dirtyObjSet.TryRemove(oid);
        }
Example #4
0
        ReadOnlySpan <byte> Vuint2ByteBuffer(uint v)
        {
            var ofs = 0;

            PackUnpack.PackVUInt(_tempBytes, ref ofs, v);
            return(_tempBytes.AsSpan(0, ofs));
        }
Example #5
0
        void PackVUIntIsOrderableCore(ulong t)
        {
            var buf1 = new byte[9];
            var o1   = 0;

            PackUnpack.PackVUInt(buf1, ref o1, t - 1);
            var buf2 = new byte[9];
            var o2   = 0;

            PackUnpack.PackVUInt(buf2, ref o2, t);
            if (t <= uint.MaxValue)
            {
                Assert.Equal(o2, PackUnpack.LengthVUInt((uint)t));
            }
            Assert.Equal(o2, PackUnpack.LengthVUInt(t));
            Assert.Equal(o2, PackUnpack.LengthVUInt(buf2, 0));
            Assert.True(0 > BitArrayManipulation.CompareByteArray(buf1, o1, buf2, o2));
            var o1A = 0;

            Assert.Equal(t - 1, PackUnpack.UnpackVUInt(buf1, ref o1A));
            Assert.Equal(o1, o1A);
            var o2A = 0;

            Assert.Equal(t, PackUnpack.UnpackVUInt(buf2, ref o2A));
            Assert.Equal(o2, o2A);
        }
Example #6
0
 uint ITableInfoResolver.GetLastPesistedVersion(uint id)
 {
     using (var tr = _keyValueDB.StartTransaction())
     {
         tr.SetKeyPrefix(TableVersionsPrefix);
         var key = new byte[PackUnpack.LengthVUInt(id) + 1];
         var ofs = 0;
         PackUnpack.PackVUInt(key, ref ofs, id);
         key[ofs] = 0xff;
         if (tr.FindKey(key, 0, key.Length, FindKeyStrategy.PreferPrevious) == FindKeyResult.NotFound)
         {
             return(0);
         }
         var key2 = tr.ReadKey();
         if (key2.Length < ofs)
         {
             return(0);
         }
         if (BitArrayManipulation.CompareByteArray(key, 0, ofs, key2, 0, ofs) != 0)
         {
             return(0);
         }
         return(checked ((uint)PackUnpack.UnpackVUInt(key2, ref ofs)));
     }
 }
Example #7
0
            public void Send(ByteBuffer data)
            {
                if (_disconnected)
                {
                    throw new SocketException((int)SocketError.NotConnected);
                }
                var vuLen = PackUnpack.LengthVUInt((uint)data.Length);
                var vuBuf = new byte[vuLen];
                int o     = 0;

                PackUnpack.PackVUInt(vuBuf, ref o, (uint)data.Length);
                SocketError socketError;

                lock (_sendlock)
                {
                    _socket.Send(new[] { new ArraySegment <byte>(vuBuf), data.ToArraySegment() }, SocketFlags.None,
                                 out socketError);
                }
                if (socketError == SocketError.Success)
                {
                    return;
                }
                if (!IsConnected())
                {
                    SignalDisconnected();
                }
                throw new SocketException((int)socketError);
            }
Example #8
0
        ByteBuffer TwiceVuint2ByteBuffer(uint v1, uint v2)
        {
            var ofs = 0;

            PackUnpack.PackVUInt(_tempBytes, ref ofs, v1);
            PackUnpack.PackVUInt(_tempBytes, ref ofs, v2);
            return(ByteBuffer.NewSync(_tempBytes, 0, ofs));
        }
Example #9
0
        static void WriteIdIl(IILGen ilGenerator, Action <IILGen> pushWriter, int id)
        {
            var bytes = new byte[PackUnpack.LengthVUInt((uint)id)];
            int o     = 0;

            PackUnpack.PackVUInt(bytes, ref o, (uint)id);
            WriteShortPrefixIl(ilGenerator, pushWriter, bytes);
        }
Example #10
0
        void GeneratePrefix()
        {
            var o = ObjectDB.AllDictionariesPrefix.Length;

            _prefix = new byte[o + PackUnpack.LengthVUInt(_id)];
            Array.Copy(ObjectDB.AllDictionariesPrefix, _prefix, o);
            PackUnpack.PackVUInt(_prefix, ref o, _id);
        }
Example #11
0
        internal static byte[] BuildKeyFromOid(ulong oid)
        {
            var key = new byte[PackUnpack.LengthVUInt(oid)];
            int ofs = 0;

            PackUnpack.PackVUInt(key, ref ofs, oid);
            return(key);
        }
Example #12
0
        static byte[] BuildRelationPrefix(uint relationIndex)
        {
            var o      = ObjectDB.AllRelationsPKPrefix.Length;
            var prefix = new byte[o + PackUnpack.LengthVUInt(relationIndex)];

            Array.Copy(ObjectDB.AllRelationsPKPrefix, prefix, o);
            PackUnpack.PackVUInt(prefix, ref o, relationIndex);
            return(prefix);
        }
Example #13
0
        internal static byte[] BuildKeyForTableVersions(uint tableId, uint tableVersion)
        {
            var key = new byte[PackUnpack.LengthVUInt(tableId) + PackUnpack.LengthVUInt(tableVersion)];
            var ofs = 0;

            PackUnpack.PackVUInt(key, ref ofs, tableId);
            PackUnpack.PackVUInt(key, ref ofs, tableVersion);
            return(key);
        }
Example #14
0
        static byte[] BuildRelationSecondaryKeyPrefix(uint relationIndex, uint secondaryKeyIndex)
        {
            var prefix = new byte[1 + PackUnpack.LengthVUInt(relationIndex) + PackUnpack.LengthVUInt(secondaryKeyIndex)];

            prefix[0] = ObjectDB.AllRelationsSKPrefixByte;
            int pos = 1;

            PackUnpack.PackVUInt(prefix, ref pos, relationIndex);
            PackUnpack.PackVUInt(prefix, ref pos, secondaryKeyIndex);
            return(prefix);
        }
Example #15
0
        internal static void FreeObject(IInternalObjectDBTransaction tr, ulong oid)
        {
            var o      = ObjectDB.AllDictionariesPrefix.Length;
            var prefix = new byte[o + PackUnpack.LengthVUInt(oid)];

            Array.Copy(ObjectDB.AllObjectsPrefix, prefix, o);
            PackUnpack.PackVUInt(prefix, ref o, oid);
            tr.TransactionProtector.Start();
            tr.KeyValueDBTransaction.SetKeyPrefixUnsafe(prefix);
            tr.KeyValueDBTransaction.EraseAll();
        }
Example #16
0
        void IterateSet(ulong dictId, IFieldHandler keyHandler)
        {
            if (_visitor != null && !_visitor.StartSet())
            {
                return;
            }
            var o      = ObjectDB.AllDictionariesPrefix.Length;
            var prefix = new byte[o + PackUnpack.LengthVUInt(dictId)];

            Array.Copy(ObjectDB.AllDictionariesPrefix, prefix, o);
            PackUnpack.PackVUInt(prefix, ref o, dictId);
            long prevProtectionCounter = 0;
            long pos = 0;

            while (true)
            {
                if (pos == 0)
                {
                    if (!_trkv.FindFirstKey(prefix))
                    {
                        break;
                    }
                }
                else
                {
                    if (_trkv.CursorMovedCounter != prevProtectionCounter)
                    {
                        if (!_trkv.SetKeyIndex(prefix, pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_trkv.FindNextKey(prefix))
                        {
                            break;
                        }
                    }
                }
                _fastVisitor.MarkCurrentKeyAsUsed(_trkv);
                prevProtectionCounter = _trkv.CursorMovedCounter;
                if (_visitor == null || _visitor.StartSetKey())
                {
                    var keyReader = new SpanReader(_trkv.GetKey().Slice(prefix.Length));
                    IterateHandler(ref keyReader, keyHandler, false, null);
                    _visitor?.EndSetKey();
                }
                pos++;
            }
            _visitor?.EndSet();
        }
Example #17
0
        internal void ResizeValue(byte[] newData, long newSize)
        {
            var currentEntrySize = CurrentEntrySize;
            var newEntrySize     = CalcEntrySize(KeyLen, newSize);
            int withValuePtr     = 0;

            if (HasValueSectorPtr && newSize > MaxValueLenInline)
            {
                withValuePtr = KeyValueDB.PtrDownSize;
            }
            // preserves all before current item including current sizes + key
            int ofs = EntryOffset + PackUnpack.LengthVUInt((uint)KeyLen);

            if (!ReferenceEquals(Data, newData))
            {
                Array.Copy(Data, 0, newData, 0, ofs);
            }
            if (newSize > ValueLen) // because resize could be inplace bytes have to be copied correct order
            {
                // preserves all after current item
                Array.Copy(Data,
                           EntryOffset + currentEntrySize - withValuePtr,
                           newData,
                           EntryOffset + newEntrySize - withValuePtr,
                           TotalLength - EntryOffset - currentEntrySize + withValuePtr);
                // preserves key of current item
                Array.Copy(Data, KeyOffset, newData, ofs + PackUnpack.LengthVUInt((ulong)newSize), ValueOffset - KeyOffset);
            }
            else
            {
                // preserves key of current item
                Array.Copy(Data, KeyOffset, newData, ofs + PackUnpack.LengthVUInt((ulong)newSize), ValueOffset - KeyOffset);
                // preserves all after current item
                Array.Copy(Data,
                           EntryOffset + currentEntrySize - withValuePtr,
                           newData,
                           EntryOffset + newEntrySize - withValuePtr,
                           TotalLength - EntryOffset - currentEntrySize + withValuePtr);
            }
            PackUnpack.PackVUInt(newData, ref ofs, (ulong)newSize);
            _data     = newData;
            _valueLen = newSize;
            _ofsAfterKeyAndValueLen = ofs;
            var delta = newEntrySize - currentEntrySize;

            for (int i = _pos; i < _count; i++)
            {
                var o = HeaderSize + HeaderForEntry * i;
                PackUnpack.PackUInt16LE(_data, o, (ushort)(PackUnpack.UnpackUInt16LE(_data, o) + delta));
            }
        }
Example #18
0
 public long GetSingletonOid(uint id)
 {
     using (var tr = _keyValueDB.StartTransaction())
     {
         tr.SetKeyPrefix(TableSingletonsPrefix);
         var key = new byte[PackUnpack.LengthVUInt(id)];
         var ofs = 0;
         PackUnpack.PackVUInt(key, ref ofs, id);
         if (tr.FindExactKey(key))
         {
             return((long)new KeyValueDBValueReader(tr).ReadVUInt64());
         }
         return(0);
     }
 }
Example #19
0
 TableVersionInfo ITableInfoResolver.LoadTableVersionInfo(uint id, uint version, string tableName)
 {
     using (var tr = _keyValueDB.StartTransaction())
     {
         tr.SetKeyPrefix(TableVersionsPrefix);
         var key = new byte[PackUnpack.LengthVUInt(id) + PackUnpack.LengthVUInt(version)];
         var ofs = 0;
         PackUnpack.PackVUInt(key, ref ofs, id);
         PackUnpack.PackVUInt(key, ref ofs, version);
         if (!tr.FindExactKey(key))
         {
             throw new BTDBException(string.Format("Missing TableVersionInfo Id:{0} Version:{1}", id, version));
         }
         return(TableVersionInfo.Load(new KeyValueDBValueReader(tr), _objectDB.FieldHandlerFactory, tableName));
     }
 }
Example #20
0
        public ODBSet(IInternalObjectDBTransaction tr, ODBDictionaryConfiguration config, ulong id)
        {
            _tr         = tr;
            _keyHandler = config.KeyHandler;
            _id         = id;
            var o = ObjectDB.AllDictionariesPrefix.Length;

            _prefix = new byte[o + PackUnpack.LengthVUInt(_id)];
            Array.Copy(ObjectDB.AllDictionariesPrefix, _prefix, o);
            PackUnpack.PackVUInt(_prefix, ref o, _id);
            _keyReader           = ((Func <AbstractBufferedReader, IReaderCtx, TKey>)config.KeyReader) !;
            _keyWriter           = ((Action <TKey, AbstractBufferedWriter, IWriterCtx>)config.KeyWriter) !;
            _keyValueTr          = _tr.KeyValueDBTransaction;
            _keyValueTrProtector = _tr.TransactionProtector;
            _count = -1;
        }
Example #21
0
        bool PersistTableInfo(TableInfo tableInfo)
        {
            var taken = false;

            try
            {
                _keyValueTrProtector.Start(ref taken);
                byte[] key;
                int    ofs;
                if (tableInfo.LastPersistedVersion <= 0)
                {
                    _keyValueTr.SetKeyPrefix(ObjectDB.TableNamesPrefix);
                    key = new byte[PackUnpack.LengthVUInt(tableInfo.Id)];
                    ofs = 0;
                    PackUnpack.PackVUInt(key, ref ofs, tableInfo.Id);
                    if (_keyValueTr.CreateKey(key))
                    {
                        using (var writer = new KeyValueDBValueWriter(_keyValueTr))
                        {
                            writer.WriteString(tableInfo.Name);
                        }
                    }
                }
                _keyValueTr.SetKeyPrefix(ObjectDB.TableVersionsPrefix);
                key = new byte[PackUnpack.LengthVUInt(tableInfo.Id) + PackUnpack.LengthVUInt(tableInfo.ClientTypeVersion)];
                ofs = 0;
                PackUnpack.PackVUInt(key, ref ofs, tableInfo.Id);
                PackUnpack.PackVUInt(key, ref ofs, tableInfo.ClientTypeVersion);
                if (_keyValueTr.CreateKey(key))
                {
                    var tableVersionInfo = tableInfo.ClientTableVersionInfo;
                    using (var writer = new KeyValueDBValueWriter(_keyValueTr))
                    {
                        tableVersionInfo.Save(writer);
                    }
                }
                return(true);
            }
            finally
            {
                if (taken)
                {
                    _keyValueTrProtector.Stop();
                }
            }
        }
Example #22
0
        public void WriteVUInt64(ulong value)
        {
            var l = PackUnpack.LengthVUInt(value);

            if (Pos + l > End)
            {
                FlushBuffer();
                if (Pos + l > End)
                {
                    var b = new byte[l];
                    int i = 0;
                    PackUnpack.PackVUInt(b, ref i, value);
                    WriteBlock(b);
                    return;
                }
            }
            PackUnpack.PackVUInt(Buf, ref Pos, value);
        }
Example #23
0
        public AbstractBufferedWriter PrepareToWriteObject(ulong id)
        {
            var shouldStop = false;

            try
            {
                _keyValueTrProtector.Start(ref shouldStop);
                _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                var key = new byte[PackUnpack.LengthVUInt(id)];
                var ofs = 0;
                PackUnpack.PackVUInt(key, ref ofs, id);
                _keyValueTr.CreateKey(key);
                var writer = new KeyValueDBValueProtectedWriter(_keyValueTr, _keyValueTrProtector);
                shouldStop = false;
                return(writer);
            }
            finally
            {
                if (shouldStop)
                {
                    _keyValueTrProtector.Stop();
                }
            }
        }
Example #24
0
        void IterateDict(ulong dictId, IFieldHandler keyHandler, IFieldHandler valueHandler)
        {
            if (_visitor != null && !_visitor.StartDictionary())
            {
                return;
            }
            var o      = ObjectDB.AllDictionariesPrefix.Length;
            var prefix = new byte[o + PackUnpack.LengthVUInt(dictId)];

            Array.Copy(ObjectDB.AllDictionariesPrefix, prefix, o);
            PackUnpack.PackVUInt(prefix, ref o, dictId);
            _trkv.SetKeyPrefix(prefix);
            var  protector             = _tr.TransactionProtector;
            long prevProtectionCounter = 0;
            long pos = 0;

            while (true)
            {
                protector.Start();
                if (pos == 0)
                {
                    _trkv.SetKeyPrefix(prefix);
                    if (!_trkv.FindFirstKey())
                    {
                        break;
                    }
                }
                else
                {
                    if (protector.WasInterupted(prevProtectionCounter))
                    {
                        _trkv.SetKeyPrefix(prefix);
                        if (!_trkv.SetKeyIndex(pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_trkv.FindNextKey())
                        {
                            break;
                        }
                    }
                }
                _fastVisitor.MarkCurrentKeyAsUsed(_trkv);
                prevProtectionCounter = protector.ProtectionCounter;
                if (_visitor == null || _visitor.StartDictKey())
                {
                    var keyReader = new KeyValueDBKeyReader(_trkv);
                    IterateHandler(keyReader, keyHandler, false, null);
                    _visitor?.EndDictKey();
                }
                if (protector.WasInterupted(prevProtectionCounter))
                {
                    _trkv.SetKeyPrefix(prefix);
                    if (!_trkv.SetKeyIndex(pos))
                    {
                        break;
                    }
                }
                if (_visitor == null || _visitor.StartDictValue())
                {
                    var valueReader = new KeyValueDBValueReader(_trkv);
                    IterateHandler(valueReader, valueHandler, false, null);
                    _visitor?.EndDictValue();
                }
                pos++;
            }
            _visitor?.EndDictionary();
        }
Example #25
0
        void IterateRelation(uint relationIndex, string name)
        {
            var relationVersions     = new Dictionary <uint, RelationVersionInfo>();
            var lastPersistedVersion = ReadRelationVersions(relationIndex, name, relationVersions);

            _tr.TransactionProtector.Start();

            var o      = ObjectDB.AllRelationsPKPrefix.Length;
            var prefix = new byte[o + PackUnpack.LengthVUInt(relationIndex)];

            Array.Copy(ObjectDB.AllRelationsPKPrefix, prefix, o);
            PackUnpack.PackVUInt(prefix, ref o, relationIndex);

            var  protector             = _tr.TransactionProtector;
            long prevProtectionCounter = 0;
            long pos = 0;

            while (true)
            {
                protector.Start();
                if (pos == 0)
                {
                    _trkv.SetKeyPrefix(prefix);
                    if (!_trkv.FindFirstKey())
                    {
                        break;
                    }
                }
                else
                {
                    if (protector.WasInterupted(prevProtectionCounter))
                    {
                        _trkv.SetKeyPrefix(prefix);
                        if (!_trkv.SetKeyIndex(pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_trkv.FindNextKey())
                        {
                            break;
                        }
                    }
                }
                _fastVisitor.MarkCurrentKeyAsUsed(_trkv);
                prevProtectionCounter = protector.ProtectionCounter;
                if (_visitor == null || _visitor.StartRelationKey())
                {
                    var keyReader    = new KeyValueDBKeyReader(_trkv);
                    var relationInfo = relationVersions[lastPersistedVersion];
                    IterateFields(keyReader, relationInfo.GetPrimaryKeyFields(), null);
                    _visitor?.EndRelationKey();
                }
                if (protector.WasInterupted(prevProtectionCounter))
                {
                    _trkv.SetKeyPrefix(prefix);
                    if (!_trkv.SetKeyIndex(pos))
                    {
                        break;
                    }
                }
                if (_visitor == null || _visitor.StartRelationValue())
                {
                    var valueReader  = new KeyValueDBValueReader(_trkv);
                    var version      = valueReader.ReadVUInt32();
                    var relationInfo = relationVersions[version];
                    IterateFields(valueReader, relationInfo.GetValueFields(), new HashSet <int>());
                    _visitor?.EndRelationValue();
                }
                pos++;
            }
        }
Example #26
0
        public unsafe void WriteString(string value)
        {
            if (value == null)
            {
                WriteByteZero();
                return;
            }
            var l = value.Length;

            if (l == 0)
            {
                WriteUInt8(1);
                return;
            }
            var buf = Buf;
            var pos = Pos;
            var end = End;

            fixed(char *strPtrStart = value)
            {
                var strPtr    = strPtrStart;
                var strPtrEnd = strPtrStart + l;
                var toEncode  = (uint)(l + 1);

doEncode:
                var toEncodeLen = PackUnpack.LengthVUInt(toEncode);

                if (pos + toEncodeLen <= end)
                {
                    PackUnpack.PackVUInt(buf, ref pos, toEncode);
                }
                else
                {
                    Pos = pos;
                    WriteVUInt32(toEncode);
                    buf = Buf;
                    pos = Pos;
                    end = End;
                }
                while (strPtr != strPtrEnd)
                {
                    var c = *strPtr++;
                    if (c < 0x80)
                    {
                        if (pos >= end)
                        {
                            Pos = pos;
                            FlushBuffer();
                            buf = Buf;
                            pos = Pos;
                            end = End;
                        }
                        buf[pos++] = (byte)c;
                    }
                    else
                    {
                        if (char.IsHighSurrogate(c) && strPtr != strPtrEnd)
                        {
                            var c2 = *strPtr;
                            if (char.IsLowSurrogate(c2))
                            {
                                toEncode = (uint)((c - 0xD800) * 0x400 + (c2 - 0xDC00) + 0x10000);
                                strPtr++;
                                goto doEncode;
                            }
                        }
                        toEncode = c;
                        goto doEncode;
                    }
                }
                Pos = pos;
            }
        }