Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
406 changes: 403 additions & 3 deletions .gitignore

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions dotnet/scalardb-cluster-sample/Commands/GetCustomerInfoCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.CommandLine;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the location, let's put them into scalardb-dotnet-samples instead of scalardb-samples/dotnet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feeblefakie
Created public scalardb-dotnet-samples repository.
Will move this sample there after there are no more questions and requests about the sample. Or is it better to move it right now and set all of you as reviewers again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm sorry, I made a mistake.
I meant the scalardb-dotnet-samples folder in the scalardb-samples repo.
My bad.

Copy link
Contributor Author

@Dima-X Dima-X Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feeblefakie
Moved to scalardb-dotnet-samples


namespace ScalarDbClusterSample.Commands;

public static class GetCustomerInfoCommand
{
private const string Name = "GetCustomerInfo";
private const string Description = "Get customer information";

private const string ArgName = "id";
private const string ArgDescription = "customer ID";

public static Command Create()
{
var customerIdArg = new Argument<int>(ArgName, ArgDescription);
var getCustomerInfoCommand = new Command(Name, Description)
{
customerIdArg
};

getCustomerInfoCommand.SetHandler(async customerId =>
{
using var sample = new Sample();
var customerInfo = await sample.GetCustomerInfo(customerId);

Console.WriteLine(customerInfo);
}, customerIdArg);

return getCustomerInfoCommand;
}
}
31 changes: 31 additions & 0 deletions dotnet/scalardb-cluster-sample/Commands/GetOrderCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.CommandLine;

namespace ScalarDbClusterSample.Commands;

public static class GetOrderCommand
{
private const string Name = "GetOrder";
private const string Description = "Get order information by order ID";

private const string ArgName = "id";
private const string ArgDescription = "order ID";

public static Command Create()
{
var orderIdArg = new Argument<string>(ArgName, ArgDescription);
var getOrderCommand = new Command(Name, Description)
{
orderIdArg
};

getOrderCommand.SetHandler(async orderId =>
{
using var sample = new Sample();
var order = await sample.GetOrderByOrderId(orderId);

Console.WriteLine(order);
}, orderIdArg);

return getOrderCommand;
}
}
31 changes: 31 additions & 0 deletions dotnet/scalardb-cluster-sample/Commands/GetOrdersCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.CommandLine;

namespace ScalarDbClusterSample.Commands;

public static class GetOrdersCommand
{
private const string Name = "GetOrders";
private const string Description = "Get information about orders by customer ID";

private const string ArgName = "customer_id";
private const string ArgDescription = "customer ID";

public static Command Create()
{
var customerIdArg = new Argument<int>(ArgName, ArgDescription);
var getOrdersCommand = new Command(Name, Description)
{
customerIdArg
};

getOrdersCommand.SetHandler(async customerId =>
{
using var sample = new Sample();
var orders = await sample.GetOrdersByCustomerId(customerId);

Console.WriteLine(orders);
}, customerIdArg);

return getOrdersCommand;
}
}
39 changes: 39 additions & 0 deletions dotnet/scalardb-cluster-sample/Commands/LoadInitialDataCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.CommandLine;
using ScalarDB.Client.Exceptions;

namespace ScalarDbClusterSample.Commands;

