Ejemplo n.º 1
0
 public void Dispose()
 {
     Base64HashDispose.Dispose();
     TagDispose.Dispose();
     Stream.Dispose();
     Data.Dispose();
 }
Ejemplo n.º 2
0
            protected override int ExecuteCmd(DocumentsOperationContext context)
            {
                var hiLoDocumentId = RavenHiloIdPrefix + Key;
                var prefix         = Key + Separator;

                var newDoc = new DynamicJsonValue();
                BlittableJsonReaderObject hiloDocReader = null;

                try
                {
                    try
                    {
                        hiloDocReader = Database.DocumentsStorage.Get(context, hiLoDocumentId)?.Data;
                    }
                    catch (DocumentConflictException e)
                    {
                        throw new InvalidDataException("Failed to fetch HiLo document due to a conflict on the document. " +
                                                       "This shouldn't happen, since it this conflict should've been resolved during replication. " +
                                                       "This exception should not happen and is likely a bug.", e);
                    }

                    if (hiloDocReader == null)
                    {
                        OldMax        = LastRangeMax;
                        newDoc["Max"] = OldMax + Capacity;
                        newDoc[Constants.Documents.Metadata.Key] = new DynamicJsonValue
                        {
                            [Constants.Documents.Metadata.Collection] = CollectionName.HiLoCollection
                        };

                        using (var freshHilo = context.ReadObject(newDoc, hiLoDocumentId, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                            Database.DocumentsStorage.Put(context, hiLoDocumentId, null, freshHilo);
                    }
                    else
                    {
                        hiloDocReader.TryGet("Max", out long oldMax);
                        OldMax = Math.Max(oldMax, LastRangeMax);

                        hiloDocReader.Modifications = new DynamicJsonValue(hiloDocReader)
                        {
                            ["Max"] = OldMax + Capacity
                        };

                        using (var freshHilo = context.ReadObject(hiloDocReader, hiLoDocumentId, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                            Database.DocumentsStorage.Put(context, hiLoDocumentId, null, freshHilo);
                    }

                    Prefix = prefix;
                }
                finally
                {
                    hiloDocReader?.Dispose();
                }
                return(1);
            }
Ejemplo n.º 3
0
            public override int Execute(DocumentsOperationContext context)
            {
                var hiLoDocumentId = RavenIdGeneratorsHilo + Key;
                var prefix         = Key + Separator;

                long oldMax = 0;
                var  newDoc = new DynamicJsonValue();
                BlittableJsonReaderObject hiloDocReader = null;

                try
                {
                    try
                    {
                        hiloDocReader = Database.DocumentsStorage.Get(context, hiLoDocumentId)?.Data;
                    }
                    catch (DocumentConflictException e)
                    {
                        throw new InvalidDataException("Failed to fetch HiLo document due to a conflict on the document. " +
                                                       "This shouldn't happen, since it this conflict should've been resolved during replication. " +
                                                       "This exception should not happen and is likely a bug.", e);
                    }

                    if (hiloDocReader != null)
                    {
                        var prop = new BlittableJsonReaderObject.PropertyDetails();
                        foreach (var propertyId in hiloDocReader.GetPropertiesByInsertionOrder())
                        {
                            hiloDocReader.GetPropertyByIndex(propertyId, ref prop);
                            if (prop.Name == "Max")
                            {
                                oldMax = (long)prop.Value;
                                continue;
                            }

                            newDoc[prop.Name] = prop.Value;
                        }
                    }

                    oldMax = Math.Max(oldMax, LastRangeMax);

                    newDoc["Max"] = oldMax + Capacity;

                    using (var freshHilo = context.ReadObject(newDoc, hiLoDocumentId, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                        Database.DocumentsStorage.Put(context, hiLoDocumentId, null, freshHilo);

                    OldMax = oldMax;
                    Prefix = prefix;
                }
                finally
                {
                    hiloDocReader?.Dispose();
                }
                return(1);
            }
Ejemplo n.º 4
0
 public static void DebugDisposeReaderAfterTransaction(Transaction tx, BlittableJsonReaderObject reader)
 {
     if (reader == null)
     {
         return;
     }
     Debug.Assert(tx != null);
     // this method is called to ensure that after the transaction is completed, all the readers are disposed
     // so we won't have read-after-tx use scenario, which can in rare case corrupt memory. This is a debug
     // helper that is used across the board, but it is meant to assert stuff during debug only
     tx.LowLevelTransaction.OnDispose += state => reader.Dispose();
 }
Ejemplo n.º 5
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            Id?.Dispose();
            Id = null;

            LowerId?.Dispose();
            LowerId = null;

            Data?.Dispose();
            Data = null;

            _disposed = true;
        }
            public async Task <bool> MoveNextAsync()
            {
                _prev?.Dispose(); // dispose the previous instance
                while (true)
                {
                    if (await _enumerator.MoveNextAsync().WithCancellation(_token).ConfigureAwait(false) == false)
                    {
                        return(false);
                    }

                    _prev = _enumerator.Current;

                    _query?.InvokeAfterStreamExecuted(_enumerator.Current);

                    Current = CreateStreamResult(_enumerator.Current);
                    return(true);
                }
            }
Ejemplo n.º 7
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            Id?.Dispose();
            Id = null;

            LowerId?.Dispose();
            LowerId = null;

            Doc?.Dispose();
            Doc = null;

            Collection?.Dispose();
            Collection = null;

            _disposed = true;
        }
Ejemplo n.º 8
0
        private static unsafe DynamicJsonValue ReadHistoryLog(TransactionOperationContext context, Table.TableValueHolder entryHolder)
        {
            var djv = new DynamicJsonValue();

            var ticks = Bits.SwapBytes(*(long *)entryHolder.Reader.Read((int)(LogHistoryColumn.Ticks), out _));

            djv["Date"] = new DateTime(ticks);

            int size;

            djv[nameof(LogHistoryColumn.Guid)]          = ReadGuid(entryHolder);
            djv[nameof(LogHistoryColumn.Index)]         = ReadIndex(entryHolder);
            djv[nameof(LogHistoryColumn.Term)]          = ReadTerm(entryHolder);
            djv[nameof(LogHistoryColumn.CommittedTerm)] = ReadCommittedTerm(entryHolder);
            djv[nameof(LogHistoryColumn.Type)]          = ReadType(entryHolder);
            djv[nameof(LogHistoryColumn.State)]         = ReadState(entryHolder).ToString();

            var resultPtr = entryHolder.Reader.Read((int)(LogHistoryColumn.Result), out size);

            if (size > 0)
            {
                var blittableResult = new BlittableJsonReaderObject(resultPtr, size, context);
                djv[nameof(LogHistoryColumn.Result)] = blittableResult.ToString();
                blittableResult.Dispose();
            }
            else
            {
                djv[nameof(LogHistoryColumn.Result)] = null;
            }

            var exTypePtr = entryHolder.Reader.Read((int)(LogHistoryColumn.ExceptionType), out size);

            djv[nameof(LogHistoryColumn.ExceptionType)] = size > 0 ? Encoding.UTF8.GetString(exTypePtr, size) : null;

            var exMsg = entryHolder.Reader.Read((int)(LogHistoryColumn.ExceptionMessage), out size);

            djv[nameof(LogHistoryColumn.ExceptionMessage)] = size > 0 ? Encoding.UTF8.GetString(exMsg, size) : null;

            return(djv);
        }
Ejemplo n.º 9
0
        public RachisEntry ReadRachisEntry(JsonOperationContext context)
        {
            // we explicitly not disposing this here, because we need to access the entry
            BlittableJsonReaderObject json = null;

            try
            {
                using (_disposerLock.EnsureNotDisposed())
                {
                    json = context.Sync.ParseToMemory(_stream, "rachis-entry",
                                                      BlittableJsonDocumentBuilder.UsageMode.None, _buffer);
                    json.BlittableValidation();
                    ValidateMessage(nameof(RachisEntry), json);
                    return(JsonDeserializationRachis <RachisEntry> .Deserialize(json));
                }
            }
            catch
            {
                json?.Dispose();
                throw;
            }
        }
            public async ValueTask <bool> MoveNextAsync()
            {
                _prev?.Dispose(); // dispose the previous instance

                while (true)
                {
                    var next = _enumerator.MoveNextAsync();
                    if (next.IsCompleted)
                    {
                        if (next.Result == false)
                        {
                            return(false);
                        }
                    }
                    else if (await next.AsTask().WithCancellation(_token).ConfigureAwait(false) == false)
                    {
                        return(false);
                    }

                    _prev   = _enumerator.Current;
                    Current = ResultCreator(_enumerator);
                    return(true);
                }
            }
Ejemplo n.º 11
0
 public void Dispose()
 {
     Document?.Dispose();
     ReturnContext?.Dispose();
 }
Ejemplo n.º 12
0
 public override void InnerDispose()
 {
     Values?.Dispose();
 }
Ejemplo n.º 13
0
 public void Dispose()
 {
     _record?.Dispose();
     _record = null;
 }
Ejemplo n.º 14
0
            public JsValue PutDocument(JsValue self, JsValue[] args)
            {
                string changeVector = null;

                if (args.Length != 2 && args.Length != 3)
                {
                    throw new InvalidOperationException("put(id, doc, changeVector) must be called with called with 2 or 3 arguments only");
                }
                AssertValidDatabaseContext();
                AssertNotReadOnly();
                if (args[0].IsString() == false && args[0].IsNull() == false && args[0].IsUndefined() == false)
                {
                    AssertValidId();
                }

                var id = args[0].IsNull() || args[0].IsUndefined() ? null : args[0].AsString();

                if (args[1].IsObject() == false)
                {
                    throw new InvalidOperationException(
                              $"Created document must be a valid object which is not null or empty. Document ID: '{id}'.");
                }

                PutOrDeleteCalled = true;

                if (args.Length == 3)
                {
                    if (args[2].IsString())
                    {
                        changeVector = args[2].AsString();
                    }
                    else if (args[2].IsNull() == false && args[0].IsUndefined() == false)
                    {
                        throw new InvalidOperationException(
                                  $"The change vector must be a string or null. Document ID: '{id}'.");
                    }
                }

                BlittableJsonReaderObject reader = null;

                try
                {
                    reader = JsBlittableBridge.Translate(_jsonCtx, ScriptEngine, args[1].AsObject(), usageMode: BlittableJsonDocumentBuilder.UsageMode.ToDisk);

                    var put = _database.DocumentsStorage.Put(_docsCtx, id, _docsCtx.GetLazyString(changeVector), reader);

                    if (DebugMode)
                    {
                        DebugActions.PutDocument.Add(new DynamicJsonValue
                        {
                            ["Id"]   = put.Id,
                            ["Data"] = reader
                        });
                    }

                    return(put.Id);
                }
                finally
                {
                    if (DebugMode == false)
                    {
                        reader?.Dispose();
                    }
                }
            }
Ejemplo n.º 15
0
            public override void Execute(DocumentsOperationContext context, RavenTransaction tx)
            {
                var hiLoDocumentKey = RavenKeyGeneratorsHilo + Key;
                var prefix          = Key + Separator;

                long oldMax = 0;
                var  newDoc = new DynamicJsonValue();
                BlittableJsonReaderObject hiloDocReader = null, serverPrefixDocReader = null;

                try
                {
                    try
                    {
                        serverPrefixDocReader = Database.DocumentsStorage.Get(context, RavenKeyServerPrefix)?.Data;
                        hiloDocReader         = Database.DocumentsStorage.Get(context, hiLoDocumentKey)?.Data;
                    }
                    catch (DocumentConflictException e)
                    {
                        throw new InvalidDataException(@"Failed to fetch HiLo document due to a conflict 
                                                            on the document. This shouldn't happen, since
                                                            it this conflict should've been resolved during replication.
                                                             This exception should not happen and is likely a bug.", e);
                    }

                    string serverPrefix;
                    if (serverPrefixDocReader != null &&
                        serverPrefixDocReader.TryGet("ServerPrefix", out serverPrefix))
                    {
                        prefix += serverPrefix;
                    }

                    if (hiloDocReader != null)
                    {
                        hiloDocReader.TryGet("Max", out oldMax);
                        var prop = new BlittableJsonReaderObject.PropertyDetails();
                        for (var i = 0; i < hiloDocReader.Count; i++)
                        {
                            hiloDocReader.GetPropertyByIndex(0, ref prop);
                            if (prop.Name == "Max")
                            {
                                continue;
                            }
                            newDoc[prop.Name] = prop.Value;
                        }
                    }
                }

                finally
                {
                    serverPrefixDocReader?.Dispose();
                    hiloDocReader?.Dispose();
                }
                oldMax = Math.Max(oldMax, LastRangeMax);

                newDoc["Max"] = oldMax + Capacity;

                using (
                    var freshHilo = context.ReadObject(newDoc, hiLoDocumentKey,
                                                       BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    Database.DocumentsStorage.Put(context, hiLoDocumentKey, null, freshHilo);
                }

                OldMax = oldMax;
                Prefix = prefix;
            }
Ejemplo n.º 16
0
 public void Dispose()
 {
     _document?.Dispose();
 }
Ejemplo n.º 17
0
 public void Dispose()
 {
     _record?.Dispose();
 }
Ejemplo n.º 18
0
            public JsValue PutDocument(JsValue self, JsValue[] args)
            {
                string changeVector = null;

                if (args.Length != 2 && args.Length != 3)
                {
                    throw new InvalidOperationException("put(id, doc, changeVector) must be called with called with 2 or 3 arguments only");
                }
                AssertValidDatabaseContext();
                AssertNotReadOnly();
                if (args[0].IsString() == false && args[0].IsNull() == false && args[0].IsUndefined() == false)
                {
                    AssertValidId();
                }

                var id = args[0].IsNull() || args[0].IsUndefined() ? null : args[0].AsString();

                if (args[1].IsObject() == false)
                {
                    throw new InvalidOperationException(
                              $"Created document must be a valid object which is not null or empty. Document ID: '{id}'.");
                }

                PutOrDeleteCalled = true;

                if (args.Length == 3)
                {
                    if (args[2].IsString())
                    {
                        changeVector = args[2].AsString();
                    }
                    else if (args[2].IsNull() == false && args[0].IsUndefined() == false)
                    {
                        throw new InvalidOperationException(
                                  $"The change vector must be a string or null. Document ID: '{id}'.");
                    }
                }

                BlittableJsonReaderObject reader = null;

                try
                {
                    reader = JsBlittableBridge.Translate(_jsonCtx, ScriptEngine, args[1].AsObject(), usageMode: BlittableJsonDocumentBuilder.UsageMode.ToDisk);

                    var put = _database.DocumentsStorage.Put(
                        _docsCtx,
                        id,
                        _docsCtx.GetLazyString(changeVector),
                        reader,
                        //RavenDB-11391 This flag was added to cause attachment metadata table check & remove metadata properties if not necessary
                        nonPersistentFlags: NonPersistentDocumentFlags.ResolveAttachmentsConflict
                        );

                    if (DebugMode)
                    {
                        DebugActions.PutDocument.Add(new DynamicJsonValue
                        {
                            ["Id"]   = put.Id,
                            ["Data"] = reader
                        });
                    }

                    if (RefreshOriginalDocument == false && string.Equals(put.Id, OriginalDocumentId, StringComparison.OrdinalIgnoreCase))
                    {
                        RefreshOriginalDocument = true;
                    }

                    return(put.Id);
                }
                finally
                {
                    if (DebugMode == false)
                    {
                        reader?.Dispose();
                    }
                }
            }
Ejemplo n.º 19
0
 public override void InnerDispose()
 {
     Data?.Dispose();
 }