Esempio n. 1
0
    protected virtual async Task <DbDoc?> ReadByIdAsync(string id, PartitionKey partitionKey)
    {
        if (id is null)
        {
            throw new ArgumentNullException(nameof(id));
        }

        using var result = await Container.ReadItemStreamAsync(
                  id : id,
                  partitionKey : partitionKey).ConfigureAwait(false);

        if (result.IsSuccessStatusCode)
        {
            var item = Serializer.FromStream <DbDoc>(result.Content) ?? throw new DbUnexpectedStateException("FromStream<T> returned null");
            Debug.Assert(item.id == id);
            Debug.Assert(partitionKey.ToString() == new PartitionKey(item.pk).ToString());
            Debug.Assert(!string.IsNullOrEmpty(item._etag));
            Debug.Assert(item._ts > 0);
            return(item);
        }
        if (result.StatusCode == HttpStatusCode.NotFound)
        {
            return(null);
        }
        throw result.ExceptionFromErrorStatus();
    }
        public void WithCosmosPartitionKey()
        {
            const string somePK = "somePK";
            PartitionKey v3PK   = new PartitionKey(somePK);
            PartitionKey pk     = new PartitionKey(v3PK);

            Assert.AreEqual(v3PK.ToString(), pk.ToString());
        }
        public void ToStringGetsJsonString()
        {
            const string somePK   = "somePK";
            string       expected = $"[\"{somePK}\"]";
            PartitionKey pk       = new PartitionKey(somePK);

            Assert.AreEqual(expected, pk.ToString());
        }
        public void WithDocumentPartitionKey()
        {
            const string somePK = "somePK";

            Documents.PartitionKey v2PK = new Documents.PartitionKey(somePK);
            PartitionKey           pk   = new PartitionKey(v2PK);

            Assert.AreEqual(v2PK.InternalKey.ToJsonString(), pk.ToString());
        }
Esempio n. 5
0
    protected virtual async Task <DbDoc?[]> ReadByIdsAsync(IEnumerable <string> ids, PartitionKey partitionKey)
    {
        if (ids is null)
        {
            throw new ArgumentNullException(nameof(ids));
        }

        var idList = ids.Select(x => x ?? throw new ArgumentNullException(nameof(ids))).ToArray() ?? throw new ArgumentNullException(nameof(ids));

        if (idList.Length == 0)
        {
            return(Array.Empty <DbDoc?>());
        }
        if (idList.Length <= ReadIdsQueryThreshhold)
        {
            var readTasks = idList.Select(id => ReadByIdAsync(id: id, partitionKey: partitionKey)).ToList();
            await Task.WhenAll(readTasks).ConfigureAwait(false);

            return(readTasks.Select(x => x.Result).ToArray());
        }

        var results   = new DbDoc?[idList.Length];
        var resultMap = idList.Select((id, index) => (id, index)).ToDictionary(x => x.id, x => x.index);

        foreach (var queryIds in idList.Segment(MaxIdsPerQuery))
        {
            var totalLength = 0;
            var queryNowIds = queryIds
                              .TakeWhile(x => (totalLength += x.Length) < MaxIdLengthPerQuery)
                              .ToList();

            var query = Container.GetItemLinqQueryable <DbDoc>(
                allowSynchronousQueryExecution: false,
                requestOptions: new QueryRequestOptions
            {
                PartitionKey = partitionKey
            })
                        .Where(x => queryNowIds.Contains(x.id));

            await foreach (var item in ExecuteQueryAsync(query))
            {
                Debug.Assert(partitionKey.ToString() == new PartitionKey(item.pk).ToString());
                Debug.Assert(!string.IsNullOrEmpty(item._etag));
                Debug.Assert(item._ts > 0);
                results[resultMap[item.id]] = item;
            }
        }

        return(results);
    }
Esempio n. 6
0
    public static string Encode(string id, PartitionKey partitionKey)
    {
        if (id.IsNullOrWhiteSpace())
        {
            throw new ArgumentNullException(nameof(id));
        }

        if (partitionKey == default)
        {
            return(id);
        }

        var json = partitionKey.ToString();
        var keys = Newtonsoft.Json.JsonConvert.DeserializeObject <string[]>(json);

        if (keys == null || keys.Length < 1)
        {
            return(id);
        }

        return(partitionKey != default ? $"{id}~{keys[0]}" : id);
    }
Esempio n. 7
0
 private bool PartitionKeyValueIs(PartitionKey pk, string value)
 {
     return(pk.ToString() == $"[\"{value}\"]");
 }
Esempio n. 8
0
 public static void LogPartitionKey(this ILogger logger, PartitionKey key, Container container)
 {
     logger.LogDebug("PartitionKey: {PartitionKey}, ContainerId: {ContainerId}",
                     key.ToString(),
                     container.Id);
 }