Exemple #1
0
        private static long GetLongField(BlittableJsonReaderObject json, CommandType type, string fieldName)
        {
            if (json.TryGet(fieldName, out long longValue) == false)
            {
                ThrowMissingField(type, fieldName);
            }

            return(longValue);
        }
Exemple #2
0
        private static bool GetBooleanField(BlittableJsonReaderObject json, CommandType type, string fieldName)
        {
            if (json.TryGet(fieldName, out bool boolValue) == false)
            {
                ThrowMissingField(type, fieldName);
            }

            return(boolValue);
        }
        public string GetTypeFromCommand(BlittableJsonReaderObject cmd)
        {
            if (cmd.TryGet("Type", out string type) == false)
            {
                throw new RachisApplyException("Cannot execute command, wrong format");
            }

            return(type);
        }
Exemple #4
0
        private static LazyStringValue GetLazyStringField(BlittableJsonReaderObject json, CommandType type, string fieldName, bool throwOnMissing = true)
        {
            if ((json.TryGet(fieldName, out LazyStringValue value) == false || value == null) && throwOnMissing)
            {
                ThrowMissingField(type, fieldName);
            }

            return(value);
        }
Exemple #5
0
                public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
                {
                    response.TryGet(nameof(FormatOperation.Result.Expression), out string function);

                    Result = new Result
                    {
                        Expression = function
                    };
                }
        public string GetGuidFromCommand(BlittableJsonReaderObject cmd)
        {
            if (cmd.TryGet(nameof(CommandBase.UniqueRequestId), out string guid))
            {
                return(guid);
            }

            return(null);
        }
Exemple #7
0
 public static LazyStringValue GetLazyCollectionNameFrom(JsonOperationContext context, BlittableJsonReaderObject document)
 {
     if (document.TryGet(MetadataKeySegment, out BlittableJsonReaderObject metadata) == false ||
         metadata.TryGet(MetadataCollectionSegment, out LazyStringValue collectionName) == false)
     {
         return(context.GetLazyStringForFieldWithCaching(EmptyCollectionSegment));
     }
     return(collectionName);
 }
Exemple #8
0
        private static IndexDefinition ReadIndexDefinition(BlittableJsonReaderObject reader)
        {
            if (reader.TryGet(nameof(IndexDefinition), out BlittableJsonReaderObject jsonObject) == false || jsonObject == null)
            {
                throw new InvalidOperationException("No persisted definition");
            }

            return(JsonDeserializationServer.IndexDefinition(jsonObject));
        }
Exemple #9
0
        public static string GetChangeVector(BlittableJsonReaderObject metadata)
        {
            if (metadata.TryGet(Constants.Documents.Metadata.ChangeVector, out string changeVector) == false)
            {
                InvalidMissingChangeVector();
            }

            return(changeVector);
        }
        public static DocumentConflictException From(BlittableJsonReaderObject json)
        {
            if (json.TryGet(nameof(DocId), out string docId) == false)
            {
                throw new InvalidDataException("Expected to find property named " + nameof(DocId) + " in the exception received from the server, but didn't find it. This is probably a bug and should be reported.");
            }

            if (json.TryGet(nameof(Message), out string message) == false)
            {
                throw new InvalidDataException("Expected to find property named " + nameof(Message) + " in the exception received from the server, but didn't find it. This is probably a bug and should be reported.");
            }

            if (json.TryGet(nameof(LargestEtag), out long etag) == false)
            {
                throw new InvalidDataException("Expected to find property named " + nameof(LargestEtag) + " in the exception received from the server, but didn't find it. This is probably a bug and should be reported.");
            }

            return(new DocumentConflictException(message, docId, etag));
        }
Exemple #11
0
        private static string GetStringFromJson(BlittableJsonReaderObject reader, string value)
        {
            string str;

            if (reader.TryGet(value, out str) == false)
            {
                throw new InvalidOperationException($"Missing string '{value}' in message : ${reader}");
            }
            return(str);
        }
        public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
        {
            if (response == null || response.TryGet("NewIdentityValue", out long results) == false)
            {
                ThrowInvalidResponse();
                return; // never hit
            }

            Result = results;
        }
Exemple #13
0
        private static DateTime GetDateTimeFromJson(BlittableJsonReaderObject reader, string value)
        {
            string str;

            if (reader.TryGet(value, out str) == false)
            {
                throw new InvalidOperationException($"Missing DateTime '{value}' in message : ${reader}");
            }
            return(DateTime.Parse(str));
        }
