Skip to content

Commit 9cb62d4

Browse files
committed
Add sample application for ScalarDB Cluster .NET Client SDK
1 parent c0d05d3 commit 9cb62d4

13 files changed

+1161
-3
lines changed

.gitignore

Lines changed: 403 additions & 3 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.CommandLine;
2+
3+
namespace ScalarDbClusterSample.Commands;
4+
5+
public static class GetCustomerInfoCommand
6+
{
7+
private const string Name = "GetCustomerInfo";
8+
private const string Description = "Get customer information";
9+
10+
private const string ArgName = "id";
11+
private const string ArgDescription = "customer ID";
12+
13+
public static Command Create()
14+
{
15+
var customerIdArg = new Argument<int>(ArgName, ArgDescription);
16+
var getCustomerInfoCommand = new Command(Name, Description)
17+
{
18+
customerIdArg
19+
};
20+
21+
getCustomerInfoCommand.SetHandler(async customerId =>
22+
{
23+
using var sample = new Sample();
24+
var customerInfo = await sample.GetCustomerInfo(customerId);
25+
26+
Console.WriteLine(customerInfo);
27+
}, customerIdArg);
28+
29+
return getCustomerInfoCommand;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.CommandLine;
2+
3+
namespace ScalarDbClusterSample.Commands;
4+
5+
public static class GetOrderCommand
6+
{
7+
private const string Name = "GetOrder";
8+
private const string Description = "Get order information by order ID";
9+
10+
private const string ArgName = "id";
11+
private const string ArgDescription = "order ID";
12+
13+
public static Command Create()
14+
{
15+
var orderIdArg = new Argument<string>(ArgName, ArgDescription);
16+
var getOrderCommand = new Command(Name, Description)
17+
{
18+
orderIdArg
19+
};
20+
21+
getOrderCommand.SetHandler(async orderId =>
22+
{
23+
using var sample = new Sample();
24+
var order = await sample.GetOrderByOrderId(orderId);
25+
26+
Console.WriteLine(order);
27+
}, orderIdArg);
28+
29+
return getOrderCommand;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.CommandLine;
2+
3+
namespace ScalarDbClusterSample.Commands;
4+
5+
public static class GetOrdersCommand
6+
{
7+
private const string Name = "GetOrders";
8+
private const string Description = "Get information about orders by customer ID";
9+
10+
private const string ArgName = "customer_id";
11+
private const string ArgDescription = "customer ID";
12+
13+
public static Command Create()
14+
{
15+
var customerIdArg = new Argument<int>(ArgName, ArgDescription);
16+
var getOrdersCommand = new Command(Name, Description)
17+
{
18+
customerIdArg
19+
};
20+
21+
getOrdersCommand.SetHandler(async customerId =>
22+
{
23+
using var sample = new Sample();
24+
var orders = await sample.GetOrdersByCustomerId(customerId);
25+
26+
Console.WriteLine(orders);
27+
}, customerIdArg);
28+
29+
return getOrdersCommand;
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.CommandLine;
2+
using ScalarDB.Client.Exceptions;
3+
4+
namespace ScalarDbClusterSample.Commands;
5+
6+
public static class LoadInitialDataCommand
7+
{
8+
private const string Name = "LoadInitialData";
9+
private const string Description = "Load initial data";
10+
11+
public static Command Create()
12+
{
13+
var loadInitialDataCommand = new Command(Name, Description);
14+
loadInitialDataCommand.SetHandler(async () =>
15+
{
16+
using var sample = new Sample();
17+
await sample.CreateTables();
18+
19+
var attempts = 10;
20+
while (attempts-- > 0)
21+
{
22+
try
23+
{
24+
await sample.LoadInitialData();
25+
}
26+
catch (IllegalArgumentException)
27+
{
28+
// there's can be a lag until ScalarDB Cluster recognize namespaces and tables created
29+
// in some databases like Cassandra, so if this command was called for the first time
30+
// the first attempts can fail with 'The namespace does not exist' error
31+
32+
await Task.Delay(TimeSpan.FromSeconds(1));
33+
}
34+
}
35+
});
36+
37+
return loadInitialDataCommand;
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.CommandLine;
2+
3+
namespace ScalarDbClusterSample.Commands;
4+
5+
public static class PlaceOrderCommand
6+
{
7+
private const string Name = "PlaceOrder";
8+
private const string Description = "Place an order";
9+
10+
private const string ArgName1 = "customer_id";
11+
private const string ArgDescription1 = "customer ID";
12+
private const string ArgName2 = "orders";
13+
private const string ArgDescription2 = "orders. The format is \"<Item ID>:<Count>,<Item ID>:<Count>,...\"";
14+
15+
public static Command Create()
16+
{
17+
var customerIdArg = new Argument<int>(ArgName1, ArgDescription1);
18+
var ordersArg = new Argument<Dictionary<int, int>>(
19+
name: ArgName2,
20+
parse: arg =>
21+
{
22+
var argStr = arg.Tokens.First().Value;
23+
var orders = argStr
24+
.Split(',')
25+
.Select(s => s.Split(':'))
26+
.ToDictionary(
27+
s => Int32.Parse(s[0]),
28+
s => Int32.Parse(s[1])
29+
);
30+
31+
return orders;
32+
},
33+
description: ArgDescription2)
34+
{
35+
Arity = ArgumentArity.ExactlyOne
36+
};
37+
38+
var placeOrderCommand = new Command(Name, Description)
39+
{
40+
customerIdArg,
41+
ordersArg
42+
};
43+
44+
placeOrderCommand.SetHandler(async (customerId, orders) =>
45+
{
46+
using var sample = new Sample();
47+
var order = await sample.PlaceOrder(customerId, orders);
48+
49+
Console.WriteLine(order);
50+
}, customerIdArg, ordersArg);
51+
52+
return placeOrderCommand;
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.CommandLine;
2+
3+
namespace ScalarDbClusterSample.Commands;
4+
5+
public static class RepaymentCommand
6+
{
7+
private const string Name = "Repayment";
8+
private const string Description = "Repayment";
9+
10+
private const string ArgName1 = "customer_id";
11+
private const string ArgDescription1 = "customer ID";
12+
private const string ArgName2 = "amount";
13+
private const string ArgDescription2 = "amount of the money for repayment";
14+
15+
public static Command Create()
16+
{
17+
var customerIdArg = new Argument<int>(ArgName1, ArgDescription1);
18+
var amountArg = new Argument<int>(ArgName2, ArgDescription2);
19+
var repaymentCommand = new Command(Name, Description)
20+
{
21+
customerIdArg,
22+
amountArg
23+
};
24+
25+
repaymentCommand.SetHandler(async (customerId, amount) =>
26+
{
27+
using var sample = new Sample();
28+
await sample.Repayment(customerId, amount);
29+
}, customerIdArg, amountArg);
30+
31+
return repaymentCommand;
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.CommandLine;
2+
using ScalarDbClusterSample.Commands;
3+
4+
var rootCommand = new RootCommand("Sample application for ScalarDB Cluster .NET Client SDK")
5+
{
6+
LoadInitialDataCommand.Create(),
7+
GetCustomerInfoCommand.Create(),
8+
GetOrderCommand.Create(),
9+
GetOrdersCommand.Create(),
10+
PlaceOrderCommand.Create(),
11+
RepaymentCommand.Create()
12+
};
13+
14+
await rootCommand.InvokeAsync(args);

0 commit comments

Comments
 (0)