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