Exemple #1
0
        public void WriteVUInt64(ulong value)
        {
            var len = PackUnpack.LengthVUInt(value);

            if ((uint)_buf.Length < (uint)len)
            {
                Resize(len);
            }

            PackUnpack.UnsafePackVUInt(ref PackUnpack.UnsafeGetAndAdvance(ref _buf, len), value, len);
        }
Exemple #2
0
        // ReSharper disable once MemberCanBePrivate.Global used by FieldHandler.Load
        public ODBSet(IInternalObjectDBTransaction tr, ODBDictionaryConfiguration config, ulong id)
        {
            _tr         = tr;
            _keyHandler = config.KeyHandler;
            _id         = id;
            var len    = PackUnpack.LengthVUInt(id);
            var prefix = new byte[ObjectDB.AllDictionariesPrefixLen + len];

            MemoryMarshal.GetReference(prefix.AsSpan()) = ObjectDB.AllDictionariesPrefixByte;
            PackUnpack.UnsafePackVUInt(ref Unsafe.AddByteOffset(ref MemoryMarshal.GetReference(prefix.AsSpan()), (IntPtr)ObjectDB.AllDictionariesPrefixLen), id, len);
            _prefix     = prefix;
            _keyReader  = ((ReaderFun <TKey>)config.KeyReader) !;
            _keyWriter  = ((WriterFun <TKey>)config.KeyWriter) !;
            _keyValueTr = _tr.KeyValueDBTransaction;
            _count      = -1;
        }
Exemple #3
0
        public void WriteVUInt64(ulong value)
        {
            var len = PackUnpack.LengthVUInt(value);

            if (Pos + len > End)
            {
                FlushBuffer();
                if (Pos + len > End)
                {
                    Span <byte> b = stackalloc byte[len];
                    PackUnpack.UnsafePackVUInt(ref MemoryMarshal.GetReference(b), value, len);
                    WriteBlock(b);
                    return;
                }
            }

            PackUnpack.UnsafePackVUInt(ref Unsafe.AddByteOffset(ref MemoryMarshal.GetReference(Buf.AsSpan()), (IntPtr)Pos), value, len);
            Pos += len;
        }
Exemple #4
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.UnsafePackVUInt(ref Unsafe.AddByteOffset(ref MemoryMarshal.GetReference(buf.AsSpan()), (IntPtr)pos), toEncode, toEncodeLen);
                    pos += toEncodeLen;
                }
                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;
            }
        }