Some improvements to the auto increment system.

This commit is contained in:
Olof Larsson 2013-04-12 10:42:58 +02:00
parent 6b4e8e5502
commit 63b3eae101
3 changed files with 142 additions and 95 deletions

View File

@ -0,0 +1,40 @@
package com.massivecraft.mcore.store.idstrategy;
import com.massivecraft.mcore.store.CollInterface;
public abstract class IdStrategyAiAbstract extends IdStrategyAbstract<String, String>
{
//----------------------------------------------//
// 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<?, String> coll)
{
Integer ret = this.getNextAndUpdate(coll);
if (ret == null) return null;
return ret.toString();
}
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract Integer getNextAndUpdate(CollInterface<?, String> coll);
public abstract Integer getNext(CollInterface<?, String> coll);
public abstract boolean setNext(CollInterface<?, String> coll, int next);
}

View File

@ -7,65 +7,64 @@ import com.massivecraft.mcore.store.CollInterface;
import com.massivecraft.mcore.store.DbGson; import com.massivecraft.mcore.store.DbGson;
import com.massivecraft.mcore.util.DiscUtil; import com.massivecraft.mcore.util.DiscUtil;
public class IdStrategyAiGson extends IdStrategyAbstract<String, String> 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; } private static IdStrategyAiGson i = new IdStrategyAiGson();
@Override public String remoteToLocal(Object remote) { return (String)remote; } public static IdStrategyAiGson get() { return i; }
private IdStrategyAiGson() { super(); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override @Override
public String generateAttempt(CollInterface<?, String> coll) public Integer getNextAndUpdate(CollInterface<?, String> coll)
{ {
File file = getAiFile(coll); Integer next = this.getNext(coll);
if (next == null) return null;
// Ensure the file exists
if (this.ensureFileExists(file) == false)
{
return null;
}
String content = DiscUtil.readCatch(file); Integer newNext = next + 1;
if (content == null) if (!this.setNext(coll, newNext)) return 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();
return next;
} }
protected File getAiFile(CollInterface<?, String> coll) @Override
public Integer getNext(CollInterface<?, String> 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<?, String> 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<?, String> coll)
{ {
DbGson cdb = (DbGson)coll.getDb(); DbGson cdb = (DbGson)coll.getDb();
return new File(cdb.dir, coll.getName() + "_ai.txt"); 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.isFile()) return true;
if (file.isDirectory()) return false; if (file.isDirectory()) return false;
@ -79,13 +78,4 @@ public class IdStrategyAiGson extends IdStrategyAbstract<String, String>
} }
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
protected static IdStrategyAiGson instance = new IdStrategyAiGson();
public static IdStrategyAiGson get()
{
return instance;
}
} }

View File

@ -6,62 +6,79 @@ import com.massivecraft.mcore.xlib.mongodb.BasicDBObject;
import com.massivecraft.mcore.xlib.mongodb.DBCollection; import com.massivecraft.mcore.xlib.mongodb.DBCollection;
import com.massivecraft.mcore.xlib.mongodb.DBObject; import com.massivecraft.mcore.xlib.mongodb.DBObject;
public class IdStrategyAiMongo extends IdStrategyAbstract<String, String> 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/ // http://dev.bubblemix.net/blog/2011/04/auto-increment-for-mongodb-with-the-java-driver/
@Override @Override
public String generateAttempt(CollInterface<?, String> coll) public Integer getNextAndUpdate(CollInterface<?, String> coll)
{ {
String sequenseName = coll.getName(); DBCollection dbcoll = this.getSeqColl(coll);
DBCollection sequenseCollection = ((DbMongo)coll.getDb()).db.getCollection(SEC); 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<?, String> coll)
{
DBCollection dbcoll = this.getSeqColl(coll);
BasicDBObject res = (BasicDBObject) dbcoll.findOne(createQueryObject(coll));
return res.getInt(SEC_FIELD) + 1;
}
@Override
public boolean setNext(CollInterface<?, String> 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<?, String> coll)
{
return ((DbMongo)coll.getDb()).db.getCollection(SEC_COLL);
}
public static DBObject createQueryObject(CollInterface<?, String> coll)
{
// this object represents your "query", its analogous to a WHERE clause in SQL // this object represents your "query", its analogous to a WHERE clause in SQL
DBObject query = new BasicDBObject(); DBObject query = new BasicDBObject();
query.put("_id", sequenseName); // where _id = the input sequence name query.put("_id", coll.getName()); // where _id = the input sequence name
return query;
// 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();
} }
// -------------------------------------------- // public static DBObject createUpdateObject()
// INSTANCE
// -------------------------------------------- //
protected static IdStrategyAiMongo instance = new IdStrategyAiMongo();
public static IdStrategyAiMongo get()
{ {
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;
} }
} }