protected override ReplicationBatchItem CloneInternal(JsonOperationContext context) { MemoryStream stream = null; if (Stream != null) { stream = new MemoryStream(); Stream.CopyTo(stream); stream.Position = 0; Stream.Position = 0; } var item = new AttachmentReplicationItem { ContentType = ContentType.Clone(context), Name = Name.Clone(context), Stream = stream }; var baseMem = Base64Hash.CloneToJsonContext(context, out item.Base64Hash); var keyMem = Key.CloneToJsonContext(context, out item.Key); item.ToDispose(new DisposableAction(() => { context.ReturnMemory(baseMem); context.ReturnMemory(keyMem); })); return(item); }
public void WriteString(LazyCompressedStringValue str) { var strBuffer = str.DecompressToTempBuffer(out AllocatedMemoryData allocated, _context); try { var strSrcBuffer = str.Buffer; var size = str.UncompressedSize; var escapeSequencePos = str.CompressedSize; var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos); // We ensure our buffer will have enough space to deal with the whole string. int bufferSize = 2 * numberOfEscapeSequences + size + 2; if (bufferSize >= JsonOperationContext.ManagedPinnedBuffer.Size) { goto WriteLargeCompressedString; // OK, do it the slow way instead. } EnsureBuffer(bufferSize); _buffer[_pos++] = Quote; while (numberOfEscapeSequences > 0) { numberOfEscapeSequences--; var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos); WriteRawString(strBuffer, bytesToSkip); strBuffer += bytesToSkip; size -= bytesToSkip + 1 /*for the escaped char we skip*/; var b = *(strBuffer++); var auxPos = _pos; _buffer[auxPos++] = (byte)'\\'; _buffer[auxPos++] = GetEscapeCharacter(b); _pos = auxPos; } // write remaining (or full string) to the buffer in one shot WriteRawString(strBuffer, size); _buffer[_pos++] = Quote; return; WriteLargeCompressedString: UnlikelyWriteCompressedString(numberOfEscapeSequences, strSrcBuffer, escapeSequencePos, strBuffer, size); } finally { if (allocated != null) //precaution { _context.ReturnMemory(allocated); } } }
public void ValidateFloat() { try { int numLength = _unmanagedWriteBuffer.SizeInBytes; if (numLength <= 100) { byte *tmpBuff = stackalloc byte[numLength]; _unmanagedWriteBuffer.CopyTo(tmpBuff); _ctx.ParseDouble(tmpBuff, numLength); } else { var memoryForNumber = _ctx.GetMemory(numLength); try { _unmanagedWriteBuffer.CopyTo(memoryForNumber.Address); _ctx.ParseDouble(memoryForNumber.Address, numLength); } finally { _ctx.ReturnMemory(memoryForNumber); } } } #pragma warning disable RDB0004 // Exception handler is empty or just logging catch (Exception e) { ThrowException("Could not parse double", e); } #pragma warning restore RDB0004 // Exception handler is empty or just logging }
public void Can_reuse_reader_multiple_times() { var r = new Random(); for (int i = 0; i < 10; i++) { var bytes = new byte[r.Next(1, 2000)]; r.NextBytes(bytes); for (int j = 0; j < bytes.Length; j++) { if (bytes[j] < 32) { bytes[j] += 32; } } var expected = Encoding.UTF8.GetString(bytes); var lazyString = _ctx.GetLazyString(expected); var stringResult = LazyStringReader.GetStringFor(lazyString); var readerResult = _sut.GetTextReaderFor(lazyString); Assert.Equal(expected, stringResult); Assert.Equal(expected, readerResult.ReadToEnd()); _ctx.ReturnMemory(lazyString.AllocatedMemoryData); } }
public void Reset(object root) { if (_currentStateBuffer != null) { _ctx.ReturnMemory(_currentStateBuffer); _currentStateBuffer = null; } _elements.Clear(); _seenValues.Clear(); if (root != null) { _elements.Push(root); } }
public void Dispose() { if (IsDisposed) { return; } // The actual lifetime of the _head Segment is unbounded: it will // be released by the GC when we no longer have any references to // it (i.e. no more copies of this struct) // // We can, however, force the deallocation of all the previous // Segments by ensuring we don't keep any references after the // Dispose is run. var head = _head; _head = null; // Further references are NREs. for (Segment next; head != null; head = next) { _context.ReturnMemory(head.Allocation); // This is used to signal that Dispose has run to other copies head.Address = null; // `next` is used to keep a reference to the previous Segment. // Since `next` lives only within this for loop and we clear up // all other references, non-head Segments should be GC'd. next = head.DeallocationPendingPrevious; head.Previous = null; head.DeallocationPendingPrevious = null; } }
public void Dispose() { for (int i = _allocations.Count - 1; i >= 0; i--) { _ctx.ReturnMemory(_allocations[i]); } _allocations.Clear(); }
protected override ReplicationBatchItem CloneInternal(JsonOperationContext context) { var item = new TimeSeriesReplicationItem { Collection = Collection.Clone(context) }; var mem = Segment.Clone(context, out item.Segment); var keyMem = Key.CloneToJsonContext(context, out item.Key); item.ToDispose(new DisposableAction(() => { context.ReturnMemory(keyMem); context.ReturnMemory(mem); })); return(item); }
protected override ReplicationBatchItem CloneInternal(JsonOperationContext context) { var item = new AttachmentTombstoneReplicationItem(); var keyMem = Key.CloneToJsonContext(context, out item.Key); item.ToDispose(new DisposableAction(() => { context.ReturnMemory(keyMem); })); return(item); }
public void Reset(object root) { _seenIndex = ++ThreadLocalSeenIndex; if (ThreadLocalSeenIndex > short.MaxValue) { ThreadLocalSeenIndex = 1; } if (_currentStateBuffer != null) { _ctx.ReturnMemory(_currentStateBuffer); _currentStateBuffer = null; } _elements.Clear(); if (root != null) { _elements.Push(root); } }
public void Dispose() { if (IsDisposed) { ThrowAlreadyDisposed(); } if (AllocatedMemoryData != null) { _context.ReturnMemory(AllocatedMemoryData); } IsDisposed = true; }
public void Can_get_simple_values() { var now = SystemTime.UtcNow; using (var lazyStringValue = _ctx.GetLazyString("22.0")) { var stringValue = _ctx.GetLazyString("Arek"); var doc = create_doc(new DynamicJsonValue { ["Name"] = "Arek", ["Address"] = new DynamicJsonValue { ["City"] = "NYC" }, ["NullField"] = null, ["Age"] = new LazyNumberValue(lazyStringValue), ["LazyName"] = stringValue, ["Friends"] = new DynamicJsonArray { new DynamicJsonValue { ["Name"] = "Joe" }, new DynamicJsonValue { ["Name"] = "John" } }, [Constants.Documents.Metadata.Key] = new DynamicJsonValue { [Constants.Documents.Metadata.Collection] = "Users", [Constants.Documents.Metadata.LastModified] = now.GetDefaultRavenFormat(true) } }, "users/1"); dynamic user = new DynamicBlittableJson(doc); Assert.Equal("Arek", user.Name); Assert.Equal("NYC", user.Address.City); Assert.Equal("users/1", user.Id); Assert.Equal(DynamicNullObject.Null, user.NullField); Assert.Equal(22.0, user.Age); Assert.Equal("Arek", user.LazyName); Assert.Equal(2, user.Friends.Length); Assert.Equal("Users", user[Constants.Documents.Metadata.Key][Constants.Documents.Metadata.Collection]); Assert.Equal(now, user[Constants.Documents.Metadata.Key].Value <DateTime>(Constants.Documents.Metadata.LastModified)); _ctx.ReturnMemory(stringValue.AllocatedMemoryData); } }
public void Dispose() { if (IsDisposed) { return; } #if MEM_GUARD FreedBy = Environment.StackTrace; #endif // The actual lifetime of the _head Segment is unbounded: it will // be released by the GC when we no longer have any references to // it (i.e. no more copies of this struct) // // We can, however, force the deallocation of all the previous // Segments by ensuring we don't keep any references after the // Dispose is run. var head = _head; _head = null; // Further references are NREs. for (Segment next; head != null; head = next) { _context.ReturnMemory(head.Allocation); // This is used to signal that Dispose has run to other copies head.Address = null; #if DEBUG // Helps to avoid program errors, albeit unnecessary head.Allocation = null; head.AccumulatedSizeInBytes = -1; head.Used = -1; #endif // `next` is used to keep a reference to the previous Segment. // Since `next` lives only within this for loop and we clear up // all other references, non-head Segments should be GC'd. next = head.DeallocationPendingPrevious; head.Previous = null; head.DeallocationPendingPrevious = null; } }
public void Reset() { _unmanagedWriteBuffer.Dispose(); if (_compressionBuffer != null) { _context.ReturnMemory(_compressionBuffer); _compressionBuffer = null; } if (_innerBuffer != null) { _context.ReturnMemory(_innerBuffer); _innerBuffer = null; } }
public void Can_reuse_reader_multiple_times() { var r = new Random(); for (int i = 0; i < 10; i++) { var bytes = RandomString(2000); var expected = Encoding.UTF8.GetString(bytes); var lazyString = _ctx.GetLazyString(expected); var stringResult = LazyStringReader.GetStringFor(lazyString); var readerResult = _sut.GetTextReaderFor(lazyString); Assert.Equal(expected, stringResult); Assert.Equal(expected, readerResult.ReadToEnd()); _ctx.ReturnMemory(lazyString.AllocatedMemoryData); } }