Changed database implementation. Removed static objects.

This commit is contained in:
Victor Zalevskii
2021-08-17 16:25:59 +03:00
parent bbdff14a66
commit 399ad53b09
9 changed files with 326 additions and 320 deletions

View File

@ -23,6 +23,8 @@
package com.iluwatar.caching; package com.iluwatar.caching;
import com.iluwatar.caching.database.DbManager;
import com.iluwatar.caching.database.DbManagerFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
@ -55,111 +57,138 @@ import lombok.extern.slf4j.Slf4j;
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager} </i> * <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager} </i>
* </p> * </p>
* *
* <p>
* To run the application with MongoDb, just start it with parameter --mongo
* Example: java -jar app.jar --mongo
* </p>
*
* @see CacheStore * @see CacheStore
* @see LruCache * @see LruCache
* @see CachingPolicy * @see CachingPolicy
*/ */
@Slf4j @Slf4j
public class App { public class App {
private static final String USE_MONGO_DB = "--mongo";
private DbManager dbManager;
private AppManager appManager;
/** public App(boolean isMongo) {
* Program entry point. dbManager = DbManagerFactory.initDb(isMongo);
* appManager = new AppManager(dbManager);
* @param args command line args appManager.initDb();
*/ }
public static void main(String[] args) {
AppManager.initDb(false); // 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).
AppManager.initCacheCapacity(3);
var app = new App();
app.useReadAndWriteThroughStrategy();
app.useReadThroughAndWriteAroundStrategy();
app.useReadThroughAndWriteBehindStrategy();
app.useCacheAsideStategy();
}
/** /**
* Read-through and write-through. * Program entry point.
*/ *
public void useReadAndWriteThroughStrategy() { * @param args command line args
LOGGER.info("# CachingPolicy.THROUGH"); */
AppManager.initCachingPolicy(CachingPolicy.THROUGH); public static void main(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();
app.useReadThroughAndWriteAroundStrategy();
app.useReadThroughAndWriteBehindStrategy();
app.useCacheAsideStategy();
}
var userAccount1 = new UserAccount("001", "John", "He is a boy."); /**
* Check the input parameters. if
* @param args input params
* @return true if there is "--mongo" parameter in arguments
*/
private static boolean isDbMongo(String[] args) {
for (String arg : args) {
if (arg.equals(USE_MONGO_DB)) {
return true;
}
}
return false;
}
AppManager.save(userAccount1); /**
LOGGER.info(AppManager.printCacheContent()); * Read-through and write-through.
AppManager.find("001"); */
AppManager.find("001"); public void useReadAndWriteThroughStrategy() {
} LOGGER.info("# CachingPolicy.THROUGH");
appManager.initCachingPolicy(CachingPolicy.THROUGH);
/** 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);
var userAccount2 = new UserAccount("002", "Jane", "She is a girl."); appManager.save(userAccount1);
LOGGER.info(appManager.printCacheContent());
appManager.find("001");
appManager.find("001");
}
AppManager.save(userAccount2); /**
LOGGER.info(AppManager.printCacheContent()); * Read-through and write-around.
AppManager.find("002"); */
LOGGER.info(AppManager.printCacheContent()); public void useReadThroughAndWriteAroundStrategy() {
userAccount2 = AppManager.find("002"); LOGGER.info("# CachingPolicy.AROUND");
userAccount2.setUserName("Jane G."); appManager.initCachingPolicy(CachingPolicy.AROUND);
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);
var userAccount3 = new UserAccount("003", "Adam", "He likes food."); appManager.save(userAccount2);
var userAccount4 = new UserAccount("004", "Rita", "She hates cats."); LOGGER.info(appManager.printCacheContent());
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard."); 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");
}
AppManager.save(userAccount3); /**
AppManager.save(userAccount4); * Read-through and write-behind.
AppManager.save(userAccount5); */
LOGGER.info(AppManager.printCacheContent()); public void useReadThroughAndWriteBehindStrategy() {
AppManager.find("003"); LOGGER.info("# CachingPolicy.BEHIND");
LOGGER.info(AppManager.printCacheContent()); appManager.initCachingPolicy(CachingPolicy.BEHIND);
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.");
* Cache-Aside. var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
*/ var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
public void useCacheAsideStategy() {
LOGGER.info("# CachingPolicy.ASIDE");
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
LOGGER.info(AppManager.printCacheContent());
var userAccount3 = new UserAccount("003", "Adam", "He likes food."); appManager.save(userAccount3);
var userAccount4 = new UserAccount("004", "Rita", "She hates cats."); appManager.save(userAccount4);
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard."); appManager.save(userAccount5);
AppManager.save(userAccount3); LOGGER.info(appManager.printCacheContent());
AppManager.save(userAccount4); appManager.find("003");
AppManager.save(userAccount5); 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());
}
LOGGER.info(AppManager.printCacheContent()); /**
AppManager.find("003"); * Cache-Aside.
LOGGER.info(AppManager.printCacheContent()); */
AppManager.find("004"); public void useCacheAsideStategy() {
LOGGER.info(AppManager.printCacheContent()); 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());
}
} }

