package ezdb.lmdb;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.lmdbjava.Dbi;
import org.lmdbjava.Env;
import org.lmdbjava.Txn;

import ezdb.DbException;
import ezdb.batch.RangeBatch;
import ezdb.lmdb.util.DirectBuffers;
import ezdb.serde.Serde;
import ezdb.util.Util;

public class EzLmDbBatch<H, R, V> implements RangeBatch<H, R, V> {

	private final Env<ByteBuffer> env;
	private final Dbi<ByteBuffer> db;
	private final Txn<ByteBuffer> txn;
	private Serde<H> hashKeySerde;
	private Serde<R> rangeKeySerde;
	private Serde<V> valueSerde;

	public EzLmDbBatch(Env<ByteBuffer> env, Dbi<ByteBuffer> db, Serde<H> hashKeySerde, Serde<R> rangeKeySerde,
			Serde<V> valueSerde) {
		this.env = env;
		this.db = db;
		this.txn = env.txnWrite();
		this.hashKeySerde = hashKeySerde;
		this.rangeKeySerde = rangeKeySerde;
		this.valueSerde = valueSerde;
	}

	@Override
	public void put(H hashKey, V value) {
		put(hashKey, null, value);
	}

	@Override
	public void delete(H hashKey) {
		delete(hashKey, null);
	}

	@Override
	public void flush() {
		txn.commit();
	}

	@Override
	public void close() throws IOException {
		flush();
		txn.close();
	}

	@Override
	public void put(H hashKey, R rangeKey, V value) {
		db.put(txn, DirectBuffers.wrap(Util.combine(hashKeySerde, rangeKeySerde, hashKey, rangeKey)),
				DirectBuffers.wrap(valueSerde.toBytes(value)));
	}

	@Override
	public void delete(H hashKey, R rangeKey) {
		db.delete(txn, DirectBuffers.wrap(Util.combine(hashKeySerde, rangeKeySerde, hashKey, rangeKey)));
	}

}

@@ -144,16 +144,59 @@
 ava.Txn;
+%0Aimport org.lmdbjava.Txn.NotReadyException;
 %0A%0Aimport
@@ -1098,16 +1098,25 @@
 ush() %7B%0A
+%09%09try %7B%0A%09
 %09%09txn.co
@@ -1119,24 +1119,75 @@
 n.commit();%0A
+%09%09%7D catch (NotReadyException e) %7B%0A%09%09%09// ignore%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Overri

use lmdb for now