* If no crawl is pending, a new one is created.
*/
public long getCreatePendingCrawl(final long fsid, boolean shouldCreate) {
long crawlid = dbQueue.execute(new SQLiteJob<Long>() {
protected Long job(SQLiteConnection db) throws SQLiteException {
SQLiteStatement stmt = db.prepare("SELECT crawlid from Crawls WHERE fsid = ? AND inprogress = 'True'");
try {
stmt.bind(1, fsid);
if (stmt.step()) {
return stmt.columnLong(0);
} else {
return -1L;
}
} finally {
stmt.dispose();
}
}
}).complete();
if (crawlid >= 0) {
return crawlid;
}
// Time to insert
if (shouldCreate) {
return dbQueue.execute(new SQLiteJob<Long>() {
protected Long job(SQLiteConnection db) throws SQLiteException {
Date now = new Date(System.currentTimeMillis());
String dateCreated = fileDateFormat.format(now);
String syntheticDateFinished = fileDateFormat.format(new Date(0));
String inprogress = "True";
SQLiteStatement stmt = db.prepare("INSERT into Crawls VALUES(null, ?, ?, ?, ?)");
try {
stmt.bind(1, dateCreated).bind(2, syntheticDateFinished).bind(3, inprogress).bind(4, fsid);
stmt.step();
return db.getLastInsertId();
} finally {
stmt.dispose();
}
}
}).complete();
}
return -1L;