Get rid of Auto Increment alltogether.

This commit is contained in:
Olof Larsson 2013-04-12 14:36:49 +02:00
parent 7a358a80d5
commit d2bad2bb20
11 changed files with 20 additions and 227 deletions

View File

@ -14,7 +14,7 @@ public class MCoreConfColl extends Coll<MCoreConf>
public static MCoreConfColl get() { return i; }
private MCoreConfColl()
{
super(MStore.getDb(ConfServer.dburi), MCore.get(), "ai", "mcore_conf", MCoreConf.class, true);
super(MStore.getDb(ConfServer.dburi), MCore.get(), "uuid", "mcore_conf", MCoreConf.class, true);
}
// -------------------------------------------- //

View File

@ -11,7 +11,6 @@ import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import com.massivecraft.mcore.store.idstrategy.IdStrategyAiGson;
import com.massivecraft.mcore.store.idstrategy.IdStrategyOid;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
@ -193,7 +192,6 @@ public class DriverGson extends DriverAbstract<JsonElement>
static
{
instance = new DriverGson();
instance.registerIdStrategy(IdStrategyAiGson.get());
instance.registerIdStrategy(IdStrategyOid.get());
instance.registerIdStrategy(IdStrategyUuid.get());
}

View File

@ -9,7 +9,6 @@ import java.util.Set;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import com.massivecraft.mcore.store.idstrategy.IdStrategyAiMongo;
import com.massivecraft.mcore.store.idstrategy.IdStrategyOid;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
@ -230,7 +229,6 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
static
{
instance = new DriverMongo();
instance.registerIdStrategy(IdStrategyAiMongo.get());
instance.registerIdStrategy(IdStrategyOid.get());
instance.registerIdStrategy(IdStrategyUuid.get());
}

View File

@ -5,19 +5,27 @@ import java.io.FileFilter;
public class JsonFileFilter implements FileFilter
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
private final static String DOTJSON = ".json";
@Override
public boolean accept(File pathname)
{
return pathname.getName().toLowerCase().endsWith(DOTJSON);
}
// -------------------------------------------- //
// INSTANCE
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static JsonFileFilter i = new JsonFileFilter();
public static JsonFileFilter get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean accept(File pathname)
{
return pathname.getName().toLowerCase().endsWith(DOTJSON);
}
}

View File

@ -41,7 +41,7 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
public SenderColl(Db<?> db, Plugin plugin, String name, Class<E> entityClass, boolean creative, boolean lowercasing, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
{
super(db, plugin, "ai", name, entityClass, creative, idComparator, entityComparator);
super(db, plugin, "uuid", name, entityClass, creative, idComparator, entityComparator);
this.lowercasing = lowercasing;
}

View File

@ -2,18 +2,9 @@ package com.massivecraft.mcore.store.idstrategy;
import com.massivecraft.mcore.store.CollInterface;
/**
* The tasks of the IdStrategy:
* 1. Generate a new free id that has not yet been used.
* 2. Convert the id between java object and raw database data.
*
* To complete the tasks the IdStrategy must be tailored for a specific database-driver.
* Thus you will find multiple implementations with the name "ai" (auto increment).
* There must be one implementation per driver.
*/
public interface IdStrategy
{
// The name of the strategy (such as "auto_increment")
// The name of the strategy (such as "uuid")
public String getName();
// Generate

View File

@ -1,37 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import com.massivecraft.mcore.store.CollInterface;
public abstract class IdStrategyAiAbstract extends IdStrategyAbstract
{
//----------------------------------------------//
// CONSTRUCT
//----------------------------------------------//
public IdStrategyAiAbstract()
{
super("ai");
}
// -------------------------------------------- //
// OVERRIDE: IdStrategyAbstract
// -------------------------------------------- //
@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);
}

View File

@ -1,81 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import java.io.File;
import java.io.IOException;
import com.massivecraft.mcore.store.CollInterface;
import com.massivecraft.mcore.store.DbGson;
import com.massivecraft.mcore.util.DiscUtil;
public class IdStrategyAiGson extends IdStrategyAiAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static IdStrategyAiGson i = new IdStrategyAiGson();
public static IdStrategyAiGson get() { return i; }
private IdStrategyAiGson() { super(); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Integer getNextAndUpdate(CollInterface<?> coll)
{
Integer next = this.getNext(coll);
if (next == null) return null;
Integer newNext = next + 1;
if (!this.setNext(coll, newNext)) return null;
return next;
}
@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");
}
private boolean ensureFileExists(File file)
{
if (file.isFile()) return true;
if (file.isDirectory()) return false;
try
{
return file.createNewFile();
}
catch (IOException e)
{
return false;
}
}
}

View File

@ -1,84 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import com.massivecraft.mcore.store.CollInterface;
import com.massivecraft.mcore.store.DbMongo;
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 IdStrategyAiAbstract
{
// -------------------------------------------- //
// 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(); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
// http://dev.bubblemix.net/blog/2011/04/auto-increment-for-mongodb-with-the-java-driver/
@Override
public Integer getNextAndUpdate(CollInterface<?> 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<?> 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", coll.getName()); // where _id = the input sequence name
return query;
}
public static DBObject createUpdateObject()
{
// 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;
}
}

View File

@ -16,7 +16,7 @@ public class AspectColl extends Coll<Aspect>
public static AspectColl get() { return i; }
private AspectColl()
{
super(MCore.get(), "ai", "mcore_aspect", Aspect.class, false);
super(MCore.get(), "uuid", "mcore_aspect", Aspect.class, false);
}
// -------------------------------------------- //

View File

@ -13,7 +13,7 @@ public class MultiverseColl extends Coll<Multiverse>
public static MultiverseColl get() { return i; }
private MultiverseColl()
{
super(MCore.get(), "ai", "mcore_multiverse", Multiverse.class, false);
super(MCore.get(), "uuid", "mcore_multiverse", Multiverse.class, false);
}
// -------------------------------------------- //