Revision 42184
Added by Michele Artini about 8 years ago
MongoSetCollection.java | ||
---|---|---|
3 | 3 |
import java.text.Normalizer; |
4 | 4 |
import java.util.List; |
5 | 5 |
|
6 |
import org.apache.commons.lang3.StringEscapeUtils; |
|
7 |
import org.apache.commons.lang3.StringUtils; |
|
8 |
import org.bson.conversions.Bson; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
|
|
6 | 11 |
import com.google.common.base.Function; |
7 | 12 |
import com.google.common.collect.Iterables; |
8 | 13 |
import com.google.common.collect.Lists; |
... | ... | |
16 | 21 |
import com.mongodb.client.model.FindOneAndReplaceOptions; |
17 | 22 |
import com.mongodb.client.model.FindOneAndUpdateOptions; |
18 | 23 |
import com.mongodb.client.model.IndexOptions; |
19 |
import eu.dnetlib.data.information.oai.publisher.info.SetInfo; |
|
20 |
import org.apache.commons.lang.StringEscapeUtils; |
|
21 |
import org.apache.commons.lang.StringUtils; |
|
22 |
import org.bson.conversions.Bson; |
|
23 |
import org.springframework.beans.factory.annotation.Autowired; |
|
24 | 24 |
|
25 |
import eu.dnetlib.oai.info.SetInfo; |
|
26 |
|
|
25 | 27 |
public class MongoSetCollection implements SetCollection { |
26 | 28 |
|
27 | 29 |
public static String DEFAULT_SET = "OTHER"; |
... | ... | |
42 | 44 |
if (!enabledOnly) { |
43 | 45 |
iter = this.getSetsCollection(dbName).find(); |
44 | 46 |
} else { |
45 |
Bson where = Filters.eq("enabled", true); |
|
47 |
final Bson where = Filters.eq("enabled", true);
|
|
46 | 48 |
iter = this.getSetsCollection(dbName).find(where); |
47 | 49 |
} |
48 | 50 |
return Lists.newArrayList(Iterables.transform(iter, new Function<DBObject, SetInfo>() { |
... | ... | |
56 | 58 |
|
57 | 59 |
@Override |
58 | 60 |
public boolean containSet(final String set, final String dbName) { |
59 |
Bson query = Filters.eq("spec", set); |
|
61 |
final Bson query = Filters.eq("spec", set);
|
|
60 | 62 |
return this.getSetsCollection(dbName).count(query) != 0; |
61 | 63 |
} |
62 | 64 |
|
63 | 65 |
@Override |
64 | 66 |
public boolean containEnabledSet(final String set, final String publisherDBName) { |
65 |
Bson query = Filters.and(Filters.eq("spec", set), Filters.eq("enabled", true)); |
|
67 |
final Bson query = Filters.and(Filters.eq("spec", set), Filters.eq("enabled", true));
|
|
66 | 68 |
return this.getSetsCollection(publisherDBName).count(query) != 0; |
67 | 69 |
} |
68 | 70 |
|
69 | 71 |
@Override |
70 | 72 |
public String getSetQuery(final String set, final String dbName) { |
71 |
Bson query = Filters.eq("spec", set); |
|
72 |
BasicDBObject returnField = new BasicDBObject("query", 1); |
|
73 |
DBObject obj = this.getSetsCollection(dbName).find(query).projection(returnField).first(); |
|
73 |
final Bson query = Filters.eq("spec", set);
|
|
74 |
final BasicDBObject returnField = new BasicDBObject("query", 1);
|
|
75 |
final DBObject obj = this.getSetsCollection(dbName).find(query).projection(returnField).first();
|
|
74 | 76 |
return (String) obj.get("query"); |
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
@Override |
78 | 80 |
public int count(final String setSpec, final String mdPrefix, final String dbName) { |
79 |
Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix)); |
|
80 |
BasicDBObject returnField = new BasicDBObject("count", 1); |
|
81 |
DBObject obj = this.getSetsCountCollection(dbName).find(query).projection(returnField).first(); |
|
82 |
if (obj == null) return 0;
|
|
81 |
final Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix));
|
|
82 |
final BasicDBObject returnField = new BasicDBObject("count", 1);
|
|
83 |
final DBObject obj = this.getSetsCountCollection(dbName).find(query).projection(returnField).first();
|
|
84 |
if (obj == null) { return 0; }
|
|
83 | 85 |
return (Integer) obj.get("count"); |
84 | 86 |
} |
85 | 87 |
|
86 | 88 |
public void updateCounts(final String setSpec, final String mdPrefix, final int count, final String dbName) { |
87 |
BasicDBObject countUpdate = new BasicDBObject("$set", new BasicDBObject("count", count)); |
|
88 |
Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix)); |
|
89 |
//DBObject setCount = BasicDBObjectBuilder.start("spec", setSpec).add("mdPrefix", mdPrefix).get(); |
|
90 |
//this.getSetsCountCollection(dbName).update(setCount, new BasicDBObject("$set", new BasicDBObject("count", count)), true, false); |
|
89 |
final BasicDBObject countUpdate = new BasicDBObject("$set", new BasicDBObject("count", count));
|
|
90 |
final Bson query = Filters.and(Filters.eq("spec", setSpec), Filters.eq("mdPrefix", mdPrefix));
|
|
91 |
// DBObject setCount = BasicDBObjectBuilder.start("spec", setSpec).add("mdPrefix", mdPrefix).get();
|
|
92 |
// this.getSetsCountCollection(dbName).update(setCount, new BasicDBObject("$set", new BasicDBObject("count", count)), true, false);
|
|
91 | 93 |
this.getSetsCountCollection(dbName).findOneAndUpdate(query, countUpdate, new FindOneAndUpdateOptions().upsert(true)); |
92 | 94 |
} |
93 | 95 |
|
94 | 96 |
public void upsertSet(final SetInfo setInfo, final boolean fromConfiguration, final String dbName) { |
95 |
DBObject obj = this.getObjectFromSet(setInfo); |
|
97 |
final DBObject obj = this.getObjectFromSet(setInfo);
|
|
96 | 98 |
obj.put("fromConfiguration", fromConfiguration); |
97 |
//this.getSetsCollection(dbName).update(new BasicDBObject("spec", setInfo.getSetSpec()), obj, true, false); |
|
99 |
// this.getSetsCollection(dbName).update(new BasicDBObject("spec", setInfo.getSetSpec()), obj, true, false);
|
|
98 | 100 |
this.getSetsCollection(dbName).findOneAndReplace(Filters.eq("spec", setInfo.getSetSpec()), obj, new FindOneAndReplaceOptions().upsert(true)); |
99 | 101 |
} |
100 | 102 |
|
... | ... | |
119 | 121 |
} |
120 | 122 |
|
121 | 123 |
public List<SetInfo> getConfiguredSets(final String dbName) { |
122 |
Bson query = Filters.eq("fromConfiguration", true); |
|
124 |
final Bson query = Filters.eq("fromConfiguration", true);
|
|
123 | 125 |
return this.findSets(query, dbName); |
124 | 126 |
} |
125 | 127 |
|
126 | 128 |
public List<SetInfo> getSetsFromData(final String dbName) { |
127 |
Bson query = Filters.eq("fromConfiguration", false); |
|
129 |
final Bson query = Filters.eq("fromConfiguration", false);
|
|
128 | 130 |
return this.findSets(query, dbName); |
129 | 131 |
} |
130 | 132 |
|
... | ... | |
134 | 136 |
} |
135 | 137 |
|
136 | 138 |
public void dropSet(final String dbName, final String setSpec) { |
137 |
Bson query = Filters.eq("spec", setSpec); |
|
139 |
final Bson query = Filters.eq("spec", setSpec);
|
|
138 | 140 |
this.getSetsCollection(dbName).deleteMany(query); |
139 | 141 |
this.getSetsCountCollection(dbName).deleteMany(query); |
140 | 142 |
} |
... | ... | |
145 | 147 |
|
146 | 148 |
protected List<SetInfo> findSets(final Bson query, final String dbName) { |
147 | 149 |
final FindIterable<DBObject> sets = this.getSetsCollection(dbName).find(query); |
148 |
List<SetInfo> res = Lists.newArrayList(); |
|
149 |
for (DBObject obj : sets) { |
|
150 |
final List<SetInfo> res = Lists.newArrayList();
|
|
151 |
for (final DBObject obj : sets) {
|
|
150 | 152 |
res.add(this.getSetFromDBObject(obj)); |
151 | 153 |
} |
152 | 154 |
return res; |
153 | 155 |
} |
154 | 156 |
|
155 | 157 |
private SetInfo getSetFromDBObject(final DBObject obj) { |
156 |
SetInfo setInfo = new SetInfo(); |
|
158 |
final SetInfo setInfo = new SetInfo();
|
|
157 | 159 |
setInfo.setEnabled((Boolean) obj.get("enabled")); |
158 | 160 |
setInfo.setQuery((String) obj.get("query")); |
159 | 161 |
setInfo.setSetDescription((String) obj.get("description")); |
... | ... | |
163 | 165 |
} |
164 | 166 |
|
165 | 167 |
private DBObject getObjectFromSet(final SetInfo s) { |
166 |
DBObject obj = BasicDBObjectBuilder.start("spec", s.getSetSpec()).add("name", s.getSetName()).add("description", s.getSetDescription()) |
|
168 |
final DBObject obj = BasicDBObjectBuilder.start("spec", s.getSetSpec()).add("name", s.getSetName()).add("description", s.getSetDescription())
|
|
167 | 169 |
.add("query", s.getQuery()).add("enabled", s.isEnabled()).get(); |
168 | 170 |
return obj; |
169 | 171 |
} |
... | ... | |
174 | 176 |
} |
175 | 177 |
|
176 | 178 |
private void ensureIndexesOnCount(final String dbName) { |
177 |
BasicDBObject index = (BasicDBObject) BasicDBObjectBuilder.start("spec", 1).add("mdPrefix", 1).get(); |
|
179 |
final BasicDBObject index = (BasicDBObject) BasicDBObjectBuilder.start("spec", 1).add("mdPrefix", 1).get();
|
|
178 | 180 |
this.getSetsCountCollection(dbName).createIndex(index, new IndexOptions().background(true)); |
179 | 181 |
} |
180 | 182 |
|
... | ... | |
187 | 189 |
} |
188 | 190 |
|
189 | 191 |
private MongoCollection<DBObject> getCollection(final String collectionName, final String dbName) { |
190 |
return publisherMongoClient.getDatabase(dbName).getCollection(collectionName, DBObject.class); |
|
192 |
return this.publisherMongoClient.getDatabase(dbName).getCollection(collectionName, DBObject.class);
|
|
191 | 193 |
} |
192 | 194 |
|
193 | 195 |
public String getSetCollection() { |
194 |
return setCollection; |
|
196 |
return this.setCollection;
|
|
195 | 197 |
} |
196 | 198 |
|
197 | 199 |
public void setSetCollection(final String setCollection) { |
... | ... | |
199 | 201 |
} |
200 | 202 |
|
201 | 203 |
public String getSetCountCollection() { |
202 |
return setCountCollection; |
|
204 |
return this.setCountCollection;
|
|
203 | 205 |
} |
204 | 206 |
|
205 | 207 |
public void setSetCountCollection(final String setCountCollection) { |
... | ... | |
207 | 209 |
} |
208 | 210 |
|
209 | 211 |
public MongoClient getPublisherMongoClient() { |
210 |
return publisherMongoClient; |
|
212 |
return this.publisherMongoClient;
|
|
211 | 213 |
} |
212 | 214 |
|
213 | 215 |
public void setPublisherMongoClient(final MongoClient publisherMongoClient) { |
Also available in: Unified diff
oai import