Пример #1
0
        public string ToString(int lengthLimitInBytes)
        {
            string queryPlanString = this.QueryPlan?.ToString();
            bool   shouldSerializeQueryPlan;

            if (queryPlanString == null)
            {
                shouldSerializeQueryPlan = false;
            }
            else
            {
                shouldSerializeQueryPlan = (queryPlanString.Length + this.SourceContinuationToken.ToString().Length) < lengthLimitInBytes;
            }

            return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
            {
                {
                    PipelineContinuationToken.VersionPropertyName,
                    CosmosString.Create(this.Version.ToString())
                },
                {
                    PipelineContinuationTokenV1_1.QueryPlanPropertyName,
                    shouldSerializeQueryPlan ? CosmosString.Create(queryPlanString) : (CosmosElement)CosmosNull.Create()
                },
                {
                    PipelineContinuationTokenV1_1.SourceContinuationTokenPropertyName,
                    this.SourceContinuationToken
                },
            }).ToString());
        }
        public async Task <CosmosObject> DecryptAsync(
            CosmosObject document,
            Encryptor encryptor,
            CosmosDiagnosticsContext diagnosticsContext,
            CancellationToken cancellationToken)
        {
            Debug.Assert(document != null);
            Debug.Assert(encryptor != null);
            Debug.Assert(diagnosticsContext != null);

            if (!document.TryGetValue(Constants.Properties.EncryptedInfo, out CosmosElement encryptedInfo))
            {
                return(document);
            }

            EncryptionProperties encryptionProperties = JsonConvert.DeserializeObject <EncryptionProperties>(encryptedInfo.ToString());

            JObject plainTextJObj = await this.DecryptContentAsync(
                encryptionProperties,
                encryptor,
                diagnosticsContext,
                cancellationToken);

            Dictionary <string, CosmosElement> documentContent = document.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            documentContent.Remove(Constants.Properties.EncryptedInfo);

            foreach (JProperty property in plainTextJObj.Properties())
            {
                documentContent.Add(property.Name, property.Value.ToObject <CosmosElement>());
            }

            return(CosmosObject.Create(documentContent));
        }
Пример #3
0
        public static CosmosElement ToCosmosElement(FeedRangeState <ReadFeedState> feedRangeState)
        {
            Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>()
            {
                {
                    PropertyNames.FeedRange,
                    FeedRangeCosmosElementSerializer.ToCosmosElement(feedRangeState.FeedRange)
                }
            };

            if (feedRangeState.State is ReadFeedBeginningState)
            {
                dictionary[PropertyNames.State] = CosmosNull.Create();
            }
            else if (feedRangeState.State is ReadFeedContinuationState readFeedContinuationState)
            {
                dictionary[PropertyNames.State] = readFeedContinuationState.ContinuationToken;
            }
            else
            {
                throw new InvalidOperationException("Unknown FeedRange State.");
            }

            return(CosmosObject.Create(dictionary));
        }
Пример #4
0
        private CosmosObject CreateVertexDocument(string id, string label, string pkName, string pkValue, IEnumerable <Tuple <string, IEnumerable <object> > > userProperties)
        {
            Dictionary <string, CosmosElement> vertexDocumentProperties = new Dictionary <string, CosmosElement>()
            {
                { GremlinKeywords.KW_DOC_ID, CosmosString.Create(id) },
                { GremlinKeywords.KW_VERTEX_LABEL, CosmosString.Create(label) },
            };

            if (!string.IsNullOrEmpty(pkName) && !string.IsNullOrEmpty(pkValue))
            {
                vertexDocumentProperties.Add(pkName, CosmosString.Create(pkValue));
            }

            foreach (Tuple <string, IEnumerable <object> > userProperty in userProperties)
            {
                List <CosmosElement> singleValues = new List <CosmosElement>();
                foreach (object userPropertyValue in userProperty.Item2)
                {
                    string propertyId = Guid.NewGuid().ToString();
                    singleValues.Add(
                        this.CreateVertexPropertySingleComplexValue(
                            CosmosString.Create(propertyId),
                            this.CreateVertexPropertyPrimitiveValueElement(userPropertyValue)));
                }
            }

            return(CosmosObject.Create(vertexDocumentProperties));
        }
        public static CosmosElement ToCosmosElement(OrderByContinuationToken orderByContinuationToken)
        {
            CosmosElement        compositeContinuationToken = ParallelContinuationToken.ToCosmosElement(orderByContinuationToken.ParallelContinuationToken);
            List <CosmosElement> orderByItemsRaw            = new List <CosmosElement>();

            foreach (OrderByItem orderByItem in orderByContinuationToken.OrderByItems)
            {
                orderByItemsRaw.Add(OrderByItem.ToCosmosElement(orderByItem));
            }

            CosmosArray orderByItems = CosmosArray.Create(orderByItemsRaw);

            CosmosElement filter = orderByContinuationToken.Filter == null?CosmosNull.Create() : (CosmosElement)CosmosString.Create(orderByContinuationToken.Filter);

            CosmosObject cosmosObject = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { PropertyNames.CompositeToken, compositeContinuationToken },
                { PropertyNames.OrderByItems, orderByItems },
                { PropertyNames.Rid, CosmosString.Create(orderByContinuationToken.Rid) },
                { PropertyNames.SkipCount, CosmosNumber64.Create(orderByContinuationToken.SkipCount) },
                { PropertyNames.Filter, filter },
            });

            return(cosmosObject);
        }
Пример #6
0
 public CosmosElement Visit(FeedRangePartitionKey feedRange)
 {
     return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
     {
         { TypePropertyName, LogicalPartitionKeyCosmosElement },
         { ValuePropertyName, CosmosString.Create(feedRange.PartitionKey.ToJsonString()) },
     }));
 }
Пример #7
0
 public CosmosElement Visit(FeedRangePartitionKeyRange feedRange)
 {
     return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
     {
         { TypePropertyName, PhysicalPartitionKeyRangeIdCosmosElement },
         { ValuePropertyName, CosmosString.Create(feedRange.PartitionKeyRangeId) },
     }));
 }
 public override string ToString()
 {
     return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
     {
         { nameof(this.GroupingTableContinuationToken), CosmosString.Create(this.GroupingTableContinuationToken) },
         { nameof(this.SourceContinuationToken), CosmosString.Create(this.SourceContinuationToken) }
     }).ToString());
 }
Пример #9
0
        public void ObjectPropertyOrderDoesNotMatter()
        {
            CosmosObject cosmosObject         = (CosmosObject)Elements.ObjectWithItems;
            CosmosObject cosmosObjectReversed = CosmosObject.Create(cosmosObject.Reverse().ToList());

            Assert.AreEqual(cosmosObject, cosmosObjectReversed);
            Assert.AreEqual(cosmosObject.GetHashCode(), cosmosObjectReversed.GetHashCode());
        }
Пример #10
0
 public CosmosElement Transform(ChangeFeedStateContinuation changeFeedStateContinuation)
 {
     return(CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
     {
         { TypePropertyName, ContinuationTypeValueSingleton },
         { ValuePropertyName, changeFeedStateContinuation.ContinuationToken }
     }));
 }
        /// <summary>
        /// Visits a SqlAliasedCollectionExpression and transforms the documents accordingly.
        /// </summary>
        /// <param name="collectionExpression">The collection expression to visit.</param>
        /// <param name="documents">The documents to transform.</param>
        /// <returns>The transformed documents according to the collection expression.</returns>
        public override IEnumerable <CosmosElement> Visit(SqlAliasedCollectionExpression collectionExpression, IEnumerable <CosmosElement> documents)
        {
            //If the query was:

            //SELECT *
            //FROM c

            //and the document was:
            //{
            //    "name" : "John"
            //}

            //Then this function would return:
            //{
            //    "c" : { "name" : "John" }
            //}

            //which is just wrapping the original document in the identifier.

            // Get the sub collection
            CollectionEvaluationResult collectionEvaluationResult = collectionExpression.Collection.Accept(
                SqlInterpreterCollectionVisitor.Singleton,
                documents);
            string identifer = collectionEvaluationResult.Identifer;

            // Figure out what the alias should be
            int    aliasCounter = 0;
            string alias;

            if (collectionExpression.Alias != null)
            {
                alias = collectionExpression.Alias.Value;
            }
            else if (identifer != null)
            {
                alias = identifer;
            }
            else
            {
                alias = $"#{aliasCounter++}";
            }

            // Wrap the collection in the alias so it's easier to bind later
            foreach (Tuple <CosmosElement, string> subDocumentAndRid in collectionEvaluationResult.SubDocumentsAndRids)
            {
                Dictionary <string, CosmosElement> wrappedDocument = new Dictionary <string, CosmosElement>
                {
                    [alias] = subDocumentAndRid.Item1,

                    //// Add the _rid so that we can break ties for sort later
                    ["_rid"] = CosmosString.Create(subDocumentAndRid.Item2)
                };

                yield return(CosmosObject.Create(wrappedDocument));
            }
        }
