Skip to content

Commit 84d035c

Browse files
Chaklader Asfak ArefeChaklader Asfak Arefe
Chaklader Asfak Arefe
authored and
Chaklader Asfak Arefe
committed
updated
1 parent 45a683a commit 84d035c

File tree

117 files changed

+1152
-2362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1152
-2362
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Design_Patterns/.DS_Store

0 Bytes
Binary file not shown.

Design_Patterns/CQRS/Library/commandes/CommandServiceImpl.java

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import com.iluwatar.cqrs.domain.model.Book;
77
import com.iluwatar.cqrs.util.HibernateUtil;
88

9+
910
/**
1011
* This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
1112
*
1213
*/
1314
public class CommandServiceImpl implements ICommandService {
1415

16+
1517
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
1618

1719
private Author getAuthorByUsername(String username) {
@@ -33,38 +35,45 @@ private Author getAuthorByUsername(String username) {
3335
}
3436

3537
private Book getBookByTitle(String title) {
36-
Book book = null;
37-
try (Session session = sessionFactory.openSession()) {
38-
Query query = session.createQuery("from Book where title=:title");
39-
query.setParameter("title", title);
40-
book = (Book) query.uniqueResult();
41-
}
42-
if (book == null) {
43-
HibernateUtil.getSessionFactory().close();
44-
throw new NullPointerException("Book " + title + " doesn't exist!");
45-
}
46-
return book;
38+
39+
Book book = null;
40+
41+
try (Session session = sessionFactory.openSession()) {
42+
Query query = session.createQuery("from Book where title=:title");
43+
query.setParameter("title", title);
44+
book = (Book) query.uniqueResult();
45+
}
46+
47+
if (book == null) {
48+
HibernateUtil.getSessionFactory().close();
49+
throw new NullPointerException("Book " + title + " doesn't exist!");
50+
}
51+
return book;
4752
}
4853

54+
4955
@Override
5056
public void authorCreated(String username, String name, String email) {
51-
Author author = new Author(username, name, email);
52-
try (Session session = sessionFactory.openSession()) {
53-
session.beginTransaction();
54-
session.save(author);
55-
session.getTransaction().commit();
56-
}
57+
Author author = new Author(username, name, email);
58+
59+
try (Session session = sessionFactory.openSession()) {
60+
session.beginTransaction();
61+
session.save(author);
62+
session.getTransaction().commit();
63+
}
5764
}
5865

5966
@Override
6067
public void bookAddedToAuthor(String title, double price, String username) {
61-
Author author = getAuthorByUsername(username);
62-
Book book = new Book(title, price, author);
63-
try (Session session = sessionFactory.openSession()) {
64-
session.beginTransaction();
65-
session.save(book);
66-
session.getTransaction().commit();
67-
}
68+
69+
Author author = getAuthorByUsername(username);
70+
Book book = new Book(title, price, author);
71+
72+
try (Session session = sessionFactory.openSession()) {
73+
session.beginTransaction();
74+
session.save(book);
75+
session.getTransaction().commit();
76+
}
6877
}
6978

7079
@Override
@@ -80,46 +89,53 @@ public void authorNameUpdated(String username, String name) {
8089

8190
@Override
8291
public void authorUsernameUpdated(String oldUsername, String newUsername) {
83-
Author author = getAuthorByUsername(oldUsername);
84-
author.setUsername(newUsername);
85-
try (Session session = sessionFactory.openSession()) {
86-
session.beginTransaction();
87-
session.update(author);
88-
session.getTransaction().commit();
89-
}
92+
93+
Author author = getAuthorByUsername(oldUsername);
94+
author.setUsername(newUsername);
95+
96+
try (Session session = sessionFactory.openSession()) {
97+
session.beginTransaction();
98+
session.update(author);
99+
session.getTransaction().commit();
100+
}
90101
}
91102

92103
@Override
93104
public void authorEmailUpdated(String username, String email) {
94-
Author author = getAuthorByUsername(username);
95-
author.setEmail(email);
96-
try (Session session = sessionFactory.openSession()) {
97-
session.beginTransaction();
98-
session.update(author);
99-
session.getTransaction().commit();
100-
}
105+
106+
Author author = getAuthorByUsername(username);
107+
author.setEmail(email);
108+
109+
try (Session session = sessionFactory.openSession()) {
110+
session.beginTransaction();
111+
session.update(author);
112+
session.getTransaction().commit();
113+
}
101114
}
102115

103116
@Override
104117
public void bookTitleUpdated(String oldTitle, String newTitle) {
105-
Book book = getBookByTitle(oldTitle);
106-
book.setTitle(newTitle);
107-
try (Session session = sessionFactory.openSession()) {
108-
session.beginTransaction();
109-
session.update(book);
110-
session.getTransaction().commit();
111-
}
118+
119+
Book book = getBookByTitle(oldTitle);
120+
book.setTitle(newTitle);
121+
122+
try (Session session = sessionFactory.openSession()) {
123+
session.beginTransaction();
124+
session.update(book);
125+
session.getTransaction().commit();
126+
}
112127
}
113128

114129
@Override
115130
public void bookPriceUpdated(String title, double price) {
116-
Book book = getBookByTitle(title);
117-
book.setPrice(price);
118-
try (Session session = sessionFactory.openSession()) {
119-
session.beginTransaction();
120-
session.update(book);
121-
session.getTransaction().commit();
122-
}
131+
Book book = getBookByTitle(title);
132+
book.setPrice(price);
133+
134+
try (Session session = sessionFactory.openSession()) {
135+
session.beginTransaction();
136+
session.update(book);
137+
session.getTransaction().commit();
138+
}
123139
}
124140

125141
}

Design_Patterns/CQRS/Library/commandes/ICommandService.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
*/
66
public interface ICommandService {
77

8-
void authorCreated(String username, String name, String email);
8+
void authorCreated(String username, String name, String email);
99

10-
void bookAddedToAuthor(String title, double price, String username);
10+
void bookAddedToAuthor(String title, double price, String username);
1111

12-
void authorNameUpdated(String username, String name);
12+
void authorNameUpdated(String username, String name);
1313

14-
void authorUsernameUpdated(String oldUsername, String newUsername);
14+
void authorUsernameUpdated(String oldUsername, String newUsername);
1515

16-
void authorEmailUpdated(String username, String email);
16+
void authorEmailUpdated(String username, String email);
1717

18-
void bookTitleUpdated(String oldTitle, String newTitle);
19-
20-
void bookPriceUpdated(String title, double price);
18+
void bookTitleUpdated(String oldTitle, String newTitle);
2119

20+
void bookPriceUpdated(String title, double price);
2221
}
0 Bytes
Binary file not shown.

Design_Patterns/Caching/.DS_Store

0 Bytes
Binary file not shown.

Design_Patterns/Caching/README.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ Use the Caching pattern(s) when
99

1010
* Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead.
1111

12+
1213
## Credits
14+
## -------
15+
16+
[Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
17+
18+
[Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
1319

14-
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
15-
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
16-
* [Cache-Aside](https://msdn.microsoft.com/en-us/library/dn589799.aspx)
20+
[Cache-Aside](https://msdn.microsoft.com/en-us/library/dn589799.aspx)

Design_Patterns/Caching/CacheStore/App.java renamed to Design_Patterns/Caching/Store/App.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ public class App {
5555
* @param args command line args
5656
*/
5757
public static void main(String[] args) {
58+
5859
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
5960
// and the App class to avoid Maven compilation errors. Set flag to
6061
// true to run the tests with MongoDB (provided that MongoDB is
6162
// installed and socket connection is open).
63+
6264
AppManager.initCacheCapacity(3);
6365
App app = new App();
66+
6467
app.useReadAndWriteThroughStrategy();
6568
app.useReadThroughAndWriteAroundStrategy();
6669
app.useReadThroughAndWriteBehindStrategy();
@@ -69,39 +72,39 @@ public static void main(String[] args) {
6972

7073
/**
7174
* Read-through and write-through
72-
*/
75+
*/
7376
public void useReadAndWriteThroughStrategy() {
74-
LOGGER.info("# CachingPolicy.THROUGH");
75-
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
77+
LOGGER.info("# CachingPolicy.THROUGH");
78+
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
7679

77-
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
80+
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
7881

79-
AppManager.save(userAccount1);
80-
LOGGER.info(AppManager.printCacheContent());
81-
AppManager.find("001");
82-
AppManager.find("001");
82+
AppManager.save(userAccount1);
83+
LOGGER.info(AppManager.printCacheContent());
84+
AppManager.find("001");
85+
AppManager.find("001");
8386
}
8487

8588
/**
8689
* Read-through and write-around
8790
*/
8891
public void useReadThroughAndWriteAroundStrategy() {
89-
LOGGER.info("# CachingPolicy.AROUND");
90-
AppManager.initCachingPolicy(CachingPolicy.AROUND);
91-
92-
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
93-
94-
AppManager.save(userAccount2);
95-
LOGGER.info(AppManager.printCacheContent());
96-
AppManager.find("002");
97-
LOGGER.info(AppManager.printCacheContent());
98-
userAccount2 = AppManager.find("002");
99-
userAccount2.setUserName("Jane G.");
100-
AppManager.save(userAccount2);
101-
LOGGER.info(AppManager.printCacheContent());
102-
AppManager.find("002");
103-
LOGGER.info(AppManager.printCacheContent());
104-
AppManager.find("002");
92+
LOGGER.info("# CachingPolicy.AROUND");
93+
AppManager.initCachingPolicy(CachingPolicy.AROUND);
94+
95+
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
96+
97+
AppManager.save(userAccount2);
98+
LOGGER.info(AppManager.printCacheContent());
99+
AppManager.find("002");
100+
LOGGER.info(AppManager.printCacheContent());
101+
userAccount2 = AppManager.find("002");
102+
userAccount2.setUserName("Jane G.");
103+
AppManager.save(userAccount2);
104+
LOGGER.info(AppManager.printCacheContent());
105+
AppManager.find("002");
106+
LOGGER.info(AppManager.printCacheContent());
107+
AppManager.find("002");
105108
}
106109

107110
/**

Design_Patterns/Caching/CacheStore/CacheStore.java renamed to Design_Patterns/Caching/Store/CacheStore.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,40 @@ private CacheStore() {
2222
* Init cache capacity
2323
*/
2424
public static void initCapacity(int capacity) {
25-
if (cache == null) {
26-
cache = new LruCache(capacity);
27-
} else {
28-
cache.setCapacity(capacity);
29-
}
25+
if (cache == null) {
26+
cache = new LruCache(capacity);
27+
} else {
28+
cache.setCapacity(capacity);
29+
}
3030
}
3131

3232
/**
3333
* Get user account using read-through cache
3434
*/
3535
public static UserAccount readThrough(String userId) {
36-
if (cache.contains(userId)) {
37-
LOGGER.info("# Cache Hit!");
38-
return cache.get(userId);
39-
}
40-
LOGGER.info("# Cache Miss!");
41-
UserAccount userAccount = DbManager.readFromDb(userId);
42-
cache.set(userId, userAccount);
43-
return userAccount;
36+
37+
if (cache.contains(userId)) {
38+
LOGGER.info("# Cache Hit!");
39+
return cache.get(userId);
40+
}
41+
42+
LOGGER.info("# Cache Miss!");
43+
UserAccount userAccount = DbManager.readFromDb(userId);
44+
45+
cache.set(userId, userAccount);
46+
return userAccount;
4447
}
4548

4649
/**
4750
* Get user account using write-through cache
4851
*/
4952
public static void writeThrough(UserAccount userAccount) {
50-
if (cache.contains(userAccount.getUserId())) {
51-
DbManager.updateDb(userAccount);
52-
} else {
53-
DbManager.writeToDb(userAccount);
54-
}
55-
cache.set(userAccount.getUserId(), userAccount);
53+
if (cache.contains(userAccount.getUserId())) {
54+
DbManager.updateDb(userAccount);
55+
} else {
56+
DbManager.writeToDb(userAccount);
57+
}
58+
cache.set(userAccount.getUserId(), userAccount);
5659
}
5760

5861
/**
@@ -72,19 +75,20 @@ public static void writeAround(UserAccount userAccount) {
7275
* Get user account using read-through cache with write-back policy
7376
*/
7477
public static UserAccount readThroughWithWriteBackPolicy(String userId) {
75-
if (cache.contains(userId)) {
76-
LOGGER.info("# Cache Hit!");
77-
return cache.get(userId);
78-
}
79-
LOGGER.info("# Cache Miss!");
80-
UserAccount userAccount = DbManager.readFromDb(userId);
81-
if (cache.isFull()) {
82-
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
83-
UserAccount toBeWrittenToDb = cache.getLruData();
84-
DbManager.upsertDb(toBeWrittenToDb);
85-
}
86-
cache.set(userId, userAccount);
87-
return userAccount;
78+
if (cache.contains(userId)) {
79+
LOGGER.info("# Cache Hit!");
80+
return cache.get(userId);
81+
}
82+
83+
LOGGER.info("# Cache Miss!");
84+
UserAccount userAccount = DbManager.readFromDb(userId);
85+
if (cache.isFull()) {
86+
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
87+
UserAccount toBeWrittenToDb = cache.getLruData();
88+
DbManager.upsertDb(toBeWrittenToDb);
89+
}
90+
cache.set(userId, userAccount);
91+
return userAccount;
8892
}
8993

9094
/**

0 commit comments

Comments
 (0)