Project

General

Profile

1
package eu.dnetlib.data.objectstore.filesystem;
2

    
3
import com.mongodb.BasicDBObject;
4
import com.mongodb.DBObject;
5
import com.mongodb.client.MongoCollection;
6
import com.mongodb.client.MongoDatabase;
7
import com.mongodb.client.model.IndexOptions;
8
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13

    
14
import static eu.dnetlib.data.objectstore.filesystem.FileSystemObjectStoreDao.OBJECTSTORE_ID_FIELD;
15
import static eu.dnetlib.data.objectstore.filesystem.FileSystemObjectStoreDao.OBJECTSTORE_METADATA_NAME_FIELD;
16

    
17
/**
18
 * Created by claudio on 15/09/16.
19
 */
20
public class IndexIntegrityChecker {
21

    
22
	private static final Log log = LogFactory.getLog(IndexIntegrityChecker.class);
23

    
24
	@Autowired
25
	private ObjectStoreDao dao;
26

    
27
	public void check() {
28
		checkIndexes();
29
	}
30

    
31
	private void checkIndexes() {
32
		log.info("objectStore indexes integrity start");
33

    
34
		final MongoDatabase db = ((FileSystemObjectStoreDao) dao).getDb();
35
		final IndexOptions bg = new IndexOptions().background(true);
36

    
37
		for (String objectStoreId : dao.listObjectStores()) {
38
			final String id = StringUtils.substringBefore(objectStoreId, "_");
39
			final MongoCollection<DBObject> objectStore = db.getCollection(id, DBObject.class);
40
			if (log.isDebugEnabled()) {
41
				log.debug(String.format("creating index (id, timestamp) on objectStore %s", id));
42
			}
43
			objectStore.createIndex(new BasicDBObject("id", 1), bg);
44
			objectStore.createIndex(new BasicDBObject("timestamp", 1), bg);
45
		}
46
		if (log.isDebugEnabled()) {
47
			log.debug(String.format("creating index (%s) on %s", OBJECTSTORE_ID_FIELD, OBJECTSTORE_METADATA_NAME_FIELD));
48
		}
49
		final MongoCollection<DBObject> metadata = db.getCollection(OBJECTSTORE_METADATA_NAME_FIELD, DBObject.class);
50
		metadata.createIndex(new BasicDBObject(OBJECTSTORE_ID_FIELD, 1), bg);
51

    
52
		log.info("objectStore indexes integrity completed");
53
	}
54

    
55
}
(5-5/8)