Remove stuff I never use and make the database system GSON centric.

This commit is contained in:
Olof Larsson 2013-05-01 14:06:47 +02:00
parent f6206d0392
commit 00304be8a2
22 changed files with 117 additions and 410 deletions

View File

@ -90,7 +90,7 @@ permissions:
mcore.kit.rank0: mcore.kit.rank0:
default: false default: false
children: children:
massivechat.kit.default: mcore.kit.default:
default: true default: true
children: children:
massivechat.kit.rank0: true mcore.kit.rank0: true

View File

@ -18,9 +18,8 @@ import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.MPlugin; import com.massivecraft.mcore.MPlugin;
import com.massivecraft.mcore.Predictate; import com.massivecraft.mcore.Predictate;
import com.massivecraft.mcore.store.accessor.Accessor; import com.massivecraft.mcore.store.accessor.Accessor;
import com.massivecraft.mcore.store.idstrategy.IdStrategy;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
import com.massivecraft.mcore.xlib.gson.Gson; import com.massivecraft.mcore.xlib.gson.Gson;
import com.massivecraft.mcore.xlib.gson.JsonElement;
public class Coll<E> implements CollInterface<E> public class Coll<E> implements CollInterface<E>
{ {
@ -64,15 +63,9 @@ public class Coll<E> implements CollInterface<E>
} }
} }
protected Db<?> db; protected Db db;
@Override public Db<?> getDb() { return this.db; } @Override public Db getDb() { return this.db; }
@Override public Driver<?> getDriver() { return this.db.getDriver(); } @Override public Driver getDriver() { return this.db.getDriver(); }
protected IdStrategy idStrategy;
@Override public IdStrategy getIdStrategy() { return this.idStrategy; }
protected StoreAdapter storeAdapter;
@Override public StoreAdapter getStoreAdapter() { return this.storeAdapter; }
protected Object collDriverObject; protected Object collDriverObject;
@Override public Object getCollDriverObject() { return this.collDriverObject; } @Override public Object getCollDriverObject() { return this.collDriverObject; }
@ -260,7 +253,7 @@ public class Coll<E> implements CollInterface<E>
// Check/Fix id // Check/Fix id
if (oid == null) if (oid == null)
{ {
id = this.getIdStrategy().generate(this); id = MStore.createId();
} }
else else
{ {
@ -333,7 +326,7 @@ public class Coll<E> implements CollInterface<E>
// -------------------------------------------- // // -------------------------------------------- //
protected Map<String, Long> lastMtime; protected Map<String, Long> lastMtime;
protected Map<String, Object> lastRaw; protected Map<String, JsonElement> lastRaw;
protected Set<String> lastDefault; protected Set<String> lastDefault;
protected synchronized void clearSynclog(Object oid) protected synchronized void clearSynclog(Object oid)
@ -393,7 +386,7 @@ public class Coll<E> implements CollInterface<E>
E entity = this.id2entity.get(id); E entity = this.id2entity.get(id);
if (entity == null) return; if (entity == null) return;
Object raw = this.getStoreAdapter().read(this, entity); JsonElement raw = this.getGson().toJsonTree(entity, this.getEntityClass());
this.lastRaw.put(id, raw); this.lastRaw.put(id, raw);
if (this.isDefault(entity)) if (this.isDefault(entity))
@ -416,10 +409,10 @@ public class Coll<E> implements CollInterface<E>
this.clearIdentifiedChanges(id); this.clearIdentifiedChanges(id);
Entry<?, Long> entry = this.getDb().getDriver().load(this, id); Entry<JsonElement, Long> entry = this.getDriver().load(this, id);
if (entry == null) return; if (entry == null) return;
Object raw = entry.getKey(); JsonElement raw = entry.getKey();
if (raw == null) return; if (raw == null) return;
Long mtime = entry.getValue(); Long mtime = entry.getValue();
@ -427,7 +420,7 @@ public class Coll<E> implements CollInterface<E>
E entity = this.get(id, true, false); E entity = this.get(id, true, false);
this.getStoreAdapter().write(this, raw, entity); this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity);
// this.lastRaw.put(id, this.getStoreAdapter().read(this, entity)); // this.lastRaw.put(id, this.getStoreAdapter().read(this, entity));
// Store adapter again since result of a database read may be "different" from entity read. // Store adapter again since result of a database read may be "different" from entity read.
@ -500,11 +493,13 @@ public class Coll<E> implements CollInterface<E>
return ModificationState.NONE; return ModificationState.NONE;
} }
protected boolean examineHasLocalAlter(String id, E entity) protected boolean examineHasLocalAlter(String id, E entity)
{ {
Object lastRaw = this.lastRaw.get(id); JsonElement lastRaw = this.lastRaw.get(id);
Object currentRaw = this.storeAdapter.read(this, entity); JsonElement currentRaw = this.getGson().toJsonTree(entity, this.getEntityClass());
return (this.getDriver().equal(currentRaw, lastRaw) == false);
return !MStore.equal(lastRaw, currentRaw);
} }
@Override @Override
@ -602,7 +597,7 @@ public class Coll<E> implements CollInterface<E>
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public Coll(String name, Class<E> entityClass, Db<?> db, Plugin plugin, boolean creative, boolean lowercasing, String idStrategyName, Comparator<? super String> idComparator, Comparator<? super E> entityComparator) public Coll(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing, String idStrategyName, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
{ {
// Setup the name and the parsed parts // Setup the name and the parsed parts
this.name = name; this.name = name;
@ -625,12 +620,6 @@ public class Coll<E> implements CollInterface<E>
// SUPPORTING SYSTEM // SUPPORTING SYSTEM
this.plugin = plugin; this.plugin = plugin;
this.db = db; this.db = db;
this.storeAdapter = this.db.getDriver().getStoreAdapter();
this.idStrategy = this.db.getDriver().getIdStrategy(idStrategyName);
if (this.idStrategy == null)
{
throw new IllegalArgumentException("UNKNOWN: The id stragegy \""+idStrategyName+"\" is unknown to the driver \""+db.getDriver().getName()+"\".");
}
this.collDriverObject = db.getCollDriverObject(this); this.collDriverObject = db.getCollDriverObject(this);
// STORAGE // STORAGE
@ -644,7 +633,7 @@ public class Coll<E> implements CollInterface<E>
// SYNCLOG // SYNCLOG
this.lastMtime = new ConcurrentSkipListMap<String, Long>(idComparator); this.lastMtime = new ConcurrentSkipListMap<String, Long>(idComparator);
this.lastRaw = new ConcurrentSkipListMap<String, Object>(idComparator); this.lastRaw = new ConcurrentSkipListMap<String, JsonElement>(idComparator);
this.lastDefault = new ConcurrentSkipListSet<String>(idComparator); this.lastDefault = new ConcurrentSkipListSet<String>(idComparator);
final Coll<E> me = this; final Coll<E> me = this;
@ -654,12 +643,12 @@ public class Coll<E> implements CollInterface<E>
}; };
} }
public Coll(String name, Class<E> entityClass, Db<?> db, Plugin plugin, boolean creative, boolean lowercasing) public Coll(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing)
{ {
this(name, entityClass, db, plugin, creative, lowercasing, "uuid", null, null); this(name, entityClass, db, plugin, creative, lowercasing, "uuid", null, null);
} }
public Coll(String name, Class<E> entityClass, Db<?> db, Plugin plugin) public Coll(String name, Class<E> entityClass, Db db, Plugin plugin)
{ {
this(name, entityClass, db, plugin, false, false); this(name, entityClass, db, plugin, false, false);
} }

View File

@ -7,8 +7,6 @@ import java.util.Map;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.massivecraft.mcore.Predictate; import com.massivecraft.mcore.Predictate;
import com.massivecraft.mcore.store.idstrategy.IdStrategy;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
public interface CollInterface<E> public interface CollInterface<E>
{ {
@ -25,10 +23,8 @@ public interface CollInterface<E>
// -------------------------------------------- // // -------------------------------------------- //
public Plugin getPlugin(); public Plugin getPlugin();
public Db<?> getDb(); public Db getDb();
public Driver<?> getDriver(); public Driver getDriver();
public StoreAdapter getStoreAdapter();
public IdStrategy getIdStrategy();
public Object getCollDriverObject(); public Object getCollDriverObject();
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore.store;
import java.util.Set; import java.util.Set;
public interface Db<R> public interface Db
{ {
public String getName(); public String getName();
@ -10,7 +10,7 @@ public interface Db<R>
public Set<String> getCollnames(); public Set<String> getCollnames();
public Driver<R> getDriver(); public Driver getDriver();
public Object getCollDriverObject(Coll<?> coll); public Object getCollDriverObject(Coll<?> coll);
} }

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore.store;
import java.util.Set; import java.util.Set;
public abstract class DbAbstract<R> implements Db<R> public abstract class DbAbstract implements Db
{ {
@Override @Override
public Set<String> getCollnames() public Set<String> getCollnames()

View File

@ -3,9 +3,8 @@ package com.massivecraft.mcore.store;
import java.io.File; import java.io.File;
import com.massivecraft.mcore.util.DiscUtil; import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.xlib.gson.JsonElement;
public class DbGson extends DbAbstract<JsonElement> public class DbGson extends DbAbstract
{ {
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS

View File

@ -1,9 +1,8 @@
package com.massivecraft.mcore.store; package com.massivecraft.mcore.store;
import com.massivecraft.mcore.xlib.mongodb.BasicDBObject;
import com.massivecraft.mcore.xlib.mongodb.DB; import com.massivecraft.mcore.xlib.mongodb.DB;
public class DbMongo extends DbAbstract<BasicDBObject> public class DbMongo extends DbAbstract
{ {
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS

View File

@ -5,33 +5,18 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import com.massivecraft.mcore.store.idstrategy.IdStrategy; import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
public interface Driver<R> public interface Driver
{ {
// Returns the name of the driver. // Returns the name of the driver.
public String getName(); public String getName();
// This is the rawdata format this driver works with.
// Could for example be JsonElement or DBObject
public Class<R> getRawdataClass();
// Comparison of raw data should be done through this method
public boolean equal(Object rawOne, Object rawTwo);
// This is some sort of database specific id strategy with built in adapter
public boolean registerIdStrategy(IdStrategy idStrategy);
public IdStrategy getIdStrategy(String idStrategyName);
// Get the default store adapter for the driver.
public StoreAdapter getStoreAdapter();
// Get a database instance from the driver // Get a database instance from the driver
public Db<R> getDb(String uri); public Db getDb(String uri);
// What collections are in the database? // What collections are in the database?
public Set<String> getCollnames(Db<?> db); public Set<String> getCollnames(Db db);
// Is id X in the collection? // Is id X in the collection?
public boolean containsId(Coll<?> coll, String id); public boolean containsId(Coll<?> coll, String id);
@ -46,12 +31,12 @@ public interface Driver<R>
public Map<String, Long> getId2mtime(Coll<?> coll); public Map<String, Long> getId2mtime(Coll<?> coll);
// Load the raw data for X. The second part of the entry is the remote mtime at the load. // Load the raw data for X. The second part of the entry is the remote mtime at the load.
public Entry<R, Long> load(Coll<?> coll, String id); public Entry<JsonElement, Long> load(Coll<?> coll, String id);
// Save raw data as X // Save raw data as X
// Return value is the new mtime (we caused the change). // Return value is the new mtime (we caused the change).
// If the mtime is null something failed. // If the mtime is null something failed.
public Long save(Coll<?> coll, String id, final Object rawData); public Long save(Coll<?> coll, String id, JsonElement data);
// Delete X // Delete X
public void delete(Coll<?> coll, String id); public void delete(Coll<?> coll, String id);

View File

@ -1,32 +1,21 @@
package com.massivecraft.mcore.store; package com.massivecraft.mcore.store;
import java.util.HashMap; public abstract class DriverAbstract implements Driver
import java.util.Map;
import com.massivecraft.mcore.store.idstrategy.IdStrategy;
public abstract class DriverAbstract<R> implements Driver<R>
{ {
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String name;
@Override public String getName() { return this.name; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public DriverAbstract(String name) public DriverAbstract(String name)
{ {
this.name = name; this.name = name;
} }
protected String name;
@Override public String getName() { return this.name; }
protected Map<String, IdStrategy> idStrategies = new HashMap<String, IdStrategy>();
@Override
public boolean registerIdStrategy(IdStrategy idStrategy)
{
if (idStrategies.containsKey(idStrategy.getName())) return false;
idStrategies.put(idStrategy.getName(), idStrategy);
return true;
}
@Override
public IdStrategy getIdStrategy(String idStrategyName)
{
return idStrategies.get(idStrategyName);
}
} }

View File

@ -11,44 +11,33 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.massivecraft.mcore.store.idstrategy.IdStrategyOid;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
import com.massivecraft.mcore.store.storeadapter.StoreAdapterGson;
import com.massivecraft.mcore.util.DiscUtil; import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.xlib.gson.JsonElement; import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonParser; import com.massivecraft.mcore.xlib.gson.JsonParser;
public class DriverGson extends DriverAbstract<JsonElement> public class DriverGson extends DriverAbstract
{ {
protected final static String DOTJSON = ".json";
// -------------------------------------------- // // -------------------------------------------- //
// IMPLEMENTATION // CONSTANTS
// -------------------------------------------- // // -------------------------------------------- //
@Override public Class<JsonElement> getRawdataClass() { return JsonElement.class; } private static final String DOTJSON = ".json";
public static final String NAME = "gson";
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static DriverGson i = new DriverGson();
public static DriverGson get() { return i; }
private DriverGson() { super(NAME); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override @Override
public boolean equal(Object rawOne, Object rawTwo) public Db getDb(String uri)
{
JsonElement one = (JsonElement)rawOne;
JsonElement two = (JsonElement)rawTwo;
if (one == null && two == null) return true;
if (one == null || two == null) return false;
return one.toString().equals(two.toString());
}
@Override
public StoreAdapter getStoreAdapter()
{
return StoreAdapterGson.get();
}
@Override
public Db<JsonElement> getDb(String uri)
{ {
// "gson://" is 7 chars // "gson://" is 7 chars
File folder = new File(uri.substring(7)); File folder = new File(uri.substring(7));
@ -57,7 +46,7 @@ public class DriverGson extends DriverAbstract<JsonElement>
} }
@Override @Override
public Set<String> getCollnames(Db<?> db) public Set<String> getCollnames(Db db)
{ {
Set<String> ret = new LinkedHashSet<String>(); Set<String> ret = new LinkedHashSet<String>();
@ -130,10 +119,10 @@ public class DriverGson extends DriverAbstract<JsonElement>
} }
@Override @Override
public Long save(Coll<?> coll, String id, Object rawData) public Long save(Coll<?> coll, String id, JsonElement data)
{ {
File file = fileFromId(coll, id); File file = fileFromId(coll, id);
String content = coll.getGson().toJson((JsonElement)rawData); String content = coll.getGson().toJson(data);
if (DiscUtil.writeCatch(file, content) == false) return null; if (DiscUtil.writeCatch(file, content) == false) return null;
return file.lastModified(); return file.lastModified();
} }
@ -168,32 +157,5 @@ public class DriverGson extends DriverAbstract<JsonElement>
return idFile; return idFile;
} }
//----------------------------------------------//
// CONSTRUCTORS
//----------------------------------------------//
public static String NAME = "gson";
private DriverGson()
{
super(NAME);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
protected static DriverGson instance;
public static DriverGson get()
{
return instance;
}
static
{
instance = new DriverGson();
instance.registerIdStrategy(IdStrategyOid.get());
instance.registerIdStrategy(IdStrategyUuid.get());
}
} }

View File

@ -9,10 +9,7 @@ import java.util.Set;
import java.util.AbstractMap.SimpleEntry; import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.massivecraft.mcore.store.idstrategy.IdStrategyOid; import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.store.idstrategy.IdStrategyUuid;
import com.massivecraft.mcore.store.storeadapter.StoreAdapter;
import com.massivecraft.mcore.store.storeadapter.StoreAdapterMongo;
import com.massivecraft.mcore.xlib.mongodb.BasicDBObject; import com.massivecraft.mcore.xlib.mongodb.BasicDBObject;
import com.massivecraft.mcore.xlib.mongodb.DB; import com.massivecraft.mcore.xlib.mongodb.DB;
import com.massivecraft.mcore.xlib.mongodb.DBCollection; import com.massivecraft.mcore.xlib.mongodb.DBCollection;
@ -20,7 +17,7 @@ import com.massivecraft.mcore.xlib.mongodb.DBCursor;
import com.massivecraft.mcore.xlib.mongodb.MongoClient; import com.massivecraft.mcore.xlib.mongodb.MongoClient;
import com.massivecraft.mcore.xlib.mongodb.MongoClientURI; import com.massivecraft.mcore.xlib.mongodb.MongoClientURI;
public class DriverMongo extends DriverAbstract<BasicDBObject> public class DriverMongo extends DriverAbstract
{ {
// -------------------------------------------- // // -------------------------------------------- //
// CONSTANTS // CONSTANTS
@ -34,66 +31,27 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
public final static BasicDBObject dboKeysMtime = new BasicDBObject().append(MTIME_FIELD, 1); public final static BasicDBObject dboKeysMtime = new BasicDBObject().append(MTIME_FIELD, 1);
public final static BasicDBObject dboKeysIdandMtime = new BasicDBObject().append(ID_FIELD, 1).append(MTIME_FIELD, 1); public final static BasicDBObject dboKeysIdandMtime = new BasicDBObject().append(ID_FIELD, 1).append(MTIME_FIELD, 1);
//----------------------------------------------//
// CONSTRUCT
//----------------------------------------------//
private DriverMongo()
{
super("mongodb");
}
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
protected static DriverMongo instance; protected static DriverMongo i = new DriverMongo();
public static DriverMongo get() public static DriverMongo get() { return i; }
{ private DriverMongo() { super("mongodb"); }
return instance;
}
static
{
instance = new DriverMongo();
instance.registerIdStrategy(IdStrategyOid.get());
instance.registerIdStrategy(IdStrategyUuid.get());
}
// -------------------------------------------- // // -------------------------------------------- //
// IMPLEMENTATION // IMPLEMENTATION
// -------------------------------------------- // // -------------------------------------------- //
@Override public Class<BasicDBObject> getRawdataClass() { return BasicDBObject.class; }
@Override @Override
public boolean equal(Object rawOne, Object rawTwo) public Db getDb(String uri)
{
BasicDBObject one = (BasicDBObject)rawOne;
BasicDBObject two = (BasicDBObject)rawTwo;
if (one == null && two == null) return true;
if (one == null || two == null) return false;
return one.equals(two);
}
@Override
public StoreAdapter getStoreAdapter()
{
return StoreAdapterMongo.get();
}
@Override
public Db<BasicDBObject> getDb(String uri)
{ {
DB db = this.getDbInner(uri); DB db = this.getDbInner(uri);
return new DbMongo(this, db); return new DbMongo(this, db);
} }
@Override @Override
public Set<String> getCollnames(Db<?> db) public Set<String> getCollnames(Db db)
{ {
return ((DbMongo)db).db.getCollectionNames(); return ((DbMongo)db).db.getCollectionNames();
} }
@ -170,27 +128,28 @@ public class DriverMongo extends DriverAbstract<BasicDBObject>
} }
@Override @Override
public Entry<BasicDBObject, Long> load(Coll<?> coll, String id) public Entry<JsonElement, Long> load(Coll<?> coll, String id)
{ {
DBCollection dbcoll = fixColl(coll); DBCollection dbcoll = fixColl(coll);
BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, id)); BasicDBObject raw = (BasicDBObject)dbcoll.findOne(new BasicDBObject(ID_FIELD, id));
if (raw == null) return null; if (raw == null) return null;
Long mtime = (Long) raw.removeField(MTIME_FIELD); Long mtime = (Long) raw.removeField(MTIME_FIELD);
return new SimpleEntry<BasicDBObject, Long>(raw, mtime);
JsonElement element = MongoGsonConverter.mongo2GsonObject(raw);
return new SimpleEntry<JsonElement, Long>(element, mtime);
} }
@Override @Override
public Long save(Coll<?> coll, String id, Object rawData) public Long save(Coll<?> coll, String id, JsonElement data)
{ {
DBCollection dbcoll = fixColl(coll); DBCollection dbcoll = fixColl(coll);
// We shallow copy here in order to stop the extra "_mtime" field from messing up the lastRaw. BasicDBObject dbo = MongoGsonConverter.gson2MongoObject(data);
BasicDBObject data = (BasicDBObject)rawData;
data = (BasicDBObject)data.clone();
Long mtime = System.currentTimeMillis(); Long mtime = System.currentTimeMillis();
data.put(MTIME_FIELD, mtime); dbo.put(MTIME_FIELD, mtime);
dbcoll.update(new BasicDBObject(ID_FIELD, id), data, true, false); dbcoll.update(new BasicDBObject(ID_FIELD, id), dbo, true, false);
return mtime; return mtime;
} }

View File

@ -4,8 +4,10 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import com.massivecraft.mcore.ConfServer; import com.massivecraft.mcore.ConfServer;
import com.massivecraft.mcore.xlib.gson.JsonElement;
public class MStore public class MStore
{ {
@ -13,15 +15,15 @@ public class MStore
// DRIVER REGISTRY // DRIVER REGISTRY
// -------------------------------------------- // // -------------------------------------------- //
protected static Map<String, Driver<?>> drivers = new HashMap<String, Driver<?>>(); private static Map<String, Driver> drivers = new HashMap<String, Driver>();
public static boolean registerDriver(Driver<?> driver) public static boolean registerDriver(Driver driver)
{ {
if (drivers.containsKey(driver.getName())) return false; if (drivers.containsKey(driver.getName())) return false;
drivers.put(driver.getName(), driver); drivers.put(driver.getName(), driver);
return true; return true;
} }
public static Driver<?> getDriver(String id) public static Driver getDriver(String id)
{ {
return drivers.get(id); return drivers.get(id);
} }
@ -32,12 +34,33 @@ public class MStore
registerDriver(DriverGson.get()); registerDriver(DriverGson.get());
} }
// -------------------------------------------- //
// ID CREATION
// -------------------------------------------- //
public static String createId()
{
return UUID.randomUUID().toString();
}
// -------------------------------------------- //
// JSON ELEMENT EQUAL
// -------------------------------------------- //
public static boolean equal(JsonElement one, JsonElement two)
{
if (one == null) return two == null;
if (two == null) return one == null;
return one.toString().equals(two.toString());
}
// -------------------------------------------- // // -------------------------------------------- //
// FROODLSCHTEIN // FROODLSCHTEIN
// -------------------------------------------- // // -------------------------------------------- //
// We cache databases here // We cache databases here
private static Map<String, Db<?>> uri2db = new HashMap<String, Db<?>>(); private static Map<String, Db> uri2db = new HashMap<String, Db>();
public static String resolveAlias(String alias) public static String resolveAlias(String alias)
{ {
@ -46,10 +69,10 @@ public class MStore
return resolveAlias(uri); return resolveAlias(uri);
} }
public static Db<?> getDb(String alias) public static Db getDb(String alias)
{ {
String uri = resolveAlias(alias); String uri = resolveAlias(alias);
Db<?> ret = uri2db.get(uri); Db ret = uri2db.get(uri);
if (ret != null) return ret; if (ret != null) return ret;
try try
@ -65,10 +88,10 @@ public class MStore
return ret; return ret;
} }
public static Db<?> getDb(URI uri) public static Db getDb(URI uri)
{ {
String scheme = uri.getScheme(); String scheme = uri.getScheme();
Driver<?> driver = getDriver(scheme); Driver driver = getDriver(scheme);
if (driver == null) return null; if (driver == null) return null;
return driver.getDb(uri.toString()); return driver.getDb(uri.toString());
} }

View File

@ -1,4 +1,4 @@
package com.massivecraft.mcore.store.storeadapter; package com.massivecraft.mcore.store;
import java.util.Map.Entry; import java.util.Map.Entry;

View File

@ -25,17 +25,17 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public SenderColl(String name, Class<E> entityClass, Db<?> db, Plugin plugin, boolean creative, boolean lowercasing, String idStrategyName, Comparator<? super String> idComparator, Comparator<? super E> entityComparator) public SenderColl(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing, String idStrategyName, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
{ {
super(name, entityClass, db, plugin, creative, lowercasing, idStrategyName, idComparator, entityComparator); super(name, entityClass, db, plugin, creative, lowercasing, idStrategyName, idComparator, entityComparator);
} }
public SenderColl(String name, Class<E> entityClass, Db<?> db, Plugin plugin, boolean creative, boolean lowercasing) public SenderColl(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing)
{ {
super(name, entityClass, db, plugin, creative, lowercasing); super(name, entityClass, db, plugin, creative, lowercasing);
} }
public SenderColl(String name, Class<E> entityClass, Db<?> db, Plugin plugin) public SenderColl(String name, Class<E> entityClass, Db db, Plugin plugin)
{ {
super(name, entityClass, db, plugin, true, true); super(name, entityClass, db, plugin, true, true);
} }

View File

@ -1,12 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import com.massivecraft.mcore.store.CollInterface;
public interface IdStrategy
{
// The name of the strategy (such as "uuid")
public String getName();
// Generate
public String generate(CollInterface<?> coll);
}

View File

@ -1,32 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import java.util.Collection;
import com.massivecraft.mcore.store.CollInterface;
public abstract class IdStrategyAbstract implements IdStrategy
{
public IdStrategyAbstract(String name)
{
this.name = name;
}
protected String name;
@Override public String getName() { return this.name; }
@Override
public String generate(CollInterface<?> coll)
{
Collection<String> alreadyInUse = coll.getIds();
String ret = null;
do
{
ret = this.generateAttempt(coll);
if (ret == null) return null;
}
while (alreadyInUse.contains(ret));
return ret;
}
public abstract String generateAttempt(CollInterface<?> coll);
}

View File

@ -1,26 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import com.massivecraft.mcore.store.CollInterface;
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
public class IdStrategyOid extends IdStrategyAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static IdStrategyOid i = new IdStrategyOid();
public static IdStrategyOid get() { return i; }
private IdStrategyOid() { super("oid"); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String generateAttempt(CollInterface<?> coll)
{
return ObjectId.get().toString();
}
}

View File

@ -1,27 +0,0 @@
package com.massivecraft.mcore.store.idstrategy;
import java.util.UUID;
import com.massivecraft.mcore.store.CollInterface;
public class IdStrategyUuid extends IdStrategyAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static IdStrategyUuid i = new IdStrategyUuid();
public static IdStrategyUuid get() { return i; }
private IdStrategyUuid() { super("uuid"); }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String generateAttempt(CollInterface<?> coll)
{
return UUID.randomUUID().toString();
}
}

View File

@ -1,13 +0,0 @@
package com.massivecraft.mcore.store.storeadapter;
import com.massivecraft.mcore.store.Coll;
public interface StoreAdapter
{
// A store adapter is supposed to be used with a certain driver.
// This method returns the name of that driver.
public String forDriverName();
public Object read(Coll<?> coll, Object entity);
public void write(Coll<?> coll, Object raw, Object entity); // (This is an opaque/complete write)
}

View File

@ -1,12 +0,0 @@
package com.massivecraft.mcore.store.storeadapter;
public abstract class StoreAdapterAbstract implements StoreAdapter
{
protected String forDriverName;
@Override public String forDriverName() { return this.forDriverName; }
public StoreAdapterAbstract(String forDriverName)
{
this.forDriverName = forDriverName;
}
}

View File

@ -1,37 +0,0 @@
package com.massivecraft.mcore.store.storeadapter;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.xlib.gson.JsonElement;
public class StoreAdapterGson extends StoreAdapterAbstract
{
public StoreAdapterGson()
{
super("gson");
}
@Override
public Object read(Coll<?> coll, Object entity)
{
return coll.getGson().toJsonTree(entity, coll.getEntityClass());
}
@Override
public void write(Coll<?> coll, Object raw, Object entity)
{
if (raw == null) throw new NullPointerException("raw");
if (entity == null) throw new NullPointerException("entity");
Object temp = coll.getGson().fromJson((JsonElement)raw, coll.getEntityClass());
coll.copy(temp, entity);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
protected static StoreAdapterGson instance = new StoreAdapterGson();
public static StoreAdapterGson get()
{
return instance;
}
}

View File

@ -1,35 +0,0 @@
package com.massivecraft.mcore.store.storeadapter;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.mongodb.DBObject;
public class StoreAdapterMongo extends StoreAdapterAbstract
{
public StoreAdapterMongo()
{
super("mongo");
}
@Override
public Object read(Coll<?> coll, Object entity)
{
return MongoGsonConverter.gson2MongoObject((JsonElement)StoreAdapterGson.get().read(coll, entity));
}
@Override
public void write(Coll<?> coll, Object raw, Object entity)
{
StoreAdapterGson.get().write(coll, MongoGsonConverter.mongo2GsonObject((DBObject) raw), entity);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
protected static StoreAdapterMongo instance = new StoreAdapterMongo();
public static StoreAdapterMongo get()
{
return instance;
}
}