Last active
July 28, 2021 07:57
-
-
Save mmpataki/39c259b6fea72f94310e1810c23c112e to your computer and use it in GitHub Desktop.
Revisions
-
mmpataki revised this gist
Jul 28, 2021 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ new object Dish{name='idli', originCountry='india', recipe='ask your mom!', updates={}} update an object Dish{name='idli', originCountry='india', recipe='mom is not home, get it from a hotel', updates={recipe=mom is not home, get it from a hotel}} -
mmpataki created this gist
Jul 28, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ /* * Model to be stored */ public class Dish extends Updateable { String name; String originCountry; String recipe; public Dish(String name, String originCountry, String recipe) { this.name = name; this.originCountry = originCountry; this.recipe = recipe; } /* updates */ public void setName(String name) { doUpdate("name", this.name, name); this.name = name; } public void setOriginCountry(String originCountry) { doUpdate("originCountry", this.originCountry, originCountry); this.originCountry = originCountry; } public void setRecipe(String recipe) { doUpdate("recipe", this.recipe, recipe); this.recipe = recipe; } /* getters */ public String getName() { return name; } public String getOriginCountry() { return originCountry; } public String getRecipe() { return recipe; } @Override public String toString() { return "Dish{" + "name='" + name + '\'' + ", originCountry='" + originCountry + '\'' + ", recipe='" + recipe + '\'' + ", updates=" + updates + '}'; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ public class Main { public static void main(String[] args) { UpdateableMongoStore ums = new UpdateableMongoStore(); Dish idli = new Dish("idli", "india", "ask your mom!"); /* save it first time */ ums.save(idli); /* update it */ idli.setRecipe("mom is not home, get it from a hotel"); ums.save(idli); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ import java.util.HashMap; public abstract class Updateable { public HashMap<String, Object> updates = new HashMap<>(); public void put(String field, Object value) { updates.put(field, value); } public boolean isUpdated() { return !updates.isEmpty(); } public HashMap<String, Object> getUpdates() { return updates; } protected void doUpdate(String key, String curVal, String newVal) { if(!curVal.equals(newVal)) updates.put(key, newVal); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ public class UpdateableMongoStore { public void save(Updateable updateable) { if(!updateable.isUpdated()) { System.out.println("new object"); System.out.println(updateable); } else { System.out.println("update an object"); System.out.println(updateable); /* build some update query using `updateable.getUpdates()` */ /* save them to store */ } } /* other store methods */ }