1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import java
.util
.Arrays
;
10 import java
.util
.ArrayList
;
12 import org
.junit
.ClassRule
;
13 import org
.junit
.Rule
;
14 import org
.junit
.Test
;
15 import org
.junit
.rules
.TemporaryFolder
;
17 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
19 public class MergeTest
{
22 public static final RocksMemoryResource rocksMemoryResource
=
23 new RocksMemoryResource();
26 public TemporaryFolder dbFolder
= new TemporaryFolder();
29 public void stringOption()
30 throws InterruptedException
, RocksDBException
{
31 try (final Options opt
= new Options()
32 .setCreateIfMissing(true)
33 .setMergeOperatorName("stringappend");
34 final RocksDB db
= RocksDB
.open(opt
,
35 dbFolder
.getRoot().getAbsolutePath())) {
36 // writing aa under key
37 db
.put("key".getBytes(), "aa".getBytes());
39 db
.merge("key".getBytes(), "bb".getBytes());
41 final byte[] value
= db
.get("key".getBytes());
42 final String strValue
= new String(value
);
43 assertThat(strValue
).isEqualTo("aa,bb");
48 public void cFStringOption()
49 throws InterruptedException
, RocksDBException
{
51 try (final ColumnFamilyOptions cfOpt1
= new ColumnFamilyOptions()
52 .setMergeOperatorName("stringappend");
53 final ColumnFamilyOptions cfOpt2
= new ColumnFamilyOptions()
54 .setMergeOperatorName("stringappend")
56 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
57 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpt1
),
58 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpt2
)
61 final List
<ColumnFamilyHandle
> columnFamilyHandleList
= new ArrayList
<>();
62 try (final DBOptions opt
= new DBOptions()
63 .setCreateIfMissing(true)
64 .setCreateMissingColumnFamilies(true);
65 final RocksDB db
= RocksDB
.open(opt
,
66 dbFolder
.getRoot().getAbsolutePath(), cfDescriptors
,
67 columnFamilyHandleList
)) {
69 // writing aa under key
70 db
.put(columnFamilyHandleList
.get(1),
71 "cfkey".getBytes(), "aa".getBytes());
73 db
.merge(columnFamilyHandleList
.get(1),
74 "cfkey".getBytes(), "bb".getBytes());
76 byte[] value
= db
.get(columnFamilyHandleList
.get(1),
78 String strValue
= new String(value
);
79 assertThat(strValue
).isEqualTo("aa,bb");
81 for (final ColumnFamilyHandle handle
: columnFamilyHandleList
) {
90 public void operatorOption()
91 throws InterruptedException
, RocksDBException
{
92 try (final StringAppendOperator stringAppendOperator
= new StringAppendOperator();
93 final Options opt
= new Options()
94 .setCreateIfMissing(true)
95 .setMergeOperator(stringAppendOperator
);
96 final RocksDB db
= RocksDB
.open(opt
,
97 dbFolder
.getRoot().getAbsolutePath())) {
98 // Writing aa under key
99 db
.put("key".getBytes(), "aa".getBytes());
101 // Writing bb under key
102 db
.merge("key".getBytes(), "bb".getBytes());
104 final byte[] value
= db
.get("key".getBytes());
105 final String strValue
= new String(value
);
107 assertThat(strValue
).isEqualTo("aa,bb");
112 public void cFOperatorOption()
113 throws InterruptedException
, RocksDBException
{
114 try (final StringAppendOperator stringAppendOperator
= new StringAppendOperator();
115 final ColumnFamilyOptions cfOpt1
= new ColumnFamilyOptions()
116 .setMergeOperator(stringAppendOperator
);
117 final ColumnFamilyOptions cfOpt2
= new ColumnFamilyOptions()
118 .setMergeOperator(stringAppendOperator
)
120 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
121 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpt1
),
122 new ColumnFamilyDescriptor("new_cf".getBytes(), cfOpt2
)
124 final List
<ColumnFamilyHandle
> columnFamilyHandleList
= new ArrayList
<>();
125 try (final DBOptions opt
= new DBOptions()
126 .setCreateIfMissing(true)
127 .setCreateMissingColumnFamilies(true);
128 final RocksDB db
= RocksDB
.open(opt
,
129 dbFolder
.getRoot().getAbsolutePath(), cfDescriptors
,
130 columnFamilyHandleList
)
133 // writing aa under key
134 db
.put(columnFamilyHandleList
.get(1),
135 "cfkey".getBytes(), "aa".getBytes());
136 // merge bb under key
137 db
.merge(columnFamilyHandleList
.get(1),
138 "cfkey".getBytes(), "bb".getBytes());
139 byte[] value
= db
.get(columnFamilyHandleList
.get(1),
141 String strValue
= new String(value
);
143 // Test also with createColumnFamily
144 try (final ColumnFamilyOptions cfHandleOpts
=
145 new ColumnFamilyOptions()
146 .setMergeOperator(stringAppendOperator
);
147 final ColumnFamilyHandle cfHandle
=
148 db
.createColumnFamily(
149 new ColumnFamilyDescriptor("new_cf2".getBytes(),
152 // writing xx under cfkey2
153 db
.put(cfHandle
, "cfkey2".getBytes(), "xx".getBytes());
154 // merge yy under cfkey2
155 db
.merge(cfHandle
, new WriteOptions(), "cfkey2".getBytes(),
157 value
= db
.get(cfHandle
, "cfkey2".getBytes());
158 String strValueTmpCf
= new String(value
);
160 assertThat(strValue
).isEqualTo("aa,bb");
161 assertThat(strValueTmpCf
).isEqualTo("xx,yy");
164 for (final ColumnFamilyHandle columnFamilyHandle
:
165 columnFamilyHandleList
) {
166 columnFamilyHandle
.close();
174 public void operatorGcBehaviour()
175 throws RocksDBException
{
176 try (final StringAppendOperator stringAppendOperator
= new StringAppendOperator()) {
177 try (final Options opt
= new Options()
178 .setCreateIfMissing(true)
179 .setMergeOperator(stringAppendOperator
);
180 final RocksDB db
= RocksDB
.open(opt
,
181 dbFolder
.getRoot().getAbsolutePath())) {
187 try (final Options opt
= new Options()
188 .setMergeOperator(stringAppendOperator
);
189 final RocksDB db
= RocksDB
.open(opt
,
190 dbFolder
.getRoot().getAbsolutePath())) {
195 try (final StringAppendOperator stringAppendOperator2
= new StringAppendOperator();
196 final Options opt
= new Options()
197 .setMergeOperator(stringAppendOperator2
);
198 final RocksDB db
= RocksDB
.open(opt
,
199 dbFolder
.getRoot().getAbsolutePath())) {
203 // test replace one with another merge operator instance
204 try (final Options opt
= new Options()
205 .setMergeOperator(stringAppendOperator
);
206 final StringAppendOperator newStringAppendOperator
= new StringAppendOperator()) {
207 opt
.setMergeOperator(newStringAppendOperator
);
208 try (final RocksDB db
= RocksDB
.open(opt
,
209 dbFolder
.getRoot().getAbsolutePath())) {
217 public void emptyStringInSetMergeOperatorByName() {
218 try (final Options opt
= new Options()
219 .setMergeOperatorName("");
220 final ColumnFamilyOptions cOpt
= new ColumnFamilyOptions()
221 .setMergeOperatorName("")) {
226 @Test(expected
= IllegalArgumentException
.class)
227 public void nullStringInSetMergeOperatorByNameOptions() {
228 try (final Options opt
= new Options()) {
229 opt
.setMergeOperatorName(null);
233 @Test(expected
= IllegalArgumentException
.class)
235 nullStringInSetMergeOperatorByNameColumnFamilyOptions() {
236 try (final ColumnFamilyOptions opt
= new ColumnFamilyOptions()) {
237 opt
.setMergeOperatorName(null);