Exemplo n.º 1
0
        private Dictionary <string, FacetResult> ProcessResults(QueryResult queryResult, DocumentConventions conventions)
        {
            InvokeAfterQueryExecuted(queryResult);

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

            foreach (BlittableJsonReaderObject result in queryResult.Results)
            {
                var facetResult = (FacetResult)EntityToBlittable.ConvertToEntity(typeof(FacetResult), "facet/result", result, conventions);
                results[facetResult.Name] = facetResult;
            }

            QueryOperation.EnsureIsAcceptable(queryResult, _query.WaitForNonStaleResults, _duration, _session);

            return(results);
        }
Exemplo n.º 2
0
        public async Task Huge_document_hints_are_stored_and_can_be_read()
        {
            using (var store = GetDocumentStore())
            {
                var database = await GetDatabase(store.Database);

                // this tests write to storage
                database.HugeDocuments.AddIfDocIsHuge("orders/1-A", 10 * 1024 * 1024);

                // this tests merge with existing item
                database.HugeDocuments.AddIfDocIsHuge("orders/2-A", 20 * 1024 * 1024);
                database.HugeDocuments.AddIfDocIsHuge("orders/3-A", 30 * 1024 * 1024);
                database.HugeDocuments.AddIfDocIsHuge("orders/4-A", 40 * 1024 * 1024);

                database.HugeDocuments.UpdateHugeDocuments(null);

                Assert.True(database.ConfigurationStorage.NotificationsStorage.GetPerformanceHintCount() > 0);

                // now read directly from storage and verify
                using (database.ConfigurationStorage.NotificationsStorage.Read(HugeDocuments.HugeDocumentsId, out var ntv))
                {
                    if (ntv == null || ntv.Json.TryGet(nameof(PerformanceHint.Details), out BlittableJsonReaderObject detailsJson) == false || detailsJson == null)
                    {
                        Assert.False(true, "Unable to read stored notification");
                    }
                    else
                    {
                        HugeDocumentsDetails details = (HugeDocumentsDetails)EntityToBlittable.ConvertToEntity(
                            typeof(HugeDocumentsDetails),
                            HugeDocuments.HugeDocumentsId,
                            detailsJson,
                            DocumentConventions.Default);

                        Assert.NotNull(details);
                        Assert.Equal(4, details.HugeDocuments.Count);

                        var ids = details.HugeDocuments.Values.Select(x => x.Id).ToList();
                        Assert.Contains("orders/1-A", ids);
                        Assert.Contains("orders/2-A", ids);
                        Assert.Contains("orders/3-A", ids);
                        Assert.Contains("orders/4-A", ids);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public async Task ReadDocWithCompressedStringFromOneContextAndWriteToAnother()
        {
            using (var documentStore = this.GetDocumentStore())
            {
                Server.ServerStore.Observer.Suspended = true;
                var originalDoc = new Doc
                {
                    Id            = "doc/1",
                    StrVal        = new string(Enumerable.Repeat('.', 129).ToArray()),
                    LongByteArray = Enumerable.Repeat((byte)2, 1024).ToArray()
                };

                using (var session = documentStore.OpenAsyncSession())
                {
                    await session.StoreAsync(originalDoc);

                    await session.SaveChangesAsync();
                }

                var database = await Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(documentStore.Database);

                using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                    using (context.OpenReadTransaction())
                    {
                        var          doc = database.DocumentsStorage.Get(context, "doc/1");
                        MemoryStream ms  = new MemoryStream();
                        using (var newContext = JsonOperationContext.ShortTermSingleUse())
                            using (var writer = new BlittableJsonTextWriter(newContext, ms))
                            {
                                writer.WriteDocument(newContext, doc, metadataOnly: false);
                                writer.Flush();
                                var bjro             = GetReaderFromMemoryStream(ms, context);
                                var desereializedDoc = (Doc)EntityToBlittable.ConvertToEntity(typeof(Doc), null, bjro, DocumentConventions.Default);

                                Assert.Equal(originalDoc.StrVal, desereializedDoc.StrVal);
                                Assert.Equal(originalDoc.LongByteArray, originalDoc.LongByteArray);
                            }
                    }
            }
        }
Exemplo n.º 4
0
        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
            });
        }
Exemplo n.º 5
0
            public override void SetResponse(BlittableJsonReaderObject response, bool fromCache)
            {
                if (response.TryGet(nameof(RawClusterValueResult.Index), out long index) == false)
                {
                    ThrowInvalidResponse();
                }

                response.TryGet(nameof(RawClusterValueResult.Value), out BlittableJsonReaderObject raw);

                T      result;
                object val = null;

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

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

                Result = new ClusterValueResult <T>
                {
                    Index = index,
                    Value = result
                };
            }
Exemplo n.º 6
0
        public async Task SubscriptionShouldRespectDocumentsWithCompressedData()
        {
            using (var documentStore = this.GetDocumentStore())
            {
                Server.ServerStore.Observer.Suspended = true;
                var originalDoc = new Doc
                {
                    Id            = "doc/1",
                    StrVal        = new string(Enumerable.Repeat('.', 129).ToArray()),
                    LongByteArray = Enumerable.Repeat((byte)2, 1024).ToArray()
                };

                using (var session = documentStore.OpenAsyncSession())
                {
                    await session.StoreAsync(originalDoc);

                    await session.SaveChangesAsync();
                }

                var database = await Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(documentStore.Database);

                using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                    using (context.OpenReadTransaction())
                    {
                        var          doc = database.DocumentsStorage.Get(context, "doc/1");
                        MemoryStream ms  = new MemoryStream();
                        using (var newContext = JsonOperationContext.ShortTermSingleUse())
                            using (var writer = new BlittableJsonTextWriter(newContext, ms))
                            {
                                writer.WriteDocument(newContext, doc, metadataOnly: false);
                                writer.Flush();
                                var bjro             = GetReaderFromMemoryStream(ms, context);
                                var desereializedDoc = (Doc)EntityToBlittable.ConvertToEntity(typeof(Doc), null, bjro, DocumentConventions.Default);

                                Assert.Equal(originalDoc.StrVal, desereializedDoc.StrVal);
                                Assert.Equal(originalDoc.LongByteArray, originalDoc.LongByteArray);
                            }
                    }

                var subscriptionCreationParams = new SubscriptionCreationOptions
                {
                    Query = "from Docs",
                };

                var subsId = await documentStore.Subscriptions.CreateAsync(subscriptionCreationParams).ConfigureAwait(false);

                var amre = new AsyncManualResetEvent();
                using (var subscription = documentStore.Subscriptions.GetSubscriptionWorker <Doc>(new SubscriptionWorkerOptions(subsId)))
                {
                    var t = subscription.Run(batch =>
                    {
                        var receivedDoc = batch.Items.First().Result;
                        Assert.Equal(originalDoc.LongByteArray, receivedDoc.LongByteArray);
                        Assert.Equal(originalDoc.StrVal, receivedDoc.StrVal);
                        amre.Set();
                    });

                    try
                    {
                        Assert.True(await amre.WaitAsync(TimeSpan.FromSeconds(60)));
                    }
                    catch
                    {
                        if (t.IsFaulted)
                        {
                            t.Wait();
                        }
                        throw;
                    }
                }
            }
        }
        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(T);
                    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(T));
                    }
                    else
                    {
                        T convereted;
                        if (typeof(T) == typeof(BlittableJsonReaderObject))
                        {
                            convereted = (T)(object)val;
                        }
                        else
                        {
                            convereted = (T)EntityToBlittable.ConvertToEntity(typeof(T), null, val, conventions);
                        }

                        results[key] = new CompareExchangeValue <T>(key, index, convereted);
                    }
                }
            }

            return(results);
        }
