diff --git a/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiAbstract.java b/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiAbstract.java new file mode 100644 index 00000000..dce9b557 --- /dev/null +++ b/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiAbstract.java @@ -0,0 +1,40 @@ +package com.massivecraft.mcore.store.idstrategy; + +import com.massivecraft.mcore.store.CollInterface; + +public abstract class IdStrategyAiAbstract extends IdStrategyAbstract +{ + //----------------------------------------------// + // CONSTRUCT + //----------------------------------------------// + + public IdStrategyAiAbstract() + { + super("ai", String.class, String.class); + } + + // -------------------------------------------- // + // OVERRIDE: IdStrategyAbstract + // -------------------------------------------- // + + @Override public String localToRemote(Object local) { return (String)local; } + @Override public String remoteToLocal(Object remote) { return (String)remote; } + + @Override + public String generateAttempt(CollInterface coll) + { + Integer ret = this.getNextAndUpdate(coll); + if (ret == null) return null; + + return ret.toString(); + } + + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public abstract Integer getNextAndUpdate(CollInterface coll); + public abstract Integer getNext(CollInterface coll); + public abstract boolean setNext(CollInterface coll, int next); + +} diff --git a/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiGson.java b/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiGson.java index 6ce3672e..63f7e502 100644 --- a/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiGson.java +++ b/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiGson.java @@ -7,65 +7,64 @@ import com.massivecraft.mcore.store.CollInterface; import com.massivecraft.mcore.store.DbGson; import com.massivecraft.mcore.util.DiscUtil; -public class IdStrategyAiGson extends IdStrategyAbstract +public class IdStrategyAiGson extends IdStrategyAiAbstract { - - //----------------------------------------------// - // CONSTRUCTORS - //----------------------------------------------// - - private IdStrategyAiGson() - { - super("ai", String.class, String.class); - } - // -------------------------------------------- // - // IMPLEMENTATION + // INSTANCE & CONSTRUCT // -------------------------------------------- // - @Override public String localToRemote(Object local) { return (String)local; } - @Override public String remoteToLocal(Object remote) { return (String)remote; } + private static IdStrategyAiGson i = new IdStrategyAiGson(); + public static IdStrategyAiGson get() { return i; } + private IdStrategyAiGson() { super(); } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // @Override - public String generateAttempt(CollInterface coll) + public Integer getNextAndUpdate(CollInterface coll) { - File file = getAiFile(coll); - - // Ensure the file exists - if (this.ensureFileExists(file) == false) - { - return null; - } + Integer next = this.getNext(coll); + if (next == null) return null; - String content = DiscUtil.readCatch(file); - if (content == null) - { - return null; - } - - Integer current = 0; - if (content.length() > 0) - { - current = Integer.valueOf(content); - } - - Integer next = current + 1; - if (DiscUtil.writeCatch(file, next.toString()) == false) - { - return null; - } - - return current.toString(); + Integer newNext = next + 1; + if (!this.setNext(coll, newNext)) return null; + return next; } - protected File getAiFile(CollInterface coll) + @Override + public Integer getNext(CollInterface coll) + { + File file = this.getAiFile(coll); + if (this.ensureFileExists(file) == false) return null; + String content = DiscUtil.readCatch(file); + if (content == null) return null; + Integer current = 0; + if (content.length() > 0) current = Integer.valueOf(content); + return current; + } + + @Override + public boolean setNext(CollInterface coll, int next) + { + File file = this.getAiFile(coll); + if (this.ensureFileExists(file) == false) return false; + if (DiscUtil.writeCatch(file, String.valueOf(next)) == false) return false; + return true; + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + private File getAiFile(CollInterface coll) { DbGson cdb = (DbGson)coll.getDb(); return new File(cdb.dir, coll.getName() + "_ai.txt"); } - protected boolean ensureFileExists(File file) + private boolean ensureFileExists(File file) { if (file.isFile()) return true; if (file.isDirectory()) return false; @@ -79,13 +78,4 @@ public class IdStrategyAiGson extends IdStrategyAbstract } } - // -------------------------------------------- // - // INSTANCE - // -------------------------------------------- // - - protected static IdStrategyAiGson instance = new IdStrategyAiGson(); - public static IdStrategyAiGson get() - { - return instance; - } } diff --git a/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiMongo.java b/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiMongo.java index 6dc13dd9..56466182 100644 --- a/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiMongo.java +++ b/src/com/massivecraft/mcore/store/idstrategy/IdStrategyAiMongo.java @@ -6,62 +6,79 @@ import com.massivecraft.mcore.xlib.mongodb.BasicDBObject; import com.massivecraft.mcore.xlib.mongodb.DBCollection; import com.massivecraft.mcore.xlib.mongodb.DBObject; -public class IdStrategyAiMongo extends IdStrategyAbstract +public class IdStrategyAiMongo extends IdStrategyAiAbstract { - private static String SEC = "seq"; + // -------------------------------------------- // + // CONST + // -------------------------------------------- // + + public static final String SEC_COLL = "seq"; + public static final String SEC_FIELD = "seq"; + + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + protected static IdStrategyAiMongo i = new IdStrategyAiMongo(); + public static IdStrategyAiMongo get() { return i; } + private IdStrategyAiMongo() { super(); } // -------------------------------------------- // - // FIELDS + // OVERRIDE // -------------------------------------------- // - // String sequenseName; - // DBCollection sequenseCollection; - String sequenseField; - - //----------------------------------------------// - // CONSTRUCTORS - //----------------------------------------------// - - private IdStrategyAiMongo() - { - super("ai", String.class, String.class); - this.sequenseField = SEC; - } - - // -------------------------------------------- // - // IMPLEMENTATION - // -------------------------------------------- // - - @Override public String localToRemote(Object local) { return (String)local; } - @Override public String remoteToLocal(Object remote) { return (String)remote; } - // http://dev.bubblemix.net/blog/2011/04/auto-increment-for-mongodb-with-the-java-driver/ + @Override - public String generateAttempt(CollInterface coll) + public Integer getNextAndUpdate(CollInterface coll) { - String sequenseName = coll.getName(); - DBCollection sequenseCollection = ((DbMongo)coll.getDb()).db.getCollection(SEC); + DBCollection dbcoll = this.getSeqColl(coll); + BasicDBObject res = (BasicDBObject) dbcoll.findAndModify(createQueryObject(coll), new BasicDBObject(), new BasicDBObject(), false, createUpdateObject(), true, true); + return res.getInt(SEC_FIELD); + } + + @Override + public Integer getNext(CollInterface coll) + { + DBCollection dbcoll = this.getSeqColl(coll); + BasicDBObject res = (BasicDBObject) dbcoll.findOne(createQueryObject(coll)); + return res.getInt(SEC_FIELD) + 1; + } + + @Override + public boolean setNext(CollInterface coll, int next) + { + throw new RuntimeException("Not implemented yet"); + /*File file = this.getAiFile(coll); + if (this.ensureFileExists(file) == false) return false; + if (DiscUtil.writeCatch(file, String.valueOf(next)) == false) return false; + return true;*/ + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public DBCollection getSeqColl(CollInterface coll) + { + return ((DbMongo)coll.getDb()).db.getCollection(SEC_COLL); + } + + public static DBObject createQueryObject(CollInterface coll) + { // this object represents your "query", its analogous to a WHERE clause in SQL DBObject query = new BasicDBObject(); - query.put("_id", sequenseName); // where _id = the input sequence name - - // this object represents the "update" or the SET blah=blah in SQL - DBObject change = new BasicDBObject(this.sequenseField, 1); - DBObject update = new BasicDBObject("$inc", change); // the $inc here is a mongodb command for increment - - // Atomically updates the sequence field and returns the value for you - DBObject res = sequenseCollection.findAndModify(query, new BasicDBObject(), new BasicDBObject(), false, update, true, true); - return res.get(this.sequenseField).toString(); + query.put("_id", coll.getName()); // where _id = the input sequence name + return query; } - // -------------------------------------------- // - // INSTANCE - // -------------------------------------------- // - - protected static IdStrategyAiMongo instance = new IdStrategyAiMongo(); - public static IdStrategyAiMongo get() + public static DBObject createUpdateObject() { - return instance; + // this object represents the "update" or the SET blah=blah in SQL + DBObject change = new BasicDBObject(SEC_FIELD, 1); + DBObject update = new BasicDBObject("$inc", change); // the $inc here is a mongodb command for increment + return update; } + }