public Transaction ReadTransaction(ByteStringContext context = null) { var transactionPersistentContext = new TransactionPersistentContext(); var newLowLevelTransaction = NewLowLevelTransaction(transactionPersistentContext, TransactionFlags.Read, context); return(new Transaction(newLowLevelTransaction)); }
public ByteStringContext.ExternalScope GetSlice(ByteStringContext context, TableValueReader value, out Slice slice) { int totalSize; var ptr = value.Read(StartIndex, out totalSize); #if DEBUG if (totalSize < 0) { throw new ArgumentOutOfRangeException(nameof(totalSize), "Size cannot be negative"); } #endif for (var i = 1; i < Count; i++) { int size; value.Read(i + StartIndex, out size); #if DEBUG if (size < 0) { throw new ArgumentOutOfRangeException(nameof(size), "Size cannot be negative"); } #endif totalSize += size; } #if DEBUG if (totalSize < 0 || totalSize > value.Size) { throw new ArgumentOutOfRangeException(nameof(value), "Reading a slice that is longer than the value"); } if (totalSize > ushort.MaxValue) { throw new ArgumentOutOfRangeException(nameof(totalSize), "Reading a slice that too big to be a slice"); } #endif return(Slice.External(context, ptr, (ushort)totalSize, out slice)); }
public static ByteStringContext.InternalScope From(ByteStringContext context, Span <byte> value, out Slice str) { fixed(byte *k = value) { return(From(context, k, value.Length, ByteStringType.Immutable, out str)); } }
public RetrieveDocumentIdsVisitor(MapQueryResultRetriever resultsRetriever, QueryMetadata metadata, ByteStringContext allocator) : base(metadata.Query.QueryText) { _query = metadata.Query; _resultsRetriever = resultsRetriever; _metadata = metadata; _allocator = allocator; }
public static ByteStringContext.InternalScope From(ByteStringContext context, byte[] value, ByteStringType type, out Slice str) { var scope = context.From(value, 0, value.Length, type, out ByteString byteString); str = new Slice(byteString); return(scope); }
public long GetValue(ByteStringContext context, TableValueBuilder value) { using (value.SliceFromLocation(context, StartIndex, out Slice slice)) { return(Bits.SwapBytes(*(long *)slice.Content.Ptr)); } }
public static ByteStringContext.ExternalScope External(ByteStringContext context, byte *value, int size, ByteStringType type, out Slice slice) { var scope = context.FromPtr(value, size, type | ByteStringType.External, out ByteString str); slice = new Slice(str); return(scope); }
public static ByteStringContext.InternalScope GetLower(ByteStringContext byteStringContext, byte *str, int size, out Slice loweredKey) { var release = byteStringContext.Allocate(size, out var ptr); byte *pointer = ptr.Ptr; for (int i = 0; i < size; i++) { byte ch = str[i]; // PERF: Trick to avoid multiple compare instructions on hot loops. // This is the same as (ch >= 65 && ch <= 90) if (ch - 65 <= 90 - 65) { ch = (byte)(ch | 0x20); } else { if (ch > 127) // not ASCII, use slower mode { goto UnlikelyUnicode; } } pointer[i] = ch; } loweredKey = new Slice(ptr); return(release); UnlikelyUnicode: release.Dispose(); return(UnlikelyGetLowerUnicode(byteStringContext, str, size, out loweredKey)); }
public void Lifecycle() { using (var context = new ByteStringContext <ByteStringDirectAllocator>(SharedMultipleUseFlag.None)) { context.Allocate(512, out var byteString); Assert.Equal(512, byteString.Length); Assert.True(byteString.HasValue); Assert.True((ByteStringType.Mutable & byteString.Flags) != 0); Assert.True(byteString.IsMutable); Assert.Equal(1024, byteString._pointer->Size); context.Allocate(1024 - sizeof(ByteStringStorage), out var byteStringWithExactSize); Assert.True(byteStringWithExactSize.HasValue); Assert.Equal(1024 - sizeof(ByteStringStorage), byteStringWithExactSize.Length); Assert.True((ByteStringType.Mutable & byteStringWithExactSize.Flags) != 0); Assert.True(byteStringWithExactSize.IsMutable); Assert.Equal(1024, byteStringWithExactSize._pointer->Size); context.Release(ref byteString); Assert.False(byteString.HasValue); Assert.True(byteString._pointer == null); } }
public void AllocateAndReleaseShouldReuseAsSegment() { int allocationBlockSize = 2 * ByteStringContext.MinBlockSizeInBytes + 128 + sizeof(ByteStringStorage); using (var context = new ByteStringContext <ByteStringDirectAllocator>(SharedMultipleUseFlag.None, allocationBlockSize)) { // Will be only 128 bytes left for the allocation unit. context.Allocate(2 * ByteStringContext.MinBlockSizeInBytes - sizeof(ByteStringStorage), out var byteStringInFirst); long ptrLocation = (long)byteStringInFirst._pointer; long nextPtrLocation = ptrLocation + byteStringInFirst._pointer->Size; context.Release(ref byteStringInFirst); // After the release the block should be reserved as a new segment. // We use a different size to ensure we are not reusing a reuse bucket but big enough to avoid having space available. context.Allocate(512, out var byteStringReused); Assert.InRange((long)byteStringReused._pointer, ptrLocation, ptrLocation + allocationBlockSize); Assert.Equal(ptrLocation, (long)byteStringReused._pointer); // We are the first in the segment. // This allocation will have an allocation unit size of 128 and fit into the rest of the initial segment, which should be // available for an exact reuse bucket allocation. context.Allocate(64, out var byteStringReusedFromBucket); Assert.Equal((long)byteStringReusedFromBucket._pointer, nextPtrLocation); } }
public void SetAllocatorForTestingPurposes(ByteStringContext byteStringContext) { if (ReduceOperation != null) { ReduceOperation.SetAllocatorForTestingPurposes(byteStringContext); } }
public static ByteStringContext.InternalScope From(ByteStringContext context, byte[] value, int offset, int count, ByteStringType type, out Slice str) { var scope = context.From(value, offset, count, type, out ByteString byteString); str = new Slice(byteString); return(scope); }
public static ByteStringContext.InternalScope GetLower(ByteStringContext byteStringContext, byte *str, int size, out Slice loweredKey) { var release = byteStringContext.Allocate(size, out var ptr); byte *pointer = ptr.Ptr; for (int i = 0; i < size; i++) { byte ch = str[i]; if (ch >= 65) // 65 = 'A' { if (ch <= 90) // 90 = 'Z' { ch = (byte)(ch | 0x20); //Turn on the sixth bit to apply lower case } else if (ch > 127) { goto UnlikelyUnicode; // not ASCII, use slower mode } } pointer[i] = ch; } loweredKey = new Slice(ptr); return(release); UnlikelyUnicode: release.Dispose(); return(UnlikelyGetLowerUnicode(byteStringContext, str, size, out loweredKey)); }
public static ByteStringContext.InternalScope GetLower(ByteStringContext byteStringContext, byte *str, int size, out Slice loweredKey) { var release = byteStringContext.Allocate(size, out var ptr); for (int i = 0; i < size; i++) { var ch = str[i]; if ((ch < 65) || (ch > 90)) { if (ch > 127) // not ASCII, use slower mode { goto UnlikelyUnicode; } ptr.Ptr[i] = ch; } else { ptr.Ptr[i] = (byte)(ch | 0x20); } } loweredKey = new Slice(ptr); return(release); UnlikelyUnicode: release.Dispose(); return(UnlikelyGetLowerUnicode(byteStringContext, str, size, out loweredKey)); }
public static ByteStringContext.InternalScope From(ByteStringContext context, byte *value, int size, ByteStringType type, out Slice str) { var scope = context.From(value, size, type, out ByteString byteString); str = new Slice(byteString); return(scope); }
public void Invalid_hash_calculation_on_null() { using (var bufferPool = new UnmanagedBuffersPoolWithLowMemoryHandling("RavenDB_9535")) using (var bsc = new ByteStringContext(SharedMultipleUseFlag.None)) { var sut = new ReduceKeyProcessor(1, bufferPool); try { sut.Reset(); sut.Process(bsc, null); Assert.Equal((ulong)0, sut.Hash); sut.Reset(); sut.Process(bsc, 1); Assert.NotEqual((ulong)0, sut.Hash); sut.Reset(); sut.Process(bsc, null); Assert.Equal((ulong)0, sut.Hash); } finally { sut.ReleaseBuffer(); } } }
public void BitBufferCanHandleLargeValuesAndCompression() { var values = GetFailingValues(); using (var allocator = new ByteStringContext(SharedMultipleUseFlag.None)) using (allocator.Allocate(2048, out var buffer)) { Memory.Set(buffer.Ptr, 0, buffer.Length); var bitBuffer = new BitsBuffer(buffer.Ptr, buffer.Length); var numberOfValues = values.Length; for (int i = 0; i < numberOfValues; i++) { bitBuffer.EnsureAdditionalBits(allocator, values[i].Item2); bitBuffer.AddValue(values[i].Item1, values[i].Item2); } using (bitBuffer.Uncompress(allocator, out var newBuf)) { int offset = 0; for (int i = 0; i < numberOfValues; i++) { var val = newBuf.ReadValue(ref offset, values[i].Item2); if (val != values[i].Item1) { Assert.False(true, "Unmatch value at index: " + i); } } } } }
public unsafe void BitBufferCompression() { using (var allocator = new ByteStringContext(SharedMultipleUseFlag.None)) using (allocator.Allocate(2048, out var buffer)) { Memory.Set(buffer.Ptr, 0, buffer.Length); var bitBuffer = new BitsBuffer(buffer.Ptr, buffer.Length); for (int i = 0; i < 12; i++) { bitBuffer.AddValue((ulong)(i & 1), 1); } bitBuffer.TryCompressBuffer(allocator, 0); bitBuffer.Uncompress(allocator, out var newBuffer); for (int i = 0; i < 12; i++) { int copy = i; var v = newBuffer.ReadValue(ref copy, 1); Assert.Equal((ulong)(i & 1), v); } } }
public static ByteStringContext.InternalScope From(ByteStringContext context, byte *value, int size, byte endSeparator, out Slice str) { var scope = context.From(value, size, endSeparator, ByteStringType.Immutable, out ByteString byteString); str = new Slice(byteString); return(scope); }
public static ByteStringContext.InternalScope From(ByteStringContext context, string value, ByteStringType type, out Slice str) { var scope = context.From(value, type, out ByteString s); str = new Slice(s); return(scope); }
public RetrieveDocumentIdsVisitor(TransactionOperationContext serverContext, DocumentsOperationContext context, QueryMetadata metadata, ByteStringContext allocator) : base(metadata.Query.QueryText) { _query = metadata.Query; _serverContext = serverContext; _context = context; _metadata = metadata; _allocator = allocator; }
public static ByteStringContext.ExternalScope ToSlicePtr(ByteStringContext context, TreeNodeHeader *node, ByteStringType type, out Slice slice) { ByteString str; var scope = context.FromPtr((byte *)node + Constants.Tree.NodeHeaderSize, node->KeySize, type, out str); slice = new Slice(str); return(scope); }
public static Slice ToSliceUsingBuffer(this string str, ByteStringContext context, byte[] buffer, ByteStringType type = ByteStringType.Mutable) { var sliceWriter = new SliceWriter(buffer); sliceWriter.Write(str); return(sliceWriter.CreateSlice(context, type)); }
public static ByteStringContext.InternalScope ToSlice(ByteStringContext context, TreeNodeHeader *node, ByteStringType type, out Slice str) { ByteString byteString; var scope = context.From((byte *)node + Constants.Tree.NodeHeaderSize, node->KeySize, type | (ByteStringType)SliceOptions.Key, out byteString); str = new Slice(byteString); return(scope); }
public Transaction WriteTransaction(ByteStringContext context = null, TimeSpan?timeout = null) { var transactionPersistentContext = new TransactionPersistentContext(); var newLowLevelTransaction = NewLowLevelTransaction(transactionPersistentContext, TransactionFlags.ReadWrite, context, timeout); var writeTransaction = new Transaction(newLowLevelTransaction); return(writeTransaction); }
public void SetGlobalChangeVector(Transaction tx, ByteStringContext context, Dictionary <Guid, long> changeVector) { var tree = tx.CreateTree(SchemaNameConstants.GlobalChangeVectorTree); Debug.Assert(tree != null); ReplicationUtils.WriteChangeVectorTo(context, changeVector, tree); }
public ByteStringContext.InternalScope AsPartialSlice(ByteStringContext context, int removeFromEnd, out Slice str) { if (_len >= ushort.MaxValue) { throw new InvalidOperationException("Cannot convert to slice, len is too big: " + _len); } return(Slice.From(context, _val, _len - removeFromEnd, out str)); }
public void FailValidationTryingToReleaseInAnotherContext() { using (var context = new ByteStringContext <ByteStringDirectAllocator>(SharedMultipleUseFlag.None, ByteStringContext.MinBlockSizeInBytes)) using (var otherContext = new ByteStringContext <ByteStringDirectAllocator>(SharedMultipleUseFlag.None, ByteStringContext.MinBlockSizeInBytes)) { context.Allocate(1, out var first); Assert.Throws <ByteStringValidationException>(() => otherContext.Release(ref first)); } }
public Slice AsSlice(ByteStringContext context) { if (_len >= ushort.MaxValue) { throw new InvalidOperationException("Cannot convert to slice, len is too big: " + _len); } return(Slice.From(context, _val, _len)); }
public ByteStringContext.InternalScope ToSlice(ByteStringContext context, ByteStringType type, out Slice str) { var buffer = ToBuffer(); ByteString byteString; var scope = context.From(buffer, 0, buffer.Length, type, out byteString); str = new Slice(byteString); return(scope); }