Skip to content

Commit

Permalink
Replace deprecated Gradle/ScalarDB features (#74)
Browse files Browse the repository at this point in the history
* Replace deprecated Gradle features

* Replace deprecated put with update and insert

* Replace deprecated put with update and upsert in the Kotilin sample

* Change function names from put to insert

* Renamed the delete task
  • Loading branch information
KodaiD authored Nov 20, 2024
1 parent 2f63061 commit bdedc82
Show file tree
Hide file tree
Showing 23 changed files with 225 additions and 123 deletions.
13 changes: 9 additions & 4 deletions microservice-transaction-sample/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ dependencies {
}

application {
mainClassName = 'sample.client.Client'
mainClass = 'sample.client.Client'
}

archivesBaseName = "sample-order-service"
base {
archivesName = "sample-order-service"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
13 changes: 9 additions & 4 deletions microservice-transaction-sample/customer-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
}

application {
mainClassName = 'sample.customer.CustomerServiceServer'
mainClass = 'sample.customer.CustomerServiceServer'
}

task copyFilesToDockerBuildContextDir(type: Copy) {
Expand Down Expand Up @@ -47,7 +47,12 @@ installDist {
duplicatesStrategy DuplicatesStrategy.EXCLUDE
}

archivesBaseName = "sample-customer-service"
base {
archivesName = "sample-customer-service"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void loadCustomerIfNotExists(
throws CrudException {
Optional<Customer> customer = Customer.get(transaction, id);
if (!customer.isPresent()) {
Customer.put(transaction, id, name, creditLimit, creditTotal);
Customer.insert(transaction, id, name, creditLimit, creditTotal);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package sample.customer.model;

import com.scalar.db.api.Get;
import com.scalar.db.api.Put;
import com.scalar.db.api.Insert;
import com.scalar.db.api.TransactionCrudOperable;
import com.scalar.db.api.Update;
import com.scalar.db.exception.transaction.CrudException;
import com.scalar.db.io.Key;
import java.util.Optional;
Expand All @@ -27,11 +28,11 @@ public Customer(int id, String name, int creditLimit, int creditTotal) {
this.creditTotal = creditTotal;
}

public static void put(
public static void insert(
TransactionCrudOperable transaction, int id, String name, int creditLimit, int creditTotal)
throws CrudException {
transaction.put(
Put.newBuilder()
transaction.insert(
Insert.newBuilder()
.namespace(NAMESPACE)
.table(TABLE)
.partitionKey(Key.ofInt(COL_CUSTOMER_ID, id))
Expand All @@ -43,8 +44,8 @@ public static void put(

public static void updateCreditTotal(TransactionCrudOperable transaction, int id, int creditTotal)
throws CrudException {
transaction.put(
Put.newBuilder()
transaction.update(
Update.newBuilder()
.namespace(NAMESPACE)
.table(TABLE)
.partitionKey(Key.ofInt(COL_CUSTOMER_ID, id))
Expand Down
13 changes: 9 additions & 4 deletions microservice-transaction-sample/order-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
}

application {
mainClassName = 'sample.order.OrderServiceServer'
mainClass = 'sample.order.OrderServiceServer'
}

task copyFilesToDockerBuildContextDir(type: Copy) {
Expand Down Expand Up @@ -47,7 +47,12 @@ installDist {
duplicatesStrategy DuplicatesStrategy.EXCLUDE
}

archivesBaseName = "sample-order-service"
base {
archivesName = "sample-order-service"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void loadItemIfNotExists(
DistributedTransaction transaction, int id, String name, int price) throws CrudException {
Optional<Item> item = Item.get(transaction, id);
if (!item.isPresent()) {
Item.put(transaction, id, name, price);
Item.insert(transaction, id, name, price);
}
}

Expand All @@ -106,13 +106,13 @@ public void placeOrder(
transaction -> {
String orderId = UUID.randomUUID().toString();

// Put the order info into the orders table
Order.put(transaction, orderId, request.getCustomerId(), System.currentTimeMillis());
// Insert the order info into the orders table
Order.insert(transaction, orderId, request.getCustomerId(), System.currentTimeMillis());

int amount = 0;
for (ItemOrder itemOrder : request.getItemOrderList()) {
// Put the order statement into the statements table
Statement.put(transaction, orderId, itemOrder.getItemId(), itemOrder.getCount());
// Insert the order statement into the statements table
Statement.insert(transaction, orderId, itemOrder.getItemId(), itemOrder.getCount());

// Retrieve the item info from the items table
Optional<Item> item = Item.get(transaction, itemOrder.getItemId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sample.order.model;

import com.scalar.db.api.Get;
import com.scalar.db.api.Put;
import com.scalar.db.api.Insert;
import com.scalar.db.api.TransactionCrudOperable;
import com.scalar.db.exception.transaction.CrudException;
import com.scalar.db.io.Key;
Expand All @@ -25,10 +25,10 @@ public Item(int id, String name, int price) {
this.price = price;
}

public static void put(TransactionCrudOperable transaction, int id, String name, int price)
public static void insert(TransactionCrudOperable transaction, int id, String name, int price)
throws CrudException {
transaction.put(
Put.newBuilder()
transaction.insert(
Insert.newBuilder()
.namespace(NAMESPACE)
.table(TABLE)
.partitionKey(Key.ofInt(COL_ITEM_ID, id))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sample.order.model;

import com.scalar.db.api.Get;
import com.scalar.db.api.Put;
import com.scalar.db.api.Insert;
import com.scalar.db.api.Result;
import com.scalar.db.api.Scan;
import com.scalar.db.api.Scan.Ordering;
Expand Down Expand Up @@ -30,11 +30,11 @@ public Order(String id, int customerId, long timestamp) {
this.timestamp = timestamp;
}

public static void put(
public static void insert(
TransactionCrudOperable transaction, String id, int customerId, long timestamp)
throws CrudException {
transaction.put(
Put.newBuilder()
transaction.insert(
Insert.newBuilder()
.namespace(NAMESPACE)
.table(TABLE)
.partitionKey(Key.ofInt(COL_CUSTOMER_ID, customerId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sample.order.model;

import com.scalar.db.api.Put;
import com.scalar.db.api.Insert;
import com.scalar.db.api.Scan;
import com.scalar.db.api.TransactionCrudOperable;
import com.scalar.db.exception.transaction.CrudException;
Expand All @@ -26,10 +26,10 @@ public Statement(String orderId, int itemId, int count) {
this.count = count;
}

public static void put(TransactionCrudOperable transaction, String orderId, int itemId, int count)
public static void insert(TransactionCrudOperable transaction, String orderId, int itemId, int count)
throws CrudException {
transaction.put(
Put.newBuilder()
transaction.insert(
Insert.newBuilder()
.namespace(NAMESPACE)
.table(TABLE)
.partitionKey(Key.ofText(COL_ORDER_ID, orderId))
Expand Down
34 changes: 29 additions & 5 deletions microservice-transaction-sample/rpc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'java-library-distribution'
id 'com.google.protobuf' version '0.9.1'
id 'com.google.protobuf' version '0.9.4'
}

dependencies {
Expand All @@ -20,16 +20,40 @@ protobuf {
generateProtoTasks {
all()*.plugins { grpc {} }
}
generatedFilesBaseDir = "$projectDir/src"
}

archivesBaseName = "sample-rpc"
base {
archivesName = "sample-rpc"
}

// Task copyGeneratedProtoToSrc copies the generated .java files into src directory
task copyGeneratedProtoToSrc(type: Copy) {
description 'Copies generated Protocol Buffer classes to src/main/java/sample/rpc'
dependsOn generateProto
from "$buildDir/generated/source/proto/main/java/sample/rpc"
into "$projectDir/src/main/java/sample/rpc"
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

// Task deleteGeneratedProto deletes the generated .java files in build directory
task deleteGeneratedProto(type: Delete) {
dependsOn copyGeneratedProtoToSrc
delete fileTree(dir: "$buildDir/generated/source/proto/main/java/sample/rpc")
}

// The processResources task needs to depend on the generateProto task because it uses the output
// of the the generateProto task
processResources {
dependsOn generateProto
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
// Task deleteGeneratedProto needs to depend on deleteGeneratedProto to avoid duplicate class error
tasks.named("compileJava").configure {
dependsOn deleteGeneratedProto
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
13 changes: 9 additions & 4 deletions multi-storage-transaction-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ dependencies {
}

application {
mainClassName = 'sample.command.SampleCommand'
mainClass = 'sample.command.SampleCommand'
}

archivesBaseName = "sample"
base {
archivesName = "sample"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
31 changes: 16 additions & 15 deletions multi-storage-transaction-sample/src/main/java/sample/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.scalar.db.api.DistributedTransaction;
import com.scalar.db.api.DistributedTransactionManager;
import com.scalar.db.api.Get;
import com.scalar.db.api.Put;
import com.scalar.db.api.Insert;
import com.scalar.db.api.Result;
import com.scalar.db.api.Scan;
import com.scalar.db.api.Update;
import com.scalar.db.exception.transaction.TransactionException;
import com.scalar.db.io.Key;
import com.scalar.db.service.TransactionFactory;
Expand Down Expand Up @@ -62,8 +63,8 @@ private void loadCustomerIfNotExists(
.partitionKey(Key.ofInt("customer_id", customerId))
.build());
if (!customer.isPresent()) {
transaction.put(
Put.newBuilder()
transaction.insert(
Insert.newBuilder()
.namespace("customer")
.table("customers")
.partitionKey(Key.ofInt("customer_id", customerId))
Expand All @@ -85,8 +86,8 @@ private void loadItemIfNotExists(
.partitionKey(Key.ofInt("item_id", itemId))
.build());
if (!item.isPresent()) {
transaction.put(
Put.newBuilder()
transaction.insert(
Insert.newBuilder()
.namespace("order")
.table("items")
.partitionKey(Key.ofInt("item_id", itemId))
Expand Down Expand Up @@ -146,9 +147,9 @@ public String placeOrder(int customerId, int[] itemIds, int[] itemCounts)
// Start a transaction
transaction = manager.start();

// Put the order info into the orders table
transaction.put(
Put.newBuilder()
// Insert the order info into the orders table
transaction.insert(
Insert.newBuilder()
.namespace("order")
.table("orders")
.partitionKey(Key.ofInt("customer_id", customerId))
Expand All @@ -161,9 +162,9 @@ public String placeOrder(int customerId, int[] itemIds, int[] itemCounts)
int itemId = itemIds[i];
int count = itemCounts[i];

// Put the order statement into the statements table
transaction.put(
Put.newBuilder()
// Insert the order statement into the statements table
transaction.insert(
Insert.newBuilder()
.namespace("order")
.table("statements")
.partitionKey(Key.ofText("order_id", orderId))
Expand Down Expand Up @@ -206,8 +207,8 @@ public String placeOrder(int customerId, int[] itemIds, int[] itemCounts)
}

// Update credit_total for the customer
transaction.put(
Put.newBuilder()
transaction.update(
Update.newBuilder()
.namespace("customer")
.table("customers")
.partitionKey(Key.ofInt("customer_id", customerId))
Expand Down Expand Up @@ -389,8 +390,8 @@ public void repayment(int customerId, int amount) throws TransactionException {
}

// Reduce credit_total for the customer
transaction.put(
Put.newBuilder()
transaction.update(
Update.newBuilder()
.namespace("customer")
.table("customers")
.partitionKey(Key.ofInt("customer_id", customerId))
Expand Down
11 changes: 3 additions & 8 deletions scalardb-kotlin-sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.8.21"
kotlin("jvm") version "2.0.21"
application
}

Expand All @@ -21,13 +21,8 @@ tasks.test {
useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
kotlin {
jvmToolchain(8)
}

application {
Expand Down
Loading

0 comments on commit bdedc82

Please sign in to comment.