Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions src/main/java/com/yourssu/Application.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
package com.yourssu;

import com.yourssu.controller.Controller;
import com.yourssu.controller.EntityManager;
import com.yourssu.controller.StudentManager;
import com.yourssu.model.Student;

import java.util.HashMap;
import java.util.Map;

public class Application {
public static void main(String[] args) {
// Controller controller = new BoardGameController();
// controller.run();
final Map<Long, Student> studentDB = new HashMap<>();

Student ducks = new Student(1L, "Ducks", "Computer Science");
Student emin = new Student(2L, "Emin", "Mathematics");
Student citrus = new Student(3L, "Citrus", "Physics");

EntityManager studentManager = new StudentManager(studentDB);

studentManager.persist(ducks);
studentManager.persist(emin);
studentManager.persist(citrus);

System.out.println("before flush - current DB members:");
for (Student student : studentDB.values()) {
System.out.println("ID: " + student.getId());
}

studentManager.flush();

System.out.println("after flush - current DB members:");
for (Student student : studentDB.values()) {
System.out.println("ID: " + student.getId() + ", Name: " + student.getName() + ", Major: " + student.getMajor());
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/yourssu/controller/EntityManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yourssu.controller;

import com.yourssu.model.Student;

public interface EntityManager {
void persist(Student entity);
Student merge(Student entity);

void flush();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yourssu.controller;

public interface SpringDataRepository {
void save(Object entity);
}
58 changes: 58 additions & 0 deletions src/main/java/com/yourssu/controller/StudentManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.yourssu.controller;

import com.yourssu.model.Student;

import java.util.HashMap;
import java.util.Map;

public class StudentManager implements EntityManager{
private final Map<Long, Student> studentCache;
private final Map<Long, Student> studentDB;
private final Map<Long, Boolean> flushFlag;

public StudentManager(Map<Long, Student> studentDB) {
studentCache = new HashMap<>();
flushFlag = new HashMap<>();
this.studentDB = studentDB;
}

@Override
public void persist(Student entity) {
if (!studentCache.containsKey(entity.getId())) {
studentCache.put(entity.getId(), entity);
flushFlag.put(entity.getId(), false);
}
}

@Override
public Student merge(Student entity) {
// cache nonempty
if (studentCache.get(entity.getId()) != null) {
Student cachedStudent = studentCache.get(entity.getId());
flushFlag.put(entity.getId(), false);
return cachedStudent;
}

// cache is empty
if (studentDB.get(entity.getId()) != null) {
Student dbStudent = studentDB.get(entity.getId());
studentCache.put(entity.getId(), dbStudent);
flushFlag.put(entity.getId(), false);
return dbStudent;
}

persist(entity);
return new Student(entity);
}

@Override
public void flush() {
for (Student student : studentCache.values()) {
if (flushFlag.get(student.getId())) {
continue;
}
studentDB.put(student.getId(), student);
flushFlag.put(student.getId(), true);
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/yourssu/model/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.yourssu.model;

public class Student {
private final Long id;
private String name;
private String major;

public Student(Long id, String name, String major) {
this.id = id;
this.name = name;
this.major = major;
}

public Student(Student student) {
this.id = student.id;
this.name = student.name;
this.major = student.major;
}

public boolean equals(Student student) {
return this.id.equals(student.id);
}

public Long getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public void setMajor(String major) {
this.major = major;
}

public String getName() {
return name;
}

public String getMajor() {
return major;
}
}