From 49039843e5921bc61ce3e00bdb75465c91df13b8 Mon Sep 17 00:00:00 2001
From: Victor Zalevskii
Date: Fri, 27 Aug 2021 15:29:58 +0300
Subject: [PATCH] Fix issues in VALIDATE phase
---
.../main/java/com/iluwatar/caching/App.java | 274 +++++++++---------
.../java/com/iluwatar/caching/AppManager.java | 4 +-
.../java/com/iluwatar/caching/CacheStore.java | 2 +-
.../java/com/iluwatar/caching/LruCache.java | 50 ++--
.../iluwatar/caching/database/DbManager.java | 59 ++--
.../caching/database/DbManagerFactory.java | 32 +-
.../iluwatar/caching/database/MongoDb.java | 192 ++++++------
.../iluwatar/caching/database/VirtualDb.java | 104 +++----
.../com/iluwatar/caching/package-info.java | 3 -
9 files changed, 368 insertions(+), 352 deletions(-)
diff --git a/caching/src/main/java/com/iluwatar/caching/App.java b/caching/src/main/java/com/iluwatar/caching/App.java
index 631d23a2a..2c7cbebf7 100644
--- a/caching/src/main/java/com/iluwatar/caching/App.java
+++ b/caching/src/main/java/com/iluwatar/caching/App.java
@@ -54,155 +54,155 @@ import lombok.extern.slf4j.Slf4j;
*/
@Slf4j
public class App {
- /**
- * Constant parameter name to use mongoDB.
- */
- private static final String USE_MONGO_DB = "--mongo";
- /**
- * Application manager.
- */
- private final AppManager appManager;
+ /**
+ * Constant parameter name to use mongoDB.
+ */
+ private static final String USE_MONGO_DB = "--mongo";
+ /**
+ * Application manager.
+ */
+ private final AppManager appManager;
- /**
- * Constructor of current App.
- * @param isMongo boolean
- */
- public App(final boolean isMongo) {
- DbManager dbManager = DbManagerFactory.initDb(isMongo);
- appManager = new AppManager(dbManager);
- appManager.initDb();
+ /**
+ * Constructor of current App.
+ * @param isMongo boolean
+ */
+ public App(final boolean isMongo) {
+ DbManager dbManager = DbManagerFactory.initDb(isMongo);
+ appManager = new AppManager(dbManager);
+ appManager.initDb();
+ }
+
+ /**
+ * Program entry point.
+ *
+ * @param args command line args
+ */
+ public static void main(final String[] args) {
+ // VirtualDB (instead of MongoDB) was used in running the JUnit tests
+ // and the App class to avoid Maven compilation errors. Set flag to
+ // true to run the tests with MongoDB (provided that MongoDB is
+ // installed and socket connection is open).
+ App app = new App(isDbMongo(args));
+ app.useReadAndWriteThroughStrategy();
+ String splitLine = "==============================================";
+ LOGGER.info(splitLine);
+ app.useReadThroughAndWriteAroundStrategy();
+ LOGGER.info(splitLine);
+ app.useReadThroughAndWriteBehindStrategy();
+ LOGGER.info(splitLine);
+ app.useCacheAsideStategy();
+ LOGGER.info(splitLine);
+ }
+
+ /**
+ * Check the input parameters. if
+ * @param args input params
+ * @return true if there is "--mongo" parameter in arguments
+ */
+ private static boolean isDbMongo(final String[] args) {
+ for (String arg : args) {
+ if (arg.equals(USE_MONGO_DB)) {
+ return true;
+ }
}
+ return false;
+ }
- /**
- * Program entry point.
- *
- * @param args command line args
- */
- public static void main(final String[] args) {
- // VirtualDB (instead of MongoDB) was used in running the JUnit tests
- // and the App class to avoid Maven compilation errors. Set flag to
- // true to run the tests with MongoDB (provided that MongoDB is
- // installed and socket connection is open).
- App app = new App(isDbMongo(args));
- app.useReadAndWriteThroughStrategy();
- String splitLine = "==============================================";
- LOGGER.info(splitLine);
- app.useReadThroughAndWriteAroundStrategy();
- LOGGER.info(splitLine);
- app.useReadThroughAndWriteBehindStrategy();
- LOGGER.info(splitLine);
- app.useCacheAsideStategy();
- LOGGER.info(splitLine);
- }
+ /**
+ * Read-through and write-through.
+ */
+ public void useReadAndWriteThroughStrategy() {
+ LOGGER.info("# CachingPolicy.THROUGH");
+ appManager.initCachingPolicy(CachingPolicy.THROUGH);
- /**
- * Check the input parameters. if
- * @param args input params
- * @return true if there is "--mongo" parameter in arguments
- */
- private static boolean isDbMongo(final String[] args) {
- for (String arg : args) {
- if (arg.equals(USE_MONGO_DB)) {
- return true;
- }
- }
- return false;
- }
+ var userAccount1 = new UserAccount("001", "John", "He is a boy.");
- /**
- * Read-through and write-through.
- */
- public void useReadAndWriteThroughStrategy() {
- LOGGER.info("# CachingPolicy.THROUGH");
- appManager.initCachingPolicy(CachingPolicy.THROUGH);
+ appManager.save(userAccount1);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("001");
+ appManager.find("001");
+ }
- var userAccount1 = new UserAccount("001", "John", "He is a boy.");
+ /**
+ * Read-through and write-around.
+ */
+ public void useReadThroughAndWriteAroundStrategy() {
+ LOGGER.info("# CachingPolicy.AROUND");
+ appManager.initCachingPolicy(CachingPolicy.AROUND);
- appManager.save(userAccount1);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("001");
- appManager.find("001");
- }
+ var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
- /**
- * Read-through and write-around.
- */
- public void useReadThroughAndWriteAroundStrategy() {
- LOGGER.info("# CachingPolicy.AROUND");
- appManager.initCachingPolicy(CachingPolicy.AROUND);
+ appManager.save(userAccount2);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("002");
+ LOGGER.info(appManager.printCacheContent());
+ userAccount2 = appManager.find("002");
+ userAccount2.setUserName("Jane G.");
+ appManager.save(userAccount2);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("002");
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("002");
+ }
- var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
+ /**
+ * Read-through and write-behind.
+ */
+ public void useReadThroughAndWriteBehindStrategy() {
+ LOGGER.info("# CachingPolicy.BEHIND");
+ appManager.initCachingPolicy(CachingPolicy.BEHIND);
- appManager.save(userAccount2);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("002");
- LOGGER.info(appManager.printCacheContent());
- userAccount2 = appManager.find("002");
- userAccount2.setUserName("Jane G.");
- appManager.save(userAccount2);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("002");
- LOGGER.info(appManager.printCacheContent());
- appManager.find("002");
- }
+ var userAccount3 = new UserAccount("003",
+ "Adam",
+ "He likes food.");
+ var userAccount4 = new UserAccount("004",
+ "Rita",
+ "She hates cats.");
+ var userAccount5 = new UserAccount("005",
+ "Isaac",
+ "He is allergic to mustard.");
- /**
- * Read-through and write-behind.
- */
- public void useReadThroughAndWriteBehindStrategy() {
- LOGGER.info("# CachingPolicy.BEHIND");
- appManager.initCachingPolicy(CachingPolicy.BEHIND);
+ appManager.save(userAccount3);
+ appManager.save(userAccount4);
+ appManager.save(userAccount5);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("003");
+ LOGGER.info(appManager.printCacheContent());
+ UserAccount userAccount6 = new UserAccount("006",
+ "Yasha",
+ "She is an only child.");
+ appManager.save(userAccount6);
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("004");
+ LOGGER.info(appManager.printCacheContent());
+ }
- var userAccount3 = new UserAccount("003",
- "Adam",
- "He likes food.");
- var userAccount4 = new UserAccount("004",
- "Rita",
- "She hates cats.");
- var userAccount5 = new UserAccount("005",
- "Isaac",
- "He is allergic to mustard.");
+ /**
+ * Cache-Aside.
+ */
+ public void useCacheAsideStategy() {
+ LOGGER.info("# CachingPolicy.ASIDE");
+ appManager.initCachingPolicy(CachingPolicy.ASIDE);
+ LOGGER.info(appManager.printCacheContent());
- appManager.save(userAccount3);
- appManager.save(userAccount4);
- appManager.save(userAccount5);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("003");
- LOGGER.info(appManager.printCacheContent());
- UserAccount userAccount6 = new UserAccount("006",
- "Yasha",
- "She is an only child.");
- appManager.save(userAccount6);
- LOGGER.info(appManager.printCacheContent());
- appManager.find("004");
- LOGGER.info(appManager.printCacheContent());
- }
+ var userAccount3 = new UserAccount("003",
+ "Adam",
+ "He likes food.");
+ var userAccount4 = new UserAccount("004",
+ "Rita",
+ "She hates cats.");
+ var userAccount5 = new UserAccount("005",
+ "Isaac",
+ "He is allergic to mustard.");
+ appManager.save(userAccount3);
+ appManager.save(userAccount4);
+ appManager.save(userAccount5);
- /**
- * Cache-Aside.
- */
- public void useCacheAsideStategy() {
- LOGGER.info("# CachingPolicy.ASIDE");
- appManager.initCachingPolicy(CachingPolicy.ASIDE);
- LOGGER.info(appManager.printCacheContent());
-
- var userAccount3 = new UserAccount("003",
- "Adam",
- "He likes food.");
- var userAccount4 = new UserAccount("004",
- "Rita",
- "She hates cats.");
- var userAccount5 = new UserAccount("005",
- "Isaac",
- "He is allergic to mustard.");
- appManager.save(userAccount3);
- appManager.save(userAccount4);
- appManager.save(userAccount5);
-
- LOGGER.info(appManager.printCacheContent());
- appManager.find("003");
- LOGGER.info(appManager.printCacheContent());
- appManager.find("004");
- LOGGER.info(appManager.printCacheContent());
- }
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("003");
+ LOGGER.info(appManager.printCacheContent());
+ appManager.find("004");
+ LOGGER.info(appManager.printCacheContent());
+ }
}
diff --git a/caching/src/main/java/com/iluwatar/caching/AppManager.java b/caching/src/main/java/com/iluwatar/caching/AppManager.java
index ac3100523..1bafa8ff9 100644
--- a/caching/src/main/java/com/iluwatar/caching/AppManager.java
+++ b/caching/src/main/java/com/iluwatar/caching/AppManager.java
@@ -23,9 +23,9 @@
package com.iluwatar.caching;
+import com.iluwatar.caching.database.DbManager;
import java.util.Optional;
-import com.iluwatar.caching.database.DbManager;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -68,7 +68,7 @@ public class AppManager {
* to (temporarily) store the data/objects during runtime.
*/
public void initDb() {
- dbManager.connect();
+ dbManager.connect();
}
/**
diff --git a/caching/src/main/java/com/iluwatar/caching/CacheStore.java b/caching/src/main/java/com/iluwatar/caching/CacheStore.java
index ca67fd334..d9d2e900b 100644
--- a/caching/src/main/java/com/iluwatar/caching/CacheStore.java
+++ b/caching/src/main/java/com/iluwatar/caching/CacheStore.java
@@ -23,11 +23,11 @@
package com.iluwatar.caching;
+import com.iluwatar.caching.database.DbManager;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
-import com.iluwatar.caching.database.DbManager;
import lombok.extern.slf4j.Slf4j;
/**
diff --git a/caching/src/main/java/com/iluwatar/caching/LruCache.java b/caching/src/main/java/com/iluwatar/caching/LruCache.java
index f23eb79b4..a9a93a4f6 100644
--- a/caching/src/main/java/com/iluwatar/caching/LruCache.java
+++ b/caching/src/main/java/com/iluwatar/caching/LruCache.java
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
+
/**
* Data structure/implementation of the application's cache. The data structure
* consists of a hash table attached with a doubly linked-list. The linked-list
@@ -62,7 +63,8 @@ public class LruCache {
/**
* Node definition.
- * @param id String
+ *
+ * @param id String
* @param account {@link UserAccount}
*/
Node(final String id, final UserAccount account) {
@@ -90,6 +92,7 @@ public class LruCache {
/**
* Constructor.
+ *
* @param cap Integer.
*/
public LruCache(final int cap) {
@@ -98,6 +101,7 @@ public class LruCache {
/**
* Get user account.
+ *
* @param userId String
* @return {@link UserAccount}
*/
@@ -113,6 +117,7 @@ public class LruCache {
/**
* Remove node from linked list.
+ *
* @param node {@link Node}
*/
public void remove(final Node node) {
@@ -130,6 +135,7 @@ public class LruCache {
/**
* Move node to the front of the list.
+ *
* @param node {@link Node}
*/
public void setHead(final Node node) {
@@ -146,8 +152,9 @@ public class LruCache {
/**
* Set user account.
+ *
* @param userAccount {@link UserAccount}
- * @param userId {@link String}
+ * @param userId {@link String}
*/
public void set(final String userId, final UserAccount userAccount) {
if (cache.containsKey(userId)) {
@@ -169,19 +176,21 @@ public class LruCache {
}
}
- /**
- * Che if Cache cintains the userId.
- * @param userId {@link String}
- * @return boolean
- */
+ /**
+ * Che if Cache cintains the userId.
+ *
+ * @param userId {@link String}
+ * @return boolean
+ */
public boolean contains(final String userId) {
return cache.containsKey(userId);
}
- /**
- * Invalidate cache for user.
- * @param userId {@link String}
- */
+ /**
+ * Invalidate cache for user.
+ *
+ * @param userId {@link String}
+ */
public void invalidate(final String userId) {
var toBeRemoved = cache.remove(userId);
if (toBeRemoved != null) {
@@ -191,18 +200,19 @@ public class LruCache {
}
}
- /**
- * Is cache full?
- * @return boolean
- */
+ /**
+ * Check if the cache is full.
+ * @return boolean
+ */
public boolean isFull() {
return cache.size() >= capacity;
}
- /**
- * Get LRU data.
- * @return {@link UserAccount}
- */
+ /**
+ * Get LRU data.
+ *
+ * @return {@link UserAccount}
+ */
public UserAccount getLruData() {
return end.userAccount;
}
@@ -218,6 +228,7 @@ public class LruCache {
/**
* Returns cache data in list form.
+ *
* @return {@link List}
*/
public List getCacheDataInListForm() {
@@ -232,6 +243,7 @@ public class LruCache {
/**
* Set cache capacity.
+ *
* @param newCapacity int
*/
public void setCapacity(final int newCapacity) {
diff --git a/caching/src/main/java/com/iluwatar/caching/database/DbManager.java b/caching/src/main/java/com/iluwatar/caching/database/DbManager.java
index 496384abc..c8388e93d 100644
--- a/caching/src/main/java/com/iluwatar/caching/database/DbManager.java
+++ b/caching/src/main/java/com/iluwatar/caching/database/DbManager.java
@@ -8,33 +8,36 @@ import com.iluwatar.caching.UserAccount;
* and updating data. MongoDB was used as the database for the application.
*/
public interface DbManager {
- /**
- * Connect to DB.
- */
- void connect();
+ /**
+ * Connect to DB.
+ */
+ void connect();
- /**
- * Read from DB.
- * @param userId {@link String}
- * @return {@link UserAccount}
- */
- UserAccount readFromDb(String userId);
- /**
- * Write to DB.
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- UserAccount writeToDb(UserAccount userAccount);
- /**
- * Update record.
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- UserAccount updateDb(UserAccount userAccount);
- /**
- * Update record or Insert if not exists.
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- UserAccount upsertDb(UserAccount userAccount);
+ /**
+ * Read from DB.
+ * @param userId {@link String}
+ * @return {@link UserAccount}
+ */
+ UserAccount readFromDb(String userId);
+
+ /**
+ * Write to DB.
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ UserAccount writeToDb(UserAccount userAccount);
+
+ /**
+ * Update record.
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ UserAccount updateDb(UserAccount userAccount);
+
+ /**
+ * Update record or Insert if not exists.
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ UserAccount upsertDb(UserAccount userAccount);
}
diff --git a/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java b/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java
index f68177203..90ef432cc 100644
--- a/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java
+++ b/caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java
@@ -4,22 +4,22 @@ package com.iluwatar.caching.database;
* Creates the database connection accroding the input parameter.
*/
public final class DbManagerFactory {
- /**
- * Private constructor.
- */
- private DbManagerFactory() {
- }
+ /**
+ * Private constructor.
+ */
+ private DbManagerFactory() {
+ }
- /**
- * Init database.
- *
- * @param isMongo boolean
- * @return {@link DbManager}
- */
- public static DbManager initDb(final boolean isMongo) {
- if (isMongo) {
- return new MongoDb();
- }
- return new VirtualDb();
+ /**
+ * Init database.
+ *
+ * @param isMongo boolean
+ * @return {@link DbManager}
+ */
+ public static DbManager initDb(final boolean isMongo) {
+ if (isMongo) {
+ return new MongoDb();
}
+ return new VirtualDb();
+ }
}
diff --git a/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java b/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java
index fceedc9aa..083f3b615 100644
--- a/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java
+++ b/caching/src/main/java/com/iluwatar/caching/database/MongoDb.java
@@ -1,5 +1,10 @@
package com.iluwatar.caching.database;
+import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO;
+import static com.iluwatar.caching.constants.CachingConstants.USER_ACCOUNT;
+import static com.iluwatar.caching.constants.CachingConstants.USER_ID;
+import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
+
import com.iluwatar.caching.UserAccount;
import com.iluwatar.caching.constants.CachingConstants;
import com.mongodb.MongoClient;
@@ -7,114 +12,109 @@ import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;
-import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
-import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO;
-import static com.iluwatar.caching.constants.CachingConstants.USER_ID;
-import static com.iluwatar.caching.constants.CachingConstants.USER_ACCOUNT;
-
/**
* Implementation of DatabaseManager.
* implements base methods to work with MongoDb.
*/
public class MongoDb implements DbManager {
- /**
- * Mongo db.
- */
- private MongoDatabase db;
+ /**
+ * Mongo db.
+ */
+ private MongoDatabase db;
- /**
- * Connect to Db.
- */
- @Override
- public void connect() {
- MongoClient mongoClient = new MongoClient();
- db = mongoClient.getDatabase("test");
- }
+ /**
+ * Connect to Db.
+ */
+ @Override
+ public void connect() {
+ MongoClient mongoClient = new MongoClient();
+ db = mongoClient.getDatabase("test");
+ }
- /**
- * Read data from DB.
- *
- * @param userId {@link String}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount readFromDb(final String userId) {
- if (db == null) {
- connect();
- }
- var iterable = db
- .getCollection(CachingConstants.USER_ACCOUNT)
- .find(new Document(USER_ID, userId));
- if (iterable.first()==null) {
- return null;
- }
- Document doc = iterable.first();
- String userName = doc.getString(USER_NAME);
- String appInfo = doc.getString(ADD_INFO);
- return new UserAccount(userId, userName, appInfo);
+ /**
+ * Read data from DB.
+ *
+ * @param userId {@link String}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount readFromDb(final String userId) {
+ if (db == null) {
+ connect();
}
+ var iterable = db
+ .getCollection(CachingConstants.USER_ACCOUNT)
+ .find(new Document(USER_ID, userId));
+ if (iterable.first() == null) {
+ return null;
+ }
+ Document doc = iterable.first();
+ String userName = doc.getString(USER_NAME);
+ String appInfo = doc.getString(ADD_INFO);
+ return new UserAccount(userId, userName, appInfo);
+ }
- /**
- * Write data to DB.
- *
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount writeToDb(final UserAccount userAccount) {
- if (db == null) {
- connect();
- }
- db.getCollection(USER_ACCOUNT).insertOne(
- new Document(USER_ID, userAccount.getUserId())
- .append(USER_NAME, userAccount.getUserName())
- .append(ADD_INFO, userAccount.getAdditionalInfo())
- );
- return userAccount;
+ /**
+ * Write data to DB.
+ *
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount writeToDb(final UserAccount userAccount) {
+ if (db == null) {
+ connect();
}
+ db.getCollection(USER_ACCOUNT).insertOne(
+ new Document(USER_ID, userAccount.getUserId())
+ .append(USER_NAME, userAccount.getUserName())
+ .append(ADD_INFO, userAccount.getAdditionalInfo())
+ );
+ return userAccount;
+ }
- /**
- * Update DB.
- *
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount updateDb(final UserAccount userAccount) {
- if (db == null) {
- connect();
- }
- Document id = new Document(USER_ID, userAccount.getUserId());
- Document dataSet = new Document(USER_NAME, userAccount.getUserName())
- .append(ADD_INFO, userAccount.getAdditionalInfo());
- db.getCollection(CachingConstants.USER_ACCOUNT)
- .updateOne(id, new Document("$set", dataSet));
- return userAccount;
+ /**
+ * Update DB.
+ *
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount updateDb(final UserAccount userAccount) {
+ if (db == null) {
+ connect();
}
+ Document id = new Document(USER_ID, userAccount.getUserId());
+ Document dataSet = new Document(USER_NAME, userAccount.getUserName())
+ .append(ADD_INFO, userAccount.getAdditionalInfo());
+ db.getCollection(CachingConstants.USER_ACCOUNT)
+ .updateOne(id, new Document("$set", dataSet));
+ return userAccount;
+ }
- /**
- * Update data if exists.
- *
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount upsertDb(final UserAccount userAccount) {
- if (db == null) {
- connect();
- }
- String userId = userAccount.getUserId();
- String userName = userAccount.getUserName();
- String additionalInfo = userAccount.getAdditionalInfo();
- db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
- new Document(USER_ID, userId),
- new Document("$set",
- new Document(USER_ID, userId)
- .append(USER_NAME, userName)
- .append(ADD_INFO, additionalInfo)
- ),
- new UpdateOptions().upsert(true)
- );
- return userAccount;
+ /**
+ * Update data if exists.
+ *
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount upsertDb(final UserAccount userAccount) {
+ if (db == null) {
+ connect();
}
+ String userId = userAccount.getUserId();
+ String userName = userAccount.getUserName();
+ String additionalInfo = userAccount.getAdditionalInfo();
+ db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
+ new Document(USER_ID, userId),
+ new Document("$set",
+ new Document(USER_ID, userId)
+ .append(USER_NAME, userName)
+ .append(ADD_INFO, additionalInfo)
+ ),
+ new UpdateOptions().upsert(true)
+ );
+ return userAccount;
+ }
}
diff --git a/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java b/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java
index bd1601ee7..283dda47a 100644
--- a/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java
+++ b/caching/src/main/java/com/iluwatar/caching/database/VirtualDb.java
@@ -10,60 +10,64 @@ import java.util.Map;
* implements base methods to work with hashMap as database.
*/
public class VirtualDb implements DbManager {
- /**
- * Virtual DataBase.
- */
- private Map db;
+ /**
+ * Virtual DataBase.
+ */
+ private Map db;
- /**
- * Creates new HashMap.
- */
- @Override
- public void connect() {
- db = new HashMap<>();
- }
+ /**
+ * Creates new HashMap.
+ */
+ @Override
+ public void connect() {
+ db = new HashMap<>();
+ }
- /**
- * Read from Db.
- * @param userId {@link String}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount readFromDb(final String userId) {
- if (db.containsKey(userId)) {
- return db.get(userId);
- }
- return null;
+ /**
+ * Read from Db.
+ *
+ * @param userId {@link String}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount readFromDb(final String userId) {
+ if (db.containsKey(userId)) {
+ return db.get(userId);
}
+ return null;
+ }
- /**
- * Write to DB.
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount writeToDb(final UserAccount userAccount) {
- db.put(userAccount.getUserId(), userAccount);
- return userAccount;
- }
+ /**
+ * Write to DB.
+ *
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount writeToDb(final UserAccount userAccount) {
+ db.put(userAccount.getUserId(), userAccount);
+ return userAccount;
+ }
- /**
- * Update reecord in DB.
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount updateDb(final UserAccount userAccount) {
- return writeToDb(userAccount);
- }
+ /**
+ * Update reecord in DB.
+ *
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount updateDb(final UserAccount userAccount) {
+ return writeToDb(userAccount);
+ }
- /**
- * Update.
- * @param userAccount {@link UserAccount}
- * @return {@link UserAccount}
- */
- @Override
- public UserAccount upsertDb(final UserAccount userAccount) {
- return updateDb(userAccount);
- }
+ /**
+ * Update.
+ *
+ * @param userAccount {@link UserAccount}
+ * @return {@link UserAccount}
+ */
+ @Override
+ public UserAccount upsertDb(final UserAccount userAccount) {
+ return updateDb(userAccount);
+ }
}
diff --git a/caching/src/main/java/com/iluwatar/caching/package-info.java b/caching/src/main/java/com/iluwatar/caching/package-info.java
index fed1ab2a8..00687084f 100644
--- a/caching/src/main/java/com/iluwatar/caching/package-info.java
+++ b/caching/src/main/java/com/iluwatar/caching/package-info.java
@@ -1,17 +1,14 @@
/**
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
- *
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
- *
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
- *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE