Summary: Snapshots integration into RocksJava. Added support for the following functionalities: - getSnapshot - releaseSnapshot - ReadOptions support to set a Snapshot - ReadOptions support to retrieve Snapshot - SnapshotTest Test Plan: make rocksdbjava make jtest Differential Revision: https://reviews.facebook.net/D24801main
parent
bc3bc4bc2f
commit
b8ce526487
@ -0,0 +1,24 @@ |
||||
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
package org.rocksdb; |
||||
|
||||
/** |
||||
* Snapshot of database |
||||
*/ |
||||
public class Snapshot extends RocksObject { |
||||
Snapshot(long nativeHandle) { |
||||
super(); |
||||
nativeHandle_ = nativeHandle; |
||||
} |
||||
|
||||
/** |
||||
* Dont release C++ Snapshot pointer. The pointer |
||||
* to the snapshot is released by the database |
||||
* instance. |
||||
*/ |
||||
@Override protected void disposeInternal() { |
||||
} |
||||
} |
@ -0,0 +1,82 @@ |
||||
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
package org.rocksdb.test; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.rocksdb.ColumnFamilyHandle; |
||||
import org.rocksdb.Options; |
||||
import org.rocksdb.ReadOptions; |
||||
import org.rocksdb.RocksDB; |
||||
import org.rocksdb.RocksDBException; |
||||
import org.rocksdb.Snapshot; |
||||
import org.rocksdb.WriteBatch; |
||||
import org.rocksdb.WriteOptions; |
||||
|
||||
|
||||
public class SnapshotTest |
||||
{ |
||||
static final String DB_PATH = "/tmp/rocksdbjni_snapshot_test"; |
||||
static { |
||||
RocksDB.loadLibrary(); |
||||
} |
||||
|
||||
public static void main(String[] args){ |
||||
RocksDB db = null; |
||||
Options options = new Options(); |
||||
options.setCreateIfMissing(true); |
||||
try { |
||||
db = RocksDB.open(options, DB_PATH); |
||||
db.put("key".getBytes(), "value".getBytes()); |
||||
// Get new Snapshot of database
|
||||
Snapshot snapshot = db.getSnapshot(); |
||||
ReadOptions readOptions = new ReadOptions(); |
||||
// set snapshot in ReadOptions
|
||||
readOptions.setSnapshot(snapshot); |
||||
// retrieve key value pair
|
||||
assert(new String(db.get("key".getBytes())) |
||||
.equals("value")); |
||||
// retrieve key value pair created before
|
||||
// the snapshot was made
|
||||
assert(new String(db.get(readOptions, |
||||
"key".getBytes())).equals("value")); |
||||
// add new key/value pair
|
||||
db.put("newkey".getBytes(), "newvalue".getBytes()); |
||||
// using no snapshot the latest db entries
|
||||
// will be taken into account
|
||||
assert(new String(db.get("newkey".getBytes())) |
||||
.equals("newvalue")); |
||||
// snapshopot was created before newkey
|
||||
assert(db.get(readOptions, "newkey".getBytes()) |
||||
== null); |
||||
// Retrieve snapshot from read options
|
||||
Snapshot sameSnapshot = readOptions.snapshot(); |
||||
readOptions.setSnapshot(sameSnapshot); |
||||
// results must be the same with new Snapshot
|
||||
// instance using the same native pointer
|
||||
assert(new String(db.get(readOptions, |
||||
"key".getBytes())).equals("value")); |
||||
// update key value pair to newvalue
|
||||
db.put("key".getBytes(), "newvalue".getBytes()); |
||||
// read with previously created snapshot will
|
||||
// read previous version of key value pair
|
||||
assert(new String(db.get(readOptions, |
||||
"key".getBytes())).equals("value")); |
||||
// read for newkey using the snapshot must be
|
||||
// null
|
||||
assert(db.get(readOptions, "newkey".getBytes()) |
||||
== null); |
||||
// release Snapshot
|
||||
db.releaseSnapshot(snapshot); |
||||
// Close database
|
||||
db.close(); |
||||
}catch (RocksDBException e){ |
||||
e.printStackTrace(); |
||||
assert(false); |
||||
} |
||||
System.out.println("Passed SnapshotTest"); |
||||
} |
||||
} |
Loading…
Reference in new issue