Commit effabc6f authored by drnull03's avatar drnull03

Finished the homework

parents
**/target/
mongosh --file RemoveStudentsCollection.js
### Project Structure
ClearDatabase.sh (clear student_db manually although the code clear it before it starts running)
README.md (This File !!!)
RemoveStudentsCollection.js (file used by ClearDatabase.sh can be ignored)
Report (report for this project pdf file)
SpringMongo (java commandline project (not web) using springboot and mongodb)
### Before running make sure mongodb is running using the following command
``` sudo systemctl status mongod ```
check mongodb port
``` nmap localhost -p- ```
or
``` sudo ss --tunlp ```
db = db.getSiblingDB('student_db');
db.students.drop();
{
"snyk.advanced.autoSelectOrganization": true
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mongo</groupId>
<artifactId>SpringMongo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 1. Add Spring Boot Parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- 2. Spring Data MongoDB Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 3. Spring Boot Build Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.mongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StopWatch;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
@SpringBootApplication
public class App implements CommandLineRunner {
@Autowired
private MongoTemplate mongoTemplate;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
public void run(String... args) {
// Configuration
int totalRecords = 2_000_000;
int batchSize = 10_000;
// Clean database before starting
mongoTemplate.dropCollection(Student.class);
// Run Tests
performWriteTest(totalRecords, batchSize);
performReadTest();
}
/**
* Measures batch write performance for 1 million records
*/
private void performWriteTest(int totalRecords, int batchSize) {
System.out.println("\n STARTING WRITE BENCHMARK ");
StopWatch watch = new StopWatch();
watch.start();
for (int i = 0; i < totalRecords / batchSize; i++) {
List<Student> batch = new ArrayList<>();
for (int j = 0; j < batchSize; j++) {
batch.add(new Student("Student_" + (i * batchSize + j), "ID_" + (i * batchSize + j)));
}
//insert here is faster than save() because we don't worry about duplication here
mongoTemplate.insert(batch, Student.class);
}
watch.stop();
System.out.printf("WRITE COMPLETE: %d records in %.2f seconds (%d ops/sec)%n",
totalRecords, watch.getTotalTimeSeconds(), (int)(totalRecords / watch.getTotalTimeSeconds()));
}
/**
* Measures smart read performance (Indexed vs Streaming)
*/
private void performReadTest() {
System.out.println("\n STARTING SMART READ BENCHMARK ");
// We pick a random ID from the middle of the set
String searchId = "ID_543210";
StopWatch pointWatch = new StopWatch();
pointWatch.start();
Query query = new Query(Criteria.where("studentId").is(searchId));
Student found = mongoTemplate.findOne(query, Student.class);
pointWatch.stop();
System.out.println("1. POINT READ (Indexed Search):");
System.out.println(" - Found: " + found);
System.out.println(" - Time: " + pointWatch.getTotalTimeMillis() + " ms");
// Full Stream Read (Testing Retrieval Speed without crashing RAM)
StopWatch streamWatch = new StopWatch();
streamWatch.start();
long count = 0;
// Using Stream ensures we don't load 1M objects into RAM at once
try (Stream<Student> studentStream = mongoTemplate.stream(new Query(), Student.class)) {
count = studentStream.count();
}
streamWatch.stop();
System.out.println("2. STREAM READ (Full Collection Scan):");
System.out.println(" - Processed: " + count + " records");
System.out.println(" - Time: " + streamWatch.getTotalTimeSeconds() + " seconds");
}
}
\ No newline at end of file
package com.mongo;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "students")
public class Student {
@Id
private String id;
private String name;
@Indexed
private String studentId;
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
}
public String getStudentId() { return studentId; }
@Override
public String toString() {
return "Student{name='" + name + "', studentId='" + studentId + "'}";
}
}
\ No newline at end of file
# Connect to localhost on default port and use student_db
spring.data.mongodb.uri=mongodb://localhost:27017/student_db
# Optional: This shows the actual Mongo queries in your console for debugging
#logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
package com.mongo;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for simple App.
*/
public class AppTest {
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment