-
Rohan Kumar authoredRohan Kumar authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
MongoDBInteraction.java 3.82 KiB
package database;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
import object_detection.types.ObjectSet;
import object_detection.types.Point;
import object_detection.types.PointSet;
import org.bson.Document;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MongoDBInteraction {
private MongoClient mongoClient;
private MongoDatabase database;
private MongoCollection<Document> objectCollection;
public MongoDBInteraction() {
String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true";
// Construct a ServerApi instance using the ServerApi.builder() method
ServerApi serverApi = ServerApi.builder()
.version(ServerApiVersion.V1)
.build();
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(uri))
.serverApi(serverApi)
.build();
this.mongoClient = MongoClients.create(settings);
this.database = mongoClient.getDatabase("Objects");
this.objectCollection = database.getCollection("objectSets");
}
/**
* calls convertDocumentToObjectSet
* @return
*/
public ObjectSet retrieveLatestObjectSet() {
Document doc = objectCollection.find().sort(new Document("index", -1)).first();
return doc != null ? convertDocumentToObjectSet(doc) : null;
}
private ObjectSet convertDocumentToObjectSet(Document doc) {
List<Document> pointSetsDocs = doc.getList("objectSets", Document.class);
ObjectSet objectSet = new ObjectSet();
for (Document pointSetDoc : pointSetsDocs) {
objectSet.objects.add(convertDocumentToPointSet(pointSetDoc));
}
return objectSet;
}
private PointSet convertDocumentToPointSet(Document doc) {
int idx = Integer.parseInt(doc.getString("setId"));
List<Document> pointsDocs = doc.getList("points", Document.class);
PointSet pointSet = new PointSet(idx);
for (Document pointDoc : pointsDocs) {
float x = pointDoc.getDouble("x").floatValue();
float y = pointDoc.getDouble("y").floatValue();
float z = pointDoc.getDouble("z").floatValue();
pointSet.addPoint(new Point(x, y, z, 0));
}
return pointSet;
}
// PointSet management methods
private Document pointSetToDocument(String setId, PointSet pointSet) {
List<Document> pointsList = new ArrayList<>();
for (Point p : pointSet.getPoints()) {
pointsList.add(new Document("x", p.getX())
.append("y", p.getY())
.append("z", p.getZ()));
}
return new Document("setId", setId)
.append("points", pointsList);
}
// ObjectSet management methods
private Document objectSetToDocument(int index, ObjectSet objectSet) {
List<Document> pointSetsList = new ArrayList<>();
for (object_detection.types.PointSet ps : objectSet.objects) {
Document pointSetDoc = pointSetToDocument(Integer.toString(ps.getIDX()), ps);
pointSetsList.add(pointSetDoc);
}
return new Document("index", index)
.append("objectSets", pointSetsList);
}
public void updateObjectSet(int index, ObjectSet objectSet) {
Document doc = objectSetToDocument(index, objectSet);
ReplaceOptions options = new ReplaceOptions().upsert(true);
objectCollection.replaceOne(Filters.eq("index", index), doc, options);
}
}