1
|
package eu.dnetlib.data.oai.store.mongo;
|
2
|
|
3
|
import java.util.List;
|
4
|
|
5
|
import javax.annotation.Resource;
|
6
|
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.beans.factory.annotation.Required;
|
9
|
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.mongodb.BasicDBObject;
|
12
|
import com.mongodb.BasicDBObjectBuilder;
|
13
|
import com.mongodb.DB;
|
14
|
import com.mongodb.DBCollection;
|
15
|
import com.mongodb.DBCursor;
|
16
|
import com.mongodb.DBObject;
|
17
|
import com.mongodb.Mongo;
|
18
|
|
19
|
import eu.dnetlib.data.information.oai.publisher.OaiPublisherException;
|
20
|
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
|
21
|
import eu.dnetlib.data.information.oai.publisher.conf.OAIConfigurationReader;
|
22
|
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
|
23
|
import eu.dnetlib.data.oai.store.PublisherStoreDAO;
|
24
|
import eu.dnetlib.data.oai.store.RecordChangeDetector;
|
25
|
import eu.dnetlib.data.oai.store.parser.MongoQueryParser;
|
26
|
import eu.dnetlib.data.oai.store.sets.MongoSetCollection;
|
27
|
|
28
|
public class MongoPublisherStoreDAO implements PublisherStoreDAO<MongoPublisherStore, MongoCursor> {
|
29
|
|
30
|
@Autowired
|
31
|
private Mongo publisherMongoServer;
|
32
|
|
33
|
/** Name of the collection with information about the OAI stores. **/
|
34
|
private String metadataCollection;
|
35
|
|
36
|
@Resource
|
37
|
private RecordInfoGenerator recordInfoGenerator;
|
38
|
@Resource
|
39
|
private MetadataExtractor metadataExtractor;
|
40
|
@Resource
|
41
|
private MongoQueryParser queryParser;
|
42
|
@Resource(name = "oaiConfigurationExistReader")
|
43
|
private OAIConfigurationReader configuration;
|
44
|
@Resource
|
45
|
private RecordChangeDetector recordChangeDetector;
|
46
|
@Resource
|
47
|
private MongoSetCollection mongoSetCollection;
|
48
|
|
49
|
protected DB getDB(final String dbName) {
|
50
|
return this.publisherMongoServer.getDB(dbName);
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public List<MongoPublisherStore> listPublisherStores(final String dbName) {
|
55
|
List<MongoPublisherStore> stores = Lists.newArrayList();
|
56
|
DB db = getDB(dbName);
|
57
|
DBCursor cursor = db.getCollection(this.metadataCollection).find();
|
58
|
for (DBObject storeInfo : cursor) {
|
59
|
stores.add(this.createFromDBObject(storeInfo, db));
|
60
|
}
|
61
|
return stores;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public MongoPublisherStore getStore(final String storeId, final String dbName) {
|
66
|
DBObject storeInfo = getDB(dbName).getCollection(this.metadataCollection).findOne(new BasicDBObject("id", storeId));
|
67
|
return this.createFromDBObject(storeInfo, getDB(dbName));
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public MongoPublisherStore getStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName) {
|
72
|
return this.getStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName);
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public MongoPublisherStore getStoreFor(final String targetMetadataPrefix, final String dbName) {
|
77
|
MDFInfo info = this.configuration.getMetadataFormatInfo(targetMetadataPrefix);
|
78
|
return this.getStore(info.getSourceFormatName(), info.getSourceFormatInterpretation(), info.getSourceFormatLayout(), dbName);
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public MongoPublisherStore createStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName)
|
83
|
throws OaiPublisherException {
|
84
|
DB db = getDB(dbName);
|
85
|
DBObject store = createMetadataEntry(mdfName, mdfInterpretation, mdfLayout);
|
86
|
DBCollection metadata = db.getCollection(this.metadataCollection);
|
87
|
metadata.insert(store);
|
88
|
MongoPublisherStore theStore = this.createFromDBObject(store, db);
|
89
|
return theStore;
|
90
|
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public boolean deleteStore(final String storeId, final String dbName) {
|
95
|
DB db = getDB(dbName);
|
96
|
DBCollection metadata = db.getCollection(this.metadataCollection);
|
97
|
DBObject storeInfo = metadata.findOne(new BasicDBObject("id", storeId));
|
98
|
if (storeInfo == null) return false;
|
99
|
else {
|
100
|
db.getCollection(storeId).drop();
|
101
|
metadata.remove(storeInfo);
|
102
|
// TODO: should drop entries related to mdPrefix served by the store we are deleting, not all of them.
|
103
|
this.mongoSetCollection.dropOAISets(dbName);
|
104
|
return true;
|
105
|
}
|
106
|
}
|
107
|
|
108
|
@Override
|
109
|
public boolean deleteFromStore(final String storeId, final String dbName, final String set) {
|
110
|
DB db = getDB(dbName);
|
111
|
DBCollection metadata = db.getCollection(this.metadataCollection);
|
112
|
DBObject storeInfo = metadata.findOne(new BasicDBObject("id", storeId));
|
113
|
if (storeInfo == null) return false;
|
114
|
else {
|
115
|
db.getCollection(storeId).remove(new BasicDBObject(OAIConfigurationReader.SET_FIELD, set));
|
116
|
this.mongoSetCollection.dropSet(dbName, set);
|
117
|
return true;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public boolean deleteStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName) {
|
123
|
return this.deleteStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName);
|
124
|
}
|
125
|
|
126
|
public void ensureIndex(final MongoPublisherStore store) {
|
127
|
if (store == null) throw new OaiPublisherRuntimeException("Can't ensure index on null store");
|
128
|
Thread t = new Thread() {
|
129
|
|
130
|
@Override
|
131
|
public void run() {
|
132
|
store.ensureIndices();
|
133
|
}
|
134
|
};
|
135
|
t.start();
|
136
|
}
|
137
|
|
138
|
public void ensureIndex(final String dbName) {
|
139
|
List<MongoPublisherStore> stores = this.listPublisherStores(dbName);
|
140
|
for (final MongoPublisherStore s : stores) {
|
141
|
s.ensureIndices();
|
142
|
}
|
143
|
|
144
|
}
|
145
|
|
146
|
private MongoPublisherStore createFromDBObject(final DBObject storeInfo, final DB db) {
|
147
|
if (storeInfo == null) return null;
|
148
|
String storeId = (String) storeInfo.get("id");
|
149
|
String mdFormat = (String) storeInfo.get("metadataFormat");
|
150
|
String mdInterpreation = (String) storeInfo.get("interpretation");
|
151
|
String mdLayout = (String) storeInfo.get("layout");
|
152
|
|
153
|
MongoPublisherStore store = new MongoPublisherStore(storeId, mdFormat, mdInterpreation, mdLayout, db.getCollection(storeId),
|
154
|
this.configuration.getFields(mdFormat, mdInterpreation, mdLayout), queryParser, recordInfoGenerator, this.configuration.getIdScheme(),
|
155
|
this.configuration.getIdNamespace(), this.metadataExtractor, this.recordChangeDetector);
|
156
|
store.setMongoSetCollection(mongoSetCollection);
|
157
|
return store;
|
158
|
}
|
159
|
|
160
|
private DBObject createMetadataEntry(final String mdfName, final String mdfInterpretation, final String mdfLayout) {
|
161
|
DBObject info = BasicDBObjectBuilder.start("id", generateStoreId(mdfName, mdfInterpretation, mdfLayout)).append("metadataFormat", mdfName)
|
162
|
.append("interpretation", mdfInterpretation).append("layout", mdfLayout).get();
|
163
|
return info;
|
164
|
|
165
|
}
|
166
|
|
167
|
private String generateStoreId(final String mdfName, final String mdfInterpretation, final String mdfLayout) {
|
168
|
return mdfName + "-" + mdfLayout + "-" + mdfInterpretation;
|
169
|
}
|
170
|
|
171
|
public String getMetadataCollection() {
|
172
|
return metadataCollection;
|
173
|
}
|
174
|
|
175
|
@Required
|
176
|
public void setMetadataCollection(final String metadataCollection) {
|
177
|
this.metadataCollection = metadataCollection;
|
178
|
}
|
179
|
|
180
|
public MongoQueryParser getQueryParser() {
|
181
|
return queryParser;
|
182
|
}
|
183
|
|
184
|
public void setQueryParser(final MongoQueryParser queryParser) {
|
185
|
this.queryParser = queryParser;
|
186
|
}
|
187
|
|
188
|
public OAIConfigurationReader getConfiguration() {
|
189
|
return configuration;
|
190
|
}
|
191
|
|
192
|
public void setConfiguration(final OAIConfigurationReader configuration) {
|
193
|
this.configuration = configuration;
|
194
|
}
|
195
|
|
196
|
public RecordInfoGenerator getRecordInfoGenerator() {
|
197
|
return recordInfoGenerator;
|
198
|
}
|
199
|
|
200
|
public void setRecordInfoGenerator(final RecordInfoGenerator recordInfoGenerator) {
|
201
|
this.recordInfoGenerator = recordInfoGenerator;
|
202
|
}
|
203
|
|
204
|
public MetadataExtractor getMetadataExtractor() {
|
205
|
return metadataExtractor;
|
206
|
}
|
207
|
|
208
|
public void setMetadataExtractor(final MetadataExtractor metadataExtractor) {
|
209
|
this.metadataExtractor = metadataExtractor;
|
210
|
}
|
211
|
|
212
|
public RecordChangeDetector getRecordChangeDetector() {
|
213
|
return recordChangeDetector;
|
214
|
}
|
215
|
|
216
|
public void setRecordChangeDetector(final RecordChangeDetector recordChangeDetector) {
|
217
|
this.recordChangeDetector = recordChangeDetector;
|
218
|
}
|
219
|
|
220
|
public MongoSetCollection getMongoSetCollection() {
|
221
|
return mongoSetCollection;
|
222
|
}
|
223
|
|
224
|
public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
|
225
|
this.mongoSetCollection = mongoSetCollection;
|
226
|
}
|
227
|
|
228
|
public Mongo getPublisherMongoServer() {
|
229
|
return publisherMongoServer;
|
230
|
}
|
231
|
|
232
|
public void setPublisherMongoServer(final Mongo publisherMongoServer) {
|
233
|
this.publisherMongoServer = publisherMongoServer;
|
234
|
}
|
235
|
|
236
|
}
|