Project

General

Profile

1
package eu.dnetlib.data.oai.store.mongo;
2

    
3
import java.util.List;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Required;
11

    
12
import com.google.common.collect.Lists;
13
import com.mongodb.BasicDBObject;
14
import com.mongodb.BasicDBObjectBuilder;
15
import com.mongodb.DB;
16
import com.mongodb.DBCollection;
17
import com.mongodb.DBCursor;
18
import com.mongodb.DBObject;
19
import com.mongodb.Mongo;
20

    
21
import eu.dnetlib.data.information.oai.publisher.OaiPublisherException;
22
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
23
import eu.dnetlib.data.information.oai.publisher.conf.OAIConfigurationReader;
24
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
25
import eu.dnetlib.data.oai.store.PublisherStoreDAO;
26
import eu.dnetlib.data.oai.store.RecordChangeDetector;
27
import eu.dnetlib.data.oai.store.parser.MongoQueryParser;
28
import eu.dnetlib.data.oai.store.sets.MongoSetCollection;
29
import eu.dnetlib.miscutils.cache.EhCache;
30

    
31
public class MongoPublisherStoreDAO implements PublisherStoreDAO<MongoPublisherStore, MongoCursor> {
32

    
33
	private static final Log log = LogFactory.getLog(MongoPublisherStoreDAO.class); // NOPMD by marko on 11/24/08 5:02 PM
34

    
35
	@Autowired
36
	private Mongo publisherMongoServer;
37

    
38
	/** Name of the collection with information about the OAI stores. **/
39
	private String metadataCollection;
40

    
41
	@Resource
42
	private RecordInfoGenerator recordInfoGenerator;
43
	@Resource
44
	private MetadataExtractor metadataExtractor;
45
	@Resource
46
	private MongoQueryParser queryParser;
47
	@Resource(name = "oaiConfigurationExistReader")
48
	private OAIConfigurationReader configuration;
49
	@Resource
50
	private RecordChangeDetector recordChangeDetector;
51
	@Resource
52
	private MongoSetCollection mongoSetCollection;
53

    
54
	/**
55
	 * Cache for oaistores. Keys are dbName-storeId, values are objects of type MongoPublisherStore.
56
	 */
57
	@Resource
58
	private EhCache<String, MongoPublisherStore> mongoOaistoreCache;
59

    
60
	/**
61
	 * Cache for oaistores. Keys is OAI metadata prefixes, values are objects of type MongoPublisherStore.
62
	 */
63
	@Resource
64
	private EhCache<String, MongoPublisherStore> mongoOaistoreCacheByMdPrefix;
65

    
66
	private boolean alwaysNewRecord;
67

    
68
	protected DB getDB(final String dbName) {
69
		return this.publisherMongoServer.getDB(dbName);
70
	}
71

    
72
	@Override
73
	public List<MongoPublisherStore> listPublisherStores(final String dbName) {
74
		List<MongoPublisherStore> stores = Lists.newArrayList();
75
		DB db = getDB(dbName);
76
		DBCursor cursor = db.getCollection(this.metadataCollection).find();
77
		for (DBObject storeInfo : cursor) {
78
			stores.add(this.createFromDBObject(storeInfo, db));
79
		}
80
		return stores;
81
	}
82

    
83
	@Override
84
	public MongoPublisherStore getStore(final String storeId, final String dbName) {
85
		DBObject storeInfo = getDB(dbName).getCollection(this.metadataCollection).findOne(new BasicDBObject("id", storeId));
86
		return this.createFromDBObject(storeInfo, getDB(dbName));
87
	}
88

    
89
	@Override
90
	public MongoPublisherStore getStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName) {
91
		return this.getStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName);
92
	}
93

    
94
	@Override
