1
|
package eu.dnetlib.common.logging.dao;
|
2
|
|
3
|
import java.util.*;
|
4
|
import java.util.Map.Entry;
|
5
|
|
6
|
import com.google.common.collect.Lists;
|
7
|
import com.google.common.collect.Maps;
|
8
|
import com.mongodb.BasicDBObject;
|
9
|
import com.mongodb.BasicDBObjectBuilder;
|
10
|
import com.mongodb.DBObject;
|
11
|
import com.mongodb.client.model.Filters;
|
12
|
import eu.dnetlib.common.logging.LogMessage;
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.bson.conversions.Bson;
|
17
|
import org.jongo.Jongo;
|
18
|
import org.jongo.MongoCollection;
|
19
|
import org.jongo.MongoCursor;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
|
22
|
public class DnetLoggerMongoDao implements DnetLoggerDao {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(DnetLoggerMongoDao.class);
|
25
|
|
26
|
@Autowired
|
27
|
private Jongo db;
|
28
|
|
29
|
@Override
|
30
|
public void init(final String collection) {
|
31
|
final Set<String> collections = db.getDatabase().getCollectionNames();
|
32
|
if (!Lists.newArrayList(collections).contains(collection)) {
|
33
|
log.info(String.format("creating collection %s", collection));
|
34
|
db.getDatabase().createCollection(collection, new BasicDBObject());
|
35
|
}
|
36
|
}
|
37
|
|
38
|
@Override
|
39
|
public void configureIndex(final String collection, final Map<String, IndexConf> conf) {
|
40
|
final MongoCollection coll = db.getCollection(collection);
|
41
|
coll.dropIndexes();
|
42
|
for (String key : conf.keySet()) {
|
43
|
coll.ensureIndex("{'"+key+"':1}");
|
44
|
}
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public void writeLog(final String collection, final Map<String, Object> map) {
|
49
|
final MongoCollection coll = db.getCollection(collection);
|
50
|
DBObject obj = BasicDBObjectBuilder.start(replaceKeyNames(map)).get();
|
51
|
coll.insert(obj);
|
52
|
}
|
53
|
|
54
|
private Map<String, Object> replaceKeyNames(final Map<String, Object> inputMap) {
|
55
|
final Map<String, Object> outMap = Maps.newHashMap();
|
56
|
|
57
|
if (inputMap != null) {
|
58
|
for (Entry<String, Object> e : inputMap.entrySet()) {
|
59
|
final String k = e.getKey();
|
60
|
if (!StringUtils.isBlank(k)) {
|
61
|
Object v = e.getValue();
|
62
|
outMap.put(k.replaceAll("\\.", "_"), v != null ? v : "null");
|
63
|
}
|
64
|
}
|
65
|
}
|
66
|
return outMap;
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public Map<String, String> findOne(final String collection, final String key, final String value) {
|
71
|
final MongoCollection coll = db.getCollection(collection);
|
72
|
//final DBObject obj = coll.find(Filters.eq(key, value)).first();
|
73
|
final DBObject obj = coll.findOne(getBasicQuery(key, value)).as(BasicDBObject.class);
|
74
|
|
75
|
return dbObject2Map(obj);
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public Iterator<Map<String, String>> obtainLogIterator(final String collection) {
|
80
|
final MongoCollection coll = db.getCollection(collection);
|
81
|
return iter(collection, coll.find().as(BasicDBObject.class));
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public Iterator<Map<String, String>> find(final String collection, final String key, final String value) {
|
86
|
final MongoCollection coll = db.getCollection(collection);
|
87
|
return iter(collection, coll.find(getBasicQuery(key, value)).as(BasicDBObject.class));
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public Iterator<Map<String, String>> find(final String collection, final Map<String, Object> criteria) {
|
92
|
final MongoCollection coll = db.getCollection(collection);
|
93
|
final String query = getCustomQuery(criteria);
|
94
|
return iter(collection, coll.find(query).as(BasicDBObject.class));
|
95
|
}
|
96
|
|
97
|
@Override
|
98
|
public Iterator<Map<String, String>> findByDateRange(final String collection, final Date startDate, final Date endDate) {
|
99
|
final MongoCollection coll = db.getCollection(collection);
|
100
|
//Bson dateQuery = dateRangeQuery(startDate, endDate);
|
101
|
|
102
|
//Filters.and(Filters.gte(LogMessage.LOG_DATE_FIELD, startDate.getTime()), Filters.lt(LogMessage.LOG_DATE_FIELD, endDate.getTime()));
|
103
|
final String query = getDateQuery(startDate, endDate);
|
104
|
|
105
|
// System.out.println(mongoQueryObject);
|
106
|
return iter(collection, coll.find(query).as(BasicDBObject.class));
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
public Iterator<Map<String, String>> findByDateRange(final String collection, final Date startDate, final Date endDate, final String key, final String value) {
|
111
|
final MongoCollection coll = db.getCollection(collection);
|
112
|
final String dateQuery = getDateQuery(startDate, endDate);
|
113
|
final String customQuery = getBasicQuery(key, value);
|
114
|
|
115
|
|
116
|
final StringBuilder sb = new StringBuilder("{'"+key+"':'"+value+"',");
|
117
|
sb.append(StringUtils.removeEnd(sb.toString(), ","))
|
118
|
.append("'" + LogMessage.LOG_DATE_FIELD + "' : {$gte:"+startDate.getTime()+"},")
|
119
|
.append("'" + LogMessage.LOG_DATE_FIELD + "' : {$lt:"+endDate.getTime()+"}}");
|
120
|
|
121
|
return iter(collection, coll.find(sb.toString()).as(BasicDBObject.class));
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public Iterator<Map<String, String>> findByDateRange(final String collection, final Date startDate, final Date endDate, final Map<String, Object> criteria) {
|
126
|
final MongoCollection coll = db.getCollection(collection);
|
127
|
//final Bson dateQuery = dateRangeQuery(startDate, endDate);
|
128
|
//final BasicDBObject customQuery = new BasicDBObject(criteria);
|
129
|
|
130
|
final StringBuilder sb = new StringBuilder("{");
|
131
|
for(Entry<String, Object> e : criteria.entrySet()) {
|
132
|
sb.append("'" + e.getKey() + "':'" + e.getValue() + "', ");
|
133
|
}
|
134
|
sb.append(StringUtils.removeEnd(sb.toString(), ","))
|
135
|
.append("'" + LogMessage.LOG_DATE_FIELD + "' : {$gte:"+startDate.getTime()+"},")
|
136
|
.append("'" + LogMessage.LOG_DATE_FIELD + "' : {$lt:"+endDate.getTime()+"}}");
|
137
|
|
138
|
return iter(collection, coll.find(sb.toString()).as(BasicDBObject.class));
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public Iterator<Map<String, String>> find(final String collection, final String mongoquery, final List<String> fields) {
|
143
|
final MongoCollection coll = db.getCollection(collection);
|
144
|
return iter(collection, coll.find(mongoquery).as(BasicDBObject.class));
|
145
|
}
|
146
|
|
147
|
private String getBasicQuery(final String key, final String value) {
|
148
|
return "{'"+key+"':'"+value+"'}";
|
149
|
}
|
150
|
private String getDateQuery(final Date startDate, final Date endDate) {
|
151
|
return "{'" + LogMessage.LOG_DATE_FIELD + "' : {$gte:"+startDate.getTime()+"}, '" + LogMessage.LOG_DATE_FIELD + "' : {$lt:"+endDate.getTime()+"} }";
|
152
|
}
|
153
|
private String getCustomQuery(final Map<String, Object> criteria) {
|
154
|
final StringBuilder sb = new StringBuilder("{");
|
155
|
for(Entry<String, Object> e : criteria.entrySet()) {
|
156
|
sb.append("'" + e.getKey() + "':'" + e.getValue() + "', ");
|
157
|
}
|
158
|
return StringUtils.removeEnd(sb.toString(), ",") + "}";
|
159
|
}
|
160
|
|
161
|
|
162
|
private Bson dateRangeQuery(final Date startDate, final Date endDate) {
|
163
|
Bson dateFilter = Filters.and(Filters.gte(LogMessage.LOG_DATE_FIELD, startDate.getTime()), Filters.lt(LogMessage.LOG_DATE_FIELD, endDate.getTime()));
|
164
|
log.debug("Date filter created: " + dateFilter);
|
165
|
//return new BasicDBObject(LogMessage.LOG_DATE_FIELD, BasicDBObjectBuilder.start("$gte", startDate.getTime()).append("$lt", endDate.getTime()).get());
|
166
|
return dateFilter;
|
167
|
}
|
168
|
|
169
|
private Iterator<Map<String, String>> iter(final String collection, final MongoCursor<BasicDBObject> cursor) {
|
170
|
return new Iterator<Map<String, String>>() {
|
171
|
|
172
|
@Override
|
173
|
public boolean hasNext() {
|
174
|
return cursor.hasNext();
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public Map<String, String> next() {
|
179
|
return dbObject2Map(cursor.next());
|
180
|
}
|
181
|
|
182
|
@Override
|
183
|
public void remove() {
|
184
|
throw new RuntimeException("NOT IMPLEMENTED");
|
185
|
}
|
186
|
};
|
187
|
}
|
188
|
|
189
|
private Map<String, String> dbObject2Map(final DBObject obj) {
|
190
|
final Map<String, String> res = Maps.newHashMap();
|
191
|
if (obj != null) {
|
192
|
for (String k : obj.keySet()) {
|
193
|
res.put(k, "" + obj.get(k));
|
194
|
}
|
195
|
}
|
196
|
return res;
|
197
|
}
|
198
|
|
199
|
}
|