Browse Source

Optimisation

Arthur Brandao 5 năm trước cách đây
mục cha
commit
5a6405edda

+ 13 - 12
src/db/Table.java

@@ -18,23 +18,23 @@ public abstract class Table<T extends Persistable> {
 	
 	private Map<Long, T> cacheMap = new HashMap<>();
 	
-	public Optional<T> findById(long id) {
+	public T getById(long id) {
 		if (cacheMap.containsKey(id)) {
-			return Optional.of(cacheMap.get(id));
+			return cacheMap.get(id);
 		}
-		Optional<T> obj = getFromDbById(id);
-		if(obj.isPresent()) {
-			cache(obj.get());
+		Optional<T> opt = getFromDbById(id);
+		if(opt.isEmpty()) {
+			return null;
 		}
-		return obj;
+		return cache(opt.get());
 	}
 	
-	public T getById(long id) {
-		Optional<T> opt = findById(id);
-		if(opt.isEmpty()) {
-			return null;
+	public Optional<T> findById(long id) {
+		T obj = getById(id);
+		if (obj == null) {
+			return Optional.empty();
 		}
-		return opt.get();
+		return Optional.of(obj);
 	}
 	
 	public List<T> getByField(String fieldname, Object value) {
@@ -115,7 +115,7 @@ public abstract class Table<T extends Persistable> {
 		delete(obj.getId());
 	}
 	
-	private void cache(T obj) {
+	private T cache(T obj) {
 		if (cacheMap.containsKey(obj.getId())) {
 			cacheMap.replace(obj.getId(), obj);
 		} else {
@@ -123,6 +123,7 @@ public abstract class Table<T extends Persistable> {
 		}
 		//System.out.println("Cache size: " + cacheMap.size());
 		//cacheMap.forEach((key, val) -> System.out.println("Cache: " + key));
+		return obj;
 	}
 	
 	private void remove(long id) {

+ 0 - 9
src/db/table/EcoleTable.java

@@ -17,15 +17,6 @@ public class EcoleTable extends Table<Ecole> {
 	private EcoleTable() {
 		// Private constructor for singleton
 	}
-		
-	@Override
-	public Optional<Ecole> findById(long id) {
-		Optional<Ecole> opt = super.findById(id);
-		if (opt.isEmpty()) {
-			return opt;
-		}
-		return Optional.of(link(opt.get()));
-	}
 
 	@Override
 	public Ecole getById(long id) {

+ 0 - 9
src/db/table/FrequentationTable.java

@@ -15,15 +15,6 @@ public class FrequentationTable extends Table<Frequentation>{
 	private FrequentationTable() {
 		// Private constructor for singleton
 	}
-	
-	@Override
-	public Optional<Frequentation> findById(long id) {
-		Optional<Frequentation> opt = super.findById(id);
-		if (opt.isEmpty()) {
-			return opt;
-		}
-		return Optional.of(link(opt.get()));
-	}
 
 	@Override
 	public Frequentation getById(long id) {

+ 0 - 9
src/db/table/UsagerTable.java

@@ -17,15 +17,6 @@ public class UsagerTable extends Table<Usager> {
 	private UsagerTable() {
 		// Private constructor for singleton
 	}
-	
-	@Override
-	public Optional<Usager> findById(long id) {
-		Optional<Usager> opt = super.findById(id);
-		if (opt.isEmpty()) {
-			return opt;
-		}
-		return Optional.of(link(opt.get()));
-	}
 
 	@Override
 	public Usager getById(long id) {

+ 1 - 1
src/servlets/TestDb.java

@@ -43,7 +43,7 @@ public class TestDb extends HttpServlet {
 		List<Ecole> le = et.getAll();
 		le.forEach(elt -> System.out.println(elt.libelle + " " + elt.ville.libelle + " " + elt.niveau.libelle));
 		
-		Ecole e = et.getById(1);
+		Ecole e = et.findById(1).get();
 		e.libelle = "Lycée " + e.libelle;
 		e.ville.libelle = "Maison";
 		et.save(e);