Пример #12
0
 public static CosmosElement ToCosmosElement(VersionedAndRidCheckedCompositeToken token)
 {
     return(CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
     {
         { PropertyNames.Version, CosmosNumber64.Create((long)token.VersionNumber) },
         { PropertyNames.Rid, CosmosString.Create(token.Rid) },
         { PropertyNames.Continuation, token.ContinuationToken },
     }));
 }
 private static QueryState CreateQueryState(int count)
 {
     return(new QueryState(
                CosmosString.Create(
                    CosmosObject.Create(
                        new Dictionary <string, CosmosElement>()
     {
         { "continuationCount", CosmosNumber64.Create(++count) },
     })
                    .ToString())));
 }
Пример #14
0
            public CosmosElement GetCosmosElementContinuationToken()
            {
                Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>();

                foreach (KeyValuePair <UInt128, SingleGroupAggregator> kvp in this.table)
                {
                    dictionary.Add(kvp.Key.ToString(), kvp.Value.GetCosmosElementContinuationToken());
                }

                return(CosmosObject.Create(dictionary));
            }
Пример #15
0
            public override CosmosElement GetCosmosElementContinuationToken()
            {
                Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>();

                foreach (KeyValuePair <string, AggregateValue> kvp in this.aliasToValue)
                {
                    dictionary.Add(kvp.Key, kvp.Value.GetCosmosElementContinuationToken());
                }

                return(CosmosObject.Create(dictionary));
            }
Пример #16
0
                public override CosmosElement GetResult()
                {
                    Dictionary <string, CosmosElement> mergedResult = new Dictionary <string, CosmosElement>();

                    foreach (KeyValuePair <string, GroupByValue> aliasAndValue in this.aliasToValue)
                    {
                        string       alias        = aliasAndValue.Key;
                        GroupByValue groupByValue = aliasAndValue.Value;
                        mergedResult[alias] = groupByValue.Result;
                    }

                    return(CosmosObject.Create(mergedResult));
                }
            public static CosmosElement ToCosmosElement(AverageInfo averageInfo)
            {
                Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>();

                if (averageInfo.Sum.HasValue)
                {
                    dictionary.Add(AverageInfo.SumName, CosmosNumber64.Create(averageInfo.Sum.Value));
                }

                dictionary.Add(AverageInfo.CountName, CosmosNumber64.Create(averageInfo.Count));

                return(CosmosObject.Create(dictionary));
            }
        public void SqlObjectCreateScalarExpressionTest()
        {
            CosmosObject expected = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["name"] = CosmosString.Create("John")
            });

            SqlObjectCreateScalarExpression john = SqlObjectCreateScalarExpression.Create(
                SqlObjectProperty.Create(SqlPropertyName.Create("name"),
                                         SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("John"))));

            AssertEvaluation(expected, john);
        }
            public override string GetContinuationToken()
            {
                Dictionary <string, CosmosElement> aliasToContinuationToken = new Dictionary <string, CosmosElement>();

                foreach (KeyValuePair <string, AggregateValue> kvp in this.aliasToValue)
                {
                    aliasToContinuationToken[kvp.Key] = CosmosString.Create(kvp.Value.GetContinuationToken());
                }

                CosmosObject cosmosObject = CosmosObject.Create(aliasToContinuationToken);

                return(cosmosObject.ToString());
            }
Пример #20
0
                public override CosmosElement GetCosmosElementContinuationToken()
                {
                    Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>();

                    dictionary.Add(nameof(this.initialized), CosmosBoolean.Create(this.initialized));

                    if (this.value != null)
                    {
                        dictionary.Add(nameof(this.value), this.value);
                    }

                    return(CosmosObject.Create(dictionary));
                }
Пример #21
0
 public override string ToString()
 {
     return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
     {
         {
             PipelineContinuationToken.VersionPropertyName,
             CosmosString.Create(this.Version.ToString())
         },
         {
             PipelineContinuationTokenV1.SourceContinuationTokenPropertyName,
             this.SourceContinuationToken
         },
     }).ToString());
 }
