Added docker-compose for mongo db. MongoDb db work fixed.
This commit is contained in:
parent
c0e4bf3d1d
commit
6adb27ca3d
11
caching/docker-compose.yml
Normal file
11
caching/docker-compose.yml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
version: '3.7'
|
||||||
|
services:
|
||||||
|
mongodb_container:
|
||||||
|
image: mongo:latest
|
||||||
|
environment:
|
||||||
|
MONGO_INITDB_ROOT_USERNAME: root
|
||||||
|
MONGO_INITDB_ROOT_PASSWORD: rootpassword
|
||||||
|
ports:
|
||||||
|
- 27017:27017
|
||||||
|
volumes:
|
||||||
|
- ./mongo-data/:/data/db
|
@ -39,19 +39,21 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mongodb</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mongodb-driver</artifactId>
|
<artifactId>mockito-junit-jupiter</artifactId>
|
||||||
<version>3.12.1</version>
|
<version>3.12.4</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-all</artifactId>
|
||||||
|
<version>1.10.19</version>
|
||||||
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mongodb</groupId>
|
<groupId>org.mongodb</groupId>
|
||||||
<artifactId>mongodb-driver-core</artifactId>
|
<artifactId>mongo-java-driver</artifactId>
|
||||||
<version>3.0.4</version>
|
<version>3.4.1</version>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mongodb</groupId>
|
|
||||||
<artifactId>bson</artifactId>
|
|
||||||
<version>3.0.4</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<!--
|
<!--
|
||||||
|
@ -65,6 +65,7 @@ public class App {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of current App.
|
* Constructor of current App.
|
||||||
|
*
|
||||||
* @param isMongo boolean
|
* @param isMongo boolean
|
||||||
*/
|
*/
|
||||||
public App(final boolean isMongo) {
|
public App(final boolean isMongo) {
|
||||||
@ -97,6 +98,7 @@ public class App {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check the input parameters. if
|
* Check the input parameters. if
|
||||||
|
*
|
||||||
* @param args input params
|
* @param args input params
|
||||||
* @return true if there is "--mongo" parameter in arguments
|
* @return true if there is "--mongo" parameter in arguments
|
||||||
*/
|
*/
|
||||||
|
@ -24,10 +24,9 @@
|
|||||||
package com.iluwatar.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import com.iluwatar.caching.database.DbManager;
|
import com.iluwatar.caching.database.DbManager;
|
||||||
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,7 +38,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
* appropriate function in the CacheStore class.
|
* appropriate function in the CacheStore class.
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Data
|
|
||||||
public class AppManager {
|
public class AppManager {
|
||||||
/**
|
/**
|
||||||
* Caching Policy.
|
* Caching Policy.
|
||||||
@ -56,6 +54,7 @@ public class AppManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
*
|
||||||
* @param newDbManager database manager
|
* @param newDbManager database manager
|
||||||
*/
|
*/
|
||||||
public AppManager(final DbManager newDbManager) {
|
public AppManager(final DbManager newDbManager) {
|
||||||
@ -69,15 +68,12 @@ public class AppManager {
|
|||||||
* to (temporarily) store the data/objects during runtime.
|
* to (temporarily) store the data/objects during runtime.
|
||||||
*/
|
*/
|
||||||
public void initDb() {
|
public void initDb() {
|
||||||
try {
|
|
||||||
dbManager.connect();
|
dbManager.connect();
|
||||||
} catch (DatabaseConnectionException e) {
|
|
||||||
LOGGER.error("Could not connect to DB: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize caching policy.
|
* Initialize caching policy.
|
||||||
|
*
|
||||||
* @param policy is a {@link CachingPolicy}
|
* @param policy is a {@link CachingPolicy}
|
||||||
*/
|
*/
|
||||||
public void initCachingPolicy(final CachingPolicy policy) {
|
public void initCachingPolicy(final CachingPolicy policy) {
|
||||||
@ -90,6 +86,7 @@ public class AppManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find user account.
|
* Find user account.
|
||||||
|
*
|
||||||
* @param userId String
|
* @param userId String
|
||||||
* @return {@link UserAccount}
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
@ -108,6 +105,7 @@ public class AppManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Save user account.
|
* Save user account.
|
||||||
|
*
|
||||||
* @param userAccount {@link UserAccount}
|
* @param userAccount {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
public void save(final UserAccount userAccount) {
|
public void save(final UserAccount userAccount) {
|
||||||
@ -125,6 +123,7 @@ public class AppManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns String.
|
* Returns String.
|
||||||
|
*
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
public String printCacheContent() {
|
public String printCacheContent() {
|
||||||
@ -133,6 +132,7 @@ public class AppManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache-Aside save user account helper.
|
* Cache-Aside save user account helper.
|
||||||
|
*
|
||||||
* @param userAccount {@link UserAccount}
|
* @param userAccount {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
private void saveAside(final UserAccount userAccount) {
|
private void saveAside(final UserAccount userAccount) {
|
||||||
@ -142,6 +142,7 @@ public class AppManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache-Aside find user account helper.
|
* Cache-Aside find user account helper.
|
||||||
|
*
|
||||||
* @param userId String
|
* @param userId String
|
||||||
* @return {@link UserAccount}
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
|
@ -166,6 +166,7 @@ public class CacheStore {
|
|||||||
.map(LruCache::getCacheDataInListForm)
|
.map(LruCache::getCacheDataInListForm)
|
||||||
.orElse(List.of())
|
.orElse(List.of())
|
||||||
.forEach(dbManager::updateDb);
|
.forEach(dbManager::updateDb);
|
||||||
|
dbManager.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,17 +24,17 @@
|
|||||||
package com.iluwatar.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Data;
|
||||||
import lombok.Setter;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entity class (stored in cache and DB) used in the application.
|
* Entity class (stored in cache and DB) used in the application.
|
||||||
*/
|
*/
|
||||||
@Setter
|
@Data
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@ToString
|
@ToString
|
||||||
|
@EqualsAndHashCode
|
||||||
public class UserAccount {
|
public class UserAccount {
|
||||||
/**
|
/**
|
||||||
* User Id.
|
* User Id.
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.iluwatar.caching.database;
|
package com.iluwatar.caching.database;
|
||||||
|
|
||||||
import com.iluwatar.caching.UserAccount;
|
import com.iluwatar.caching.UserAccount;
|
||||||
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>DBManager handles the communication with the underlying data store i.e.
|
* <p>DBManager handles the communication with the underlying data store i.e.
|
||||||
@ -12,10 +11,16 @@ public interface DbManager {
|
|||||||
/**
|
/**
|
||||||
* Connect to DB.
|
* Connect to DB.
|
||||||
*/
|
*/
|
||||||
void connect() throws DatabaseConnectionException;
|
void connect();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect from DB.
|
||||||
|
*/
|
||||||
|
void disconnect();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from DB.
|
* Read from DB.
|
||||||
|
*
|
||||||
* @param userId {@link String}
|
* @param userId {@link String}
|
||||||
* @return {@link UserAccount}
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
@ -23,6 +28,7 @@ public interface DbManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to DB.
|
* Write to DB.
|
||||||
|
*
|
||||||
* @param userAccount {@link UserAccount}
|
* @param userAccount {@link UserAccount}
|
||||||
* @return {@link UserAccount}
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
@ -30,6 +36,7 @@ public interface DbManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update record.
|
* Update record.
|
||||||
|
*
|
||||||
* @param userAccount {@link UserAccount}
|
* @param userAccount {@link UserAccount}
|
||||||
* @return {@link UserAccount}
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
@ -37,6 +44,7 @@ public interface DbManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update record or Insert if not exists.
|
* Update record or Insert if not exists.
|
||||||
|
*
|
||||||
* @param userAccount {@link UserAccount}
|
* @param userAccount {@link UserAccount}
|
||||||
* @return {@link UserAccount}
|
* @return {@link UserAccount}
|
||||||
*/
|
*/
|
||||||
|
@ -7,10 +7,13 @@ import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
|
|||||||
|
|
||||||
import com.iluwatar.caching.UserAccount;
|
import com.iluwatar.caching.UserAccount;
|
||||||
import com.iluwatar.caching.constants.CachingConstants;
|
import com.iluwatar.caching.constants.CachingConstants;
|
||||||
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
|
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
|
import com.mongodb.MongoClientOptions;
|
||||||
|
import com.mongodb.MongoCredential;
|
||||||
|
import com.mongodb.ServerAddress;
|
||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import com.mongodb.client.model.UpdateOptions;
|
import com.mongodb.client.model.UpdateOptions;
|
||||||
|
import java.util.List;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
@ -20,18 +23,28 @@ import org.bson.Document;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MongoDb implements DbManager {
|
public class MongoDb implements DbManager {
|
||||||
private static final String DATABASE_NAME = "test";
|
private static final String DATABASE_NAME = "admin";
|
||||||
|
private static final String MONGO_USER = "root";
|
||||||
|
private static final String MONGO_PASSWORD = "rootpassword";
|
||||||
|
private MongoClient client;
|
||||||
|
private MongoDatabase db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to Db. Check th connection
|
* Connect to Db. Check th connection
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void connect() throws DatabaseConnectionException {
|
public void connect() {
|
||||||
try (MongoClient mongoClient = new MongoClient()) {
|
MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_USER,
|
||||||
mongoClient.getDatabase("test");
|
DATABASE_NAME,
|
||||||
} catch (NoClassDefFoundError e) {
|
MONGO_PASSWORD.toCharArray());
|
||||||
throw new DatabaseConnectionException("Could not connect to DB.");
|
MongoClientOptions options = MongoClientOptions.builder().build();
|
||||||
|
client = new MongoClient(new ServerAddress(), List.of(mongoCredential), options);
|
||||||
|
db = client.getDatabase(DATABASE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnect() {
|
||||||
|
client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,8 +55,6 @@ public class MongoDb implements DbManager {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserAccount readFromDb(final String userId) {
|
public UserAccount readFromDb(final String userId) {
|
||||||
try (MongoClient mongoClient = new MongoClient()) {
|
|
||||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
|
||||||
var iterable = db
|
var iterable = db
|
||||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||||
.find(new Document(USER_ID, userId));
|
.find(new Document(USER_ID, userId));
|
||||||
@ -59,7 +70,6 @@ public class MongoDb implements DbManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write data to DB.
|
* Write data to DB.
|
||||||
@ -69,8 +79,6 @@ public class MongoDb implements DbManager {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserAccount writeToDb(final UserAccount userAccount) {
|
public UserAccount writeToDb(final UserAccount userAccount) {
|
||||||
try (MongoClient mongoClient = new MongoClient()) {
|
|
||||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
|
||||||
db.getCollection(USER_ACCOUNT).insertOne(
|
db.getCollection(USER_ACCOUNT).insertOne(
|
||||||
new Document(USER_ID, userAccount.getUserId())
|
new Document(USER_ID, userAccount.getUserId())
|
||||||
.append(USER_NAME, userAccount.getUserName())
|
.append(USER_NAME, userAccount.getUserName())
|
||||||
@ -78,7 +86,6 @@ public class MongoDb implements DbManager {
|
|||||||
);
|
);
|
||||||
return userAccount;
|
return userAccount;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update DB.
|
* Update DB.
|
||||||
@ -88,8 +95,6 @@ public class MongoDb implements DbManager {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserAccount updateDb(final UserAccount userAccount) {
|
public UserAccount updateDb(final UserAccount userAccount) {
|
||||||
try (MongoClient mongoClient = new MongoClient()) {
|
|
||||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
|
||||||
Document id = new Document(USER_ID, userAccount.getUserId());
|
Document id = new Document(USER_ID, userAccount.getUserId());
|
||||||
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
|
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
|
||||||
.append(ADD_INFO, userAccount.getAdditionalInfo());
|
.append(ADD_INFO, userAccount.getAdditionalInfo());
|
||||||
@ -97,7 +102,6 @@ public class MongoDb implements DbManager {
|
|||||||
.updateOne(id, new Document("$set", dataSet));
|
.updateOne(id, new Document("$set", dataSet));
|
||||||
return userAccount;
|
return userAccount;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update data if exists.
|
* Update data if exists.
|
||||||
@ -107,8 +111,6 @@ public class MongoDb implements DbManager {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public UserAccount upsertDb(final UserAccount userAccount) {
|
public UserAccount upsertDb(final UserAccount userAccount) {
|
||||||
try (MongoClient mongoClient = new MongoClient()) {
|
|
||||||
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
|
|
||||||
String userId = userAccount.getUserId();
|
String userId = userAccount.getUserId();
|
||||||
String userName = userAccount.getUserName();
|
String userName = userAccount.getUserName();
|
||||||
String additionalInfo = userAccount.getAdditionalInfo();
|
String additionalInfo = userAccount.getAdditionalInfo();
|
||||||
@ -123,5 +125,4 @@ public class MongoDb implements DbManager {
|
|||||||
);
|
);
|
||||||
return userAccount;
|
return userAccount;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,11 @@ public class VirtualDb implements DbManager {
|
|||||||
db = new HashMap<>();
|
db = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnect() {
|
||||||
|
db = null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from Db.
|
* Read from Db.
|
||||||
*
|
*
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package com.iluwatar.caching.database.exceptions;
|
|
||||||
|
|
||||||
public class DatabaseConnectionException extends Exception {
|
|
||||||
public DatabaseConnectionException(String s) {
|
|
||||||
super(s);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user