From 0e013fa6e3ca35993339189ed39764f98a5a747f Mon Sep 17 00:00:00 2001 From: sinne10 Date: Sun, 24 Mar 2024 14:34:49 +0100 Subject: [PATCH 1/2] Added overloaded insert method for bulk write --- questionGenerator/src/main/java/Main.java | 2 +- .../repository/QuestionRepository.java | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/questionGenerator/src/main/java/Main.java b/questionGenerator/src/main/java/Main.java index 45e9bc0b..fce9613e 100644 --- a/questionGenerator/src/main/java/Main.java +++ b/questionGenerator/src/main/java/Main.java @@ -36,7 +36,7 @@ private static void run(QuestionGenerator qg, QuestionType type) { for(int i=0; i<3; i++) { Question question = qg.generateQuestion(type); question.setNumber(i); - QuestionRepository.getInstance().insertOne(question.getJSON().toString()); + QuestionRepository.getInstance().insert(question.getJSON().toString()); System.out.println(question.getJSON().toString()); } } diff --git a/questionGenerator/src/main/java/questionGenerator/repository/QuestionRepository.java b/questionGenerator/src/main/java/questionGenerator/repository/QuestionRepository.java index 3aee679d..b9ec14aa 100644 --- a/questionGenerator/src/main/java/questionGenerator/repository/QuestionRepository.java +++ b/questionGenerator/src/main/java/questionGenerator/repository/QuestionRepository.java @@ -4,6 +4,12 @@ import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.InsertManyOptions; +import com.mongodb.client.result.InsertManyResult; + +import java.util.ArrayList; +import java.util.List; + import org.bson.Document; public class QuestionRepository { @@ -22,7 +28,7 @@ public static QuestionRepository getInstance(){ return questionRepository; } - public boolean insertOne(String questionJSON){ + public boolean insert(String questionJSON){ try (MongoClient mongoClient = MongoClients.create(dbConnectionString)) { MongoDatabase database = mongoClient.getDatabase("questions"); @@ -35,5 +41,25 @@ public boolean insertOne(String questionJSON){ return false; } } + + public boolean insert(List questionJSONList){ + try (MongoClient mongoClient = MongoClients.create(dbConnectionString)) { + MongoDatabase database = mongoClient.getDatabase("questions"); + + MongoCollection collection = database.getCollection("questions"); + + List documents = new ArrayList<>(); + for (String questionJSON : questionJSONList) { + documents.add(Document.parse(questionJSON)); + } + + InsertManyResult result = collection.insertMany(documents, new InsertManyOptions().ordered(false)); //unordered write + // Check if all documents were inserted successfully + return result.wasAcknowledged() && result.getInsertedIds().size() == documents.size(); + } catch (Exception e) { + System.out.println(e); + return false; + } + } } \ No newline at end of file From 095aa0ff9a19d9264296afdb5f29dd27deddaa0b Mon Sep 17 00:00:00 2001 From: sinne10 Date: Sun, 24 Mar 2024 14:59:04 +0100 Subject: [PATCH 2/2] Changed Main to use bulk method --- questionGenerator/src/main/java/Main.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/questionGenerator/src/main/java/Main.java b/questionGenerator/src/main/java/Main.java index fce9613e..41a0d999 100644 --- a/questionGenerator/src/main/java/Main.java +++ b/questionGenerator/src/main/java/Main.java @@ -1,5 +1,8 @@ package main.java; +import java.util.ArrayList; +import java.util.List; + import main.java.questionGenerator.QuestionGenerator; import main.java.questionGenerator.question.Question; import main.java.questionGenerator.question.QuestionType; @@ -33,12 +36,17 @@ public static void main(String[] args) { } private static void run(QuestionGenerator qg, QuestionType type) { + List questionJSONList = new ArrayList<>(); + + //Populate JSON list here for(int i=0; i<3; i++) { Question question = qg.generateQuestion(type); question.setNumber(i); - QuestionRepository.getInstance().insert(question.getJSON().toString()); + questionJSONList.add(question.getJSON().toString()); System.out.println(question.getJSON().toString()); } + + QuestionRepository.getInstance().insert(questionJSONList); } }