* {@inheritDoc}
*/
public DataRecord addRecord(InputStream stream) throws DataStoreException {
ResultSet rs = null;
TempFileInputStream fileInput = null;
ConnectionRecoveryManager conn = getConnection();
String id = null, tempId = null;
try {
long now;
for (int i = 0; i < ConnectionRecoveryManager.TRIALS; i++) {
try {
now = System.currentTimeMillis();
id = UUID.randomUUID().toString();
tempId = TEMP_PREFIX + id;
// SELECT LENGTH, LAST_MODIFIED FROM DATASTORE WHERE ID=?
PreparedStatement prep = conn.executeStmt(selectMetaSQL, new Object[]{tempId});
rs = prep.getResultSet();
if (rs.next()) {
// re-try in the very, very unlikely event that the row already exists
continue;
}
// INSERT INTO DATASTORE VALUES(?, 0, ?, NULL)
conn.executeStmt(insertTempSQL, new Object[]{tempId, new Long(now)});
break;
} catch (Exception e) {
throw convert("Can not insert new record", e);
} finally {
DatabaseHelper.closeSilently(rs);
}
}
if (id == null) {
String msg = "Can not create new record";
log.error(msg);
throw new DataStoreException(msg);
}
temporaryInUse.add(tempId);
MessageDigest digest = getDigest();
DigestInputStream dIn = new DigestInputStream(stream, digest);
TrackingInputStream in = new TrackingInputStream(dIn);
StreamWrapper wrapper;
if (STORE_SIZE_MINUS_ONE.equals(storeStream)) {
wrapper = new StreamWrapper(in, -1);
} else if (STORE_SIZE_MAX.equals(storeStream)) {
wrapper = new StreamWrapper(in, Integer.MAX_VALUE);
} else if (STORE_TEMP_FILE.equals(storeStream)) {
File temp = moveToTempFile(in);
fileInput = new TempFileInputStream(temp);
long length = temp.length();
wrapper = new StreamWrapper(fileInput, length);
} else {
throw new DataStoreException("Unsupported stream store algorithm: " + storeStream);
}
// UPDATE DATASTORE SET DATA=? WHERE ID=?
conn.executeStmt(updateDataSQL, new Object[]{wrapper, tempId});
now = System.currentTimeMillis();
long length = in.getPosition();
DataIdentifier identifier = new DataIdentifier(digest.digest());
usesIdentifier(identifier);
id = identifier.toString();
// UPDATE DATASTORE SET ID=?, LENGTH=?, LAST_MODIFIED=?
// WHERE ID=?
// AND NOT EXISTS(SELECT ID FROM DATASTORE WHERE ID=?)
PreparedStatement prep = conn.executeStmt(updateSQL, new Object[]{
id, new Long(length), new Long(now),
tempId, id});
int count = prep.getUpdateCount();
if (count == 0) {
// update count is 0, meaning such a row already exists
// DELETE FROM DATASTORE WHERE ID=?
conn.executeStmt(deleteSQL, new Object[]{tempId});
// SELECT LENGTH, LAST_MODIFIED FROM DATASTORE WHERE ID=?
prep = conn.executeStmt(selectMetaSQL, new Object[]{id});
rs = prep.getResultSet();
if (rs.next()) {
long oldLength = rs.getLong(1);
long lastModified = rs.getLong(2);
if (oldLength != length) {