예제 #1
0
        public async Task GivenDirectoryEntry_WhenRoundTripWithETag_Success()
        {
            DirectoryClient client = TestApplication.GetDirectoryClient();

            var documentId = new DocumentId("test/unit-tests/entry1");

            var query = new QueryParameter()
            {
                Filter    = "test",
                Recursive = false,
            };

            await client.Delete(documentId);

            var entry = new DirectoryEntryBuilder()
                        .SetDirectoryId(documentId)
                        .SetClassType("test")
                        .AddProperty(new EntryProperty {
                Name = "property1", Value = "value1"
            })
                        .Build();

            await client.Set(entry);

            DirectoryEntry?readEntry = await client.Get(documentId);

            readEntry.Should().NotBeNull();
            readEntry !.ETag.Should().NotBeNull();
            readEntry.Properties.Count.Should().Be(1);

            var updateEntry = new DirectoryEntryBuilder(readEntry)
                              .SetClassType("test-next")
                              .AddProperty(new EntryProperty {
                Name = "property2", Value = "value2"
            })
                              .Build();

            await client.Set(updateEntry);

            readEntry = await client.Get(documentId);

            readEntry.Should().NotBeNull();
            readEntry !.Properties.Count.Should().Be(2);

            await client.Delete(documentId);

            IReadOnlyList <DatalakePathItem> search = (await client.Search(query).ReadNext()).Records;

            search.Any(x => x.Name == (string)documentId).Should().BeFalse();
        }
예제 #2
0
    public async Task WriteFile(string file, string path, bool recursive, CancellationToken token)
    {
        file.VerifyNotEmpty(nameof(file));

        using IDisposable scope = _logger.BeginScope(new { Command = nameof(WriteFile), File = file, Path = path, Recursive = recursive });

        _logger.LogInformation($"Reading directory at {path}, recursive={recursive}...");

        var query = new QueryParameter()
        {
            Filter    = path,
            Recursive = recursive,
        };

        BatchSetCursor <DatalakePathItem> batch = _directoryClient.Search(query);

        var list = new List <DirectoryEntry>();

        while (true)
        {
            BatchSet <DatalakePathItem> batchSet = await batch.ReadNext(token);

            if (batchSet.IsEndSignaled)
            {
                break;
            }

            foreach (var entry in batchSet.Records)
            {
                var documentId = new DocumentId(entry.Name);

                DirectoryEntry?directoryEntry = await _directoryClient.Get(documentId, token);

                if (directoryEntry == null)
                {
                    _logger.LogWarning($"Directory entry for {entry.Name} was not found");
                    continue;
                }

                list.Add(directoryEntry);
            }
        }

        string json = Json.Default.SerializeFormat(list);

        File.WriteAllText(path, json);

        _logger.LogInformation($"{list.Count} directory entries written to {file}");
    }
예제 #3
0
    internal async Task DeleteProperty(string documentId, string[] properties, CancellationToken token)
    {
        documentId.VerifyNotEmpty(nameof(documentId));
        properties.VerifyNotNull(nameof(properties));

        using IDisposable scope = _logger.BeginScope(new { Command = nameof(DeleteEntry), DirectoryId = documentId });

        var            id    = new DocumentId(documentId);
        DirectoryEntry entry = (await _directoryClient.Get(id))
                               .VerifyNotNull($"{documentId} does not exist");

        foreach (var key in properties)
        {
            entry.Properties.Remove(key);
        }

        await _directoryClient.Set(entry, token);

        _logger.LogInformation($"Removed properties {properties.Join(", ")} from {documentId}");
    }
예제 #4
0
    public async Task SetProperty(string directoryId, string[] properties, CancellationToken token)
    {
        var id = new DocumentId(directoryId);

        _logger.LogInformation($"Updating property {properties.Join(", ")} on {directoryId}");

        using IDisposable scope = _logger.BeginScope(new { Command = nameof(SetProperty), DirectoryId = directoryId, Properties = properties });

        DirectoryEntry entry = (await _directoryClient.Get(id)) ?? new DirectoryEntry {
            DirectoryId = (string)id
        };

        foreach (var item in properties)
        {
            (string key, string value) = GetProperty(item);
            entry.Properties[key]      = new EntryProperty
            {
                Name  = key,
                Value = value,
            };
        }

        await _directoryClient.Set(entry, token);

        _logger.LogInformation($"Updated property {properties.Join(", ")} on {directoryId}");

        (string key, string value) GetProperty(string value)
        {
            string[] values = value.Split('=')
                              .Select(x => x.Trim())
                              .ToArray()
                              .VerifyAssert(x => x.Length == 2, $"{value} syntax error");

            return(values[0], values[1]);
        }
    }