Skip to content

Commit

Permalink
Add proper support for inMemory storage
Browse files Browse the repository at this point in the history
  • Loading branch information
zrdzn committed Nov 5, 2023
1 parent 9bc24de commit d1f798f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.github.zrdzn.minecraft.lovelydrop.storage;

import org.jetbrains.annotations.Nullable;

import javax.sql.DataSource;

/**
* Class that represents storage by holding data source instance with associated storage type.
* Some storage types may not have data source instance, such as in-memory storage, therefore data source instance is nullable.
*/
public class Storage {

Expand All @@ -15,6 +18,7 @@ public Storage(DataSource dataSource, StorageType type) {
this.type = type;
}

@Nullable
public DataSource getDataSource() {
return this.dataSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public Storage createStorage() {

break;
case IN_MEMORY:
logger.info("Choosing in-memory as a storage provider.");

dataSource = null;
storageType = StorageType.IN_MEMORY;

break;
default:
throw new IllegalArgumentException("There is no such storage type.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import javax.sql.DataSource;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand All @@ -31,8 +25,8 @@ class SqlUserSettingRepository implements UserSettingRepository {

public SqlUserSettingRepository(DataSource dataSource, Gson gson, String createOrUpdateUserSettingQuery,
String findUserSettingByPlayerId) {
this.dataSource = dataSource;
this.gson = gson;
this.dataSource = Objects.requireNonNull(dataSource, "Data source cannot be null.");
this.gson = Objects.requireNonNull(gson, "Gson cannot be null.");

this.createOrUpdateUserSettingQuery = createOrUpdateUserSettingQuery;
this.findUserSettingByPlayerId = findUserSettingByPlayerId;
Expand Down

0 comments on commit d1f798f

Please sign in to comment.