using BerkeleyDB for thumbnail database
This commit is contained in:
parent
167d068c7b
commit
9e840209cf
2 changed files with 120 additions and 3 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
|
@ -1,5 +1,5 @@
|
|||
target
|
||||
/target
|
||||
cache.properties
|
||||
build
|
||||
db
|
||||
/build
|
||||
/db
|
||||
|
||||
|
|
|
|||
117
src/org/forkalsrud/album/db/ThumbnailDatabase.java
Normal file
117
src/org/forkalsrud/album/db/ThumbnailDatabase.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.forkalsrud.album.db;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.forkalsrud.album.web.CachedImage;
|
||||
|
||||
import com.sleepycat.bind.tuple.TupleBinding;
|
||||
import com.sleepycat.bind.tuple.TupleInput;
|
||||
import com.sleepycat.bind.tuple.TupleOutput;
|
||||
import com.sleepycat.je.Database;
|
||||
import com.sleepycat.je.DatabaseConfig;
|
||||
import com.sleepycat.je.DatabaseEntry;
|
||||
import com.sleepycat.je.Environment;
|
||||
import com.sleepycat.je.EnvironmentConfig;
|
||||
import com.sleepycat.je.OperationStatus;
|
||||
import com.sleepycat.je.Transaction;
|
||||
|
||||
/**
|
||||
* @author knut
|
||||
*
|
||||
*/
|
||||
public class ThumbnailDatabase extends TupleBinding<CachedImage> {
|
||||
|
||||
private static Charset UTF8 = Charset.forName("utf-8");
|
||||
|
||||
private Environment environment;
|
||||
private Database db;
|
||||
|
||||
public void init(File dir) {
|
||||
|
||||
String dbname = "thumbnails";
|
||||
|
||||
EnvironmentConfig environmentConfig = new EnvironmentConfig();
|
||||
environmentConfig.setAllowCreate(true);
|
||||
environmentConfig.setTransactional(true);
|
||||
// perform other environment configurations
|
||||
environment = new Environment(dir, environmentConfig);
|
||||
DatabaseConfig databaseConfig = new DatabaseConfig();
|
||||
databaseConfig.setAllowCreate(true);
|
||||
databaseConfig.setTransactional(true);
|
||||
// perform other database configurations
|
||||
this.db = environment.openDatabase(null, dbname, databaseConfig);
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
db.close();
|
||||
environment.close();
|
||||
}
|
||||
|
||||
public long size() {
|
||||
return db.count();
|
||||
}
|
||||
|
||||
public CachedImage load(String key) {
|
||||
|
||||
DatabaseEntry data = new DatabaseEntry();
|
||||
Transaction txn = environment.beginTransaction(null, null);
|
||||
OperationStatus status = db.get(txn, key(key), data, null);
|
||||
txn.commitNoSync();
|
||||
if (OperationStatus.SUCCESS.equals(status)) {
|
||||
return entryToObject(data);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void store(String key, CachedImage img) {
|
||||
|
||||
DatabaseEntry data = new DatabaseEntry();
|
||||
objectToEntry(img, data);
|
||||
DatabaseEntry binKey = key(key);
|
||||
Transaction txn = environment.beginTransaction(null, null);
|
||||
db.delete(txn, binKey);
|
||||
db.put(txn, binKey, data);
|
||||
txn.commitSync();
|
||||
}
|
||||
|
||||
|
||||
private DatabaseEntry key(String key) {
|
||||
DatabaseEntry returnValue = new DatabaseEntry();
|
||||
returnValue.setData(key.getBytes(UTF8));
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CachedImage entryToObject(TupleInput in) {
|
||||
|
||||
CachedImage img = new CachedImage();
|
||||
int version = in.readInt();
|
||||
if (version != 1) {
|
||||
throw new RuntimeException("I only understand version 1");
|
||||
}
|
||||
img.lastModified = in.readLong();
|
||||
img.mimeType = in.readString();
|
||||
int lobLen = in.readInt();
|
||||
img.bits = new byte[lobLen];
|
||||
in.read(img.bits, 0, lobLen);
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void objectToEntry(CachedImage img, TupleOutput out) {
|
||||
|
||||
out.writeInt(1); // version 1
|
||||
out.writeLong(img.lastModified);
|
||||
out.writeString(img.mimeType);
|
||||
out.writeInt(img.bits.length);
|
||||
out.write(img.bits);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue