1
|
package eu.dnetlib.oai.mongo;
|
2
|
|
3
|
import java.util.List;
|
4
|
import javax.annotation.Resource;
|
5
|
|
6
|
import com.google.common.collect.Iterables;
|
7
|
import com.google.common.collect.Lists;
|
8
|
import eu.dnetlib.oai.PublisherStoreDAO;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.beans.factory.annotation.Required;
|
13
|
|
14
|
public class MongoPublisherStoreDAO implements PublisherStoreDAO<MongoPublisherStore, DNetOAIMongoCursor> {
|
15
|
|
16
|
private static final Log log = LogFactory.getLog(MongoPublisherStoreDAO.class); // NOPMD by marko on 11/24/08 5:02 PM
|
17
|
|
18
|
@Autowired
|
19
|
private MongoClient publisherMongoClient;
|
20
|
|
21
|
/**
|
22
|
* Name of the collection with information about the OAI stores.
|
23
|
**/
|
24
|
private String metadataCollection;
|
25
|
|
26
|
@Resource
|
27
|
private RecordInfoGenerator recordInfoGenerator;
|
28
|
@Resource
|
29
|
private MetadataExtractor metadataExtractor;
|
30
|
@Resource
|
31
|
private MongoQueryParser queryParser;
|
32
|
@Resource(name = "oaiConfigurationExistReader")
|
33
|
private OAIConfigurationReader configuration;
|
34
|
@Resource
|
35
|
private RecordChangeDetector recordChangeDetector;
|
36
|
@Resource
|
37
|
private MongoSetCollection mongoSetCollection;
|
38
|
|
39
|
/**
|
40
|
* Cache for oaistores. Keys are dbName-storeId, values are objects of type MongoPublisherStore.
|
41
|
*/
|
42
|
@Resource
|
43
|
private EhCache<String, MongoPublisherStore> mongoOaistoreCache;
|
44
|
|
45
|
/**
|
46
|
* Cache for oaistores. Keys is OAI metadata prefixes, values are objects of type MongoPublisherStore.
|
47
|
*/
|
48
|
@Resource
|
49
|
private EhCache<String, MongoPublisherStore> mongoOaistoreCacheByMdPrefix;
|
50
|
|
51
|
private boolean alwaysNewRecord;
|
52
|
|
53
|
protected MongoDatabase getDB(final String dbName) {
|
54
|
return this.publisherMongoClient.getDatabase(dbName);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public List<MongoPublisherStore> listPublisherStores(final String dbName) {
|
59
|
final MongoDatabase db = getDB(dbName);
|
60
|
final FindIterable<DBObject> stores = db.getCollection(this.metadataCollection, DBObject.class).find();
|
61
|
return Lists.newArrayList(
|
62
|
Iterables.transform(stores, new Function<DBObject, MongoPublisherStore>() {
|
63
|
@Override
|
64
|
public MongoPublisherStore apply(final DBObject storeInfo) {
|
65
|
return createFromDBObject(storeInfo, db);
|
66
|
}
|
67
|
})
|
68
|
);
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public MongoPublisherStore getStore(final String storeId, final String dbName) {
|
73
|
DBObject storeInfo = getDB(dbName).getCollection(this.metadataCollection, DBObject.class).find(Filters.eq("id", storeId)).first();
|
74
|
return this.createFromDBObject(storeInfo, getDB(dbName));
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public MongoPublisherStore getStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName) {
|
79
|
return this.getStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName);
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
public MongoPublisherStore getStoreFor(final String targetMetadataPrefix, final String dbName) {
|
84
|
if (mongoOaistoreCacheByMdPrefix.containsKey(targetMetadataPrefix)) return mongoOaistoreCacheByMdPrefix.get(targetMetadataPrefix);
|
85
|
else {
|
86
|
MDFInfo info = this.configuration.getMetadataFormatInfo(targetMetadataPrefix);
|
87
|
MongoPublisherStore store = this.getStore(info.getSourceFormatName(), info.getSourceFormatInterpretation(), info.getSourceFormatLayout(), dbName);
|
88
|
mongoOaistoreCacheByMdPrefix.put(targetMetadataPrefix, store);
|
89
|
return store;
|
90
|
}
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public MongoPublisherStore createStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName)
|
95
|
throws OaiPublisherException {
|
96
|
MongoDatabase db = getDB(dbName);
|
97
|
DBObject store = createMetadataEntry(mdfName, mdfInterpretation, mdfLayout);
|
98
|
MongoCollection<DBObject> metadata = db.getCollection(this.metadataCollection, DBObject.class);
|
99
|
metadata.insertOne(store);
|
100
|
MongoPublisherStore theStore = this.createFromDBObject(store, db);
|
101
|
return theStore;
|
102
|
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public boolean deleteStore(final String storeId, final String dbName) {
|
107
|
|
108
|
MongoDatabase db = getDB(dbName);
|
109
|
MongoCollection<DBObject> metadata = db.getCollection(this.metadataCollection, DBObject.class);
|
110
|
final DBObject storeDeleted = metadata.findOneAndDelete(Filters.eq("id", storeId));
|
111
|
if (storeDeleted == null) return false;
|
112
|
else {
|
113
|
db.getCollection(storeId).drop();
|
114
|
// TODO: should drop entries related to mdPrefix served by the store we are deleting, not all of them.
|
115
|
this.mongoSetCollection.dropOAISets(dbName);
|
116
|
log.debug("Deleted oaistore " + storeId + ", db: " + dbName);
|
117
|
return true;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public boolean deleteFromStore(final String storeId, final String dbName, final String set) {
|
123
|
MongoDatabase db = getDB(dbName);
|
124
|
MongoCollection<DBObject> metadata = db.getCollection(this.metadataCollection, DBObject.class);
|
125
|
DBObject storeInfo = metadata.find(Filters.eq("id", storeId)).first();
|
126
|
if (storeInfo == null) return false;
|
127
|
else {
|
128
|
db.getCollection(storeId).deleteOne(Filters.eq(OAIConfigurationReader.SET_FIELD, set));
|
129
|
this.mongoSetCollection.dropSet(dbName, set);
|
130
|
log.debug("Deleted set " + set + " from oaistore " + storeId + ", db: " + dbName);
|
131
|
return true;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public boolean deleteFromStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName, final String set) {
|
137
|
return this.deleteFromStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName, set);
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public boolean deleteStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName) {
|
142
|
return this.deleteStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName);
|
143
|
}
|
144
|
|
145
|
public void ensureIndex(final MongoPublisherStore store) {
|
146
|
if (store == null) throw new OaiPublisherRuntimeException("Can't ensure index on null store");
|
147
|
Thread t = new Thread() {
|
148
|
|
149
|
@Override
|
150
|
public void run() {
|
151
|
store.ensureIndices();
|
152
|
}
|
153
|
};
|
154
|
t.start();
|
155
|
}
|
156
|
|
157
|
public void ensureIndex(final String dbName) {
|
158
|
List<MongoPublisherStore> stores = this.listPublisherStores(dbName);
|
159
|
for (final MongoPublisherStore s : stores) {
|
160
|
s.ensureIndices();
|
161
|
}
|
162
|
|
163
|
}
|
164
|
|
165
|
private MongoPublisherStore createFromDBObject(final DBObject storeInfo, final MongoDatabase db) {
|
166
|
if (storeInfo == null) return null;
|
167
|
String storeId = (String) storeInfo.get("id");
|
168
|
String mdFormat = (String) storeInfo.get("metadataFormat");
|
169
|
String mdInterpreation = (String) storeInfo.get("interpretation");
|
170
|
String mdLayout = (String) storeInfo.get("layout");
|
171
|
String k = db.getName() + "-" + storeId;
|
172
|
if (mongoOaistoreCache.containsKey(k)) {
|
173
|
log.debug("Store retreived from cache and alwaysNewRecord is" + alwaysNewRecord);
|
174
|
MongoPublisherStore store = mongoOaistoreCache.get(k);
|
175
|
store.setAlwaysNewRecord(alwaysNewRecord);
|
176
|
return store;
|
177
|
} else {
|
178
|
log.debug("Store retreived, cache miss, alwaysNewRecord is" + alwaysNewRecord);
|
179
|
log.fatal("Not using cache to create oaistore from dbObject: " + k);
|
180
|
MongoPublisherStore store = new MongoPublisherStore(storeId, mdFormat, mdInterpreation, mdLayout, db.getCollection(storeId, DBObject.class),
|
181
|
this.configuration.getFields(mdFormat, mdInterpreation, mdLayout), queryParser, recordInfoGenerator, this.configuration.getIdScheme(),
|
182
|
this.configuration.getIdNamespace(), this.metadataExtractor, this.recordChangeDetector, alwaysNewRecord, db);
|
183
|
store.setMongoSetCollection(mongoSetCollection);
|
184
|
mongoOaistoreCache.put(k, store);
|
185
|
return store;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
private DBObject createMetadataEntry(final String mdfName, final String mdfInterpretation, final String mdfLayout) {
|
190
|
DBObject info = BasicDBObjectBuilder.start("id", generateStoreId(mdfName, mdfInterpretation, mdfLayout)).append("metadataFormat", mdfName)
|
191
|
.append("interpretation", mdfInterpretation).append("layout", mdfLayout).get();
|
192
|
return info;
|
193
|
|
194
|
}
|
195
|
|
196
|
private String generateStoreId(final String mdfName, final String mdfInterpretation, final String mdfLayout) {
|
197
|
return mdfName + "-" + mdfLayout + "-" + mdfInterpretation;
|
198
|
}
|
199
|
|
200
|
public String getMetadataCollection() {
|
201
|
return metadataCollection;
|
202
|
}
|
203
|
|
204
|
@Required
|
205
|
public void setMetadataCollection(final String metadataCollection) {
|
206
|
this.metadataCollection = metadataCollection;
|
207
|
}
|
208
|
|
209
|
public MongoQueryParser getQueryParser() {
|
210
|
return queryParser;
|
211
|
}
|
212
|
|
213
|
public void setQueryParser(final MongoQueryParser queryParser) {
|
214
|
this.queryParser = queryParser;
|
215
|
}
|
216
|
|
217
|
public OAIConfigurationReader getConfiguration() {
|
218
|
return configuration;
|
219
|
}
|
220
|
|
221
|
public void setConfiguration(final OAIConfigurationReader configuration) {
|
222
|
this.configuration = configuration;
|
223
|
}
|
224
|
|
225
|
public RecordInfoGenerator getRecordInfoGenerator() {
|
226
|
return recordInfoGenerator;
|
227
|
}
|
228
|
|
229
|
public void setRecordInfoGenerator(final RecordInfoGenerator recordInfoGenerator) {
|
230
|
this.recordInfoGenerator = recordInfoGenerator;
|
231
|
}
|
232
|
|
233
|
public MetadataExtractor getMetadataExtractor() {
|
234
|
return metadataExtractor;
|
235
|
}
|
236
|
|
237
|
public void setMetadataExtractor(final MetadataExtractor metadataExtractor) {
|
238
|
this.metadataExtractor = metadataExtractor;
|
239
|
}
|
240
|
|
241
|
public RecordChangeDetector getRecordChangeDetector() {
|
242
|
return recordChangeDetector;
|
243
|
}
|
244
|
|
245
|
public void setRecordChangeDetector(final RecordChangeDetector recordChangeDetector) {
|
246
|
this.recordChangeDetector = recordChangeDetector;
|
247
|
}
|
248
|
|
249
|
public MongoSetCollection getMongoSetCollection() {
|
250
|
return mongoSetCollection;
|
251
|
}
|
252
|
|
253
|
public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
|
254
|
this.mongoSetCollection = mongoSetCollection;
|
255
|
}
|
256
|
|
257
|
public boolean isAlwaysNewRecord() {
|
258
|
return alwaysNewRecord;
|
259
|
}
|
260
|
|
261
|
public void setAlwaysNewRecord(final boolean alwaysNewRecord) {
|
262
|
this.alwaysNewRecord = alwaysNewRecord;
|
263
|
}
|
264
|
|
265
|
}
|