Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NCBC-3678: Docs samples for SDK 3.5 features #321

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Couchbase;
using Couchbase.KeyValue;
using Couchbase.KeyValue.RangeScan;
using Serilog;
using Serilog.Extensions.Logging;

Serilog.Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Information()
.WriteTo.Console()
.CreateLogger();

var clusterOptions = new ClusterOptions()
{
ConnectionString = "couchbase://localhost",
UserName = "Administrator",
Password = "password",
EnableDnsSrvResolution = false,
};

clusterOptions = clusterOptions.WithLogging(new SerilogLoggerFactory());

var exampleCluster = await Cluster.ConnectAsync(clusterOptions);
await exampleCluster.WaitUntilReadyAsync(TimeSpan.FromSeconds(10));
var travelSample = await exampleCluster.BucketAsync("travel-sample");
var inventoryScope = travelSample.Scope("inventory");
await Task.Delay(1000);

await RangeScanAllDocuments(inventoryScope);
await RangeScanPrefixScan(inventoryScope);

async Task RangeScanAllDocuments(IScope scope)
{
var collection = scope.Collection("hotel");
// tag::rangeScanAllDocuments[]
IAsyncEnumerable<IScanResult> results = collection.ScanAsync(new RangeScan());

await foreach (var scanResult in results)
{
Log.Information(scanResult.Id);
Log.Information(scanResult.ContentAs<Hotel>().ToString());
}

// alternate declaration
var scan2 = new RangeScan(from: ScanTerm.Inclusive("id001"), to: ScanTerm.Inclusive("id999"));
// end::rangeScanAllDocuments[]
}

async Task RangeScanPrefixScan(IScope scope)
{
var collection = scope.Collection("hotel");
// tag::rangeScanPrefix[]
IAsyncEnumerable<IScanResult> results = collection.ScanAsync(
new PrefixScan("alice::")
);

await foreach (var scanResult in results)
{
Log.Information(scanResult.Id);
}
// end::rangeScanPrefix[]
}

async Task RangeScanSamplingScan(IScope scope)
{
var collection = scope.Collection("hotel");
// tag::rangeScanSample[]
IAsyncEnumerable<IScanResult> results = collection.ScanAsync(
new SamplingScan(limit: 100)
);

await foreach (var scanResult in results)
{
Log.Information(scanResult.Id);
}
// end::rangeScanSample[]
}
async Task RangeScanAllDocumentIds(IScope scope)
{
var collection = scope.Collection("hotel");
// tag::rangeScanAllDocumentIds[]
IAsyncEnumerable<IScanResult> results = collection.ScanAsync(
new RangeScan(),
new ScanOptions().IdsOnly(true));

await foreach (var scanResult in results)
{
Log.Information(scanResult.Id);
}
// end::rangeScanAllDocumentIds[]
}

record Hotel(string name, string title, string address);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\..\couchbase-net-client\src\Couchbase\Couchbase.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Couchbase;
using Couchbase.Core.Exceptions.KeyValue;
using Couchbase.KeyValue;

var clusterOptions = new ClusterOptions()
{
ConnectionString = "couchbase://localhost",
UserName = "Administrator",
Password = "password",
EnableDnsSrvResolution = false,
};

var exampleCluster = await Cluster.ConnectAsync(clusterOptions);
await exampleCluster.WaitUntilReadyAsync(TimeSpan.FromSeconds(10));
var travelSample = await exampleCluster.BucketAsync("travel-sample");
var inventoryScope = travelSample.Scope("inventory");
await Task.Delay(1000);

await LookupInAnyReplica(travelSample.DefaultCollection());
await LookupInAllReplicas(travelSample.DefaultCollection());

async Task LookupInAnyReplica(ICouchbaseCollection collection)
{
// tag::lookup-in-any-replica[]
try
{
var result = await collection.LookupInAnyReplicaAsync(
"hotel_10138",
specs => specs.Get("geo.lat")
);

var geoLat = result.ContentAs<string>(0);
Console.Out.WriteLine($"getFunc: Latitude={geoLat}");
}
catch (PathNotFoundException)
{
Console.Error.WriteLine("The version of the document" +
" on the server node that responded quickest" +
" did not have the requested field.");
}
catch (DocumentUnretrievableException)
{
Console.Error.WriteLine("Document was not present" +
" on any server node");
}
// end::lookup-in-any-replica[]
}

async Task LookupInAllReplicas(ICouchbaseCollection collection)
{
// tag::lookup-in-all-replicas[]
IAsyncEnumerable<ILookupInResult> result = collection.LookupInAllReplicasAsync(
"hotel_10138",
specs => specs.Get("geo.lat"));

await foreach (var replicaResult in result)
{
try
{
var geoLat = replicaResult.ContentAs<string>(0);
Console.Out.WriteLine($"getFunc: Latitude={geoLat}");
}
catch (PathNotFoundException)
{
Console.Error.WriteLine("The version of the document" +
" on the server node that responded quickest" +
" did not have the requested field.");
}
}
// end::lookup-in-all-replicas[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\couchbase-net-client\src\Couchbase\Couchbase.csproj" />
</ItemGroup>

</Project>
Loading
Loading