public RelationAdvancedEnumerator( RelationDBManipulator <T> manipulator, ByteBuffer prefixBytes, uint prefixFieldCount, EnumerationOrder order, KeyProposition startKeyProposition, ByteBuffer startKeyBytes, KeyProposition endKeyProposition, ByteBuffer endKeyBytes) { _prefixFieldCount = prefixFieldCount; _manipulator = manipulator; _ascending = order == EnumerationOrder.Ascending; _tr = manipulator.Transaction; _keyValueTr = _tr.KeyValueDBTransaction; _keyValueTrProtector = _tr.TransactionProtector; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyBytes = prefixBytes; _keyValueTr.SetKeyPrefix(_keyBytes); _relationInfo = manipulator.RelationInfo; _prevModificationCounter = _relationInfo.ModificationCounter; long startIndex; long endIndex; if (endKeyProposition == KeyProposition.Ignored) { endIndex = _keyValueTr.GetKeyValueCount() - 1; } else { switch (_keyValueTr.Find(endKeyBytes)) { case FindResult.Exact: endIndex = _keyValueTr.GetKeyIndex(); if (endKeyProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.Next: endIndex = _keyValueTr.GetKeyIndex() - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (startKeyProposition == KeyProposition.Ignored) { startIndex = 0; } else { switch (_keyValueTr.Find(startKeyBytes)) { case FindResult.Exact: startIndex = _keyValueTr.GetKeyIndex(); if (startKeyProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _keyValueTr.GetKeyIndex() + 1; break; case FindResult.Next: startIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } _count = (uint)Math.Max(0, endIndex - startIndex + 1); _startPos = (uint)(_ascending ? startIndex : endIndex); _pos = 0; _seekNeeded = true; _lengthOfNonDataPrefix = ObjectDB.AllRelationsPKPrefix.Length + PackUnpack.LengthVUInt(manipulator.RelationInfo.Id); }
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(); }
/// <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)); } 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)); } } } }
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; } }
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++; } }
internal static int CalcEntrySize(int keyLen) { return(PackUnpack.LengthVUInt((uint)keyLen) + 1 + CalcKeyLenInline(keyLen) + (keyLen > MaxKeyLenInline ? KeyValueDB.PtrDownSize : 0)); }
internal static int CalcEntrySize(int keyLen, long valueLen) { return(PackUnpack.LengthVUInt((uint)keyLen) + PackUnpack.LengthVUInt((ulong)valueLen) + CalcEntrySizeWOLengths(keyLen, valueLen)); }
internal static void SetOneEntryCount(byte[] data, int entrySize) { SetCountToSectorData(data, 1); PackUnpack.PackUInt16LE(data, HeaderSize, (ushort)entrySize); }
internal static void SetCountToSectorData(byte[] data, int count) { Debug.Assert(count > 0 && count < 128 * 256); PackUnpack.PackUInt16LE(data, 0, (ushort)count); }
public RelationAdvancedOrderedEnumerator(RelationDBManipulator <TValue> manipulator, ByteBuffer prefixBytes, uint prefixFieldCount, EnumerationOrder order, KeyProposition startKeyProposition, ByteBuffer startKeyBytes, KeyProposition endKeyProposition, ByteBuffer endKeyBytes, bool initKeyReader = true) { _prefixFieldCount = prefixFieldCount; _manipulator = manipulator; _tr = manipulator.Transaction; _ascending = order == EnumerationOrder.Ascending; _keyValueTr = _tr.KeyValueDBTransaction; _keyValueTrProtector = _tr.TransactionProtector; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyBytes = prefixBytes; _keyValueTrProtector.Start(); _keyValueTr.SetKeyPrefix(_keyBytes); long startIndex; long endIndex; if (endKeyProposition == KeyProposition.Ignored) { endIndex = _keyValueTr.GetKeyValueCount() - 1; } else { switch (_keyValueTr.Find(endKeyBytes)) { case FindResult.Exact: endIndex = _keyValueTr.GetKeyIndex(); if (endKeyProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.Next: endIndex = _keyValueTr.GetKeyIndex() - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (startKeyProposition == KeyProposition.Ignored) { startIndex = 0; } else { switch (_keyValueTr.Find(startKeyBytes)) { case FindResult.Exact: startIndex = _keyValueTr.GetKeyIndex(); if (startKeyProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _keyValueTr.GetKeyIndex() + 1; break; case FindResult.Next: startIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } _count = (uint)Math.Max(0, endIndex - startIndex + 1); _startPos = (uint)(_ascending ? startIndex : endIndex); _pos = 0; _seekNeeded = true; if (initKeyReader) { var primaryKeyFields = manipulator.RelationInfo.ClientRelationVersionInfo.GetPrimaryKeyFields(); var advancedEnumParamField = primaryKeyFields.ToList()[(int)_prefixFieldCount]; if (advancedEnumParamField.Handler.NeedsCtx()) { throw new BTDBException("Not supported."); } _keyReader = (Func <AbstractBufferedReader, IReaderCtx, TKey>)manipulator.RelationInfo .GetSimpleLoader(new RelationInfo.SimpleLoaderType(advancedEnumParamField.Handler, typeof(TKey))); _lengthOfNonDataPrefix = ObjectDB.AllRelationsPKPrefix.Length + PackUnpack.LengthVUInt(manipulator.RelationInfo.Id); } }
internal static uint CountFromSectorData(byte[] data) { return(PackUnpack.UnpackUInt16LE(data, 0)); }
internal static void Pack(byte[] data, int offset, SectorPtr value) { PackUnpack.PackInt64LE(data, offset, value.Ptr); PackUnpack.PackUInt32LE(data, offset + 8, value.Checksum); }
public void ReadToEnd(IEventStoreObserver observer) { var overflowWriter = default(ByteBufferWriter); var bufferBlock = GetReadBuffer(); var bufferStartPosition = NextReadPosition & SectorMask; var bufferFullLength = 0; var bufferReadOffset = (int)(NextReadPosition - bufferStartPosition); var currentReadAhead = FirstReadAhead; var buf = ByteBuffer.NewSync(bufferBlock, bufferFullLength, currentReadAhead); var bufReadLength = (int)File.Read(buf, bufferStartPosition); bufferFullLength += bufReadLength; while (true) { if (bufferStartPosition + (ulong)bufferReadOffset + HeaderSize > File.MaxFileSize) { KnownAsFinished = true; return; } if (bufferReadOffset == bufferFullLength) { break; } if (bufferReadOffset + HeaderSize > bufferFullLength) { for (var i = bufferReadOffset; i < bufferFullLength; i++) { if (bufferBlock[i] != 0) { SetCorrupted(); return; } } break; } var blockCheckSum = PackUnpack.UnpackUInt32LE(bufferBlock, bufferReadOffset); bufferReadOffset += 4; var blockLen = PackUnpack.UnpackUInt32LE(bufferBlock, bufferReadOffset); if (blockCheckSum == 0 && blockLen == 0) { bufferReadOffset -= 4; break; } var blockType = (BlockType)(blockLen & 0xff); blockLen >>= 8; if (blockType == BlockType.LastBlock && blockLen == 0) { if (Checksum.CalcFletcher32(bufferBlock, (uint)bufferReadOffset, 4) != blockCheckSum) { SetCorrupted(); return; } KnownAsFinished = true; return; } if (blockLen == 0 && blockType != (BlockType.FirstBlock | BlockType.LastBlock)) { SetCorrupted(); return; } if (blockLen + HeaderSize > MaxBlockSize) { SetCorrupted(); return; } bufferReadOffset += 4; var bufferLenToFill = (uint)(bufferReadOffset + (int)blockLen + FirstReadAhead) & SectorMaskUInt; if (bufferLenToFill > bufferBlock.Length) { bufferLenToFill = (uint)bufferBlock.Length; } buf = ByteBuffer.NewSync(bufferBlock, bufferFullLength, (int)(bufferLenToFill - bufferFullLength)); if (buf.Length > 0) { bufferLenToFill = (uint)(bufferReadOffset + (int)blockLen + currentReadAhead) & SectorMaskUInt; if (bufferLenToFill > bufferBlock.Length) { bufferLenToFill = (uint)bufferBlock.Length; } if (bufferStartPosition + bufferLenToFill > File.MaxFileSize) { bufferLenToFill = (uint)(File.MaxFileSize - bufferStartPosition); } buf = ByteBuffer.NewSync(bufferBlock, bufferFullLength, (int)(bufferLenToFill - bufferFullLength)); if (buf.Length > 0) { if (currentReadAhead * 4 < MaxBlockSize) { currentReadAhead = currentReadAhead * 2; } bufReadLength = (int)File.Read(buf, bufferStartPosition + (ulong)bufferFullLength); bufferFullLength += bufReadLength; } } if (bufferReadOffset + (int)blockLen > bufferFullLength) { SetCorrupted(); return; } if (Checksum.CalcFletcher32(bufferBlock, (uint)bufferReadOffset - 4, blockLen + 4) != blockCheckSum) { SetCorrupted(); return; } var blockTypeBlock = blockType & (BlockType.FirstBlock | BlockType.MiddleBlock | BlockType.LastBlock); var stopReadingRequested = false; if (blockTypeBlock == (BlockType.FirstBlock | BlockType.LastBlock)) { stopReadingRequested = Process(blockType, ByteBuffer.NewSync(bufferBlock, bufferReadOffset, (int)blockLen), observer); } else { if (blockTypeBlock == BlockType.FirstBlock) { overflowWriter = new ByteBufferWriter(); } else if (blockTypeBlock == BlockType.MiddleBlock || blockTypeBlock == BlockType.LastBlock) { if (overflowWriter == null) { SetCorrupted(); return; } } else { SetCorrupted(); return; } overflowWriter.WriteBlock(ByteBuffer.NewSync(bufferBlock, bufferReadOffset, (int)blockLen)); if (blockTypeBlock == BlockType.LastBlock) { stopReadingRequested = Process(blockType, overflowWriter.Data, observer); overflowWriter = null; } } bufferReadOffset += (int)blockLen; if (overflowWriter == null) { NextReadPosition = bufferStartPosition + (ulong)bufferReadOffset; } if (stopReadingRequested) { return; } var nextBufferStartPosition = (bufferStartPosition + (ulong)bufferReadOffset) & SectorMask; var bufferMoveDistance = (int)(nextBufferStartPosition - bufferStartPosition); if (bufferMoveDistance <= 0) { continue; } Array.Copy(bufferBlock, bufferMoveDistance, bufferBlock, 0, bufferFullLength - bufferMoveDistance); bufferStartPosition = nextBufferStartPosition; bufferFullLength -= bufferMoveDistance; bufferReadOffset -= bufferMoveDistance; } if (overflowWriter != null) { // It is not corrupted here just unfinished, but definitely not appendable EndBufferPosition = ulong.MaxValue; return; } EndBufferLen = (uint)(bufferReadOffset - (bufferReadOffset & SectorMaskUInt)); EndBufferPosition = bufferStartPosition + (ulong)bufferReadOffset - EndBufferLen; Array.Copy(bufferBlock, bufferReadOffset - EndBufferLen, EndBuffer, 0, EndBufferLen); }
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); long prevProtectionCounter = 0; long pos = 0; Span <byte> keyBuffer = stackalloc byte[512]; 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.StartDictKey()) { var keyReader = new SpanReader(_trkv.GetKey(ref MemoryMarshal.GetReference(keyBuffer), keyBuffer.Length).Slice(prefix.Length)); IterateHandler(ref keyReader, keyHandler, false, null); _visitor?.EndDictKey(); } if (_trkv.CursorMovedCounter != prevProtectionCounter) { if (!_trkv.SetKeyIndex(prefix, pos)) { break; } } if (_visitor == null || _visitor.StartDictValue()) { var valueReader = new SpanReader(_trkv.GetValue()); IterateHandler(ref valueReader, valueHandler, false, null); _visitor?.EndDictValue(); } pos++; } _visitor?.EndDictionary(); }
private void BTN_CreateAccount_Click(object sender, EventArgs e) { if (POLConsole == null) { MessageBox.Show("POL does not appear to be running via POL Launch."); return; } // Checks to make sure everything was filled out, or if can use defaults. if (TB_CreateUsername.Text.Length < 3) { MessageBox.Show("Usernames must be at least 3 characters long."); return; } if (TB_CreatePassword.Text.Length < 3) { MessageBox.Show("Passwords must be at least 3 characters long."); return; } if (TB_CreateEmail.Text.IndexOf("@") == -1) { TB_CreateEmail.Text = "*****@*****.**"; } ArrayList NewAccountInfo = new ArrayList { TB_CreateUsername.Text, TB_CreatePassword.Text, LBX_CreateCmdlevel.SelectedIndex, LBX_CreateExpansion.SelectedIndex, TB_CreateEmail.Text, }; if (TB_CreateAuxPassword.Text.Length > 0) { NewAccountInfo.Add(TB_CreateAuxPassword.Text); } AuxSvcConnection CreateAuxSvc = new AuxSvcConnection("localhost", Convert.ToInt32(TB_CreateAccountPort.Text)); if (!CreateAuxSvc.Active) { MessageBox.Show("Server appears to be offline. Make sure Port is correct."); return; } CreateAuxSvc.Write(PackUnpack.Pack(NewAccountInfo)); string RcvString = CreateAuxSvc.Read(); object ResultObject = PackUnpack.Unpack(RcvString); // This is where we convert the Object back to what it needs to be. // Arrays are converted to ArrayList for storage of both Int and String // In the Account Creation only Arrays are being returned. The first // Element says "Error" or "Success", and the second is the string reason. string ResultMsg = ""; string CaptionMsg = ""; if (ResultObject.GetType() == typeof(ArrayList)) { Convert.ChangeType(ResultObject, typeof(ArrayList)); foreach (object Elem in (ArrayList)ResultObject) { string NewElem = Convert.ToString(Elem); if (NewElem.IndexOf("Error") != -1 || NewElem.IndexOf("Success") != -1) { CaptionMsg = Convert.ToString(Elem); } else { ResultMsg += NewElem; } } } MessageBox.Show(ResultMsg, CaptionMsg); }
public ResponseDetail SavePackUnpack(PackUnpack obj) { return(objTransacRepo.SavePackUnpack(obj)); }