Skip to content

Commit 6adb27c

Browse files
committed
Added docker-compose for mongo db. MongoDb db work fixed.
1 parent c0e4bf3 commit 6adb27c

File tree

10 files changed

+121
-97
lines changed

10 files changed

+121
-97
lines changed

caching/docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3.7'
2+
services:
3+
mongodb_container:
4+
image: mongo:latest
5+
environment:
6+
MONGO_INITDB_ROOT_USERNAME: root
7+
MONGO_INITDB_ROOT_PASSWORD: rootpassword
8+
ports:
9+
- 27017:27017
10+
volumes:
11+
- ./mongo-data/:/data/db

caching/pom.xml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,21 @@
3939
<scope>test</scope>
4040
</dependency>
4141
<dependency>
42-
<groupId>org.mongodb</groupId>
43-
<artifactId>mongodb-driver</artifactId>
44-
<version>3.12.1</version>
42+
<groupId>org.mockito</groupId>
43+
<artifactId>mockito-junit-jupiter</artifactId>
44+
<version>3.12.4</version>
45+
<scope>test</scope>
4546
</dependency>
4647
<dependency>
47-
<groupId>org.mongodb</groupId>
48-
<artifactId>mongodb-driver-core</artifactId>
49-
<version>3.0.4</version>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-all</artifactId>
50+
<version>1.10.19</version>
51+
<scope>test</scope>
5052
</dependency>
5153
<dependency>
5254
<groupId>org.mongodb</groupId>
53-
<artifactId>bson</artifactId>
54-
<version>3.0.4</version>
55+
<artifactId>mongo-java-driver</artifactId>
56+
<version>3.4.1</version>
5557
</dependency>
5658
</dependencies>
5759
<!--