View File

@ -23,8 +23,9 @@
package com.iluwatar.caching; package com.iluwatar.caching;
import java.text.ParseException;
import java.util.Optional; import java.util.Optional;
import com.iluwatar.caching.database.DbManager;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
@ -35,11 +36,15 @@ import lombok.extern.slf4j.Slf4j;
* CacheStore class. * CacheStore class.
*/ */
@Slf4j @Slf4j
public final class AppManager { public class AppManager {
private static CachingPolicy cachingPolicy; private static CachingPolicy cachingPolicy;
private final DbManager dbManager;
private final CacheStore cacheStore;
private AppManager() { public AppManager(DbManager dbManager) {
this.dbManager = dbManager;
this.cacheStore = new CacheStore(dbManager);
} }
/** /**
@ -47,41 +52,29 @@ public final class AppManager {
* data storage or a simple Java data structure to (temporarily) store the data/objects during * data storage or a simple Java data structure to (temporarily) store the data/objects during
* runtime. * runtime.
*/ */
public static void initDb(boolean useMongoDb) { public void initDb() {
if (useMongoDb) { dbManager.connect();
try {
DbManager.connect();
} catch (ParseException e) {
LOGGER.error("Error connecting to MongoDB", e);
}
} else {
DbManager.createVirtualDb();
}
} }
/** /**
* Initialize caching policy. * Initialize caching policy.
*/ */
public static void initCachingPolicy(CachingPolicy policy) { public void initCachingPolicy(CachingPolicy policy) {
cachingPolicy = policy; cachingPolicy = policy;
if (cachingPolicy == CachingPolicy.BEHIND) { if (cachingPolicy == CachingPolicy.BEHIND) {
Runtime.getRuntime().addShutdownHook(new Thread(CacheStore::flushCache)); Runtime.getRuntime().addShutdownHook(new Thread(cacheStore::flushCache));
} }
CacheStore.clearCache(); cacheStore.clearCache();
}
public static void initCacheCapacity(int capacity) {
CacheStore.initCapacity(capacity);
} }
/** /**
* Find user account. * Find user account.
*/ */
public static UserAccount find(String userId) { public UserAccount find(String userId) {
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) { if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
return CacheStore.readThrough(userId); return cacheStore.readThrough(userId);
} else if (cachingPolicy == CachingPolicy.BEHIND) { } else if (cachingPolicy == CachingPolicy.BEHIND) {
return CacheStore.readThroughWithWriteBackPolicy(userId); return cacheStore.readThroughWithWriteBackPolicy(userId);
} else if (cachingPolicy == CachingPolicy.ASIDE) { } else if (cachingPolicy == CachingPolicy.ASIDE) {
return findAside(userId); return findAside(userId);
} }
@ -91,38 +84,38 @@ public final class AppManager {
/** /**
* Save user account. * Save user account.
*/ */
public static void save(UserAccount userAccount) { public void save(UserAccount userAccount) {
if (cachingPolicy == CachingPolicy.THROUGH) { if (cachingPolicy == CachingPolicy.THROUGH) {
CacheStore.writeThrough(userAccount); cacheStore.writeThrough(userAccount);
} else if (cachingPolicy == CachingPolicy.AROUND) { } else if (cachingPolicy == CachingPolicy.AROUND) {
CacheStore.writeAround(userAccount); cacheStore.writeAround(userAccount);
} else if (cachingPolicy == CachingPolicy.BEHIND) { } else if (cachingPolicy == CachingPolicy.BEHIND) {
CacheStore.writeBehind(userAccount); cacheStore.writeBehind(userAccount);
} else if (cachingPolicy == CachingPolicy.ASIDE) { } else if (cachingPolicy == CachingPolicy.ASIDE) {
saveAside(userAccount); saveAside(userAccount);
} }
} }
public static String printCacheContent() { public String printCacheContent() {
return CacheStore.print(); return cacheStore.print();
} }
/** /**
* Cache-Aside save user account helper. * Cache-Aside save user account helper.
*/ */
private static void saveAside(UserAccount userAccount) { private void saveAside(UserAccount userAccount) {
DbManager.updateDb(userAccount); dbManager.updateDb(userAccount);
CacheStore.invalidate(userAccount.getUserId()); cacheStore.invalidate(userAccount.getUserId());
} }
/** /**
* Cache-Aside find user account helper. * Cache-Aside find user account helper.
*/ */
private static UserAccount findAside(String userId) { private UserAccount findAside(String userId) {
return Optional.ofNullable(CacheStore.get(userId)) return Optional.ofNullable(cacheStore.get(userId))
.or(() -> { .or(() -> {
Optional<UserAccount> userAccount = Optional.ofNullable(DbManager.readFromDb(userId)); Optional<UserAccount> userAccount = Optional.ofNullable(dbManager.readFromDb(userId));
userAccount.ifPresent(account -> CacheStore.set(userId, account)); userAccount.ifPresent(account -> cacheStore.set(userId, account));
return userAccount; return userAccount;
}) })
.orElse(null); .orElse(null);

View File

@ -26,6 +26,8 @@ package com.iluwatar.caching;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.iluwatar.caching.database.DbManager;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
@ -33,16 +35,20 @@ import lombok.extern.slf4j.Slf4j;
*/ */
@Slf4j @Slf4j
public class CacheStore { public class CacheStore {
private static final int CAPACITY = 3;
private static LruCache cache; private static LruCache cache;
private DbManager dbManager;
private CacheStore() { public CacheStore(DbManager dbManager) {
this.dbManager = dbManager;
initCapacity(CAPACITY);
} }
/** /**
* Init cache capacity. * Init cache capacity.
*/ */
public static void initCapacity(int capacity) { public void initCapacity(int capacity) {
if (cache == null) { if (cache == null) {
cache = new LruCache(capacity); cache = new LruCache(capacity);
} else { } else {
@ -53,13 +59,13 @@ public class CacheStore {
/** /**
* Get user account using read-through cache. * Get user account using read-through cache.
*/ */
public static UserAccount readThrough(String userId) { public UserAccount readThrough(String userId) {
if (cache.contains(userId)) { if (cache.contains(userId)) {
LOGGER.info("# Cache Hit!"); LOGGER.info("# Cache Hit!");
return cache.get(userId); return cache.get(userId);
} }
LOGGER.info("# Cache Miss!"); LOGGER.info("# Cache Miss!");
UserAccount userAccount = DbManager.readFromDb(userId); UserAccount userAccount = dbManager.readFromDb(userId);
cache.set(userId, userAccount); cache.set(userId, userAccount);
return userAccount; return userAccount;
} }
@ -67,11 +73,11 @@ public class CacheStore {
/** /**
* Get user account using write-through cache. * Get user account using write-through cache.
*/ */
public static void writeThrough(UserAccount userAccount) { public void writeThrough(UserAccount userAccount) {
if (cache.contains(userAccount.getUserId())) { if (cache.contains(userAccount.getUserId())) {
DbManager.updateDb(userAccount); dbManager.updateDb(userAccount);
} else { } else {
DbManager.writeToDb(userAccount); dbManager.writeToDb(userAccount);
} }
cache.set(userAccount.getUserId(), userAccount); cache.set(userAccount.getUserId(), userAccount);
} }
@ -79,30 +85,30 @@ public class CacheStore {
/** /**
* Get user account using write-around cache. * Get user account using write-around cache.
*/ */
public static void writeAround(UserAccount userAccount) { public void writeAround(UserAccount userAccount) {
if (cache.contains(userAccount.getUserId())) { if (cache.contains(userAccount.getUserId())) {
DbManager.updateDb(userAccount); dbManager.updateDb(userAccount);
cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older
// version from cache. // version from cache.
} else { } else {
DbManager.writeToDb(userAccount); dbManager.writeToDb(userAccount);
} }
} }
/** /**
* Get user account using read-through cache with write-back policy. * Get user account using read-through cache with write-back policy.
*/ */
public static UserAccount readThroughWithWriteBackPolicy(String userId) { public UserAccount readThroughWithWriteBackPolicy(String userId) {
if (cache.contains(userId)) { if (cache.contains(userId)) {
LOGGER.info("# Cache Hit!"); LOGGER.info("# Cache Hit!");
return cache.get(userId); return cache.get(userId);
} }
LOGGER.info("# Cache Miss!"); LOGGER.info("# Cache Miss!");
UserAccount userAccount = DbManager.readFromDb(userId); UserAccount userAccount = dbManager.readFromDb(userId);
if (cache.isFull()) { if (cache.isFull()) {
LOGGER.info("# Cache is FULL! Writing LRU data to DB..."); LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
UserAccount toBeWrittenToDb = cache.getLruData(); UserAccount toBeWrittenToDb = cache.getLruData();
DbManager.upsertDb(toBeWrittenToDb); dbManager.upsertDb(toBeWrittenToDb);
} }
cache.set(userId, userAccount); cache.set(userId, userAccount);
return userAccount; return userAccount;
@ -111,11 +117,11 @@ public class CacheStore {
/** /**
* Set user account. * Set user account.
*/ */
public static void writeBehind(UserAccount userAccount) { public void writeBehind(UserAccount userAccount) {
if (cache.isFull() && !cache.contains(userAccount.getUserId())) { if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
LOGGER.info("# Cache is FULL! Writing LRU data to DB..."); LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
UserAccount toBeWrittenToDb = cache.getLruData(); UserAccount toBeWrittenToDb = cache.getLruData();
DbManager.upsertDb(toBeWrittenToDb); dbManager.upsertDb(toBeWrittenToDb);
} }
cache.set(userAccount.getUserId(), userAccount); cache.set(userAccount.getUserId(), userAccount);
} }
@ -123,7 +129,7 @@ public class CacheStore {
/** /**
* Clears cache. * Clears cache.
*/ */
public static void clearCache() { public void clearCache() {
if (cache != null) { if (cache != null) {
cache.clear(); cache.clear();
} }
@ -132,18 +138,18 @@ public class CacheStore {
/** /**
* Writes remaining content in the cache into the DB. * Writes remaining content in the cache into the DB.
*/ */
public static void flushCache() { public void flushCache() {
LOGGER.info("# flushCache..."); LOGGER.info("# flushCache...");
Optional.ofNullable(cache) Optional.ofNullable(cache)
.map(LruCache::getCacheDataInListForm) .map(LruCache::getCacheDataInListForm)
.orElse(List.of()) .orElse(List.of())
.forEach(DbManager::updateDb); .forEach(dbManager::updateDb);
} }
/** /**
* Print user accounts. * Print user accounts.
*/ */
public static String print() { public String print() {
return Optional.ofNullable(cache) return Optional.ofNullable(cache)
.map(LruCache::getCacheDataInListForm) .map(LruCache::getCacheDataInListForm)
.orElse(List.of()) .orElse(List.of())
@ -155,21 +161,21 @@ public class CacheStore {
/** /**
* Delegate to backing cache store. * Delegate to backing cache store.
*/ */
public static UserAccount get(String userId) { public UserAccount get(String userId) {
return cache.get(userId); return cache.get(userId);
} }
/** /**
* Delegate to backing cache store. * Delegate to backing cache store.
*/ */
public static void set(String userId, UserAccount userAccount) { public void set(String userId, UserAccount userAccount) {
cache.set(userId, userAccount); cache.set(userId, userAccount);
} }
/** /**
* Delegate to backing cache store. * Delegate to backing cache store.
*/ */
public static void invalidate(String userId) { public void invalidate(String userId) {
cache.invalidate(userId); cache.invalidate(userId);
} }
} }

View File

@ -1,171 +0,0 @@
/*
* 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.caching;
import com.iluwatar.caching.constants.CachingConstants;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
/**
* <p>DBManager handles the communication with the underlying data store i.e. Database. It contains
* the implemented methods for querying, inserting, and updating data. MongoDB was used as the
* database for the application.</p>
*
* <p>Developer/Tester is able to choose whether the application should use MongoDB as its
* underlying data storage (connect()) or a simple Java data structure to (temporarily) store the
* data/objects during runtime (createVirtualDB()).</p>
*/
@Slf4j
public final class DbManager {
private static MongoClient mongoClient;
private static MongoDatabase db;
private static boolean useMongoDB;
private static Map<String, UserAccount> virtualDB;
private DbManager() {
}
/**
* Create DB.
*/
public static void createVirtualDb() {
useMongoDB = false;
virtualDB = new HashMap<>();
}
/**
* Connect to DB.
*/
public static void connect() throws ParseException {
useMongoDB = true;
mongoClient = new MongoClient();
db = mongoClient.getDatabase("test");
}
/**
* Read user account from DB.
*/
public static UserAccount readFromDb(String userId) {
if (!useMongoDB) {
if (virtualDB.containsKey(userId)) {
return virtualDB.get(userId);
}
return null;
}
if (db == null) {
try {
connect();
} catch (ParseException e) {
LOGGER.error("Error connecting to MongoDB", e);
}
}
var iterable = db
.getCollection(CachingConstants.USER_ACCOUNT)
.find(new Document(CachingConstants.USER_ID, userId));
if (iterable == null) {
return null;
}
Document doc = iterable.first();
String userName = doc.getString(CachingConstants.USER_NAME);
String appInfo = doc.getString(CachingConstants.ADD_INFO);
return new UserAccount(userId, userName, appInfo);
}
/**
* Write user account to DB.
*/
public static void writeToDb(UserAccount userAccount) {
if (!useMongoDB) {
virtualDB.put(userAccount.getUserId(), userAccount);
return;
}
if (db == null) {
try {
connect();
} catch (ParseException e) {
LOGGER.error("Error connecting to MongoDB", e);
}
}
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
);
}
/**
* Update DB.
*/
public static void updateDb(UserAccount userAccount) {
if (!useMongoDB) {
virtualDB.put(userAccount.getUserId(), userAccount);
return;
}
if (db == null) {
try {
connect();
} catch (ParseException e) {
LOGGER.error("Error connecting to MongoDB", e);
}
}
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())));
}
/**
* Insert data into DB if it does not exist. Else, update it.
*/
public static void upsertDb(UserAccount userAccount) {
if (!useMongoDB) {
virtualDB.put(userAccount.getUserId(), userAccount);
return;
}
if (db == null) {
try {
connect();
} catch (ParseException e) {
LOGGER.error("Error connecting to MongoDB", e);
}
}
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set",
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
),
new UpdateOptions().upsert(true)
);
}
}

View File

@ -0,0 +1,16 @@
package com.iluwatar.caching.database;
import com.iluwatar.caching.UserAccount;
/**
* <p>DBManager handles the communication with the underlying data store i.e. Database. It contains
* the implemented methods for querying, inserting, and updating data. MongoDB was used as the
* database for the application.</p>
*/
public interface DbManager {
void connect();
UserAccount readFromDb(String userId);
UserAccount writeToDb(UserAccount userAccount);
UserAccount updateDb(UserAccount userAccount);
UserAccount upsertDb(UserAccount userAccount);
}

View File

@ -0,0 +1,10 @@
package com.iluwatar.caching.database;
public class DbManagerFactory {
public static DbManager initDb(boolean isMongo){
if(isMongo){
return new MongoDb();
}
return new VirtualDb();
}
}

View File

@ -0,0 +1,81 @@
package com.iluwatar.caching.database;
import com.iluwatar.caching.UserAccount;
import com.iluwatar.caching.constants.CachingConstants;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;
/**
* Implementation of DatabaseManager.
* implements base methods to work with MongoDb.
*/
public class MongoDb implements DbManager {
private MongoDatabase db;
@Override
public void connect() {
MongoClient mongoClient = new MongoClient();
db = mongoClient.getDatabase("test");
}
@Override
public UserAccount readFromDb(String userId) {
if (db == null) {
connect();
}
var iterable = db
.getCollection(CachingConstants.USER_ACCOUNT)
.find(new Document(CachingConstants.USER_ID, userId));
if (iterable == null) {
return null;
}
Document doc = iterable.first();
String userName = doc.getString(CachingConstants.USER_NAME);
String appInfo = doc.getString(CachingConstants.ADD_INFO);
return new UserAccount(userId, userName, appInfo);
}
@Override
public UserAccount writeToDb(UserAccount userAccount) {
if (db == null) {
connect();
}
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
);
return userAccount;
}
@Override
public UserAccount updateDb(UserAccount userAccount) {
if (db == null) {
connect();
}
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())));
return userAccount;
}
@Override
public UserAccount upsertDb(UserAccount userAccount) {
if (db == null) {
connect();
}
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set",
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
),
new UpdateOptions().upsert(true)
);
return userAccount;
}
}

View File

@ -0,0 +1,44 @@
package com.iluwatar.caching.database;
import com.iluwatar.caching.UserAccount;
import java.util.HashMap;
import java.util.Map;
/**
* Implementation of DatabaseManager.
* implements base methods to work with hashMap as database.
*/
public class VirtualDb implements DbManager {
private Map<String, UserAccount> virtualDB;
@Override
public void connect() {
virtualDB = new HashMap<>();
}
@Override
public UserAccount readFromDb(String userId) {
if (virtualDB.containsKey(userId)) {
return virtualDB.get(userId);
}
return null;
}
@Override
public UserAccount writeToDb(UserAccount userAccount) {
virtualDB.put(userAccount.getUserId(), userAccount);
return userAccount;
}
@Override
public UserAccount updateDb(UserAccount userAccount) {
virtualDB.put(userAccount.getUserId(), userAccount);
return userAccount;
}
@Override
public UserAccount upsertDb(UserAccount userAccount) {
return updateDb(userAccount);
}
}

View File

@ -43,9 +43,7 @@ class CachingTest {
// to avoid Maven compilation errors. Set flag to true to run the // to avoid Maven compilation errors. Set flag to true to run the
// tests with MongoDB (provided that MongoDB is installed and socket // tests with MongoDB (provided that MongoDB is installed and socket
// connection is open). // connection is open).
AppManager.initDb(false); app = new App(false);
AppManager.initCacheCapacity(3);
app = new App();
} }
@Test @Test