@@ -43,11 +43,13 @@ public class ObjectHandler {
43
43
44
44
static Logger log = (Logger ) LoggerFactory .getLogger (ObjectHandler .class );
45
45
46
- @ Getter @ Setter
46
+ @ Getter
47
+ @ Setter
47
48
@ Value ("${aggregated.collection.name}" )
48
49
private String collectionName ;
49
50
50
- @ Getter @ Setter
51
+ @ Getter
52
+ @ Setter
51
53
@ Value ("${database.name}" )
52
54
private String databaseName ;
53
55
@@ -66,7 +68,7 @@ public class ObjectHandler {
66
68
@ Setter
67
69
@ Autowired
68
70
private SubscriptionHandler subscriptionHandler ;
69
-
71
+
70
72
@ Getter
71
73
@ Value ("${aggregated.collection.ttlValue}" )
72
74
private int ttlValue ;
@@ -81,12 +83,11 @@ public boolean insertObject(String aggregatedObject, RulesObject rulesObject, St
81
83
log .debug ("ObjectHandler: Aggregated Object document to be inserted: " + document .toString ());
82
84
83
85
mongoDbHandler .createTTLIndex (databaseName , collectionName , "Time" , ttlValue );
84
-
85
86
86
87
boolean result = mongoDbHandler .insertDocument (databaseName , collectionName , document .toString ());
87
88
if (result )
88
89
eventToObjectMap .updateEventToObjectMapInMemoryDB (rulesObject , event , id );
89
- subscriptionHandler .checkSubscriptionForObject (aggregatedObject );
90
+ subscriptionHandler .checkSubscriptionForObject (aggregatedObject );
90
91
return result ;
91
92
}
92
93
@@ -95,12 +96,18 @@ public boolean insertObject(JsonNode aggregatedObject, RulesObject rulesObject,
95
96
}
96
97
97
98
/**
98
- * This method uses previously locked in database aggregatedObject (lock was set in lockDocument method)
99
- * and modifies this document with the new values and removes the lock in one query
100
- * @param aggregatedObject String to insert in database
101
- * @param rulesObject used for fetching id
102
- * @param event String to fetch id if it was not specified
103
- * @param id String
99
+ * This method uses previously locked in database aggregatedObject (lock was
100
+ * set in lockDocument method) and modifies this document with the new
101
+ * values and removes the lock in one query
102
+ *
103
+ * @param aggregatedObject
104
+ * String to insert in database
105
+ * @param rulesObject
106
+ * used for fetching id
107
+ * @param event
108
+ * String to fetch id if it was not specified
109
+ * @param id
110
+ * String
104
111
* @return true if operation succeed
105
112
*/
106
113
public boolean updateObject (String aggregatedObject , RulesObject rulesObject , String event , String id ) {
@@ -109,15 +116,15 @@ public boolean updateObject(String aggregatedObject, RulesObject rulesObject, St
109
116
JsonNode idNode = jmespathInterface .runRuleOnEvent (idRules , event );
110
117
id = idNode .textValue ();
111
118
}
112
- log .debug ("ObjectHandler: Updating Aggregated Object:\n " + aggregatedObject +
113
- "\n Event:\n " + event );
119
+ log .debug ("ObjectHandler: Updating Aggregated Object:\n " + aggregatedObject + "\n Event:\n " + event );
114
120
JsonNode document = prepareDocumentForInsertion (id , aggregatedObject );
115
121
String condition = "{\" _id\" : \" " + id + "\" }" ;
116
122
String documentStr = document .toString ();
117
123
boolean result = mongoDbHandler .updateDocument (databaseName , collectionName , condition , documentStr );
118
- if (result )
124
+ if (result ) {
119
125
eventToObjectMap .updateEventToObjectMapInMemoryDB (rulesObject , event , id );
120
126
subscriptionHandler .checkSubscriptionForObject (aggregatedObject );
127
+ }
121
128
return result ;
122
129
}
123
130
@@ -132,9 +139,6 @@ public List<String> findObjectsByCondition(String condition) {
132
139
public String findObjectById (String id ) {
133
140
String condition = "{\" _id\" : \" " + id + "\" }" ;
134
141
String document = findObjectsByCondition (condition ).get (0 );
135
- // JsonNode result = getAggregatedObject(document);
136
- // if (result != null)
137
- // return result.asText();
138
142
return document ;
139
143
}
140
144
@@ -158,54 +162,59 @@ public JsonNode prepareDocumentForInsertion(String id, String object) {
158
162
159
163
return jsonNode ;
160
164
} catch (Exception e ) {
161
- log .info (e .getMessage (),e );
165
+ log .error (e .getMessage (), e );
162
166
}
163
167
return null ;
164
168
}
165
169
166
170
public JsonNode getAggregatedObject (String dbDocument ) {
167
- ObjectMapper mapper = new ObjectMapper ();
168
- try {
169
- JsonNode documentJson = mapper .readValue (dbDocument , JsonNode .class );
170
- JsonNode objectDoc = documentJson .get ("aggregatedObject" );
171
- return objectDoc ;
172
- } catch (Exception e ) {
173
- log .info (e .getMessage (),e );
174
- }
175
- return null ;
171
+ ObjectMapper mapper = new ObjectMapper ();
172
+ try {
173
+ JsonNode documentJson = mapper .readValue (dbDocument , JsonNode .class );
174
+ JsonNode objectDoc = documentJson .get ("aggregatedObject" );
175
+ return objectDoc ;
176
+ } catch (Exception e ) {
177
+ log .error (e .getMessage (), e );
178
+ }
179
+ return null ;
176
180
}
177
181
178
182
public String extractObjectId (JsonNode aggregatedDbObject ) {
179
183
return aggregatedDbObject .get ("_id" ).textValue ();
180
184
}
181
185
182
186
/**
183
- * Locks the document in database to achieve pessimistic locking. Method findAndModify is used to optimize
184
- * the quantity of requests towards database.
185
- * @param id String to search
187
+ * Locks the document in database to achieve pessimistic locking. Method
188
+ * findAndModify is used to optimize the quantity of requests towards
189
+ * database.
190
+ *
191
+ * @param id
192
+ * String to search
186
193
* @return String aggregated document
187
194
*/
188
- public String lockDocument (String id ){
195
+ public String lockDocument (String id ) {
189
196
boolean documentLocked = true ;
190
197
String conditionId = "{\" _id\" : \" " + id + "\" }" ;
191
198
String conditionLock = "[ { \" lock\" : null } , { \" lock\" : \" 0\" }]" ;
192
199
String setLock = "{ \" $set\" : { \" lock\" : \" 1\" }}" ;
193
200
ObjectMapper mapper = new ObjectMapper ();
194
- while (documentLocked == true ){
201
+ while (documentLocked == true ) {
195
202
try {
196
203
JsonNode documentJson = mapper .readValue (setLock , JsonNode .class );
197
204
JsonNode queryCondition = mapper .readValue (conditionId , JsonNode .class );
198
205
((ObjectNode ) queryCondition ).set ("$or" , mapper .readValue (conditionLock , JsonNode .class ));
199
- Document result = mongoDbHandler .findAndModify (databaseName , collectionName , queryCondition .toString (), documentJson .toString ());
206
+ Document result = mongoDbHandler .findAndModify (databaseName , collectionName , queryCondition .toString (),
207
+ documentJson .toString ());
200
208
if (result != null ) {
201
- log .info ("DB locked by " + Thread .currentThread ().getId () + " thread" );
209
+ log .debug ("DB locked by " + Thread .currentThread ().getId () + " thread" );
202
210
documentLocked = false ;
203
211
return JSON .serialize (result );
204
212
}
205
213
// To Remove
206
- log .info ("Waiting by " + Thread .currentThread ().getId () + " thread" );
214
+ log .debug ("Waiting by " + Thread .currentThread ().getId () + " thread" );
207
215
} catch (Exception e ) {
208
- log .info (e .getMessage (),e ); }
216
+ log .error (e .getMessage (), e );
217
+ }
209
218
}
210
219
return null ;
211
220
}
0 commit comments