RocksDBSample.java 7.6 KB
Newer Older
1 2 3 4 5
// 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.

A
Ankit Gupta 已提交
6
import java.util.Arrays;
7
import org.rocksdb.*;
8
import org.rocksdb.util.SizeUnit;
9 10 11 12 13 14 15 16 17 18 19 20 21
import java.io.IOException;

public class RocksDBSample {
  static {
    System.loadLibrary("rocksdbjni");
  }

  public static void main(String[] args) {
    if (args.length < 1) {
      System.out.println("usage: RocksDBSample db_path");
      return;
    }
    String db_path = args[0];
22
    String db_path_not_found = db_path + "_not_found";
23 24

    System.out.println("RocksDBSample");
25
    RocksDB db = null;
26 27 28 29 30 31 32 33 34
    Options options = new Options();
    try {
      db = RocksDB.open(options, db_path_not_found);
      assert(false);
    } catch (RocksDBException e) {
      System.out.format("caught the expceted exception -- %s\n", e);
      assert(db == null);
    }

35
    options.setCreateIfMissing(true)
A
Ankit Gupta 已提交
36
        .createStatistics()
37
        .setWriteBufferSize(8 * SizeUnit.KB)
38 39
        .setMaxWriteBufferNumber(3)
        .setDisableSeekCompaction(true)
40
        .setBlockSize(64 * SizeUnit.KB)
41
        .setMaxBackgroundCompactions(10);
A
Ankit Gupta 已提交
42
    Statistics stats = options.statisticsPtr();
A
Ankit Gupta 已提交
43

44
    assert(options.createIfMissing() == true);
45
    assert(options.writeBufferSize() == 8 * SizeUnit.KB);
46 47
    assert(options.maxWriteBufferNumber() == 3);
    assert(options.disableSeekCompaction() == true);
48
    assert(options.blockSize() == 64 * SizeUnit.KB);
49
    assert(options.maxBackgroundCompactions() == 10);
A
Ankit Gupta 已提交
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    assert(options.memTableFactoryName().equals("SkipListFactory"));
    options.setMemTableConfig(
        new HashSkipListMemTableConfig()
            .setHeight(4)
            .setBranchingFactor(4)
            .setBucketCount(2000000));
    assert(options.memTableFactoryName().equals("HashSkipListRepFactory"));

    options.setMemTableConfig(
        new HashLinkedListMemTableConfig()
            .setBucketCount(100000));
    assert(options.memTableFactoryName().equals("HashLinkedListRepFactory"));

    options.setMemTableConfig(
        new VectorMemTableConfig().setReservedSize(10000));
    assert(options.memTableFactoryName().equals("VectorRepFactory"));

    options.setMemTableConfig(new SkipListMemTableConfig());
    assert(options.memTableFactoryName().equals("SkipListFactory"));

    options.setTableFormatConfig(new PlainTableConfig());
    assert(options.tableFactoryName().equals("PlainTable"));

74 75 76 77 78 79 80 81 82 83 84 85
    try {
      db = RocksDB.open(options, db_path_not_found);
      db.put("hello".getBytes(), "world".getBytes());
      byte[] value = db.get("hello".getBytes());
      assert("world".equals(new String(value)));
    } catch (RocksDBException e) {
      System.out.format("[ERROR] caught the unexpceted exception -- %s\n", e);
      assert(db == null);
      assert(false);
    }
    // be sure to release the c++ pointer
    db.close();
86

87 88 89
    ReadOptions readOptions = new ReadOptions();
    readOptions.setFillCache(false);

90
    try {
91
      db = RocksDB.open(options, db_path);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      db.put("hello".getBytes(), "world".getBytes());
      byte[] value = db.get("hello".getBytes());
      System.out.format("Get('hello') = %s\n",
          new String(value));

      for (int i = 1; i <= 9; ++i) {
        for (int j = 1; j <= 9; ++j) {
          db.put(String.format("%dx%d", i, j).getBytes(),
                 String.format("%d", i * j).getBytes());
        }
      }

      for (int i = 1; i <= 9; ++i) {
        for (int j = 1; j <= 9; ++j) {
          System.out.format("%s ", new String(db.get(
              String.format("%dx%d", i, j).getBytes())));
        }
        System.out.println("");
      }

      value = db.get("1x1".getBytes());
      assert(value != null);
      value = db.get("world".getBytes());
      assert(value == null);
116 117
      value = db.get(readOptions, "world".getBytes());
      assert(value == null);
118 119 120 121 122 123 124 125 126

      byte[] testKey = "asdf".getBytes();
      byte[] testValue =
          "asdfghjkl;'?><MNBVCXZQWERTYUIOP{+_)(*&^%$#@".getBytes();
      db.put(testKey, testValue);
      byte[] testResult = db.get(testKey);
      assert(testResult != null);
      assert(Arrays.equals(testValue, testResult));
      assert(new String(testValue).equals(new String(testResult)));
127 128 129 130
      testResult = db.get(readOptions, testKey);
      assert(testResult != null);
      assert(Arrays.equals(testValue, testResult));
      assert(new String(testValue).equals(new String(testResult)));
131 132 133 134 135

      byte[] insufficientArray = new byte[10];
      byte[] enoughArray = new byte[50];
      int len;
      len = db.get(testKey, insufficientArray);
136
      assert(len > insufficientArray.length);
137 138 139 140
      len = db.get("asdfjkl;".getBytes(), enoughArray);
      assert(len == RocksDB.NOT_FOUND);
      len = db.get(testKey, enoughArray);
      assert(len == testValue.length);
141

142 143 144 145 146 147 148
      len = db.get(readOptions, testKey, insufficientArray);
      assert(len > insufficientArray.length);
      len = db.get(readOptions, "asdfjkl;".getBytes(), enoughArray);
      assert(len == RocksDB.NOT_FOUND);
      len = db.get(readOptions, testKey, enoughArray);
      assert(len == testValue.length);

149 150 151 152 153 154 155 156 157 158 159 160 161 162
      db.remove(testKey);
      len = db.get(testKey, enoughArray);
      assert(len == RocksDB.NOT_FOUND);

      // repeat the test with WriteOptions
      WriteOptions writeOpts = new WriteOptions();
      writeOpts.setSync(true);
      writeOpts.setDisableWAL(true);
      db.put(writeOpts, testKey, testValue);
      len = db.get(testKey, enoughArray);
      assert(len == testValue.length);
      assert(new String(testValue).equals(
          new String(enoughArray, 0, len)));
      writeOpts.dispose();
A
Ankit Gupta 已提交
163 164

      try {
A
Ankit Gupta 已提交
165
        for (TickerType statsType : TickerType.values()) {
A
Ankit Gupta 已提交
166 167 168
          stats.getTickerCount(statsType);
        }
        System.out.println("getTickerCount() passed.");
A
Ankit Gupta 已提交
169
      } catch (Exception e) {
A
Ankit Gupta 已提交
170 171 172
        System.out.println("Failed in call to getTickerCount()");
        assert(false); //Should never reach here.
      }
A
Ankit Gupta 已提交
173

A
Ankit Gupta 已提交
174 175 176 177 178 179 180 181 182
      try {
        for (HistogramType histogramType : HistogramType.values()) {
          HistogramData data = stats.geHistogramData(histogramType);
        }
        System.out.println("geHistogramData() passed.");
      } catch (Exception e) {
        System.out.println("Failed in call to geHistogramData()");
        assert(false); //Should never reach here.
      }
183

A
Add doc  
Ankit Gupta 已提交
184
      Iterator iterator = db.newIterator();
185

A
Ankit Gupta 已提交
186 187 188 189 190 191 192 193 194 195
      boolean seekToFirstPassed = false;
      for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
        iterator.status();
        assert(iterator.key() != null);
        assert(iterator.value() != null);
        seekToFirstPassed = true;
      }
      if(seekToFirstPassed) {
        System.out.println("iterator seekToFirst tests passed.");
      }
196

A
Ankit Gupta 已提交
197 198 199 200 201 202 203
      boolean seekToLastPassed = false;
      for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) {
        iterator.status();
        assert(iterator.key() != null);
        assert(iterator.value() != null);
        seekToLastPassed = true;
      }
204

A
Ankit Gupta 已提交
205 206 207
      if(seekToLastPassed) {
        System.out.println("iterator seekToLastPassed tests passed.");
      }
208

A
Ankit Gupta 已提交
209
      iterator.seekToFirst();
A
Ankit Gupta 已提交
210 211 212
      iterator.seek(iterator.key());
      assert(iterator.key() != null);
      assert(iterator.value() != null);
213

A
Ankit Gupta 已提交
214
      System.out.println("iterator seek test passed.");
215

A
Ankit Gupta 已提交
216 217
      iterator.close();
      System.out.println("iterator tests passed.");
218 219 220
    } catch (RocksDBException e) {
      System.err.println(e);
    }
221 222 223
    if (db != null) {
      db.close();
    }
A
Ankit Gupta 已提交
224
    // be sure to dispose c++ pointers
225
    options.dispose();
226
    readOptions.dispose();
227 228
  }
}