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