Skip to content
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
406 changes: 403 additions & 3 deletions .gitignore

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.CommandLine;

namespace ScalarDbClusterLinqSample.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(customerId =>
{
using var sample = new Sample();
var customerInfo = sample.GetCustomerInfo(customerId);

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

return getCustomerInfoCommand;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.CommandLine;

namespace ScalarDbClusterLinqSample.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.CommandLine;

namespace ScalarDbClusterLinqSample.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.CommandLine;
using System.Diagnostics;
using ScalarDB.Client.Exceptions;

namespace ScalarDbClusterLinqSample.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();

IllegalArgumentException? lastException = null;
var attempts = 10;
while (attempts-- > 0)
{
try
{
await sample.LoadInitialData();
return;
}
catch (IllegalArgumentException ex)
{
// 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
lastException = ex;
await Task.Delay(TimeSpan.FromSeconds(1));
}
}

Debug.Assert(lastException != null);
throw lastException;
});

return loadInitialDataCommand;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.CommandLine;

namespace ScalarDbClusterLinqSample.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.CommandLine;

namespace ScalarDbClusterLinqSample.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations.Schema;
using ScalarDB.Client.DataAnnotations;

namespace ScalarDbClusterLinqSample.Models;

[Table("sample.customers")]
public class Customer
{
[PartitionKey]
[Column("customer_id", Order = 0)]
public int Id { get; set; }

[Column("name", Order = 1)]
public string Name { get; set; } = "";

[Column("credit_limit", Order = 2)]
public int CreditLimit { get; set; }

[Column("credit_total", Order = 3)]
public int CreditTotal { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations.Schema;
using ScalarDB.Client.DataAnnotations;

namespace ScalarDbClusterLinqSample.Models;

[Table("sample.items")]
public class Item
{
[PartitionKey]
[Column("item_id", Order = 0)]
public int Id { get; set; }

[Column("name", Order = 1)]
public string Name { get; set; } = "";

[Column("price", Order = 2)]
public int Price { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations.Schema;
using ScalarDB.Client.DataAnnotations;

namespace ScalarDbClusterLinqSample.Models;

[Table("sample.orders")]
public class Order
{
[SecondaryIndex]
[Column("order_id", Order = 0)]
public string Id { get; set; } = "";

[PartitionKey]
[Column("customer_id", Order = 1)]
public int CustomerId { get; set; }

[ClusteringKey]
[Column("timestamp", Order = 2)]
public long Timestamp { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ScalarDB.Client;

namespace ScalarDbClusterLinqSample.Models;

public class SampleDbContext : ScalarDbContext
{
#nullable disable
public ScalarDbSet<Customer> Customers { get; set; }
public ScalarDbSet<Order> Orders { get; set; }
public ScalarDbSet<Statement> Statements { get; set; }
public ScalarDbSet<Item> Items { get; set; }
#nullable restore
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations.Schema;
using ScalarDB.Client.DataAnnotations;

namespace ScalarDbClusterLinqSample.Models;

[Table("sample.statements")]
public class Statement
{
[PartitionKey]
[Column("order_id", Order = 0)]
public string OrderId { get; set; } = "";

[ClusteringKey]
[Column("item_id", Order = 1)]
public int ItemId { get; set; }

[Column("count", Order = 2)]
public int Count { get; set; }
}
14 changes: 14 additions & 0 deletions scalardb-dotnet-samples/scalardb-cluster-linq-sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.CommandLine;
using ScalarDbClusterLinqSample.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