Last active
May 30, 2017 15:41
-
-
Save adamretter/ff9d29b7cbe783cebbd52a8c7ad04137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.rocksdb; | |
import java.io.IOException; | |
import java.nio.file.*; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by aretter on 30/05/2017. | |
*/ | |
public class TestMerge1 { | |
static { | |
RocksDB.loadLibrary(); | |
} | |
public static void main(final String args[]) throws RocksDBException, IOException { | |
DBOptions dbOptions = null; | |
RocksDB db = null; | |
String db_path = "/tmp/rocksdb"; | |
final List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>(); | |
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>(); | |
try { | |
dbOptions = new DBOptions() | |
.setCreateIfMissing(true) | |
.setCreateMissingColumnFamilies(true); | |
final ColumnFamilyOptions columnFamilyOptions = new ColumnFamilyOptions(); | |
columnFamilyOptions.setMergeOperatorName("put"); | |
columnFamilyDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); | |
columnFamilyDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes(), columnFamilyOptions)); | |
db = RocksDB.open(dbOptions, db_path, columnFamilyDescriptors, columnFamilyHandles); | |
assert (db != null); | |
final ColumnFamilyHandle columnFamilyHandle = columnFamilyHandles.get(1); | |
try(final WriteOptions writeOptions = new WriteOptions()) { | |
db.put(columnFamilyHandle, writeOptions, | |
"-2058425226^35^0^0^0".getBytes(), "�� \u0005wk\u0001\f\u0001 A= \n \u0014-2058425226^35^0^0^0 \u0002 #com.gigaspaces.data.stock.StockPojo\u0001 \u0001Z��\tY \u0002\u0001 \u0002\u0002\u0001 \u0005aaaaa".getBytes()); | |
String value = new String(db.get(columnFamilyHandle, | |
"-2058425226^35^0^0^0".getBytes())); | |
db.merge(columnFamilyHandle, | |
"-2058425226^35^0^0^0".getBytes(), "�� \u0005wn\u0005\f\u0001 A= \n \u0014-2058425226^35^0^0^0 \u0003 #com.gigaspaces.data.stock.StockPojo\u0001 \u0001Z��\tY \u0002 \u0002\u0001 \u0002\u0002\u0001 \u0004bbbb".getBytes()); | |
// db.put(columnFamilyHandle, writeOptions, | |
// "-2058425226^35^0^0^0".getBytes(), "�� \u0005wn\u0005\f\u0001 A= \n \u0014-2058425226^35^0^0^0 \u0003 #com.gigaspaces.data.stock.StockPojo\u0001 \u0001Z��\tY \u0002 \u0002\u0001 \u0002\u0002\u0001 \u0004bbbb".getBytes()); | |
String value2 = new String(db.get(columnFamilyHandle, | |
"-2058425226^35^0^0^0".getBytes())); | |
System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD " + value2); | |
} | |
} finally { | |
for(final ColumnFamilyHandle columnFamilyHandle: columnFamilyHandles) { | |
columnFamilyHandle.close(); | |
} | |
for(final ColumnFamilyDescriptor columnFamilyDescriptor : columnFamilyDescriptors) { | |
columnFamilyDescriptor.columnFamilyOptions().close(); | |
} | |
if (db != null) { | |
db.close(); | |
} | |
dbOptions.close(); | |
deleteDir(Paths.get(db_path)); | |
} | |
} | |
public static void deleteDir(final Path folder) throws IOException { | |
Files.walkFileTree(folder, new SimpleFileVisitor<Path>() { | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
Files.delete(file); | |
return FileVisitResult.CONTINUE; | |
} | |
@Override | |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | |
if (exc != null) { | |
throw exc; | |
} | |
Files.delete(dir); | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment