@Override
public String add(final JSONObject jsonObject) throws RepositoryException {
final SleepycatTransaction currentTransaction = TX.get();
if (null == currentTransaction) {
throw new RepositoryException("Invoking add() outside a transaction");
}
String ret = null;
final Database database = Sleepycat.get(getName(),
Sleepycat.DEFAULT_DB_CONFIG);
try {
if (!jsonObject.has(Keys.OBJECT_ID)) {
ret = Ids.genTimeMillisId();
jsonObject.put(Keys.OBJECT_ID, ret);
} else {
ret = jsonObject.getString(Keys.OBJECT_ID);
}
final DatabaseEntry entryKey = new DatabaseEntry(
ret.getBytes("UTF-8"));
final DatabaseEntry data = new DatabaseEntry(
Serializer.serialize(jsonObject));
final OperationStatus operationStatus =
database.putNoOverwrite(
currentTransaction.getSleepycatTransaction(), entryKey, data);
switch (operationStatus) {
case KEYEXIST:
LOGGER.log(Level.SEVERE,
"Found a duplicated object[oId={0}] in repository[name={1}], ignores this add object operation",
new Object[]{ret, getName()});
throw new RepositoryException(
"Add an object into repository[name=" + getName()
+ "] failed, caused by duplicated id[" + ret + "]");
case SUCCESS:
LOGGER.log(Level.FINER,
"Added an object[oId={0}] in repository[name={1}]",
new Object[]{ret, getName()});
break;
default:
throw new RepositoryException("Add an object[oId="
+ ret + "] fail");
}
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new RepositoryException(e);
}
return ret;
}