Пример #22
0
 public static CosmosElement ToCosmosElement(FeedRangeState <ChangeFeedState> feedRangeState)
 {
     return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
     {
         {
             PropertyNames.FeedRange,
             FeedRangeCosmosElementSerializer.ToCosmosElement(feedRangeState.FeedRange)
         },
         {
             PropertyNames.State,
             ChangeFeedStateCosmosElementSerializer.ToCosmosElement(feedRangeState.State)
         }
     }));
 }
        /// <summary>
        /// Visits a SqlArrayIteratorCollectionExpression and transforms the documents accordingly.
        /// </summary>
        /// <param name="collectionExpression">The collection expression to visit.</param>
        /// <param name="documents">The documents to transform.</param>
        /// <returns>The transformed documents according to the collection expression.</returns>
        public override IEnumerable <CosmosElement> Visit(SqlArrayIteratorCollectionExpression collectionExpression, IEnumerable <CosmosElement> documents)
        {
            //If the query was:

            //SELECT p
            //FROM p in c.parents

            //and the document was
            //{
            //    "parents" : [{"name" : "John"}, {"name" : "Sally"}]
            //}

            //then the results would be:

            //{ "p" : {"name" : "John"} }

            //and

            //{ "p" : {"name" : "Sally"} }

            //Notice that the result set is larger than the input
            //This is because we emitted one document for each parent in the original document
            //and wrapped it in the provided alias.

            // throw away the identifer since we always have an alias
            CollectionEvaluationResult collectionEvaluationResult = collectionExpression.Collection.Accept(
                SqlInterpreterCollectionVisitor.Singleton,
                documents);

            // Wrap the collection in the alias so it's easier to bind later
            foreach (Tuple <CosmosElement, string> subDocumentAndRid in collectionEvaluationResult.SubDocumentsAndRids)
            {
                CosmosElement document = subDocumentAndRid.Item1;
                string        rid      = subDocumentAndRid.Item2;
                if (document is CosmosArray array)
                {
                    foreach (CosmosElement item in array)
                    {
                        Dictionary <string, CosmosElement> wrappedDocument = new Dictionary <string, CosmosElement>
                        {
                            [collectionExpression.Identifier.Value] = item,

                            //// Add the _rid so that we can break ties for sort later
                            ["_rid"] = CosmosString.Create(rid),
                        };
                        yield return(CosmosObject.Create(wrappedDocument));
                    }
                }
            }
        }
Пример #24
0
        internal async Task <PartitionKey> GetPartitionKeyValueFromStreamAsync(
            Stream stream,
            CancellationToken cancellation = default(CancellationToken))
        {
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is needs to be seekable", nameof(stream));
            }

            try
            {
                stream.Position = 0;

                MemoryStream memoryStream = stream as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    stream.CopyTo(memoryStream);
                }

                // TODO: Avoid copy
                IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
                IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
                CosmosObject       pathTraversal     = CosmosObject.Create(jsonNavigator, jsonNavigatorNode);

                string[] tokens = await this.GetPartitionKeyPathTokensAsync(cancellation);

                for (int i = 0; i < tokens.Length - 1; i++)
                {
                    pathTraversal = pathTraversal[tokens[i]] as CosmosObject;
                    if (pathTraversal == null)
                    {
                        return(PartitionKey.None);
                    }
                }

                CosmosElement partitionKeyValue = pathTraversal[tokens[tokens.Length - 1]];
                if (partitionKeyValue == null)
                {
                    return(PartitionKey.None);
                }

                return(this.CosmosElementToPartitionKeyObject(partitionKeyValue));
            }
            finally
            {
                // MemoryStream casting leverage might change position
                stream.Position = 0;
            }
        }
 public static CosmosElement ToCosmosElement(ReadFeedContinuationToken readFeedContinuationToken)
 {
     return(CosmosObject.Create(new Dictionary <string, CosmosElement>()
     {
         {
             PropertyNames.FeedRange,
             FeedRangeCosmosElementSerializer.ToCosmosElement(readFeedContinuationToken.Range)
         },
         {
             PropertyNames.State,
             readFeedContinuationToken.State.ContinuationToken
         }
     }));
 }