Exemplo n.º 8
0
        public void CanDoFacetQueryWithAliasOnFullRange_WhenAliasIsTheSameAsOneOfTheIndexFields()
        {
            using (var store = GetDocumentStore())
            {
                new Orders_Totals().Execute(store);

                using (var session = store.OpenSession())
                {
                    session.Store(new Order
                    {
                        Company  = "C1",
                        Employee = "E1",
                        Lines    = new List <OrderLine>
                        {
                            new OrderLine
                            {
                                Quantity     = 1,
                                PricePerUnit = 10,
                                Discount     = 0
                            },
                            new OrderLine
                            {
                                Quantity     = 3,
                                PricePerUnit = 30,
                                Discount     = 0
                            }
                        }
                    });

                    session.Store(new Order
                    {
                        Company  = "C2",
                        Employee = "E2",
                        Lines    = new List <OrderLine>
                        {
                            new OrderLine
                            {
                                Quantity     = 2,
                                PricePerUnit = 20,
                                Discount     = 0
                            },
                            new OrderLine
                            {
                                Quantity     = 4,
                                PricePerUnit = 40,
                                Discount     = 0
                            }
                        }
                    });

                    session.Store(new Order
                    {
                        Company  = "C3",
                        Employee = "E3",
                        Lines    = new List <OrderLine>
                        {
                            new OrderLine
                            {
                                Quantity     = 3,
                                PricePerUnit = 30,
                                Discount     = 0
                            },
                            new OrderLine
                            {
                                Quantity     = 5,
                                PricePerUnit = 50,
                                Discount     = 0
                            }
                        }
                    });

                    session.SaveChanges();
                }

                WaitForIndexing(store);

                using (var commands = store.Commands())
                {
                    var result = commands.Query(new Raven.Client.Documents.Queries.IndexQuery
                    {
                        Query = @"
from index 'Orders/Totals'
where Total < 1000
select facet(sum(Total)) as Total"
                    });

                    var facetResult = (FacetResult)EntityToBlittable.ConvertToEntity(typeof(FacetResult), "facet/result", (BlittableJsonReaderObject)result.Results[0], store.Conventions);

                    Assert.Equal("Total", facetResult.Name);
                    Assert.Equal(1, facetResult.Values.Count);
                    Assert.Equal(Constants.Documents.Querying.Facet.AllResults, facetResult.Values[0].Range);
                    Assert.Equal(3, facetResult.Values[0].Count);
                }
            }
        }
