From 693ad321b4383282959970ee0f7483e99a11fcc4 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Thu, 29 Mar 2018 03:20:34 +0200 Subject: [PATCH] [1.4] Dataloss As A Service. DONE. --- .../java/dst/ass1/doc/impl/DocumentQuery.java | 56 +++++++++++++++++++ .../dst/ass1/doc/impl/DocumentRepository.java | 26 +++++++++ .../ass1/doc/impl/DocumentServiceFactory.java | 7 +-- 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentQuery.java create mode 100644 ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentRepository.java diff --git a/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentQuery.java b/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentQuery.java new file mode 100644 index 0000000..652f608 --- /dev/null +++ b/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentQuery.java @@ -0,0 +1,56 @@ +package dst.ass1.doc.impl; + +import com.mongodb.client.MapReduceIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import dst.ass1.doc.IDocumentQuery; +import dst.ass1.jpa.util.Constants; +import org.bson.Document; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.regex; + +public class DocumentQuery implements IDocumentQuery { + private MongoDatabase db; + + public DocumentQuery(MongoDatabase db) { + this.db = db; + } + + @Override + public Document findByMaterialId(Long materialId) { + MongoCollection coll = db.getCollection(Constants.COLL_MATERIAL_DATA); + return coll.find(eq(Constants.I_MATERIAL, materialId)).first(); + } + + @Override + public List findIdsByType(String typeSubstring) { + MongoCollection coll = db.getCollection(Constants.COLL_MATERIAL_DATA); + List ret = new ArrayList<>(); + for (Document doc : coll.find(regex(Constants.M_MATERIAL_TYPE, ".*" + Pattern.quote(typeSubstring) + ".*"))) { + ret.add(Long.valueOf("" + doc.get(Constants.I_MATERIAL))); + } + + return ret; + } + + @Override + public List getDocumentStatistics() { + MongoCollection coll = db.getCollection(Constants.COLL_MATERIAL_DATA); + + String map = "function(){if(this.type=='quiz')emit(this.quiz_type,this.questions.length);}"; + String reduce = "function(key, values) { " + + "var result = 0;" + + "values.forEach(function(value){result += value;});" + + "return result;}"; + + MapReduceIterable iter = coll.mapReduce(map, reduce); + ArrayList a = iter.into(new ArrayList()); + return a; + } + +} \ No newline at end of file diff --git a/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentRepository.java b/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentRepository.java new file mode 100644 index 0000000..1d9e4eb --- /dev/null +++ b/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentRepository.java @@ -0,0 +1,26 @@ +package dst.ass1.doc.impl; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Indexes; +import dst.ass1.doc.IDocumentRepository; +import dst.ass1.jpa.model.IMaterial; +import dst.ass1.jpa.util.Constants; +import org.bson.Document; + +import java.util.Map; + +public class DocumentRepository implements IDocumentRepository { + // FIXME: THIS IS WEIRD!!! WHY NO CONSTRUCTOR PARAMETER?! + private MongoDatabase db = new MongoClient().getDatabase(Constants.MONGO_DB_NAME); + + @Override + public void insert(IMaterial material, Map materialProperties) { + MongoCollection coll = db.getCollection(Constants.COLL_MATERIAL_DATA); + materialProperties.put(Constants.I_MATERIAL, material.getId()); + materialProperties.put(Constants.M_MATERIAL_TYPE, material.getType()); + coll.insertOne(new Document(materialProperties)); + coll.createIndex(Indexes.text(Constants.I_MATERIAL)); + } +} diff --git a/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentServiceFactory.java b/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentServiceFactory.java index 28f3ba6..30b63ee 100644 --- a/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentServiceFactory.java +++ b/ass1-doc/src/main/java/dst/ass1/doc/impl/DocumentServiceFactory.java @@ -1,7 +1,6 @@ package dst.ass1.doc.impl; import com.mongodb.client.MongoDatabase; - import dst.ass1.doc.IDocumentQuery; import dst.ass1.doc.IDocumentRepository; import dst.ass1.doc.IDocumentServiceFactory; @@ -10,13 +9,11 @@ public class DocumentServiceFactory implements IDocumentServiceFactory { @Override public IDocumentQuery createDocumentQuery(MongoDatabase db) { - // TODO - return null; + return new DocumentQuery(db); } @Override public IDocumentRepository createDocumentRepository() { - // TODO - return null; + return new DocumentRepository(); } } -- 2.43.0