Project

General

Profile

1
package eu.dnetlib.openaire.directindex.api;
2

    
3
import java.io.StringReader;
4
import java.util.Iterator;
5
import java.util.List;
6

    
7
import com.mongodb.*;
8
import com.mongodb.client.model.IndexOptions;
9
import eu.dnetlib.miscutils.datetime.DateUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.dom4j.io.SAXReader;
13
import org.springframework.beans.factory.annotation.Required;
14

    
15
/**
16
 * Created by michele on 11/11/15.
17
 */
18
public class RecentResultsQueue implements Iterable<String> {
19

    
20
	private static final Log log = LogFactory.getLog(RecentResultsQueue.class);
21

    
22
	private DB db;
23
	private String collection;
24

    
25
	public void init() {
26
		if (!db.collectionExists(collection)) {
27
			log.info(String.format("creating collection %s", collection));
28
			db.createCollection(collection, new BasicDBObject());
29
		}
30
		db.getCollection(collection).createIndex(new BasicDBObject("id", 1), new BasicDBObject("background", true));
31
	}
32

    
33
	@Override
34
	public Iterator<String> iterator() {
35

    
36
		final DBCursor cursor = db.getCollection(collection).find();
37

    
38
		return new Iterator<String>() {
39

    
40
			@Override
41
			public boolean hasNext() {
42
				return cursor.hasNext();
43
			}
44

    
45
			@Override
46
			public String next() {
47
				final DBObject obj = cursor.next();
48
				return ((obj != null) && obj.containsField("record")) ? obj.get("record").toString() : "";
49
			}
50

    
51
			@Override
52
			public void remove() {
53
				throw new RuntimeException("NOT IMPLEMENTED");
54
			}
55
		};
56
	}
57

    
58
	synchronized public void add(final String oaf) throws Exception {
59
		final String id = (new SAXReader()).read(new StringReader(oaf)).valueOf("//*[local-name() = 'objIdentifier']");
60

    
61
		log.info("Saving record " + id + " in db: " + db.getName() + ", coll: " + collection);
62

    
63
		final DBCollection coll = db.getCollection(collection);
64
		final DBObject obj = BasicDBObjectBuilder.start()
65
				.append("id", id)
66
				.append("record", oaf)
67
				.append("date", DateUtils.now())
68
				.get();
69
		coll.update(new BasicDBObject("id", id), obj, true, false);
70
	}
71

    
72
	public void remove(final List<String> list) {
73
		final DBCollection coll = db.getCollection(collection);
74
		for (final String id : list) {
75
			coll.remove(new BasicDBObject("id", id));
76
		}
77
	}
78

    
79
	public void remove(final String... ids) {
80
		final DBCollection coll = db.getCollection(collection);
81
		for (final String id : ids) {
82
			coll.remove(new BasicDBObject("id", id));
83
		}
84
	}
85

    
86
	public DB getDb() {
87
		return db;
88
	}
89

    
90
	@Required
91
	public void setDb(final DB db) {
92
		this.db = db;
93
	}
94

    
95
	public String getCollection() {
96
		return collection;
97
	}
98

    
99
	@Required
100
	public void setCollection(final String collection) {
101
		this.collection = collection;
102
	}
103
}
(7-7/8)