Exemplo n.º 9
0
        private void NotifySubscribers(BlittableJsonReaderObject curDoc, out long lastReceivedEtag)
        {
            BlittableJsonReaderObject metadata;
            string id;

            if (curDoc.TryGet(Constants.Metadata.Key, out metadata) == false)
            {
                throw new InvalidOperationException("Document must have a metadata");
            }
            if (metadata.TryGet(Constants.Metadata.Id, out id) == false)
            {
                throw new InvalidOperationException("Document must have an id");
            }
            if (metadata.TryGet(Constants.Metadata.Etag, out lastReceivedEtag) == false)
            {
                throw new InvalidOperationException("Document must have an ETag");
            }

            T instance;

            if (typeof(T) == typeof(BlittableJsonReaderObject))
            {
                instance = (T)(object)curDoc;
            }
            else
            {
                instance = (T)EntityToBlittable.ConvertToEntity(typeof(T), id, curDoc, _conventions);
            }

            if (string.IsNullOrEmpty(id) == false)
            {
                _generateEntityIdOnTheClient.TrySetIdentity(instance, id);
            }

            foreach (var subscriber in _subscribers)
            {
                _proccessingCts.Token.ThrowIfCancellationRequested();
                try
                {
                    subscriber.OnNext(instance);
                }
                catch (Exception ex)
                {
                    Logger.WarnException(
                        string.Format(
                            "Subscription #{0}. Subscriber threw an exception", _options.SubscriptionId), ex);

                    if (_options.IgnoreSubscribersErrors == false)
                    {
                        IsErroredBecauseOfSubscriber = true;
                        LastSubscriberException      = ex;

                        try
                        {
                            subscriber.OnError(ex);
                        }
                        catch (Exception)
                        {
                            // can happen if a subscriber doesn't have an onError handler - just ignore it
                        }
                        break;
                    }
                }
            }
        }