Пример #26
0
 public CosmosElement Transform(ChangeFeedStateTime changeFeedStateTime)
 {
     return(CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
     {
         { TypePropertyName, TimeTypeValueSingleton },
         {
             ValuePropertyName,
             CosmosString.Create(changeFeedStateTime.StartTime.ToString(
                                     "o",
                                     CultureInfo.InvariantCulture))
         }
     }));
 }
        public override async Task <PartitionKey> GetPartitionKeyValueFromStreamAsync(
            Stream stream,
            CancellationToken cancellation = default(CancellationToken))
        {
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream needs to be seekable", nameof(stream));
            }

            try
            {
                stream.Position = 0;

                if (!(stream is MemoryStream memoryStream))
                {
                    memoryStream = new MemoryStream();
                    stream.CopyTo(memoryStream);
                }

                // TODO: Avoid copy
                IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
                IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
                CosmosObject       pathTraversal     = CosmosObject.Create(jsonNavigator, jsonNavigatorNode);

                IReadOnlyList <IReadOnlyList <string> > tokenslist = await this.GetPartitionKeyPathTokensAsync(cancellation);

                List <CosmosElement> cosmosElementList = new List <CosmosElement>(tokenslist.Count);

                foreach (IReadOnlyList <string> tokenList in tokenslist)
                {
                    CosmosElement element;
                    if (ContainerCore.TryParseTokenListForElement(pathTraversal, tokenList, out element))
                    {
                        cosmosElementList.Add(element);
                    }
                    else
                    {
                        cosmosElementList.Add(null);
                    }
                }

                return(ContainerCore.CosmosElementToPartitionKeyObject(cosmosElementList));
            }
            finally
            {
                // MemoryStream casting leverage might change position
                stream.Position = 0;
            }
        }
Пример #28
0
        public override CosmosElement Visit(SqlSelectListSpec selectSpec, CosmosElement document)
        {
            if (selectSpec == null)
            {
                throw new ArgumentNullException(nameof(selectSpec));
            }

            Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>();

            int aliasCounter = 1;

            foreach (SqlSelectItem sqlSelectItem in selectSpec.Items)
            {
                CosmosElement value = sqlSelectItem.Expression.Accept(ScalarExpressionEvaluator.Singleton, document);
                if (value != null)
                {
                    string key = default;
                    if (sqlSelectItem.Alias != null)
                    {
                        key = sqlSelectItem.Alias.Value;
                    }
                    else
                    {
                        if (sqlSelectItem.Expression is SqlMemberIndexerScalarExpression memberIndexer)
                        {
                            SqlScalarExpression lastToken = this.GetLastMemberIndexerToken(memberIndexer);

                            if (lastToken is SqlLiteralScalarExpression literalScalarExpression)
                            {
                                if (literalScalarExpression.Literal is SqlStringLiteral stringLiteral)
                                {
                                    key = stringLiteral.Value;
                                }
                            }
                        }
                    }

                    if (key == default)
                    {
                        key = $"${aliasCounter++}";
                    }

                    dictionary[key] = value;
                }
            }

            return(CosmosObject.Create(dictionary));
        }
        public void SqlArrayScalarExpressionTest()
        {
            CosmosObject tag = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["name"] = CosmosString.Create("asdf")
            });

            CosmosObject tags = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["tags"] = CosmosArray.Create(new List <CosmosElement>()
                {
                    tag
                }),
                ["_rid"] = CosmosString.Create("AYIMAMmFOw8YAAAAAAAAAA==")
            });

            CosmosObject tagsWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["c"]    = tags,
                ["_rid"] = CosmosString.Create("AYIMAMmFOw8YAAAAAAAAAA==")
            });

            // ARRAY(SELECT VALUE t.name FROM t in c.tags)
            SqlArrayScalarExpression arrayScalarExpression = SqlArrayScalarExpression.Create(
                SqlQuery.Create(
                    SqlSelectClause.Create(
                        SqlSelectValueSpec.Create(
                            TestUtils.CreatePathExpression("t", "name"))),
                    SqlFromClause.Create(
                        SqlArrayIteratorCollectionExpression.Create(
                            SqlIdentifier.Create("t"),
                            SqlInputPathCollection.Create(
                                SqlIdentifier.Create("c"),
                                SqlStringPathExpression.Create(
                                    null,
                                    SqlStringLiteral.Create("tags"))))),
                    whereClause: null,
                    groupByClause: null,
                    orderByClause: null,
                    offsetLimitClause: null));

            CosmosArray tagNames = CosmosArray.Create(new List <CosmosElement>()
            {
                CosmosString.Create("asdf")
            });

            AssertEvaluation(tagNames, arrayScalarExpression, tagsWrapped);
        }
                public static CosmosElement ToCosmosElement(DistinctContinuationToken distinctContinuationToken)
                {
                    Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>()
                    {
                        {
                            DistinctContinuationToken.SourceTokenName,
                            distinctContinuationToken.SourceToken
                        },
                        {
                            DistinctContinuationToken.DistinctMapTokenName,
                            distinctContinuationToken.DistinctMapToken
                        }
                    };

                    return(CosmosObject.Create(dictionary));
                }