Commit 6cf58666 authored by Zane Safi Mroue's avatar Zane Safi Mroue Committed by Rohan Kumar
Browse files

fixed database

parent 6773b96e
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@
        </plugins>
    </build>


    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
+131 −0
Original line number Diff line number Diff line
package database;

<<<<<<< HEAD
import com.mongodb.client.*;
=======
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
>>>>>>> 82d4ed2 (fixed database)
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
import object_detection.types.ObjectSet;
@@ -14,6 +21,7 @@ import java.util.Map;

public class MongoDBInteraction {

<<<<<<< HEAD
    private MongoClient mongoClient;
    private MongoDatabase database;
    private MongoCollection<Document> objectCollection;
@@ -68,3 +76,126 @@ public class MongoDBInteraction {
        return pointSet;
    }
}
=======
    private static final String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
    private MongoClient mongoClient;
    private MongoDatabase database;
    private MongoCollection<Document> pointCollection;
    private MongoCollection<Document> objectCollection;
    private Map<String, PointSet> cache; // In-memory cache for PointSets

    public MongoDBInteraction() {
        this.mongoClient = MongoClients.create(uri);
        this.database = mongoClient.getDatabase("Objects");
        this.pointCollection = database.getCollection("pointSets");
        this.objectCollection = database.getCollection("objectSets");
        this.cache = new HashMap<>();
    }

    // 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);
    }

    public void update(String setId, PointSet pointSet) {
        Document doc = pointSetToDocument(setId, pointSet);
        ReplaceOptions options = new ReplaceOptions().upsert(true);
        pointCollection.replaceOne(Filters.eq("setId", setId), doc, options);
        cache.put(setId, pointSet);
    }

    public PointSet retrieve(String setId) {
        PointSet cachedPointSet = cache.get(setId);
        if (cachedPointSet != null) {
            return cachedPointSet;
        }
        Document query = new Document("setId", setId);
        Document doc = pointCollection.find(query).first();
        if (doc != null) {
            PointSet pointSet = convertDocumentToPointSet(doc);
            cache.put(setId, pointSet);
            return pointSet;
        }
        return null;
    }

    public void delete(String setId) {
        pointCollection.deleteOne(Filters.eq("setId", setId));
        cache.remove(setId);
    }

    private PointSet convertDocumentToPointSet(Document doc) {
        List<Document> pointsList = (List<Document>) doc.get("points");
        PointSet pointSet = new PointSet();
        for (Document pointDoc : pointsList) {
            float x = pointDoc.getDouble("x").floatValue();
            float y = pointDoc.getDouble("y").floatValue();
            float z = pointDoc.getDouble("z").floatValue();
            int additionalData = pointDoc.getInteger("additionalData", 0); // Assuming 'additionalData' is a field in your document
            pointSet.addPoint(new Point(x, y, z, additionalData));
        }
        return pointSet;
    }


    // ObjectSet management methods
    private Document objectSetToDocument(int index, ObjectSet objectSet) {
        List<Document> pointSetsList = new ArrayList<>();
        int setId = 1;
        for (object_detection.types.PointSet ps : objectSet.objects) {
            Document pointSetDoc = pointSetToDocument(String.valueOf(setId++), 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);
    }

    public ObjectSet retrieveObjectSet(int index) {
        Document query = new Document("index", index);
        Document doc = objectCollection.find(query).first();
        if (doc != null) {
            return convertDocumentToObjectSet(doc);
        }
        return null;
    }

    public void deleteObjectSet(int index) {
        objectCollection.deleteOne(Filters.eq("index", index));
    }

    private ObjectSet convertDocumentToObjectSet(Document doc) {
        List<Document> pointSetsDocs = (List<Document>) doc.get("objectSets");
        ObjectSet objectSet = new ObjectSet();
        for (Document pointSetDoc : pointSetsDocs) {
            PointSet pointSet = convertDocumentToPointSet(pointSetDoc);
            objectSet.objects.add(pointSet);
        }
        return objectSet;
    }
}


//public static void main(String[] args) {
//        MongoDBInteraction dbInteraction = new MongoDBInteraction();
//        PointSet pointSet = new PointSet(new Point(1, 2, 3), new Point(4, 5, 6), new Point(7, 3, 1));
//        dbInteraction.update("set13", pointSet);
//        PointSet retrieved = dbInteraction.retrieve("set13");
//        System.out.println("Retrieved Point Set: " + retrieved.getPoints());
//        dbInteraction.delete("set13");
//    }
//}
>>>>>>> 82d4ed2 (fixed database)
+35 −0
Original line number Diff line number Diff line
#!/bin/bash

# Endpoint URL for updating points
URL="http://localhost:8080/pointset/set123"

# Initialize random seed once
RANDOM_SEED=$(date +%s)

# Loop indefinitely to update points
while true; do
    # Generate random points
    x1=$(awk -v min=0 -v max=10 -v seed=$RANDOM_SEED 'BEGIN{srand(seed); print min+rand()*(max-min)}')
    y1=$(awk -v min=0 -v max=10 -v seed=$RANDOM_SEED 'BEGIN{srand(seed + 1); print min+rand()*(max-min)}')
    z1=$(awk -v min=0 -v max=10 -v seed=$RANDOM_SEED 'BEGIN{srand(seed + 2); print min+rand()*(max-min)}')

    x2=$(awk -v min=0 -v max=10 -v seed=$RANDOM_SEED 'BEGIN{srand(seed + 3); print min+rand()*(max-min)}')
    y2=$(awk -v min=0 -v max=10 -v seed=$RANDOM_SEED 'BEGIN{srand(seed + 4); print min+rand()*(max-min)}')
    z2=$(awk -v min=0 -v max=10 -v seed=$RANDOM_SEED 'BEGIN{srand(seed + 5); print min+rand()*(max-min)}')

    # Create JSON payload
    JSON_PAYLOAD="{\"points\":[{\"x\":$x1,\"y\":$y1,\"z\":$z1},{\"x\":$x2,\"y\":$y2,\"z\":$z2}]}"

    # Send POST request to update the point set
    response=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Content-Type: application/json" -d "$JSON_PAYLOAD" $URL)

    # Check if the POST was successful
    if [[ "$response" -ne 200 ]] ; then
        echo "Failed to update points, server responded with status: $response"
        break  # or handle error differently
    fi

    # Wait for a bit before the next update
    sleep 5
done
+8 −0
Original line number Diff line number Diff line
@@ -6,12 +6,20 @@ import java.util.*;

public class ObjectSet {

<<<<<<< HEAD
<<<<<<< HEAD
    public static List<PointSet> objects;
    int count = 0;
=======
    public List<PointSet> objects;
>>>>>>> 9d18406 (initial attempt)
=======
    public List<PointSet> objects;
=======
    public static List<PointSet> objects;
    int count = 0;
>>>>>>> b953275 (fixed database)
>>>>>>> 82d4ed2 (fixed database)
    public ObjectSet(){
        objects = new ArrayList<>();
    }
+4 −0
Original line number Diff line number Diff line
@@ -16,7 +16,11 @@ public class Point {
    /*
    Object Constructor of a feature in 3D
     */
<<<<<<< HEAD
    public Point(float xx, float yy, float zz){
=======
    public Point(float xx, float yy, float zz, int i){
>>>>>>> 82d4ed2 (fixed database)
        this.x = xx;
        this.y = yy;
        this.z = zz;
Loading