caching/src/main/java/com/iluwatar/caching/App.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
* the preferred caching policy/strategy).
4141
* <p>
4242
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy -->
43-
* DBManager} </i>
43+
* DBManager} </i>
4444
* </p>
4545
*
4646
* <p>
47-
* To run the application with MongoDb, just start it with parameter --mongo
48-
* Example: java -jar app.jar --mongo
47+
* To run the application with MongoDb, just start it with parameter --mongo
48+
* Example: java -jar app.jar --mongo
4949
* </p>
5050
*
5151
* @see CacheStore
@@ -65,6 +65,7 @@ public class App {
6565

6666
/**
6767
* Constructor of current App.
68+
*
6869
* @param isMongo boolean
6970
*/
7071
public App(final boolean isMongo) {
@@ -97,6 +98,7 @@ public static void main(final String[] args) {
9798

9899
/**
99100
* Check the input parameters. if
101+
*
100102
* @param args input params
101103
* @return true if there is "--mongo" parameter in arguments
102104
*/

caching/src/main/java/com/iluwatar/caching/AppManager.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
package com.iluwatar.caching;
2525

2626
import com.iluwatar.caching.database.DbManager;
27-
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
27+
2828
import java.util.Optional;
2929

30-
import lombok.Data;
3130
import lombok.extern.slf4j.Slf4j;
3231

3332
/**
@@ -39,7 +38,6 @@
3938
* appropriate function in the CacheStore class.
4039
*/
4140
@Slf4j
42-
@Data
4341
public class AppManager {
4442
/**
4543
* Caching Policy.
@@ -56,6 +54,7 @@ public class AppManager {
5654

5755
/**
5856
* Constructor.
57+
*
5958
* @param newDbManager database manager
6059
*/
6160
public AppManager(final DbManager newDbManager) {
@@ -69,15 +68,12 @@ public AppManager(final DbManager newDbManager) {
6968
* to (temporarily) store the data/objects during runtime.
7069
*/
7170
public void initDb() {
72-
try {
73-
dbManager.connect();
74-
} catch (DatabaseConnectionException e) {
75-
LOGGER.error("Could not connect to DB: {}", e.getMessage());
76-
}
71+
dbManager.connect();
7772
}
7873

7974
/**
8075
* Initialize caching policy.
76+
*
8177
* @param policy is a {@link CachingPolicy}
8278
*/
8379
public void initCachingPolicy(final CachingPolicy policy) {
@@ -90,6 +86,7 @@ public void initCachingPolicy(final CachingPolicy policy) {
9086

9187
/**
9288
* Find user account.
89+
*
9390
* @param userId String
9491
* @return {@link UserAccount}
9592
*/
@@ -108,6 +105,7 @@ public UserAccount find(final String userId) {
108105

109106
/**
110107
* Save user account.
108+
*
111109
* @param userAccount {@link UserAccount}
112110
*/
113111
public void save(final UserAccount userAccount) {
@@ -125,6 +123,7 @@ public void save(final UserAccount userAccount) {
125123

126124
/**
127125
* Returns String.
126+
*
128127
* @return String
129128
*/
130129
public String printCacheContent() {
@@ -133,6 +132,7 @@ public String printCacheContent() {
133132

134133
/**
135134
* Cache-Aside save user account helper.
135+
*
136136
* @param userAccount {@link UserAccount}
137137
*/
138138
private void saveAside(final UserAccount userAccount) {
@@ -142,17 +142,18 @@ private void saveAside(final UserAccount userAccount) {
142142

143143
/**
144144
* Cache-Aside find user account helper.
145+
*
145146
* @param userId String
146147
* @return {@link UserAccount}
147148
*/
148149
private UserAccount findAside(final String userId) {
149150
return Optional.ofNullable(cacheStore.get(userId))
150-
.or(() -> {
151-
Optional<UserAccount> userAccount =
152-
Optional.ofNullable(dbManager.readFromDb(userId));
153-
userAccount.ifPresent(account -> cacheStore.set(userId, account));
154-
return userAccount;
155-
})
156-
.orElse(null);
151+
.or(() -> {
152+
Optional<UserAccount> userAccount =
153+
Optional.ofNullable(dbManager.readFromDb(userId));
154+
userAccount.ifPresent(account -> cacheStore.set(userId, account));
155+
return userAccount;
156+
})
157+
.orElse(null);
157158
}
158159
}

caching/src/main/java/com/iluwatar/caching/CacheStore.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public void flushCache() {
166166
.map(LruCache::getCacheDataInListForm)
167167
.orElse(List.of())
168168
.forEach(dbManager::updateDb);
169+
dbManager.disconnect();
169170
}
170171

171172
/**

caching/src/main/java/com/iluwatar/caching/UserAccount.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
package com.iluwatar.caching;
2525

2626
import lombok.AllArgsConstructor;
27-
import lombok.Getter;
28-
import lombok.Setter;
27+
import lombok.Data;
28+
import lombok.EqualsAndHashCode;
2929
import lombok.ToString;
3030

3131
/**
3232
* Entity class (stored in cache and DB) used in the application.
3333
*/
34-
@Setter
35-
@Getter
34+
@Data
3635
@AllArgsConstructor
3736
@ToString
37+
@EqualsAndHashCode
3838
public class UserAccount {
3939
/**
4040
* User Id.

caching/src/main/java/com/iluwatar/caching/database/DbManager.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.iluwatar.caching.database;
22

33
import com.iluwatar.caching.UserAccount;
4-
import com.iluwatar.caching.database.exceptions.DatabaseConnectionException;
54

65
/**
76
* <p>DBManager handles the communication with the underlying data store i.e.
@@ -12,31 +11,40 @@ public interface DbManager {
1211
/**
1312
* Connect to DB.
1413
*/
15-
void connect() throws DatabaseConnectionException;
14+
void connect();
15+
16+
/**
17+
* Disconnect from DB.
18+
*/
19+
void disconnect();
1620

1721
/**
1822
* Read from DB.
23+
*
1924
* @param userId {@link String}
2025
* @return {@link UserAccount}
2126
*/
2227
UserAccount readFromDb(String userId);
2328

2429
/**
2530
* Write to DB.
31+
*
2632
* @param userAccount {@link UserAccount}
2733
* @return {@link UserAccount}
2834
*/
2935
UserAccount writeToDb(UserAccount userAccount);
3036

3137
/**
3238
* Update record.
39+
*
3340
* @param userAccount {@link UserAccount}
3441
* @return {@link UserAccount}
3542
*/
3643
UserAccount updateDb(UserAccount userAccount);
3744

3845
/**
3946
* Update record or Insert if not exists.
47+
*
4048
* @param userAccount {@link UserAccount}
4149
* @return {@link UserAccount}
4250
*/

0 commit comments

Comments
 (0)