parent
04ca7481d2
commit
1fe7a4c62f
@ -1,124 +0,0 @@ |
|||||||
// 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.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
|
||||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
||||||
package org.rocksdb; |
|
||||||
|
|
||||||
import java.util.*; |
|
||||||
import java.io.UnsupportedEncodingException; |
|
||||||
|
|
||||||
/** |
|
||||||
* This class mimics the db/write_batch_test.cc in the c++ rocksdb library. |
|
||||||
*/ |
|
||||||
public class WriteBatchTest { |
|
||||||
static { |
|
||||||
RocksDB.loadLibrary(); |
|
||||||
} |
|
||||||
|
|
||||||
public static void main(String args[]) { |
|
||||||
System.out.println("Testing WriteBatchTest.Empty ==="); |
|
||||||
Empty(); |
|
||||||
|
|
||||||
System.out.println("Testing WriteBatchTest.Multiple ==="); |
|
||||||
Multiple(); |
|
||||||
|
|
||||||
System.out.println("Testing WriteBatchTest.Append ==="); |
|
||||||
Append(); |
|
||||||
|
|
||||||
System.out.println("Testing WriteBatchTest.Blob ==="); |
|
||||||
Blob(); |
|
||||||
|
|
||||||
// The following tests have not yet ported.
|
|
||||||
// Continue();
|
|
||||||
// PutGatherSlices();
|
|
||||||
|
|
||||||
System.out.println("Passed all WriteBatchTest!"); |
|
||||||
} |
|
||||||
|
|
||||||
static void Empty() { |
|
||||||
WriteBatch batch = new WriteBatch(); |
|
||||||
assert(batch.count() == 0); |
|
||||||
} |
|
||||||
|
|
||||||
static void Multiple() { |
|
||||||
try { |
|
||||||
WriteBatch batch = new WriteBatch(); |
|
||||||
batch.put("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII")); |
|
||||||
batch.remove("box".getBytes("US-ASCII")); |
|
||||||
batch.put("baz".getBytes("US-ASCII"), "boo".getBytes("US-ASCII")); |
|
||||||
WriteBatchInternal.setSequence(batch, 100); |
|
||||||
assert(100 == WriteBatchInternal.sequence(batch)); |
|
||||||
assert(3 == batch.count()); |
|
||||||
assert(("Put(baz, boo)@102" + |
|
||||||
"Delete(box)@101" + |
|
||||||
"Put(foo, bar)@100") |
|
||||||
.equals(new String(getContents(batch), "US-ASCII"))); |
|
||||||
} catch (UnsupportedEncodingException e) { |
|
||||||
System.err.println(e); |
|
||||||
assert(false); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void Append() { |
|
||||||
WriteBatch b1 = new WriteBatch(); |
|
||||||
WriteBatch b2 = new WriteBatch(); |
|
||||||
WriteBatchInternal.setSequence(b1, 200); |
|
||||||
WriteBatchInternal.setSequence(b2, 300); |
|
||||||
WriteBatchInternal.append(b1, b2); |
|
||||||
assert(getContents(b1).length == 0); |
|
||||||
assert(b1.count() == 0); |
|
||||||
try { |
|
||||||
b2.put("a".getBytes("US-ASCII"), "va".getBytes("US-ASCII")); |
|
||||||
WriteBatchInternal.append(b1, b2); |
|
||||||
assert("Put(a, va)@200".equals(new String(getContents(b1), "US-ASCII"))); |
|
||||||
assert(1 == b1.count()); |
|
||||||
b2.clear(); |
|
||||||
b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII")); |
|
||||||
WriteBatchInternal.append(b1, b2); |
|
||||||
assert(("Put(a, va)@200" + |
|
||||||
"Put(b, vb)@201") |
|
||||||
.equals(new String(getContents(b1), "US-ASCII"))); |
|
||||||
assert(2 == b1.count()); |
|
||||||
b2.remove("foo".getBytes("US-ASCII")); |
|
||||||
WriteBatchInternal.append(b1, b2); |
|
||||||
assert(("Put(a, va)@200" + |
|
||||||
"Put(b, vb)@202" + |
|
||||||
"Put(b, vb)@201" + |
|
||||||
"Delete(foo)@203") |
|
||||||
.equals(new String(getContents(b1), "US-ASCII"))); |
|
||||||
assert(4 == b1.count()); |
|
||||||
} catch (UnsupportedEncodingException e) { |
|
||||||
System.err.println(e); |
|
||||||
assert(false); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void Blob() { |
|
||||||
WriteBatch batch = new WriteBatch(); |
|
||||||
try { |
|
||||||
batch.put("k1".getBytes("US-ASCII"), "v1".getBytes("US-ASCII")); |
|
||||||
batch.put("k2".getBytes("US-ASCII"), "v2".getBytes("US-ASCII")); |
|
||||||
batch.put("k3".getBytes("US-ASCII"), "v3".getBytes("US-ASCII")); |
|
||||||
batch.putLogData("blob1".getBytes("US-ASCII")); |
|
||||||
batch.remove("k2".getBytes("US-ASCII")); |
|
||||||
batch.putLogData("blob2".getBytes("US-ASCII")); |
|
||||||
batch.merge("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII")); |
|
||||||
assert(5 == batch.count()); |
|
||||||
assert(("Merge(foo, bar)@4" + |
|
||||||
"Put(k1, v1)@0" + |
|
||||||
"Delete(k2)@3" + |
|
||||||
"Put(k2, v2)@1" + |
|
||||||
"Put(k3, v3)@2") |
|
||||||
.equals(new String(getContents(batch), "US-ASCII"))); |
|
||||||
} catch (UnsupportedEncodingException e) { |
|
||||||
System.err.println(e); |
|
||||||
assert(false); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static native byte[] getContents(WriteBatch batch); |
|
||||||
} |
|
@ -0,0 +1,21 @@ |
|||||||
|
package org.rocksdb.test; |
||||||
|
|
||||||
|
import org.junit.rules.ExternalResource; |
||||||
|
import org.rocksdb.RocksDB; |
||||||
|
|
||||||
|
/** |
||||||
|
* Resource to trigger garbage collection after each test |
||||||
|
* run. |
||||||
|
*/ |
||||||
|
public class RocksMemoryResource extends ExternalResource { |
||||||
|
|
||||||
|
static { |
||||||
|
RocksDB.loadLibrary(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void after() { |
||||||
|
System.gc(); |
||||||
|
System.runFinalization(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,130 @@ |
|||||||
|
// 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.
|
||||||
|
//
|
||||||
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||||
|
package org.rocksdb.test; |
||||||
|
|
||||||
|
import org.junit.AfterClass; |
||||||
|
import org.junit.ClassRule; |
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.rules.TemporaryFolder; |
||||||
|
import org.rocksdb.WriteBatch; |
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* This class mimics the db/write_batch_test.cc |
||||||
|
* in the c++ rocksdb library. |
||||||
|
* |
||||||
|
* Not ported yet: |
||||||
|
* |
||||||
|
* Continue(); |
||||||
|
* PutGatherSlices(); |
||||||
|
*/ |
||||||
|
public class WriteBatchTest { |
||||||
|
@ClassRule |
||||||
|
public static final RocksMemoryResource rocksMemoryResource = |
||||||
|
new RocksMemoryResource(); |
||||||
|
|
||||||
|
@Rule |
||||||
|
public TemporaryFolder dbFolder = new TemporaryFolder(); |
||||||
|
|
||||||
|
@AfterClass |
||||||
|
public static void printMergePass(){ |
||||||
|
System.out.println("Passed WriteBatchTest."); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldTestEmptyWriteBatch() { |
||||||
|
WriteBatch batch = new WriteBatch(); |
||||||
|
assertThat(batch.count()).isEqualTo(0); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldTestMultipleBatchOperations() |
||||||
|
throws UnsupportedEncodingException { |
||||||
|
WriteBatch batch = new WriteBatch(); |
||||||
|
batch.put("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII")); |
||||||
|
batch.remove("box".getBytes("US-ASCII")); |
||||||
|
batch.put("baz".getBytes("US-ASCII"), "boo".getBytes("US-ASCII")); |
||||||
|
WriteBatchInternal.setSequence(batch, 100); |
||||||
|
assertThat(WriteBatchInternal.sequence(batch)). |
||||||
|
isNotNull(). |
||||||
|
isEqualTo(100); |
||||||
|
assertThat(batch.count()).isEqualTo(3); |
||||||
|
assertThat(new String(getContents(batch), "US-ASCII")). |
||||||
|
isEqualTo("Put(baz, boo)@102" + |
||||||
|
"Delete(box)@101" + |
||||||
|
"Put(foo, bar)@100"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldTestAppendOperation() |
||||||
|
throws UnsupportedEncodingException { |
||||||
|
WriteBatch b1 = new WriteBatch(); |
||||||
|
WriteBatch b2 = new WriteBatch(); |
||||||
|
WriteBatchInternal.setSequence(b1, 200); |
||||||
|
WriteBatchInternal.setSequence(b2, 300); |
||||||
|
WriteBatchInternal.append(b1, b2); |
||||||
|
assertThat(getContents(b1).length).isEqualTo(0); |
||||||
|
assertThat(b1.count()).isEqualTo(0); |
||||||
|
b2.put("a".getBytes("US-ASCII"), "va".getBytes("US-ASCII")); |
||||||
|
WriteBatchInternal.append(b1, b2); |
||||||
|
assertThat("Put(a, va)@200".equals(new String(getContents(b1), "US-ASCII"))); |
||||||
|
assertThat(b1.count()).isEqualTo(1); |
||||||
|
b2.clear(); |
||||||
|
b2.put("b".getBytes("US-ASCII"), "vb".getBytes("US-ASCII")); |
||||||
|
WriteBatchInternal.append(b1, b2); |
||||||
|
assertThat(("Put(a, va)@200" + |
||||||
|
"Put(b, vb)@201") |
||||||
|
.equals(new String(getContents(b1), "US-ASCII"))); |
||||||
|
assertThat(b1.count()).isEqualTo(2); |
||||||
|
b2.remove("foo".getBytes("US-ASCII")); |
||||||
|
WriteBatchInternal.append(b1, b2); |
||||||
|
assertThat(("Put(a, va)@200" + |
||||||
|
"Put(b, vb)@202" + |
||||||
|
"Put(b, vb)@201" + |
||||||
|
"Delete(foo)@203") |
||||||
|
.equals(new String(getContents(b1), "US-ASCII"))); |
||||||
|
assertThat(b1.count()).isEqualTo(4); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldTestBlobOperation() |
||||||
|
throws UnsupportedEncodingException { |
||||||
|
WriteBatch batch = new WriteBatch(); |
||||||
|
batch.put("k1".getBytes("US-ASCII"), "v1".getBytes("US-ASCII")); |
||||||
|
batch.put("k2".getBytes("US-ASCII"), "v2".getBytes("US-ASCII")); |
||||||
|
batch.put("k3".getBytes("US-ASCII"), "v3".getBytes("US-ASCII")); |
||||||
|
batch.putLogData("blob1".getBytes("US-ASCII")); |
||||||
|
batch.remove("k2".getBytes("US-ASCII")); |
||||||
|
batch.putLogData("blob2".getBytes("US-ASCII")); |
||||||
|
batch.merge("foo".getBytes("US-ASCII"), "bar".getBytes("US-ASCII")); |
||||||
|
assertThat(batch.count()).isEqualTo(5); |
||||||
|
assertThat(("Merge(foo, bar)@4" + |
||||||
|
"Put(k1, v1)@0" + |
||||||
|
"Delete(k2)@3" + |
||||||
|
"Put(k2, v2)@1" + |
||||||
|
"Put(k3, v3)@2") |
||||||
|
.equals(new String(getContents(batch), "US-ASCII"))); |
||||||
|
} |
||||||
|
|
||||||
|
static native byte[] getContents(WriteBatch batch); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Package-private class which provides java api to access |
||||||
|
* c++ WriteBatchInternal. |
||||||
|
*/ |
||||||
|
class WriteBatchInternal { |
||||||
|
static native void setSequence(WriteBatch batch, long sn); |
||||||
|
static native long sequence(WriteBatch batch); |
||||||
|
static native void append(WriteBatch b1, WriteBatch b2); |
||||||
|
} |
Loading…
Reference in new issue