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