use of org.iq80.leveldb.WriteBatch in project EventHub by Codecademy.
the class MigrateIdMapToUseLevelDB method main.
public static void main(String[] args) throws Exception {
String userStorageDirectory = args[0];
String filename = userStorageDirectory + "/id_map.ser";
File file = new File(filename);
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
//noinspection unchecked
Map<String, Integer> idMap = (Map<String, Integer>) ois.readObject();
int currentId = ois.readInt();
Options options = new Options();
options.createIfMissing(true);
try (DB idMapDb = JniDBFactory.factory.open(new File(userStorageDirectory + "/id_map.db"), options)) {
try (WriteBatch batch = idMapDb.createWriteBatch()) {
for (Map.Entry<String, Integer> entry : idMap.entrySet()) {
batch.put(bytes(entry.getKey()), bytes("" + entry.getValue()));
}
batch.put(bytes("__eventtracker__id"), bytes("" + currentId));
idMapDb.write(batch);
}
}
}
}
use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.
the class LevelDBMessageTable method rollback.
@Override
protected void rollback(byte[] startKey, byte[] stopKey, byte[] txWritePtr) throws IOException {
WriteBatch writeBatch = levelDB.createWriteBatch();
try (CloseableIterator<Map.Entry<byte[], byte[]>> rowIterator = new DBScanIterator(levelDB, startKey, stopKey)) {
while (rowIterator.hasNext()) {
Map.Entry<byte[], byte[]> rowValue = rowIterator.next();
byte[] value = rowValue.getValue();
Map<String, byte[]> columns = decodeValue(value);
writeBatch.put(rowValue.getKey(), encodeValue(txWritePtr, columns.get(PAYLOAD_COL)));
}
}
try {
levelDB.write(writeBatch, WRITE_OPTIONS);
} catch (DBException ex) {
throw new IOException(ex);
}
}
use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.
the class LevelDBMessageTable method persist.
@Override
protected void persist(Iterator<RawMessageTableEntry> entries) throws IOException {
try (WriteBatch writeBatch = levelDB.createWriteBatch()) {
while (entries.hasNext()) {
RawMessageTableEntry entry = entries.next();
byte[] rowKey = entry.getKey();
// LevelDB doesn't make copies, and since we reuse RawMessageTableEntry object, we need to create copies.
writeBatch.put(Arrays.copyOf(rowKey, rowKey.length), encodeValue(entry.getTxPtr(), entry.getPayload()));
}
levelDB.write(writeBatch, WRITE_OPTIONS);
} catch (DBException ex) {
throw new IOException(ex);
}
}
use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.
the class LevelDBPayloadTable method persist.
@Override
public void persist(Iterator<RawPayloadTableEntry> entries) throws IOException {
try (WriteBatch writeBatch = levelDB.createWriteBatch()) {
while (entries.hasNext()) {
RawPayloadTableEntry entry = entries.next();
byte[] key = entry.getKey();
byte[] value = entry.getValue();
// LevelDB doesn't make copies, and since we reuse RawPayloadTableEntry object, we need to create copies.
writeBatch.put(Arrays.copyOf(key, key.length), Arrays.copyOf(value, value.length));
}
levelDB.write(writeBatch, WRITE_OPTIONS);
} catch (DBException ex) {
throw new IOException(ex);
}
}
use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.
the class LevelDBTableCore method deleteColumn.
public void deleteColumn(byte[] row, byte[] column) throws IOException {
DB db = getDB();
WriteBatch batch = db.createWriteBatch();
try (DBIterator iterator = db.iterator()) {
addToDeleteBatch(batch, iterator, row, column);
db.write(batch);
}
}
Aggregations