public static class LoadInitialDataCommand
{
private const string Name = "LoadInitialData";
private const string Description = "Load initial data";

public static Command Create()
{
var loadInitialDataCommand = new Command(Name, Description);
loadInitialDataCommand.SetHandler(async () =>
{
using var sample = new Sample();
await sample.CreateTables();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing sample applications don't create tables within the applications, as table creation is handled by the Schema Loader tool. I think this sample application should follow the same approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think forcing .NET developers to install Java just to use the Schema Loader tool is not good; that’s why tables are created within the sample application.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a point. Maybe it's a trade off between the consistency with other sample applications and the convenience of this one. I don't have a strong opinion on it, so I'll leave the decision to @feeblefakie and @brfrn169.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dima-X Can you make it configurable?
So, users can create tables through this (default) or create tables through the schema-loader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feeblefakie Tables already can be created both ways. Tables and namespaces are created only if they don’t exist already, so there’s no problem applying the schema with the schema-loader and then using this sample app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, table schema migration is basically conducted outside applications in production environments. So I think the tables for the sample application should be explicitly created as much as possible. On the other hand, I understand that it's might be not easy for .NET developers to use the Schema Loader or equivalent. How about adding a new command CreateTablesCommand instead of implicitly creating the tables in LoadInitialDataCommand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@komamitsu

How about adding a new command CreateTablesCommand instead of implicitly creating the tables in LoadInitialDataCommand?

I thought about doing it as a separate command at first, but you always have to run LoadInitialDataCommand exactly after CreateTablesCommand, so I think it’s more logical for them to be a single command.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it’s more logical for them to be a single command.

This depends on whether Scalar wants to provide a sample application that always implicitly creates the tables. I'll wait for feedback from other members.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion with @feeblefakie and @brfrn169, we decided to use a Docker image that runs the Schema Loader tool after we make it public. So, let's go with the current PR as is.


var attempts = 10;
while (attempts-- > 0)
{
try
{
await sample.LoadInitialData();
}
catch (IllegalArgumentException)
{
// there's can be a lag until ScalarDB Cluster recognize namespaces and tables created
// in some databases like Cassandra, so if this command was called for the first time
// the first attempts can fail with 'The namespace does not exist' error

await Task.Delay(TimeSpan.FromSeconds(1));
}
}
});

return loadInitialDataCommand;
}
}
54 changes: 54 additions & 0 deletions dotnet/scalardb-cluster-sample/Commands/PlaceOrderCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.CommandLine;

namespace ScalarDbClusterSample.Commands;

public static class PlaceOrderCommand
{
private const string Name = "PlaceOrder";
private const string Description = "Place an order";

private const string ArgName1 = "customer_id";
private const string ArgDescription1 = "customer ID";
private const string ArgName2 = "orders";
private const string ArgDescription2 = "orders. The format is \"<Item ID>:<Count>,<Item ID>:<Count>,...\"";

public static Command Create()
{
var customerIdArg = new Argument<int>(ArgName1, ArgDescription1);
var ordersArg = new Argument<Dictionary<int, int>>(
name: ArgName2,
parse: arg =>
{
var argStr = arg.Tokens.First().Value;
var orders = argStr
.Split(',')
.Select(s => s.Split(':'))
.ToDictionary(
s => Int32.Parse(s[0]),
s => Int32.Parse(s[1])
);

return orders;
},
description: ArgDescription2)
{
Arity = ArgumentArity.ExactlyOne
};

var placeOrderCommand = new Command(Name, Description)
{
customerIdArg,
ordersArg
};

placeOrderCommand.SetHandler(async (customerId, orders) =>
{
using var sample = new Sample();
var order = await sample.PlaceOrder(customerId, orders);

Console.WriteLine(order);
}, customerIdArg, ordersArg);

return placeOrderCommand;
}
}
33 changes: 33 additions & 0 deletions dotnet/scalardb-cluster-sample/Commands/RepaymentCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.CommandLine;

namespace ScalarDbClusterSample.Commands;

public static class RepaymentCommand
{
private const string Name = "Repayment";
private const string Description = "Repayment";

private const string ArgName1 = "customer_id";
private const string ArgDescription1 = "customer ID";
private const string ArgName2 = "amount";
private const string ArgDescription2 = "amount of the money for repayment";

public static Command Create()
{
var customerIdArg = new Argument<int>(ArgName1, ArgDescription1);
var amountArg = new Argument<int>(ArgName2, ArgDescription2);
var repaymentCommand = new Command(Name, Description)
{
customerIdArg,
amountArg
};

repaymentCommand.SetHandler(async (customerId, amount) =>
{
using var sample = new Sample();
await sample.Repayment(customerId, amount);
}, customerIdArg, amountArg);

return repaymentCommand;
}
}
14 changes: 14 additions & 0 deletions dotnet/scalardb-cluster-sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.CommandLine;
using ScalarDbClusterSample.Commands;

var rootCommand = new RootCommand("Sample application for ScalarDB Cluster .NET Client SDK")
{
LoadInitialDataCommand.Create(),
GetCustomerInfoCommand.Create(),
GetOrderCommand.Create(),
GetOrdersCommand.Create(),
PlaceOrderCommand.Create(),
RepaymentCommand.Create()
};

await rootCommand.InvokeAsync(args);
Loading