Some improvements to the auto increment system.
This commit is contained in:
parent
6b4e8e5502
commit
63b3eae101
@ -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);
|
||||||
|
|
||||||
|
}
|
@ -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
|
Integer newNext = next + 1;
|
||||||
if (this.ensureFileExists(file) == false)
|
if (!this.setNext(coll, newNext)) return null;
|
||||||
{
|
|
||||||
return null;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getNext(CollInterface<?, String> coll)
|
||||||
|
{
|
||||||
|
File file = this.getAiFile(coll);
|
||||||
|
if (this.ensureFileExists(file) == false) return null;
|
||||||
String content = DiscUtil.readCatch(file);
|
String content = DiscUtil.readCatch(file);
|
||||||
if (content == null)
|
if (content == null) return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer current = 0;
|
Integer current = 0;
|
||||||
if (content.length() > 0)
|
if (content.length() > 0) current = Integer.valueOf(content);
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setNext(CollInterface<?, String> coll, int next)
|
||||||
{
|
{
|
||||||
current = Integer.valueOf(content);
|
File file = this.getAiFile(coll);
|
||||||
|
if (this.ensureFileExists(file) == false) return false;
|
||||||
|
if (DiscUtil.writeCatch(file, String.valueOf(next)) == false) return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer next = current + 1;
|
// -------------------------------------------- //
|
||||||
if (DiscUtil.writeCatch(file, next.toString()) == false)
|
// UTIL
|
||||||
{
|
// -------------------------------------------- //
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return current.toString();
|
private File getAiFile(CollInterface<?, String> coll)
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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";
|
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// FIELDS
|
// CONST
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
// String sequenseName;
|
public static final String SEC_COLL = "seq";
|
||||||
// DBCollection sequenseCollection;
|
public static final String SEC_FIELD = "seq";
|
||||||
String sequenseField;
|
|
||||||
|
|
||||||
//----------------------------------------------//
|
|
||||||
// CONSTRUCTORS
|
|
||||||
//----------------------------------------------//
|
|
||||||
|
|
||||||
private IdStrategyAiMongo()
|
|
||||||
{
|
|
||||||
super("ai", String.class, String.class);
|
|
||||||
this.sequenseField = SEC;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// IMPLEMENTATION
|
// INSTANCE & CONSTRUCT
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
@Override public String localToRemote(Object local) { return (String)local; }
|
protected static IdStrategyAiMongo i = new IdStrategyAiMongo();
|
||||||
@Override public String remoteToLocal(Object remote) { return (String)remote; }
|
public static IdStrategyAiMongo get() { return i; }
|
||||||
|
private IdStrategyAiMongo() { super(); }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// OVERRIDE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
// 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
|
|
||||||
public String generateAttempt(CollInterface<?, String> coll)
|
|
||||||
{
|
|
||||||
String sequenseName = coll.getName();
|
|
||||||
DBCollection sequenseCollection = ((DbMongo)coll.getDb()).db.getCollection(SEC);
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getNextAndUpdate(CollInterface<?, String> coll)
|
||||||
|
{
|
||||||
|
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<?, 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user