Exemple #14
0
        private static int GetIntFromJson(BlittableJsonReaderObject reader, string value)
        {
            int int32;

            if (reader.TryGet(value, out int32) == false)
            {
                throw new InvalidOperationException($"Missing int '{value}' in message : ${reader}");
            }
            return(int32);
        }
Exemple #15
0
        public void Put(DocumentsOperationContext context, Slice lowerId, BlittableJsonReaderObject document)
        {
            if (document.TryGet(Constants.Documents.Metadata.Key, out BlittableJsonReaderObject metadata) == false ||
                metadata.TryGet(Constants.Documents.Metadata.Expires, out string expirationDate) == false)
            {
                return;
            }

            PutInternal(context, lowerId, expirationDate);
        }
Exemple #16
0
            protected override long 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);
            }
Exemple #17
0
        public void EnsureMetadata()
        {
            if (_metadataEnsured)
            {
                return;
            }

            _metadataEnsured = true;
            DynamicJsonValue mutatedMetadata;

            if (Data.TryGet(Constants.Documents.Metadata.Key, out BlittableJsonReaderObject metadata))
            {
                if (metadata.Modifications == null)
                {
                    metadata.Modifications = new DynamicJsonValue(metadata);
                }

                mutatedMetadata = metadata.Modifications;
            }
            else
            {
                Data.Modifications = new DynamicJsonValue(Data)
                {
                    [Constants.Documents.Metadata.Key] = mutatedMetadata = new DynamicJsonValue()
                };
            }
            mutatedMetadata[Constants.Documents.Metadata.Id] = Id;
            if (ChangeVector != null)
            {
                mutatedMetadata[Constants.Documents.Metadata.ChangeVector] = ChangeVector;
            }
            if (Flags != DocumentFlags.None)
            {
                mutatedMetadata[Constants.Documents.Metadata.Flags] = Flags.ToString();
            }
            if (IndexScore.HasValue)
            {
                mutatedMetadata[Constants.Documents.Metadata.IndexScore] = IndexScore;
            }

            _hash = null;
        }
        protected static string ReadName(BlittableJsonReaderObject reader)
        {
            string name;

            if (reader.TryGet(nameof(Name), out name) == false || string.IsNullOrWhiteSpace(name))
            {
                throw new InvalidOperationException("No persisted name");
            }

            return(name);
        }
        public override void SetResponse(BlittableJsonReaderObject response, bool fromCache)
        {
            if (response == null || response.TryGet("NewSeedValue", out long result) == false)
            {
                ThrowInvalidResponse();
                return; // never hit
            }


            Result = result;
        }
Exemple #20
0
        public static Dictionary <string, CompareExchangeValue <T> > GetValues(BlittableJsonReaderObject response, DocumentConventions conventions)
        {
            if (response.TryGet("Results", out BlittableJsonReaderArray items) == false)
            {
                throw new InvalidDataException("Response is invalid. Results is missing.");
            }

            var results = new Dictionary <string, CompareExchangeValue <T> >();

            foreach (BlittableJsonReaderObject item in items)
            {
                if (item == null)
                {
                    throw new InvalidDataException("Response is invalid. Item is null.");
                }

                if (item.TryGet("Key", out string key) == false)
                {
                    throw new InvalidDataException("Response is invalid. Key is missing.");
                }
                if (item.TryGet("Index", out long index) == false)
                {
                    throw new InvalidDataException("Response is invalid. Index is missing.");
                }
                if (item.TryGet("Value", out BlittableJsonReaderObject raw) == false)
                {
                    throw new InvalidDataException("Response is invalid. Value is missing.");
                }

                if (typeof(T).GetTypeInfo().IsPrimitive || typeof(T) == typeof(string))
                {
                    // simple
                    T value = default;
                    raw?.TryGet("Object", out value);
                    results[key] = new CompareExchangeValue <T>(key, index, value);
                }
                else
                {
                    BlittableJsonReaderObject val = null;
                    raw?.TryGet("Object", out val);
                    if (val == null)
                    {
                        results[key] = new CompareExchangeValue <T>(key, index, default);
                    }
                    else
                    {
                        var converted = (T)EntityToBlittable.ConvertToEntity(typeof(T), null, val, conventions);
                        results[key] = new CompareExchangeValue <T>(key, index, converted);
                    }
                }
            }

            return(results);
        }
Exemple #21
0
                public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
                {
                    Result = new List <string>();

                    response.TryGet(nameof(IndexQueryServerSide.Diagnostics), out BlittableJsonReaderArray array);

                    foreach (var item in array)
                    {
                        Result.Add(item.ToString());
                    }
                }