95
	public MongoPublisherStore getStoreFor(final String targetMetadataPrefix, final String dbName) {
96
		if (mongoOaistoreCacheByMdPrefix.containsKey(targetMetadataPrefix)) return mongoOaistoreCacheByMdPrefix.get(targetMetadataPrefix);
97
		else {
98
			MDFInfo info = this.configuration.getMetadataFormatInfo(targetMetadataPrefix);
99
			MongoPublisherStore store = this.getStore(info.getSourceFormatName(), info.getSourceFormatInterpretation(), info.getSourceFormatLayout(), dbName);
100
			mongoOaistoreCacheByMdPrefix.put(targetMetadataPrefix, store);
101
			return store;
102
		}
103
	}
104

    
105
	@Override
106
	public MongoPublisherStore createStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName)
107
			throws OaiPublisherException {
108
		DB db = getDB(dbName);
109
		DBObject store = createMetadataEntry(mdfName, mdfInterpretation, mdfLayout);
110
		DBCollection metadata = db.getCollection(this.metadataCollection);
111
		metadata.insert(store);
112
		MongoPublisherStore theStore = this.createFromDBObject(store, db);
113
		return theStore;
114

    
115
	}
116

    
117
	@Override
118
	public boolean deleteStore(final String storeId, final String dbName) {
119
		DB db = getDB(dbName);
120
		DBCollection metadata = db.getCollection(this.metadataCollection);
121
		DBObject storeInfo = metadata.findOne(new BasicDBObject("id", storeId));
122
		if (storeInfo == null) return false;
123
		else {
124
			db.getCollection(storeId).drop();
125
			metadata.remove(storeInfo);
126
			// TODO: should drop entries related to mdPrefix served by the store we are deleting, not all of them.
127
			this.mongoSetCollection.dropOAISets(dbName);
128
			log.debug("Deleted oaistore " + storeId + ", db: " + dbName);
129
			return true;
130
		}
131
	}
132

    
133
	@Override
134
	public boolean deleteFromStore(final String storeId, final String dbName, final String set) {
135
		DB db = getDB(dbName);
136
		DBCollection metadata = db.getCollection(this.metadataCollection);
137
		DBObject storeInfo = metadata.findOne(new BasicDBObject("id", storeId));
138
		if (storeInfo == null) return false;
139
		else {
140
			db.getCollection(storeId).remove(new BasicDBObject(OAIConfigurationReader.SET_FIELD, set));
141
			this.mongoSetCollection.dropSet(dbName, set);
142
			log.debug("Deleted set " + set + " from oaistore " + storeId + ", db: " + dbName);
143
			return true;
144
		}
145
	}
146

    
147
	@Override
148
	public boolean deleteFromStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName, final String set) {
149
		return this.deleteFromStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName, set);
150
	}
151

    
152
	@Override
153
	public boolean deleteStore(final String mdfName, final String mdfInterpretation, final String mdfLayout, final String dbName) {
154
		return this.deleteStore(this.generateStoreId(mdfName, mdfInterpretation, mdfLayout), dbName);
155
	}
156

    
157
	public void ensureIndex(final MongoPublisherStore store) {
158
		if (store == null) throw new OaiPublisherRuntimeException("Can't ensure index on null store");
159
		Thread t = new Thread() {
160

    
161
			@Override
162
			public void run() {
163
				store.ensureIndices();
164
			}
165
		};
166
		t.start();
167
	}
168

    
169
	public void ensureIndex(final String dbName) {
170
		List<MongoPublisherStore> stores = this.listPublisherStores(dbName);
171
		for (final MongoPublisherStore s : stores) {
172
			s.ensureIndices();
173
		}
174

    
175
	}
