Loading Main.java 0 → 100644 +8 −0 Original line number Diff line number Diff line package org.group8; public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } } No newline at end of file MongoDBConnector.java 0 → 100644 +42 −0 Original line number Diff line number Diff line package org.group8; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import org.bson.Document; import java.util.Arrays; //THIS IS A CONNECTION TEST public class MongoDBConnector { public static void main(String[] args) { // Replace credentials and dbname with actual values final String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; try (MongoClient mongoClient = MongoClients.create(uri)) { // Connect to the MongoDB cluster and access a database MongoDatabase database = mongoClient.getDatabase("myFirstDatabase"); // Access a collection MongoCollection<Document> collection = database.getCollection("testCollection"); // Create a document to insert Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("versions", Arrays.asList("v3.2", "v4.0", "v4.2")) .append("info", new Document("x", 203).append("y", 102)); // Insert the document into the collection collection.insertOne(doc); // Retrieve and print the first document from the collection Document myDoc = collection.find().first(); System.out.println(myDoc.toJson()); } catch (Exception e) { System.err.println("An error occurred: " + e); } } } MongoDBInteraction.java 0 → 100644 +90 −0 Original line number Diff line number Diff line package org.group8; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.Filters; import com.mongodb.client.model.ReplaceOptions; import org.bson.Document; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MongoDBInteraction { 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> collection; private Map<String, PointSet> cache; // In-memory cache public MongoDBInteraction() { this.mongoClient = MongoClients.create(uri); this.database = mongoClient.getDatabase("Objects"); this.collection = database.getCollection("pointSets"); this.cache = new HashMap<>(); } 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); collection.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 = collection.find(query).first(); if (doc != null) { PointSet pointSet = convertDocumentToPointSet(doc); cache.put(setId, pointSet); return pointSet; } return null; } public void delete(String setId) { collection.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(); pointSet.addPoint(new Point(x, y, z)); } return pointSet; } 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"); } } Point.java 0 → 100644 +67 −0 Original line number Diff line number Diff line package org.group8; public class Point { // index static int count = 0; // members private float x; private float y; private float z; final int idx; // METHODS /* Object Constructor of a feature in 3D */ public Point(float xx, float yy, float zz){ this.x = xx; this.y = yy; this.z = zz; this.idx = count++; } /** * helper function for equals overriding * @param aa : first point * @param bb : second point * @param err : error between aa and bb that is acceptable * @return true if points are close enough */ public static boolean equals(Point aa, Point bb, float err){ return (aa.x - bb.x < err) && (aa.y - bb.y < err) && (aa.z - bb.z < err); } @Override public boolean equals(Object o){ if(getClass() != o.getClass()){ return false; } Point p = (Point) o; return Point.equals(this, p, (float) 0.05); } @Override public int hashCode() { float res = this.x + this.y + this.z; return Float.hashCode(res); } @Override public String toString() { return "Point(" + this.x + " ," + this.y + " ," + this.z + ")"; } public float getX() { return x; } public float getY() { return y; } public float getZ() { return z; } } PointSet.java 0 → 100644 +62 −0 Original line number Diff line number Diff line package org.group8; import java.util.*; public class PointSet { static final int NUM_REPS = 10; Set<Point> pset; Point[] reps; /** * * @param pp : the points that this PointSet will contain */ public PointSet(Point ...pp){ pset = new HashSet<>(); // add every point blankly to pointset pset.addAll(Arrays.asList(pp)); reps = new Point[NUM_REPS]; } public void addPoint(Point p){ pset.add(p); } public void addAll(Point ...p) { Collections.addAll(pset, p); } public void addAll(List<Point> p) { pset.addAll(p); } public Point[] getSetReps(){ return this.reps; } public List<Point> getPoints(){ return new ArrayList<>(pset); } /** * This method is called on a specific Point, and updates the Point via * some disjoint sets' method. */ public void updateReps(){ // get first NUM_REPS from pset for now, will make more efficient later Iterator<Point> iter = this.pset.iterator(); for(int i = 0; i < NUM_REPS; i++){ // early exit if we have less than NUM_REPS points in the current object if(i == this.pset.size()) { return; } reps[i] = iter.next(); } } } Loading
Main.java 0 → 100644 +8 −0 Original line number Diff line number Diff line package org.group8; public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } } No newline at end of file
MongoDBConnector.java 0 → 100644 +42 −0 Original line number Diff line number Diff line package org.group8; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import org.bson.Document; import java.util.Arrays; //THIS IS A CONNECTION TEST public class MongoDBConnector { public static void main(String[] args) { // Replace credentials and dbname with actual values final String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; try (MongoClient mongoClient = MongoClients.create(uri)) { // Connect to the MongoDB cluster and access a database MongoDatabase database = mongoClient.getDatabase("myFirstDatabase"); // Access a collection MongoCollection<Document> collection = database.getCollection("testCollection"); // Create a document to insert Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("versions", Arrays.asList("v3.2", "v4.0", "v4.2")) .append("info", new Document("x", 203).append("y", 102)); // Insert the document into the collection collection.insertOne(doc); // Retrieve and print the first document from the collection Document myDoc = collection.find().first(); System.out.println(myDoc.toJson()); } catch (Exception e) { System.err.println("An error occurred: " + e); } } }
MongoDBInteraction.java 0 → 100644 +90 −0 Original line number Diff line number Diff line package org.group8; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.Filters; import com.mongodb.client.model.ReplaceOptions; import org.bson.Document; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MongoDBInteraction { 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> collection; private Map<String, PointSet> cache; // In-memory cache public MongoDBInteraction() { this.mongoClient = MongoClients.create(uri); this.database = mongoClient.getDatabase("Objects"); this.collection = database.getCollection("pointSets"); this.cache = new HashMap<>(); } 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); collection.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 = collection.find(query).first(); if (doc != null) { PointSet pointSet = convertDocumentToPointSet(doc); cache.put(setId, pointSet); return pointSet; } return null; } public void delete(String setId) { collection.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(); pointSet.addPoint(new Point(x, y, z)); } return pointSet; } 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"); } }
Point.java 0 → 100644 +67 −0 Original line number Diff line number Diff line package org.group8; public class Point { // index static int count = 0; // members private float x; private float y; private float z; final int idx; // METHODS /* Object Constructor of a feature in 3D */ public Point(float xx, float yy, float zz){ this.x = xx; this.y = yy; this.z = zz; this.idx = count++; } /** * helper function for equals overriding * @param aa : first point * @param bb : second point * @param err : error between aa and bb that is acceptable * @return true if points are close enough */ public static boolean equals(Point aa, Point bb, float err){ return (aa.x - bb.x < err) && (aa.y - bb.y < err) && (aa.z - bb.z < err); } @Override public boolean equals(Object o){ if(getClass() != o.getClass()){ return false; } Point p = (Point) o; return Point.equals(this, p, (float) 0.05); } @Override public int hashCode() { float res = this.x + this.y + this.z; return Float.hashCode(res); } @Override public String toString() { return "Point(" + this.x + " ," + this.y + " ," + this.z + ")"; } public float getX() { return x; } public float getY() { return y; } public float getZ() { return z; } }
PointSet.java 0 → 100644 +62 −0 Original line number Diff line number Diff line package org.group8; import java.util.*; public class PointSet { static final int NUM_REPS = 10; Set<Point> pset; Point[] reps; /** * * @param pp : the points that this PointSet will contain */ public PointSet(Point ...pp){ pset = new HashSet<>(); // add every point blankly to pointset pset.addAll(Arrays.asList(pp)); reps = new Point[NUM_REPS]; } public void addPoint(Point p){ pset.add(p); } public void addAll(Point ...p) { Collections.addAll(pset, p); } public void addAll(List<Point> p) { pset.addAll(p); } public Point[] getSetReps(){ return this.reps; } public List<Point> getPoints(){ return new ArrayList<>(pset); } /** * This method is called on a specific Point, and updates the Point via * some disjoint sets' method. */ public void updateReps(){ // get first NUM_REPS from pset for now, will make more efficient later Iterator<Point> iter = this.pset.iterator(); for(int i = 0; i < NUM_REPS; i++){ // early exit if we have less than NUM_REPS points in the current object if(i == this.pset.size()) { return; } reps[i] = iter.next(); } } }