Exemple #22
0
                public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
                {
                    Result = new List <string>();

                    response.TryGet("Nodes", out BlittableJsonReaderArray array);

                    foreach (var item in array)
                    {
                        Result.Add(item.ToString());
                    }
                }
Exemple #23
0
        public static DateTime GetLastModified(this BlittableJsonReaderObject metadata)
        {
            DateTime lastModified;

            if (metadata.TryGet(Constants.Documents.Metadata.LastModified, out lastModified) == false)
            {
                InvalidMissingLastModified();
            }

            return(lastModified);
        }
Exemple #24
0
        public static string GetId(this BlittableJsonReaderObject metadata)
        {
            string id;

            if (metadata.TryGet(Constants.Documents.Metadata.Id, out id) == false)
            {
                throw new InvalidOperationException($"Metadata does not contain '{Constants.Documents.Metadata.Id}' field.");
            }

            return(id);
        }
Exemple #25
0
        public static BlittableJsonReaderObject GetMetadata(this BlittableJsonReaderObject document)
        {
            BlittableJsonReaderObject metadata;

            if (document.TryGet(Constants.Documents.Metadata.Key, out metadata) == false || metadata == null)
            {
                throw new InvalidOperationException($"Document does not contain '{Constants.Documents.Metadata.Key}' field.");
            }

            return(metadata);
        }
Exemple #26
0
        public static bool HasPassed(BlittableJsonReaderObject data, DateTime currentTime)
        {
            // Validate that the expiration value in metadata is still the same.
            // We have to check this as the user can update this value.
            if (data.TryGet(Constants.Documents.Metadata.Key, out BlittableJsonReaderObject metadata) == false)
            {
                return(false);
            }

            return(HasPassed(metadata, Constants.Documents.Metadata.Expires, currentTime));
        }
        protected static IndexLockMode ReadLockMode(BlittableJsonReaderObject reader)
        {
            int lockModeAsInt;

            if (reader.TryGet(nameof(LockMode), out lockModeAsInt) == false)
            {
                throw new InvalidOperationException("No persisted lock mode");
            }

            return((IndexLockMode)lockModeAsInt);
        }
        public static string GetCollectionName(BlittableJsonReaderObject document)
        {
            string collectionName;
            BlittableJsonReaderObject metadata;

            if (document == null || document.TryGet(Constants.Metadata.Key, out metadata) == false ||
                metadata.TryGet(Constants.Headers.RavenEntityName, out collectionName) == false)
            {
                collectionName = EmptyCollection;
            }
            return(collectionName);
        }
        public static CompareExchangeResult <T> ParseFromBlittable(BlittableJsonReaderObject response, DocumentConventions conventions)
        {
            if (response.TryGet(nameof(Index), out long index) == false)
            {
                throw new InvalidDataException("Response is invalid. Index is missing.");
            }

            response.TryGet(nameof(Successful), out bool successful);
            response.TryGet(nameof(Value), out BlittableJsonReaderObject raw);

            T      result;
            object val = null;

            raw?.TryGet("Object", out val);

            if (val == null)
            {
                return(new CompareExchangeResult <T>
                {
                    Index = index,
                    Value = default(T),
                    Successful = successful
                });
            }
            if (val is BlittableJsonReaderObject obj)
            {
                result = (T)EntityToBlittable.ConvertToEntity(typeof(T), "cluster-value", obj, conventions);
            }
            else
            {
                raw.TryGet("Object", out result);
            }

            return(new CompareExchangeResult <T>
            {
                Index = index,
                Value = result,
                Successful = successful
            });
        }
Exemple #30
0
        public static CounterOperation Parse(BlittableJsonReaderObject input)
        {
            if (input.TryGet(nameof(CounterName), out string name) == false || name == null)
            {
                ThrowMissingCounterName();
            }

            if (input.TryGet(nameof(Type), out string type) == false || type == null)
            {
                ThrowMissingCounterOperationType(name);
            }

            var counterOperationType = (CounterOperationType)Enum.Parse(typeof(CounterOperationType), type);

            long?delta = null;

            switch (counterOperationType)
            {
            case CounterOperationType.Increment:
            case CounterOperationType.Put:
                if (input.TryGet(nameof(Delta), out delta) == false)
                {
                    ThrowMissingDeltaProperty(name, counterOperationType);
                }
                break;
            }

            var counterOperation = new CounterOperation
            {
                Type        = counterOperationType,
                CounterName = name
            };

            if (delta != null)
            {
                counterOperation.Delta = delta.Value;
            }

            return(counterOperation);
        }