176

    
177
	private MongoPublisherStore createFromDBObject(final DBObject storeInfo, final DB db) {
178
		if (storeInfo == null) return null;
179
		String storeId = (String) storeInfo.get("id");
180
		String mdFormat = (String) storeInfo.get("metadataFormat");
181
		String mdInterpreation = (String) storeInfo.get("interpretation");
182
		String mdLayout = (String) storeInfo.get("layout");
183
		String k = db.getName() + "-" + storeId;
184
		if (mongoOaistoreCache.containsKey(k)) {
185
			log.debug("Store retreived from cache and alwaysNewRecord is" + alwaysNewRecord);
186
			MongoPublisherStore store = mongoOaistoreCache.get(k);
187
			store.setAlwaysNewRecord(alwaysNewRecord);
188
			return store;
189
		} else {
190
			log.debug("Store retreived, cache miss,  alwaysNewRecord is" + alwaysNewRecord);
191
			log.fatal("Not using cache to create oaistore from dbObject: " + k);
192
			MongoPublisherStore store = new MongoPublisherStore(storeId, mdFormat, mdInterpreation, mdLayout, db.getCollection(storeId),
193
					this.configuration.getFields(mdFormat, mdInterpreation, mdLayout), queryParser, recordInfoGenerator, this.configuration.getIdScheme(),
194
					this.configuration.getIdNamespace(), this.metadataExtractor, this.recordChangeDetector, alwaysNewRecord);
195
			store.setMongoSetCollection(mongoSetCollection);
196
			mongoOaistoreCache.put(k, store);
197
			return store;
198
		}
199
	}
200

    
201
	private DBObject createMetadataEntry(final String mdfName, final String mdfInterpretation, final String mdfLayout) {
202
		DBObject info = BasicDBObjectBuilder.start("id", generateStoreId(mdfName, mdfInterpretation, mdfLayout)).append("metadataFormat", mdfName)
203
				.append("interpretation", mdfInterpretation).append("layout", mdfLayout).get();
204
		return info;
205

    
206
	}
207

    
208
	private String generateStoreId(final String mdfName, final String mdfInterpretation, final String mdfLayout) {
209
		return mdfName + "-" + mdfLayout + "-" + mdfInterpretation;
210
	}
211

    
212
	public String getMetadataCollection() {
213
		return metadataCollection;
214
	}
215

    
216
	@Required
217
	public void setMetadataCollection(final String metadataCollection) {
218
		this.metadataCollection = metadataCollection;
219
	}
220

    
221
	public MongoQueryParser getQueryParser() {
222
		return queryParser;
223
	}
224

    
225
	public void setQueryParser(final MongoQueryParser queryParser) {
226
		this.queryParser = queryParser;
227
	}
228

    
229
	public OAIConfigurationReader getConfiguration() {
230
		return configuration;
231
	}
232

    
233
	public void setConfiguration(final OAIConfigurationReader configuration) {
234
		this.configuration = configuration;
235
	}
236

    
237
	public RecordInfoGenerator getRecordInfoGenerator() {
238
		return recordInfoGenerator;
239
	}
240

    
241
	public void setRecordInfoGenerator(final RecordInfoGenerator recordInfoGenerator) {
242
		this.recordInfoGenerator = recordInfoGenerator;
243
	}
244

    
245
	public MetadataExtractor getMetadataExtractor() {
246
		return metadataExtractor;
247
	}
248

    
249
	public void setMetadataExtractor(final MetadataExtractor metadataExtractor) {
250
		this.metadataExtractor = metadataExtractor;
251
	}
252

    
253
	public RecordChangeDetector getRecordChangeDetector() {
254
		return recordChangeDetector;
255
	}
256

    
257
	public void setRecordChangeDetector(final RecordChangeDetector recordChangeDetector) {
258
		this.recordChangeDetector = recordChangeDetector;
259
	}
260

    
261
	public MongoSetCollection getMongoSetCollection() {
262
		return mongoSetCollection;
263
	}
264

    
265
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
266
		this.mongoSetCollection = mongoSetCollection;
267
	}
268

    
269
	public Mongo getPublisherMongoServer() {
270
		return publisherMongoServer;
271
	}
272

    
273
	public void setPublisherMongoServer(final Mongo publisherMongoServer) {
274
		this.publisherMongoServer = publisherMongoServer;
275
	}
276

    
277
	public boolean isAlwaysNewRecord() {
278
		return alwaysNewRecord;
279
	}
280

    
281
	public void setAlwaysNewRecord(final boolean alwaysNewRecord) {
282
		this.alwaysNewRecord = alwaysNewRecord;
283
	}
284

    
285
}
(4-4/6)