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).
5 
6 package org.rocksdb;
7 
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.TemporaryFolder;
11 
12 import java.util.*;
13 import java.util.Comparator;
14 
15 import static org.junit.Assert.assertEquals;
16 
17 public class NativeComparatorWrapperTest {
18 
19   @Rule
20   public TemporaryFolder dbFolder = new TemporaryFolder();
21 
22   private static final Random random = new Random();
23 
24   @Test
rountrip()25   public void rountrip() throws RocksDBException {
26     final String dbPath = dbFolder.getRoot().getAbsolutePath();
27     final int ITERATIONS = 1_000;
28 
29     final String[] storedKeys = new String[ITERATIONS];
30     try (final NativeStringComparatorWrapper comparator = new NativeStringComparatorWrapper();
31         final Options opt = new Options()
32         .setCreateIfMissing(true)
33         .setComparator(comparator)) {
34 
35       // store random integer keys
36       try (final RocksDB db = RocksDB.open(opt, dbPath)) {
37         for (int i = 0; i < ITERATIONS; i++) {
38           final String strKey = randomString();
39           final byte key[] = strKey.getBytes();
40           // does key already exist (avoid duplicates)
41           if (i > 0 && db.get(key) != null) {
42             i--; // generate a different key
43           } else {
44             db.put(key, "value".getBytes());
45             storedKeys[i] = strKey;
46           }
47         }
48       }
49 
50       // sort the stored keys into ascending alpha-numeric order
51       Arrays.sort(storedKeys, new Comparator<String>() {
52         @Override
53         public int compare(final String o1, final String o2) {
54           return o1.compareTo(o2);
55         }
56       });
57 
58       // re-open db and read from start to end
59       // string keys should be in ascending
60       // order
61       try (final RocksDB db = RocksDB.open(opt, dbPath);
62            final RocksIterator it = db.newIterator()) {
63         int count = 0;
64         for (it.seekToFirst(); it.isValid(); it.next()) {
65           final String strKey = new String(it.key());
66           assertEquals(storedKeys[count++], strKey);
67         }
68       }
69     }
70   }
71 
randomString()72   private String randomString() {
73     final char[] chars = new char[12];
74     for(int i = 0; i < 12; i++) {
75       final int letterCode = random.nextInt(24);
76       final char letter = (char) (((int) 'a') + letterCode);
77       chars[i] = letter;
78     }
79     return String.copyValueOf(chars);
80   }
81 
82   public static class NativeStringComparatorWrapper
83       extends NativeComparatorWrapper {
84 
85     @Override
initializeNative(final long... nativeParameterHandles)86     protected long initializeNative(final long... nativeParameterHandles) {
87       return newStringComparator();
88     }
89 
newStringComparator()90     private native long newStringComparator();
91   }
92 }
93