c.cc 63.0 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, 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.
//
6 7 8 9
// 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.

I
Igor Canadi 已提交
10 11
#ifndef ROCKSDB_LITE

12
#include "rocksdb/c.h"
13 14 15

#include <stdlib.h>
#include <unistd.h>
16
#include "rocksdb/cache.h"
17
#include "rocksdb/compaction_filter.h"
18 19 20 21 22
#include "rocksdb/comparator.h"
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "rocksdb/filter_policy.h"
#include "rocksdb/iterator.h"
T
Thomas Adam 已提交
23
#include "rocksdb/merge_operator.h"
24 25 26
#include "rocksdb/options.h"
#include "rocksdb/status.h"
#include "rocksdb/write_batch.h"
27 28
#include "rocksdb/memtablerep.h"
#include "rocksdb/universal_compaction.h"
T
Thomas Adam 已提交
29 30
#include "rocksdb/statistics.h"
#include "rocksdb/slice_transform.h"
31
#include "rocksdb/table.h"
32

33
using rocksdb::Cache;
R
Reed Allman 已提交
34 35 36
using rocksdb::ColumnFamilyDescriptor;
using rocksdb::ColumnFamilyHandle;
using rocksdb::ColumnFamilyOptions;
37
using rocksdb::CompactionFilter;
38
using rocksdb::CompactionFilterFactory;
39 40 41
using rocksdb::CompactionFilterV2;
using rocksdb::CompactionFilterFactoryV2;
using rocksdb::CompactionFilterContext;
42
using rocksdb::CompactionOptionsFIFO;
43 44 45
using rocksdb::Comparator;
using rocksdb::CompressionType;
using rocksdb::DB;
R
Reed Allman 已提交
46
using rocksdb::DBOptions;
47
using rocksdb::Env;
T
Thomas Adam 已提交
48
using rocksdb::InfoLogLevel;
49 50
using rocksdb::FileLock;
using rocksdb::FilterPolicy;
T
Thomas Adam 已提交
51
using rocksdb::FlushOptions;
52 53
using rocksdb::Iterator;
using rocksdb::Logger;
T
Thomas Adam 已提交
54
using rocksdb::MergeOperator;
55 56 57
using rocksdb::NewBloomFilterPolicy;
using rocksdb::NewLRUCache;
using rocksdb::Options;
58
using rocksdb::BlockBasedTableOptions;
59
using rocksdb::CuckooTableOptions;
60 61 62 63 64
using rocksdb::RandomAccessFile;
using rocksdb::Range;
using rocksdb::ReadOptions;
using rocksdb::SequentialFile;
using rocksdb::Slice;
T
Thomas Adam 已提交
65
using rocksdb::SliceTransform;
66 67 68 69 70
using rocksdb::Snapshot;
using rocksdb::Status;
using rocksdb::WritableFile;
using rocksdb::WriteBatch;
using rocksdb::WriteOptions;
A
Albert Strasheim 已提交
71
using rocksdb::LiveFileMetaData;
72

73 74
using std::shared_ptr;

75 76
extern "C" {

T
Thomas Adam 已提交
77 78 79 80 81
struct rocksdb_t                 { DB*               rep; };
struct rocksdb_iterator_t        { Iterator*         rep; };
struct rocksdb_writebatch_t      { WriteBatch        rep; };
struct rocksdb_snapshot_t        { const Snapshot*   rep; };
struct rocksdb_flushoptions_t    { FlushOptions      rep; };
82
struct rocksdb_fifo_compaction_options_t { CompactionOptionsFIFO rep; };
T
Thomas Adam 已提交
83 84 85
struct rocksdb_readoptions_t     { ReadOptions       rep; };
struct rocksdb_writeoptions_t    { WriteOptions      rep; };
struct rocksdb_options_t         { Options           rep; };
86
struct rocksdb_block_based_table_options_t  { BlockBasedTableOptions rep; };
87
struct rocksdb_cuckoo_table_options_t  { CuckooTableOptions rep; };
T
Thomas Adam 已提交
88 89 90 91 92 93
struct rocksdb_seqfile_t         { SequentialFile*   rep; };
struct rocksdb_randomfile_t      { RandomAccessFile* rep; };
struct rocksdb_writablefile_t    { WritableFile*     rep; };
struct rocksdb_filelock_t        { FileLock*         rep; };
struct rocksdb_logger_t          { shared_ptr<Logger>  rep; };
struct rocksdb_cache_t           { shared_ptr<Cache>   rep; };
A
Albert Strasheim 已提交
94
struct rocksdb_livefiles_t       { std::vector<LiveFileMetaData> rep; };
R
Reed Allman 已提交
95
struct rocksdb_column_family_handle_t  { ColumnFamilyHandle* rep; };
96

97 98 99 100
struct rocksdb_compactionfiltercontext_t {
  CompactionFilter::Context rep;
};

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
struct rocksdb_compactionfilter_t : public CompactionFilter {
  void* state_;
  void (*destructor_)(void*);
  unsigned char (*filter_)(
      void*,
      int level,
      const char* key, size_t key_length,
      const char* existing_value, size_t value_length,
      char** new_value, size_t *new_value_length,
      unsigned char* value_changed);
  const char* (*name_)(void*);

  virtual ~rocksdb_compactionfilter_t() {
    (*destructor_)(state_);
  }

  virtual bool Filter(
      int level,
      const Slice& key,
      const Slice& existing_value,
      std::string* new_value,
      bool* value_changed) const {
123
    char* c_new_value = nullptr;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    size_t new_value_length = 0;
    unsigned char c_value_changed = 0;
    unsigned char result = (*filter_)(
        state_,
        level,
        key.data(), key.size(),
        existing_value.data(), existing_value.size(),
        &c_new_value, &new_value_length, &c_value_changed);
    if (c_value_changed) {
      new_value->assign(c_new_value, new_value_length);
      *value_changed = true;
    }
    return result;
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }
};

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
struct rocksdb_compactionfilterfactory_t : public CompactionFilterFactory {
  void* state_;
  void (*destructor_)(void*);
  rocksdb_compactionfilter_t* (*create_compaction_filter_)(
      void*, rocksdb_compactionfiltercontext_t* context);
  const char* (*name_)(void*);

  virtual ~rocksdb_compactionfilterfactory_t() { (*destructor_)(state_); }

  virtual std::unique_ptr<CompactionFilter> CreateCompactionFilter(
      const CompactionFilter::Context& context) {
    rocksdb_compactionfiltercontext_t ccontext;
    ccontext.rep = context;
    CompactionFilter* cf = (*create_compaction_filter_)(state_, &ccontext);
    return std::unique_ptr<CompactionFilter>(cf);
  }

  virtual const char* Name() const { return (*name_)(state_); }
};

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
struct rocksdb_compactionfilterv2_t : public CompactionFilterV2 {
  void* state_;
  void (*destructor_)(void*);
  const char* (*name_)(void*);
  void (*filter_)(void*, int level, size_t num_keys,
                  const char* const* keys_list, const size_t* keys_list_sizes,
                  const char* const* existing_values_list, const size_t* existing_values_list_sizes,
                  char** new_values_list, size_t* new_values_list_sizes,
                  unsigned char* to_delete_list);

  virtual ~rocksdb_compactionfilterv2_t() {
    (*destructor_)(state_);
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }

  virtual std::vector<bool> Filter(int level,
                                   const SliceVector& keys,
                                   const SliceVector& existing_values,
                                   std::vector<std::string>* new_values,
                                   std::vector<bool>* values_changed) const {
    // Make a vector pointing to the underlying key data.
    size_t num_keys = keys.size();
    std::vector<const char*> keys_list(num_keys);
    std::vector<size_t> keys_list_sizes(num_keys);
    for (size_t i = 0; i < num_keys; ++i) {
      keys_list[i] = keys[i].data();
      keys_list_sizes[i] = keys[i].size();
    }
    // Make a vector pointing to the underlying value data.
    std::vector<const char*> existing_values_list(num_keys);
    std::vector<size_t> existing_values_list_sizes(num_keys);
    for (size_t i = 0; i < num_keys; ++i) {
      existing_values_list[i] = existing_values[i].data();
      existing_values_list_sizes[i] = existing_values[i].size();
    }
    // Make a vector which will accept newly-allocated char* arrays
    // which we will take ownership of and assign to strings in new_values.
    new_values->clear();
    std::vector<char*> new_values_list(num_keys);
    std::vector<size_t> new_values_list_sizes(num_keys);
    // Resize values_changed to hold all keys.
    values_changed->resize(num_keys);
    // Make a vector for bools indicating a value should be deleted
    // on compaction (true) or maintained (false).
    std::vector<unsigned char> to_delete_list(num_keys);

    (*filter_)(
        state_, level, num_keys, &keys_list[0], &keys_list_sizes[0],
        &existing_values_list[0], &existing_values_list_sizes[0],
        &new_values_list[0], &new_values_list_sizes[0], &to_delete_list[0]);

    // Now, we transfer any changed values, setting values_changed and
    // initializing new_values in the event a value changed.
    std::vector<bool> to_delete(num_keys);
    for (size_t i = 0; i < num_keys; ++i) {
      to_delete[i] = to_delete_list[i];
      (*values_changed)[i] = new_values_list[i] != nullptr;
      if ((*values_changed)[i]) {
        new_values->push_back(std::string(new_values_list[i], new_values_list_sizes[i]));
        free(new_values_list[i]);
      }
    }
    return to_delete;
  }
};

struct rocksdb_compactionfilterfactoryv2_t : public CompactionFilterFactoryV2 {
  void* state_;
  void (*destructor_)(void*);
  const char* (*name_)(void*);
  rocksdb_compactionfilterv2_t* (*create_compaction_filter_v2_)(
238
      void* state, const rocksdb_compactionfiltercontext_t* context);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

  rocksdb_compactionfilterfactoryv2_t(const SliceTransform* prefix_extractor)
      : CompactionFilterFactoryV2(prefix_extractor) {
  }

  virtual ~rocksdb_compactionfilterfactoryv2_t() {
    (*destructor_)(state_);
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }

  virtual std::unique_ptr<CompactionFilterV2> CreateCompactionFilterV2(
      const CompactionFilterContext& context) {
    struct rocksdb_compactionfiltercontext_t c_context;
    c_context.rep.is_full_compaction = context.is_full_compaction;
    c_context.rep.is_manual_compaction = context.is_manual_compaction;
    return std::unique_ptr<CompactionFilterV2>(
258
        (*create_compaction_filter_v2_)(state_, &c_context));
259 260 261
  }
};

262
struct rocksdb_comparator_t : public Comparator {
263 264 265 266 267 268 269 270
  void* state_;
  void (*destructor_)(void*);
  int (*compare_)(
      void*,
      const char* a, size_t alen,
      const char* b, size_t blen);
  const char* (*name_)(void*);

271
  virtual ~rocksdb_comparator_t() {
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    (*destructor_)(state_);
  }

  virtual int Compare(const Slice& a, const Slice& b) const {
    return (*compare_)(state_, a.data(), a.size(), b.data(), b.size());
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }

  // No-ops since the C binding does not support key shortening methods.
  virtual void FindShortestSeparator(std::string*, const Slice&) const { }
  virtual void FindShortSuccessor(std::string* key) const { }
};

288
struct rocksdb_filterpolicy_t : public FilterPolicy {
S
Sanjay Ghemawat 已提交
289 290 291 292 293 294 295 296 297 298 299 300
  void* state_;
  void (*destructor_)(void*);
  const char* (*name_)(void*);
  char* (*create_)(
      void*,
      const char* const* key_array, const size_t* key_length_array,
      int num_keys,
      size_t* filter_length);
  unsigned char (*key_match_)(
      void*,
      const char* key, size_t length,
      const char* filter, size_t filter_length);
301 302 303
  void (*delete_filter_)(
      void*,
      const char* filter, size_t filter_length);
S
Sanjay Ghemawat 已提交
304

305
  virtual ~rocksdb_filterpolicy_t() {
S
Sanjay Ghemawat 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    (*destructor_)(state_);
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }

  virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
    std::vector<const char*> key_pointers(n);
    std::vector<size_t> key_sizes(n);
    for (int i = 0; i < n; i++) {
      key_pointers[i] = keys[i].data();
      key_sizes[i] = keys[i].size();
    }
    size_t len;
    char* filter = (*create_)(state_, &key_pointers[0], &key_sizes[0], n, &len);
    dst->append(filter, len);
323 324 325 326 327 328

    if (delete_filter_ != nullptr) {
      (*delete_filter_)(state_, filter, len);
    } else {
      free(filter);
    }
S
Sanjay Ghemawat 已提交
329 330 331 332 333 334 335 336
  }

  virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const {
    return (*key_match_)(state_, key.data(), key.size(),
                         filter.data(), filter.size());
  }
};

T
Thomas Adam 已提交
337 338 339 340 341 342 343 344 345 346 347
struct rocksdb_mergeoperator_t : public MergeOperator {
  void* state_;
  void (*destructor_)(void*);
  const char* (*name_)(void*);
  char* (*full_merge_)(
      void*,
      const char* key, size_t key_length,
      const char* existing_value, size_t existing_value_length,
      const char* const* operands_list, const size_t* operands_list_length,
      int num_operands,
      unsigned char* success, size_t* new_value_length);
348 349 350 351
  char* (*partial_merge_)(void*, const char* key, size_t key_length,
                          const char* const* operands_list,
                          const size_t* operands_list_length, int num_operands,
                          unsigned char* success, size_t* new_value_length);
352 353 354
  void (*delete_value_)(
      void*,
      const char* value, size_t value_length);
T
Thomas Adam 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

  virtual ~rocksdb_mergeoperator_t() {
    (*destructor_)(state_);
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }

  virtual bool FullMerge(
      const Slice& key,
      const Slice* existing_value,
      const std::deque<std::string>& operand_list,
      std::string* new_value,
      Logger* logger) const {

    size_t n = operand_list.size();
    std::vector<const char*> operand_pointers(n);
    std::vector<size_t> operand_sizes(n);
    for (size_t i = 0; i < n; i++) {
      Slice operand(operand_list[i]);
      operand_pointers[i] = operand.data();
      operand_sizes[i] = operand.size();
    }

    const char* existing_value_data = nullptr;
    size_t existing_value_len = 0;
    if (existing_value != nullptr) {
      existing_value_data = existing_value->data();
      existing_value_len = existing_value->size();
    }

    unsigned char success;
    size_t new_value_len;
    char* tmp_new_value = (*full_merge_)(
390 391 392
        state_, key.data(), key.size(), existing_value_data, existing_value_len,
        &operand_pointers[0], &operand_sizes[0], static_cast<int>(n), &success,
        &new_value_len);
T
Thomas Adam 已提交
393 394
    new_value->assign(tmp_new_value, new_value_len);

395 396 397 398 399 400
    if (delete_value_ != nullptr) {
      (*delete_value_)(state_, tmp_new_value, new_value_len);
    } else {
      free(tmp_new_value);
    }

T
Thomas Adam 已提交
401 402 403
    return success;
  }

404 405 406 407 408 409 410 411 412 413 414
  virtual bool PartialMergeMulti(const Slice& key,
                                 const std::deque<Slice>& operand_list,
                                 std::string* new_value, Logger* logger) const {
    size_t operand_count = operand_list.size();
    std::vector<const char*> operand_pointers(operand_count);
    std::vector<size_t> operand_sizes(operand_count);
    for (size_t i = 0; i < operand_count; ++i) {
      Slice operand(operand_list[i]);
      operand_pointers[i] = operand.data();
      operand_sizes[i] = operand.size();
    }
T
Thomas Adam 已提交
415 416 417 418

    unsigned char success;
    size_t new_value_len;
    char* tmp_new_value = (*partial_merge_)(
419
        state_, key.data(), key.size(), &operand_pointers[0], &operand_sizes[0],
420
        static_cast<int>(operand_count), &success, &new_value_len);
T
Thomas Adam 已提交
421 422
    new_value->assign(tmp_new_value, new_value_len);

423 424 425 426 427 428
    if (delete_value_ != nullptr) {
      (*delete_value_)(state_, tmp_new_value, new_value_len);
    } else {
      free(tmp_new_value);
    }

T
Thomas Adam 已提交
429 430 431 432
    return success;
  }
};

433
struct rocksdb_env_t {
434 435 436 437
  Env* rep;
  bool is_default;
};

T
Thomas Adam 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
struct rocksdb_slicetransform_t : public SliceTransform {
  void* state_;
  void (*destructor_)(void*);
  const char* (*name_)(void*);
  char* (*transform_)(
      void*,
      const char* key, size_t length,
      size_t* dst_length);
  unsigned char (*in_domain_)(
      void*,
      const char* key, size_t length);
  unsigned char (*in_range_)(
      void*,
      const char* key, size_t length);

  virtual ~rocksdb_slicetransform_t() {
    (*destructor_)(state_);
  }

  virtual const char* Name() const {
    return (*name_)(state_);
  }

  virtual Slice Transform(const Slice& src) const {
    size_t len;
    char* dst = (*transform_)(state_, src.data(), src.size(), &len);
    return Slice(dst, len);
  }

  virtual bool InDomain(const Slice& src) const {
    return (*in_domain_)(state_, src.data(), src.size());
  }

  virtual bool InRange(const Slice& src) const {
    return (*in_range_)(state_, src.data(), src.size());
  }
};

476 477 478 479
struct rocksdb_universal_compaction_options_t {
  rocksdb::CompactionOptionsUniversal *rep;
};

480
static bool SaveError(char** errptr, const Status& s) {
481
  assert(errptr != nullptr);
482 483
  if (s.ok()) {
    return false;
484
  } else if (*errptr == nullptr) {
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    *errptr = strdup(s.ToString().c_str());
  } else {
    // TODO(sanjay): Merge with existing error?
    free(*errptr);
    *errptr = strdup(s.ToString().c_str());
  }
  return true;
}

static char* CopyString(const std::string& str) {
  char* result = reinterpret_cast<char*>(malloc(sizeof(char) * str.size()));
  memcpy(result, str.data(), sizeof(char) * str.size());
  return result;
}

500 501
rocksdb_t* rocksdb_open(
    const rocksdb_options_t* options,
502 503 504 505
    const char* name,
    char** errptr) {
  DB* db;
  if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
506
    return nullptr;
507
  }
508
  rocksdb_t* result = new rocksdb_t;
509
  result->rep = db;
510 511 512 513 514 515 516 517 518 519 520 521 522 523
  return result;
}

rocksdb_t* rocksdb_open_for_read_only(
    const rocksdb_options_t* options,
    const char* name,
    unsigned char error_if_log_file_exist,
    char** errptr) {
  DB* db;
  if (SaveError(errptr, DB::OpenForReadOnly(options->rep, std::string(name), &db, error_if_log_file_exist))) {
    return nullptr;
  }
  rocksdb_t* result = new rocksdb_t;
  result->rep = db;
524 525 526
  return result;
}

527
void rocksdb_close(rocksdb_t* db) {
528 529 530 531
  delete db->rep;
  delete db;
}

R
Reed Allman 已提交
532 533 534 535
rocksdb_t* rocksdb_open_column_families(
    const rocksdb_options_t* db_options,
    const char* name,
    int num_column_families,
I
Igor Canadi 已提交
536 537
    const char** column_family_names,
    const rocksdb_options_t** column_family_options,
R
Reed Allman 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    rocksdb_column_family_handle_t** column_family_handles,
    char** errptr) {
  std::vector<ColumnFamilyDescriptor> column_families;
  for (int i = 0; i < num_column_families; i++) {
    column_families.push_back(ColumnFamilyDescriptor(
        std::string(column_family_names[i]),
        ColumnFamilyOptions(column_family_options[i]->rep)));
  }

  DB* db;
  std::vector<ColumnFamilyHandle*> handles;
  if (SaveError(errptr, DB::Open(DBOptions(db_options->rep),
          std::string(name), column_families, &handles, &db))) {
    return nullptr;
  }

  for (size_t i = 0; i < handles.size(); i++) {
    rocksdb_column_family_handle_t* c_handle = new rocksdb_column_family_handle_t;
    c_handle->rep = handles[i];
    column_family_handles[i] = c_handle;
  }
  rocksdb_t* result = new rocksdb_t;
  result->rep = db;
  return result;
}

rocksdb_t* rocksdb_open_for_read_only_column_families(
    const rocksdb_options_t* db_options,
    const char* name,
    int num_column_families,
    const char** column_family_names,
I
Igor Canadi 已提交
569
    const rocksdb_options_t** column_family_options,
R
Reed Allman 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    rocksdb_column_family_handle_t** column_family_handles,
    unsigned char error_if_log_file_exist,
    char** errptr) {
  std::vector<ColumnFamilyDescriptor> column_families;
  for (int i = 0; i < num_column_families; i++) {
    column_families.push_back(ColumnFamilyDescriptor(
        std::string(column_family_names[i]),
        ColumnFamilyOptions(column_family_options[i]->rep)));
  }

  DB* db;
  std::vector<ColumnFamilyHandle*> handles;
  if (SaveError(errptr, DB::OpenForReadOnly(DBOptions(db_options->rep),
          std::string(name), column_families, &handles, &db, error_if_log_file_exist))) {
    return nullptr;
  }

  for (size_t i = 0; i < handles.size(); i++) {
    rocksdb_column_family_handle_t* c_handle = new rocksdb_column_family_handle_t;
    c_handle->rep = handles[i];
    column_family_handles[i] = c_handle;
  }
  rocksdb_t* result = new rocksdb_t;
  result->rep = db;
  return result;
}

char** rocksdb_list_column_families(
    const rocksdb_options_t* options,
    const char* name,
    size_t* lencfs,
    char** errptr) {
  std::vector<std::string> fams;
  SaveError(errptr,
      DB::ListColumnFamilies(DBOptions(options->rep),
        std::string(name), &fams));

  *lencfs = fams.size();
  char** column_families = static_cast<char**>(malloc(sizeof(char*) * fams.size()));
  for (size_t i = 0; i < fams.size(); i++) {
    column_families[i] = strdup(fams[i].c_str());
  }
  return column_families;
}

I
Igor Canadi 已提交
615 616
void rocksdb_list_column_families_destroy(char** list, size_t len) {
  for (size_t i = 0; i < len; ++i) {
I
Igor Canadi 已提交
617
    free(list[i]);
I
Igor Canadi 已提交
618
  }
I
Igor Canadi 已提交
619
  free(list);
I
Igor Canadi 已提交
620 621
}

R
Reed Allman 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
rocksdb_column_family_handle_t* rocksdb_create_column_family(
    rocksdb_t* db,
    const rocksdb_options_t* column_family_options,
    const char* column_family_name,
    char** errptr) {
  rocksdb_column_family_handle_t* handle = new rocksdb_column_family_handle_t;
  SaveError(errptr,
      db->rep->CreateColumnFamily(ColumnFamilyOptions(column_family_options->rep),
        std::string(column_family_name), &(handle->rep)));
  return handle;
}

void rocksdb_drop_column_family(
    rocksdb_t* db,
    rocksdb_column_family_handle_t* handle,
    char** errptr) {
  SaveError(errptr, db->rep->DropColumnFamily(handle->rep));
}

void rocksdb_column_family_handle_destroy(rocksdb_column_family_handle_t* handle) {
  delete handle->rep;
  delete handle;
}

646 647 648
void rocksdb_put(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
649 650 651 652 653 654 655
    const char* key, size_t keylen,
    const char* val, size_t vallen,
    char** errptr) {
  SaveError(errptr,
            db->rep->Put(options->rep, Slice(key, keylen), Slice(val, vallen)));
}

R
Reed Allman 已提交
656 657 658 659 660 661 662 663 664 665 666 667
void rocksdb_put_cf(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t keylen,
    const char* val, size_t vallen,
    char** errptr) {
  SaveError(errptr,
            db->rep->Put(options->rep, column_family->rep,
              Slice(key, keylen), Slice(val, vallen)));
}

668 669 670
void rocksdb_delete(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
671 672 673 674 675
    const char* key, size_t keylen,
    char** errptr) {
  SaveError(errptr, db->rep->Delete(options->rep, Slice(key, keylen)));
}

R
Reed Allman 已提交
676 677 678 679 680 681 682 683 684 685
void rocksdb_delete_cf(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t keylen,
    char** errptr) {
  SaveError(errptr, db->rep->Delete(options->rep, column_family->rep,
        Slice(key, keylen)));
}

T
Thomas Adam 已提交
686 687 688 689 690 691 692 693 694
void rocksdb_merge(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
    const char* key, size_t keylen,
    const char* val, size_t vallen,
    char** errptr) {
  SaveError(errptr,
            db->rep->Merge(options->rep, Slice(key, keylen), Slice(val, vallen)));
}
695

R
Reed Allman 已提交
696 697 698 699 700 701 702 703 704 705 706 707
void rocksdb_merge_cf(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t keylen,
    const char* val, size_t vallen,
    char** errptr) {
  SaveError(errptr,
            db->rep->Merge(options->rep, column_family->rep,
              Slice(key, keylen), Slice(val, vallen)));
}

708 709 710 711
void rocksdb_write(
    rocksdb_t* db,
    const rocksdb_writeoptions_t* options,
    rocksdb_writebatch_t* batch,
712 713 714 715
    char** errptr) {
  SaveError(errptr, db->rep->Write(options->rep, &batch->rep));
}

716 717 718
char* rocksdb_get(
    rocksdb_t* db,
    const rocksdb_readoptions_t* options,
719 720 721
    const char* key, size_t keylen,
    size_t* vallen,
    char** errptr) {
722
  char* result = nullptr;
723 724 725 726 727 728 729 730 731 732 733 734 735 736
  std::string tmp;
  Status s = db->rep->Get(options->rep, Slice(key, keylen), &tmp);
  if (s.ok()) {
    *vallen = tmp.size();
    result = CopyString(tmp);
  } else {
    *vallen = 0;
    if (!s.IsNotFound()) {
      SaveError(errptr, s);
    }
  }
  return result;
}

R
Reed Allman 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
char* rocksdb_get_cf(
    rocksdb_t* db,
    const rocksdb_readoptions_t* options,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t keylen,
    size_t* vallen,
    char** errptr) {
  char* result = nullptr;
  std::string tmp;
  Status s = db->rep->Get(options->rep, column_family->rep,
      Slice(key, keylen), &tmp);
  if (s.ok()) {
    *vallen = tmp.size();
    result = CopyString(tmp);
  } else {
    *vallen = 0;
    if (!s.IsNotFound()) {
      SaveError(errptr, s);
    }
  }
  return result;
}

760 761 762 763
rocksdb_iterator_t* rocksdb_create_iterator(
    rocksdb_t* db,
    const rocksdb_readoptions_t* options) {
  rocksdb_iterator_t* result = new rocksdb_iterator_t;
764 765 766 767
  result->rep = db->rep->NewIterator(options->rep);
  return result;
}

R
Reed Allman 已提交
768 769 770 771 772 773 774 775 776
rocksdb_iterator_t* rocksdb_create_iterator_cf(
    rocksdb_t* db,
    const rocksdb_readoptions_t* options,
    rocksdb_column_family_handle_t* column_family) {
  rocksdb_iterator_t* result = new rocksdb_iterator_t;
  result->rep = db->rep->NewIterator(options->rep, column_family->rep);
  return result;
}

777 778 779
const rocksdb_snapshot_t* rocksdb_create_snapshot(
    rocksdb_t* db) {
  rocksdb_snapshot_t* result = new rocksdb_snapshot_t;
780 781 782 783
  result->rep = db->rep->GetSnapshot();
  return result;
}

784 785 786
void rocksdb_release_snapshot(
    rocksdb_t* db,
    const rocksdb_snapshot_t* snapshot) {
787 788 789 790
  db->rep->ReleaseSnapshot(snapshot->rep);
  delete snapshot;
}

791 792
char* rocksdb_property_value(
    rocksdb_t* db,
793 794 795
    const char* propname) {
  std::string tmp;
  if (db->rep->GetProperty(Slice(propname), &tmp)) {
796 797
    // We use strdup() since we expect human readable output.
    return strdup(tmp.c_str());
798
  } else {
799
    return nullptr;
800 801 802
  }
}

R
Reed Allman 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815
char* rocksdb_property_value_cf(
    rocksdb_t* db,
    rocksdb_column_family_handle_t* column_family,
    const char* propname) {
  std::string tmp;
  if (db->rep->GetProperty(column_family->rep, Slice(propname), &tmp)) {
    // We use strdup() since we expect human readable output.
    return strdup(tmp.c_str());
  } else {
    return nullptr;
  }
}

816 817
void rocksdb_approximate_sizes(
    rocksdb_t* db,
818 819 820 821 822 823 824 825 826 827 828 829 830
    int num_ranges,
    const char* const* range_start_key, const size_t* range_start_key_len,
    const char* const* range_limit_key, const size_t* range_limit_key_len,
    uint64_t* sizes) {
  Range* ranges = new Range[num_ranges];
  for (int i = 0; i < num_ranges; i++) {
    ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
    ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
  }
  db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
  delete[] ranges;
}

R
Reed Allman 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
void rocksdb_approximate_sizes_cf(
    rocksdb_t* db,
    rocksdb_column_family_handle_t* column_family,
    int num_ranges,
    const char* const* range_start_key, const size_t* range_start_key_len,
    const char* const* range_limit_key, const size_t* range_limit_key_len,
    uint64_t* sizes) {
  Range* ranges = new Range[num_ranges];
  for (int i = 0; i < num_ranges; i++) {
    ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
    ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
  }
  db->rep->GetApproximateSizes(column_family->rep, ranges, num_ranges, sizes);
  delete[] ranges;
}

A
Albert Strasheim 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859
void rocksdb_delete_file(
    rocksdb_t* db,
    const char* name) {
  db->rep->DeleteFile(name);
}

const rocksdb_livefiles_t* rocksdb_livefiles(
    rocksdb_t* db) {
  rocksdb_livefiles_t* result = new rocksdb_livefiles_t;
  db->rep->GetLiveFilesMetaData(&result->rep);
  return result;
}

860 861
void rocksdb_compact_range(
    rocksdb_t* db,
S
Sanjay Ghemawat 已提交
862 863 864 865
    const char* start_key, size_t start_key_len,
    const char* limit_key, size_t limit_key_len) {
  Slice a, b;
  db->rep->CompactRange(
866 867 868
      // Pass nullptr Slice if corresponding "const char*" is nullptr
      (start_key ? (a = Slice(start_key, start_key_len), &a) : nullptr),
      (limit_key ? (b = Slice(limit_key, limit_key_len), &b) : nullptr));
S
Sanjay Ghemawat 已提交
869 870
}

R
Reed Allman 已提交
871 872 873 874 875 876 877
void rocksdb_compact_range_cf(
    rocksdb_t* db,
    rocksdb_column_family_handle_t* column_family,
    const char* start_key, size_t start_key_len,
    const char* limit_key, size_t limit_key_len) {
  Slice a, b;
  db->rep->CompactRange(
878
      column_family->rep,
R
Reed Allman 已提交
879 880 881 882 883
      // Pass nullptr Slice if corresponding "const char*" is nullptr
      (start_key ? (a = Slice(start_key, start_key_len), &a) : nullptr),
      (limit_key ? (b = Slice(limit_key, limit_key_len), &b) : nullptr));
}

T
Thomas Adam 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
void rocksdb_flush(
    rocksdb_t* db,
    const rocksdb_flushoptions_t* options,
    char** errptr) {
  SaveError(errptr, db->rep->Flush(options->rep));
}

void rocksdb_disable_file_deletions(
    rocksdb_t* db,
    char** errptr) {
  SaveError(errptr, db->rep->DisableFileDeletions());
}

void rocksdb_enable_file_deletions(
    rocksdb_t* db,
    unsigned char force,
    char** errptr) {
  SaveError(errptr, db->rep->EnableFileDeletions(force));
}

904 905
void rocksdb_destroy_db(
    const rocksdb_options_t* options,
906 907 908 909 910
    const char* name,
    char** errptr) {
  SaveError(errptr, DestroyDB(name, options->rep));
}

911 912
void rocksdb_repair_db(
    const rocksdb_options_t* options,
913 914 915 916 917
    const char* name,
    char** errptr) {
  SaveError(errptr, RepairDB(name, options->rep));
}

918
void rocksdb_iter_destroy(rocksdb_iterator_t* iter) {
919 920 921 922
  delete iter->rep;
  delete iter;
}

923
unsigned char rocksdb_iter_valid(const rocksdb_iterator_t* iter) {
924 925 926
  return iter->rep->Valid();
}

927
void rocksdb_iter_seek_to_first(rocksdb_iterator_t* iter) {
928 929 930
  iter->rep->SeekToFirst();
}

931
void rocksdb_iter_seek_to_last(rocksdb_iterator_t* iter) {
932 933 934
  iter->rep->SeekToLast();
}

935
void rocksdb_iter_seek(rocksdb_iterator_t* iter, const char* k, size_t klen) {
936 937 938
  iter->rep->Seek(Slice(k, klen));
}

939
void rocksdb_iter_next(rocksdb_iterator_t* iter) {
940 941 942
  iter->rep->Next();
}

943
void rocksdb_iter_prev(rocksdb_iterator_t* iter) {
944 945 946
  iter->rep->Prev();
}

947
const char* rocksdb_iter_key(const rocksdb_iterator_t* iter, size_t* klen) {
948 949 950 951 952
  Slice s = iter->rep->key();
  *klen = s.size();
  return s.data();
}

953
const char* rocksdb_iter_value(const rocksdb_iterator_t* iter, size_t* vlen) {
954 955 956 957 958
  Slice s = iter->rep->value();
  *vlen = s.size();
  return s.data();
}

959
void rocksdb_iter_get_error(const rocksdb_iterator_t* iter, char** errptr) {
960 961 962
  SaveError(errptr, iter->rep->status());
}

963 964
rocksdb_writebatch_t* rocksdb_writebatch_create() {
  return new rocksdb_writebatch_t;
965 966
}

967 968 969 970 971 972 973
rocksdb_writebatch_t* rocksdb_writebatch_create_from(const char* rep,
                                                     size_t size) {
  rocksdb_writebatch_t* b = new rocksdb_writebatch_t;
  b->rep = WriteBatch(std::string(rep, size));
  return b;
}

974
void rocksdb_writebatch_destroy(rocksdb_writebatch_t* b) {
975 976 977
  delete b;
}

978
void rocksdb_writebatch_clear(rocksdb_writebatch_t* b) {
979 980 981
  b->rep.Clear();
}

A
Albert Strasheim 已提交
982 983 984 985
int rocksdb_writebatch_count(rocksdb_writebatch_t* b) {
  return b->rep.Count();
}

986 987
void rocksdb_writebatch_put(
    rocksdb_writebatch_t* b,
988 989 990 991 992
    const char* key, size_t klen,
    const char* val, size_t vlen) {
  b->rep.Put(Slice(key, klen), Slice(val, vlen));
}

R
Reed Allman 已提交
993 994 995 996 997 998 999 1000
void rocksdb_writebatch_put_cf(
    rocksdb_writebatch_t* b,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t klen,
    const char* val, size_t vlen) {
  b->rep.Put(column_family->rep, Slice(key, klen), Slice(val, vlen));
}

T
Thomas Adam 已提交
1001 1002 1003 1004 1005 1006 1007
void rocksdb_writebatch_merge(
    rocksdb_writebatch_t* b,
    const char* key, size_t klen,
    const char* val, size_t vlen) {
  b->rep.Merge(Slice(key, klen), Slice(val, vlen));
}

R
Reed Allman 已提交
1008 1009 1010 1011 1012 1013 1014 1015
void rocksdb_writebatch_merge_cf(
    rocksdb_writebatch_t* b,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t klen,
    const char* val, size_t vlen) {
  b->rep.Merge(column_family->rep, Slice(key, klen), Slice(val, vlen));
}

1016 1017
void rocksdb_writebatch_delete(
    rocksdb_writebatch_t* b,
1018 1019 1020 1021
    const char* key, size_t klen) {
  b->rep.Delete(Slice(key, klen));
}

R
Reed Allman 已提交
1022 1023 1024 1025 1026 1027 1028
void rocksdb_writebatch_delete_cf(
    rocksdb_writebatch_t* b,
    rocksdb_column_family_handle_t* column_family,
    const char* key, size_t klen) {
  b->rep.Delete(column_family->rep, Slice(key, klen));
}

1029 1030
void rocksdb_writebatch_iterate(
    rocksdb_writebatch_t* b,
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    void* state,
    void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
    void (*deleted)(void*, const char* k, size_t klen)) {
  class H : public WriteBatch::Handler {
   public:
    void* state_;
    void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
    void (*deleted_)(void*, const char* k, size_t klen);
    virtual void Put(const Slice& key, const Slice& value) {
      (*put_)(state_, key.data(), key.size(), value.data(), value.size());
    }
    virtual void Delete(const Slice& key) {
      (*deleted_)(state_, key.data(), key.size());
    }
  };
  H handler;
  handler.state_ = state;
  handler.put_ = put;
  handler.deleted_ = deleted;
  b->rep.Iterate(&handler);
}

A
Albert Strasheim 已提交
1053 1054 1055 1056 1057
const char* rocksdb_writebatch_data(rocksdb_writebatch_t* b, size_t* size) {
  *size = b->rep.GetDataSize();
  return b->rep.Data().c_str();
}

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
rocksdb_block_based_table_options_t*
rocksdb_block_based_options_create() {
  return new rocksdb_block_based_table_options_t;
}

void rocksdb_block_based_options_destroy(
    rocksdb_block_based_table_options_t* options) {
  delete options;
}

void rocksdb_block_based_options_set_block_size(
    rocksdb_block_based_table_options_t* options, size_t block_size) {
  options->rep.block_size = block_size;
}

void rocksdb_block_based_options_set_block_size_deviation(
    rocksdb_block_based_table_options_t* options, int block_size_deviation) {
  options->rep.block_size_deviation = block_size_deviation;
}

void rocksdb_block_based_options_set_block_restart_interval(
    rocksdb_block_based_table_options_t* options, int block_restart_interval) {
  options->rep.block_restart_interval = block_restart_interval;
}

void rocksdb_block_based_options_set_filter_policy(
    rocksdb_block_based_table_options_t* options,
    rocksdb_filterpolicy_t* filter_policy) {
  options->rep.filter_policy.reset(filter_policy);
}

void rocksdb_block_based_options_set_no_block_cache(
    rocksdb_block_based_table_options_t* options,
    unsigned char no_block_cache) {
  options->rep.no_block_cache = no_block_cache;
}

void rocksdb_block_based_options_set_block_cache(
    rocksdb_block_based_table_options_t* options,
    rocksdb_cache_t* block_cache) {
  if (block_cache) {
    options->rep.block_cache = block_cache->rep;
  }
}

void rocksdb_block_based_options_set_block_cache_compressed(
    rocksdb_block_based_table_options_t* options,
    rocksdb_cache_t* block_cache_compressed) {
  if (block_cache_compressed) {
    options->rep.block_cache_compressed = block_cache_compressed->rep;
  }
}

void rocksdb_block_based_options_set_whole_key_filtering(
    rocksdb_block_based_table_options_t* options, unsigned char v) {
  options->rep.whole_key_filtering = v;
}

void rocksdb_options_set_block_based_table_factory(
    rocksdb_options_t *opt,
    rocksdb_block_based_table_options_t* table_options) {
  if (table_options) {
    opt->rep.table_factory.reset(
        rocksdb::NewBlockBasedTableFactory(table_options->rep));
  }
}


1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
rocksdb_cuckoo_table_options_t*
rocksdb_cuckoo_options_create() {
  return new rocksdb_cuckoo_table_options_t;
}

void rocksdb_cuckoo_options_destroy(
    rocksdb_cuckoo_table_options_t* options) {
  delete options;
}

void rocksdb_cuckoo_options_set_hash_ratio(
    rocksdb_cuckoo_table_options_t* options, double v) {
  options->rep.hash_table_ratio = v;
}

void rocksdb_cuckoo_options_set_max_search_depth(
    rocksdb_cuckoo_table_options_t* options, uint32_t v) {
  options->rep.max_search_depth = v;
}

void rocksdb_cuckoo_options_set_cuckoo_block_size(
    rocksdb_cuckoo_table_options_t* options, uint32_t v) {
  options->rep.cuckoo_block_size = v;
}

void rocksdb_cuckoo_options_set_identity_as_first_hash(
    rocksdb_cuckoo_table_options_t* options, unsigned char v) {
  options->rep.identity_as_first_hash = v;
}

void rocksdb_cuckoo_options_set_use_module_hash(
    rocksdb_cuckoo_table_options_t* options, unsigned char v) {
  options->rep.use_module_hash = v;
}

void rocksdb_options_set_cuckoo_table_factory(
    rocksdb_options_t *opt,
    rocksdb_cuckoo_table_options_t* table_options) {
  if (table_options) {
    opt->rep.table_factory.reset(
        rocksdb::NewCuckooTableFactory(table_options->rep));
  }
}


1171 1172
rocksdb_options_t* rocksdb_options_create() {
  return new rocksdb_options_t;
1173 1174
}

1175
void rocksdb_options_destroy(rocksdb_options_t* options) {
1176 1177 1178
  delete options;
}

1179 1180 1181 1182 1183 1184
void rocksdb_options_increase_parallelism(
    rocksdb_options_t* opt, int total_threads) {
  opt->rep.IncreaseParallelism(total_threads);
}

void rocksdb_options_optimize_for_point_lookup(
L
Lei Jin 已提交
1185 1186
    rocksdb_options_t* opt, uint64_t block_cache_size_mb) {
  opt->rep.OptimizeForPointLookup(block_cache_size_mb);
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
}

void rocksdb_options_optimize_level_style_compaction(
    rocksdb_options_t* opt, uint64_t memtable_memory_budget) {
  opt->rep.OptimizeLevelStyleCompaction(memtable_memory_budget);
}

void rocksdb_options_optimize_universal_style_compaction(
    rocksdb_options_t* opt, uint64_t memtable_memory_budget) {
  opt->rep.OptimizeUniversalStyleCompaction(memtable_memory_budget);
}

1199 1200 1201 1202 1203 1204
void rocksdb_options_set_compaction_filter(
    rocksdb_options_t* opt,
    rocksdb_compactionfilter_t* filter) {
  opt->rep.compaction_filter = filter;
}

1205 1206 1207 1208 1209 1210
void rocksdb_options_set_compaction_filter_factory(
    rocksdb_options_t* opt, rocksdb_compactionfilterfactory_t* factory) {
  opt->rep.compaction_filter_factory =
      std::shared_ptr<CompactionFilterFactory>(factory);
}

1211 1212 1213
void rocksdb_options_set_comparator(
    rocksdb_options_t* opt,
    rocksdb_comparator_t* cmp) {
1214 1215 1216
  opt->rep.comparator = cmp;
}

I
Igor Canadi 已提交
1217
void rocksdb_options_set_merge_operator(
T
Thomas Adam 已提交
1218 1219 1220 1221 1222
    rocksdb_options_t* opt,
    rocksdb_mergeoperator_t* merge_operator) {
  opt->rep.merge_operator = std::shared_ptr<MergeOperator>(merge_operator);
}

1223 1224 1225 1226 1227 1228
void rocksdb_options_set_compaction_filter_factory_v2(
    rocksdb_options_t* opt,
    rocksdb_compactionfilterfactoryv2_t* compaction_filter_factory_v2) {
  opt->rep.compaction_filter_factory_v2 = std::shared_ptr<CompactionFilterFactoryV2>(compaction_filter_factory_v2);
}

1229 1230
void rocksdb_options_set_create_if_missing(
    rocksdb_options_t* opt, unsigned char v) {
1231 1232 1233
  opt->rep.create_if_missing = v;
}

1234 1235 1236 1237 1238
void rocksdb_options_set_create_missing_column_families(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.create_missing_column_families = v;
}

1239 1240
void rocksdb_options_set_error_if_exists(
    rocksdb_options_t* opt, unsigned char v) {
1241 1242 1243
  opt->rep.error_if_exists = v;
}

1244 1245
void rocksdb_options_set_paranoid_checks(
    rocksdb_options_t* opt, unsigned char v) {
1246 1247 1248
  opt->rep.paranoid_checks = v;
}

1249
void rocksdb_options_set_env(rocksdb_options_t* opt, rocksdb_env_t* env) {
1250
  opt->rep.env = (env ? env->rep : nullptr);
1251 1252
}

1253
void rocksdb_options_set_info_log(rocksdb_options_t* opt, rocksdb_logger_t* l) {
1254 1255 1256
  if (l) {
    opt->rep.info_log = l->rep;
  }
1257 1258
}

T
Thomas Adam 已提交
1259 1260 1261 1262 1263
void rocksdb_options_set_info_log_level(
    rocksdb_options_t* opt, int v) {
  opt->rep.info_log_level = static_cast<InfoLogLevel>(v);
}

1264
void rocksdb_options_set_write_buffer_size(rocksdb_options_t* opt, size_t s) {
1265 1266 1267
  opt->rep.write_buffer_size = s;
}

1268
void rocksdb_options_set_max_open_files(rocksdb_options_t* opt, int n) {
1269 1270 1271
  opt->rep.max_open_files = n;
}

1272 1273
void rocksdb_options_set_target_file_size_base(
    rocksdb_options_t* opt, uint64_t n) {
1274 1275 1276
  opt->rep.target_file_size_base = n;
}

1277 1278
void rocksdb_options_set_target_file_size_multiplier(
    rocksdb_options_t* opt, int n) {
1279 1280 1281
  opt->rep.target_file_size_multiplier = n;
}

1282 1283
void rocksdb_options_set_max_bytes_for_level_base(
    rocksdb_options_t* opt, uint64_t n) {
1284 1285 1286
  opt->rep.max_bytes_for_level_base = n;
}

1287 1288
void rocksdb_options_set_max_bytes_for_level_multiplier(
    rocksdb_options_t* opt, int n) {
1289 1290 1291
  opt->rep.max_bytes_for_level_multiplier = n;
}

1292 1293
void rocksdb_options_set_expanded_compaction_factor(
    rocksdb_options_t* opt, int n) {
1294 1295 1296
  opt->rep.expanded_compaction_factor = n;
}

1297 1298
void rocksdb_options_set_max_grandparent_overlap_factor(
    rocksdb_options_t* opt, int n) {
1299 1300 1301
  opt->rep.max_grandparent_overlap_factor = n;
}

T
Thomas Adam 已提交
1302 1303 1304 1305 1306 1307 1308 1309
void rocksdb_options_set_max_bytes_for_level_multiplier_additional(
    rocksdb_options_t* opt, int* level_values, size_t num_levels) {
  opt->rep.max_bytes_for_level_multiplier_additional.resize(num_levels);
  for (size_t i = 0; i < num_levels; ++i) {
    opt->rep.max_bytes_for_level_multiplier_additional[i] = level_values[i];
  }
}

T
Thomas Adam 已提交
1310 1311 1312 1313
void rocksdb_options_enable_statistics(rocksdb_options_t* opt) {
  opt->rep.statistics = rocksdb::CreateDBStatistics();
}

1314
void rocksdb_options_set_num_levels(rocksdb_options_t* opt, int n) {
A
Abhishek Kona 已提交
1315
  opt->rep.num_levels = n;
1316 1317
}

1318 1319
void rocksdb_options_set_level0_file_num_compaction_trigger(
    rocksdb_options_t* opt, int n) {
A
Abhishek Kona 已提交
1320
  opt->rep.level0_file_num_compaction_trigger = n;
1321 1322
}

1323 1324
void rocksdb_options_set_level0_slowdown_writes_trigger(
    rocksdb_options_t* opt, int n) {
A
Abhishek Kona 已提交
1325
  opt->rep.level0_slowdown_writes_trigger = n;
1326 1327
}

1328 1329
void rocksdb_options_set_level0_stop_writes_trigger(
    rocksdb_options_t* opt, int n) {
A
Abhishek Kona 已提交
1330
  opt->rep.level0_stop_writes_trigger = n;
1331 1332
}

1333 1334
void rocksdb_options_set_max_mem_compaction_level(
    rocksdb_options_t* opt, int n) {
A
Abhishek Kona 已提交
1335
  opt->rep.max_mem_compaction_level = n;
1336 1337
}

1338
void rocksdb_options_set_compression(rocksdb_options_t* opt, int t) {
1339 1340 1341
  opt->rep.compression = static_cast<CompressionType>(t);
}

1342
void rocksdb_options_set_compression_per_level(rocksdb_options_t* opt,
1343 1344 1345 1346 1347 1348 1349 1350 1351
                                               int* level_values,
                                               size_t num_levels) {
  opt->rep.compression_per_level.resize(num_levels);
  for (size_t i = 0; i < num_levels; ++i) {
    opt->rep.compression_per_level[i] =
      static_cast<CompressionType>(level_values[i]);
  }
}

1352 1353
void rocksdb_options_set_compression_options(
    rocksdb_options_t* opt, int w_bits, int level, int strategy) {
1354 1355 1356 1357 1358
  opt->rep.compression_opts.window_bits = w_bits;
  opt->rep.compression_opts.level = level;
  opt->rep.compression_opts.strategy = strategy;
}

T
Thomas Adam 已提交
1359 1360
void rocksdb_options_set_prefix_extractor(
    rocksdb_options_t* opt, rocksdb_slicetransform_t* prefix_extractor) {
1361
  opt->rep.prefix_extractor.reset(prefix_extractor);
T
Thomas Adam 已提交
1362 1363
}

1364
void rocksdb_options_set_disable_data_sync(
1365
    rocksdb_options_t* opt, int disable_data_sync) {
H
heyongqiang 已提交
1366 1367 1368
  opt->rep.disableDataSync = disable_data_sync;
}

1369
void rocksdb_options_set_use_fsync(
1370
    rocksdb_options_t* opt, int use_fsync) {
H
heyongqiang 已提交
1371 1372 1373
  opt->rep.use_fsync = use_fsync;
}

1374 1375
void rocksdb_options_set_db_log_dir(
    rocksdb_options_t* opt, const char* db_log_dir) {
H
heyongqiang 已提交
1376 1377 1378
  opt->rep.db_log_dir = db_log_dir;
}

T
Thomas Adam 已提交
1379 1380 1381 1382 1383
void rocksdb_options_set_wal_dir(
    rocksdb_options_t* opt, const char* v) {
  opt->rep.wal_dir = v;
}

1384
void rocksdb_options_set_WAL_ttl_seconds(rocksdb_options_t* opt, uint64_t ttl) {
1385 1386 1387
  opt->rep.WAL_ttl_seconds = ttl;
}

1388 1389
void rocksdb_options_set_WAL_size_limit_MB(
    rocksdb_options_t* opt, uint64_t limit) {
1390 1391 1392
  opt->rep.WAL_size_limit_MB = limit;
}

T
Thomas Adam 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
void rocksdb_options_set_manifest_preallocation_size(
    rocksdb_options_t* opt, size_t v) {
  opt->rep.manifest_preallocation_size = v;
}

void rocksdb_options_set_purge_redundant_kvs_while_flush(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.purge_redundant_kvs_while_flush = v;
}

I
Igor Canadi 已提交
1403 1404
void rocksdb_options_set_allow_os_buffer(rocksdb_options_t* opt,
                                         unsigned char v) {
T
Thomas Adam 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
  opt->rep.allow_os_buffer = v;
}

void rocksdb_options_set_allow_mmap_reads(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.allow_mmap_reads = v;
}

void rocksdb_options_set_allow_mmap_writes(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.allow_mmap_writes = v;
}

void rocksdb_options_set_is_fd_close_on_exec(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.is_fd_close_on_exec = v;
}

void rocksdb_options_set_skip_log_error_on_recovery(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.skip_log_error_on_recovery = v;
}

void rocksdb_options_set_stats_dump_period_sec(
    rocksdb_options_t* opt, unsigned int v) {
  opt->rep.stats_dump_period_sec = v;
}

void rocksdb_options_set_advise_random_on_open(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.advise_random_on_open = v;
}

T
Thomas Adam 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
void rocksdb_options_set_access_hint_on_compaction_start(
    rocksdb_options_t* opt, int v) {
  switch(v) {
    case 0:
      opt->rep.access_hint_on_compaction_start = rocksdb::Options::NONE;
      break;
    case 1:
      opt->rep.access_hint_on_compaction_start = rocksdb::Options::NORMAL;
      break;
    case 2:
      opt->rep.access_hint_on_compaction_start = rocksdb::Options::SEQUENTIAL;
      break;
    case 3:
      opt->rep.access_hint_on_compaction_start = rocksdb::Options::WILLNEED;
      break;
  }
}

T
Thomas Adam 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
void rocksdb_options_set_use_adaptive_mutex(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.use_adaptive_mutex = v;
}

void rocksdb_options_set_bytes_per_sync(
    rocksdb_options_t* opt, uint64_t v) {
  opt->rep.bytes_per_sync = v;
}

T
Thomas Adam 已提交
1466 1467 1468 1469 1470
void rocksdb_options_set_verify_checksums_in_compaction(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.verify_checksums_in_compaction = v;
}

T
Thomas Adam 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
void rocksdb_options_set_filter_deletes(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.filter_deletes = v;
}

void rocksdb_options_set_max_sequential_skip_in_iterations(
    rocksdb_options_t* opt, uint64_t v) {
  opt->rep.max_sequential_skip_in_iterations = v;
}

1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
void rocksdb_options_set_max_write_buffer_number(rocksdb_options_t* opt, int n) {
  opt->rep.max_write_buffer_number = n;
}

void rocksdb_options_set_min_write_buffer_number_to_merge(rocksdb_options_t* opt, int n) {
  opt->rep.min_write_buffer_number_to_merge = n;
}

void rocksdb_options_set_max_background_compactions(rocksdb_options_t* opt, int n) {
  opt->rep.max_background_compactions = n;
}

void rocksdb_options_set_max_background_flushes(rocksdb_options_t* opt, int n) {
  opt->rep.max_background_flushes = n;
}

T
Thomas Adam 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
void rocksdb_options_set_max_log_file_size(rocksdb_options_t* opt, size_t v) {
  opt->rep.max_log_file_size = v;
}

void rocksdb_options_set_log_file_time_to_roll(rocksdb_options_t* opt, size_t v) {
  opt->rep.log_file_time_to_roll = v;
}

void rocksdb_options_set_keep_log_file_num(rocksdb_options_t* opt, size_t v) {
  opt->rep.keep_log_file_num = v;
}

void rocksdb_options_set_soft_rate_limit(rocksdb_options_t* opt, double v) {
  opt->rep.soft_rate_limit = v;
}

void rocksdb_options_set_hard_rate_limit(rocksdb_options_t* opt, double v) {
  opt->rep.hard_rate_limit = v;
}

void rocksdb_options_set_rate_limit_delay_max_milliseconds(
    rocksdb_options_t* opt, unsigned int v) {
  opt->rep.rate_limit_delay_max_milliseconds = v;
}

void rocksdb_options_set_max_manifest_file_size(
    rocksdb_options_t* opt, size_t v) {
  opt->rep.max_manifest_file_size = v;
}

void rocksdb_options_set_table_cache_numshardbits(
    rocksdb_options_t* opt, int v) {
  opt->rep.table_cache_numshardbits = v;
}

void rocksdb_options_set_table_cache_remove_scan_count_limit(
    rocksdb_options_t* opt, int v) {
  opt->rep.table_cache_remove_scan_count_limit = v;
}

void rocksdb_options_set_arena_block_size(
    rocksdb_options_t* opt, size_t v) {
  opt->rep.arena_block_size = v;
}

1542 1543 1544 1545
void rocksdb_options_set_disable_auto_compactions(rocksdb_options_t* opt, int disable) {
  opt->rep.disable_auto_compactions = disable;
}

T
Thomas Adam 已提交
1546 1547 1548 1549 1550
void rocksdb_options_set_delete_obsolete_files_period_micros(
    rocksdb_options_t* opt, uint64_t v) {
  opt->rep.delete_obsolete_files_period_micros = v;
}

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
void rocksdb_options_set_source_compaction_factor(
    rocksdb_options_t* opt, int n) {
  opt->rep.expanded_compaction_factor = n;
}

void rocksdb_options_prepare_for_bulk_load(rocksdb_options_t* opt) {
  opt->rep.PrepareForBulkLoad();
}

void rocksdb_options_set_memtable_vector_rep(rocksdb_options_t *opt) {
  static rocksdb::VectorRepFactory* factory = 0;
  if (!factory) {
    factory = new rocksdb::VectorRepFactory;
  }
  opt->rep.memtable_factory.reset(factory);
}

T
Thomas Adam 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
void rocksdb_options_set_memtable_prefix_bloom_bits(
    rocksdb_options_t* opt, uint32_t v) {
  opt->rep.memtable_prefix_bloom_bits = v;
}

void rocksdb_options_set_memtable_prefix_bloom_probes(
    rocksdb_options_t* opt, uint32_t v) {
  opt->rep.memtable_prefix_bloom_probes = v;
}

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
void rocksdb_options_set_hash_skip_list_rep(
    rocksdb_options_t *opt, size_t bucket_count,
    int32_t skiplist_height, int32_t skiplist_branching_factor) {
  static rocksdb::MemTableRepFactory* factory = 0;
  if (!factory) {
    factory = rocksdb::NewHashSkipListRepFactory(
        bucket_count, skiplist_height, skiplist_branching_factor);
  }
  opt->rep.memtable_factory.reset(factory);
}

void rocksdb_options_set_hash_link_list_rep(
    rocksdb_options_t *opt, size_t bucket_count) {
  static rocksdb::MemTableRepFactory* factory = 0;
  if (!factory) {
    factory = rocksdb::NewHashLinkListRepFactory(bucket_count);
  }
  opt->rep.memtable_factory.reset(factory);
}

1598 1599 1600 1601 1602
void rocksdb_options_set_plain_table_factory(
    rocksdb_options_t *opt, uint32_t user_key_len, int bloom_bits_per_key,
    double hash_table_ratio, size_t index_sparseness) {
  static rocksdb::TableFactory* factory = 0;
  if (!factory) {
S
Stanislau Hlebik 已提交
1603 1604 1605 1606 1607 1608 1609
    rocksdb::PlainTableOptions options;
    options.user_key_len = user_key_len;
    options.bloom_bits_per_key = bloom_bits_per_key;
    options.hash_table_ratio = hash_table_ratio;
    options.index_sparseness = index_sparseness;

    factory = rocksdb::NewPlainTableFactory(options);
1610 1611 1612 1613
  }
  opt->rep.table_factory.reset(factory);
}

T
Thomas Adam 已提交
1614 1615 1616 1617 1618
void rocksdb_options_set_max_successive_merges(
    rocksdb_options_t* opt, size_t v) {
  opt->rep.max_successive_merges = v;
}

T
Thomas Adam 已提交
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
void rocksdb_options_set_min_partial_merge_operands(
    rocksdb_options_t* opt, uint32_t v) {
  opt->rep.min_partial_merge_operands = v;
}

void rocksdb_options_set_bloom_locality(
    rocksdb_options_t* opt, uint32_t v) {
  opt->rep.bloom_locality = v;
}

void rocksdb_options_set_inplace_update_support(
    rocksdb_options_t* opt, unsigned char v) {
  opt->rep.inplace_update_support = v;
}

void rocksdb_options_set_inplace_update_num_locks(
    rocksdb_options_t* opt, size_t v) {
  opt->rep.inplace_update_num_locks = v;
}

1639 1640 1641 1642 1643 1644 1645 1646
void rocksdb_options_set_compaction_style(rocksdb_options_t *opt, int style) {
  opt->rep.compaction_style = static_cast<rocksdb::CompactionStyle>(style);
}

void rocksdb_options_set_universal_compaction_options(rocksdb_options_t *opt, rocksdb_universal_compaction_options_t *uco) {
  opt->rep.compaction_options_universal = *(uco->rep);
}

1647 1648 1649 1650 1651 1652
void rocksdb_options_set_fifo_compaction_options(
    rocksdb_options_t* opt,
    rocksdb_fifo_compaction_options_t* fifo) {
  opt->rep.compaction_options_fifo = fifo->rep;
}

1653 1654
/*
TODO:
T
Thomas Adam 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
DB::OpenForReadOnly
DB::MultiGet
DB::KeyMayExist
DB::GetOptions
DB::GetSortedWalFiles
DB::GetLatestSequenceNumber
DB::GetUpdatesSince
DB::GetDbIdentity
DB::RunManualCompaction
custom cache
1665 1666 1667
table_properties_collectors
*/

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
rocksdb_compactionfilter_t* rocksdb_compactionfilter_create(
    void* state,
    void (*destructor)(void*),
    unsigned char (*filter)(
        void*,
        int level,
        const char* key, size_t key_length,
        const char* existing_value, size_t value_length,
        char** new_value, size_t *new_value_length,
        unsigned char* value_changed),
    const char* (*name)(void*)) {
  rocksdb_compactionfilter_t* result = new rocksdb_compactionfilter_t;
  result->state_ = state;
  result->destructor_ = destructor;
  result->filter_ = filter;
  result->name_ = name;
  return result;
}

void rocksdb_compactionfilter_destroy(rocksdb_compactionfilter_t* filter) {
  delete filter;
}

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
unsigned char rocksdb_compactionfiltercontext_is_full_compaction(
    rocksdb_compactionfiltercontext_t* context) {
  return context->rep.is_full_compaction;
}

unsigned char rocksdb_compactionfiltercontext_is_manual_compaction(
    rocksdb_compactionfiltercontext_t* context) {
  return context->rep.is_manual_compaction;
}

rocksdb_compactionfilterfactory_t* rocksdb_compactionfilterfactory_create(
    void* state, void (*destructor)(void*),
    rocksdb_compactionfilter_t* (*create_compaction_filter)(
        void*, rocksdb_compactionfiltercontext_t* context),
    const char* (*name)(void*)) {
  rocksdb_compactionfilterfactory_t* result =
      new rocksdb_compactionfilterfactory_t;
  result->state_ = state;
  result->destructor_ = destructor;
  result->create_compaction_filter_ = create_compaction_filter;
  result->name_ = name;
  return result;
}

void rocksdb_compactionfilterfactory_destroy(
    rocksdb_compactionfilterfactory_t* factory) {
  delete factory;
}

1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
rocksdb_compactionfilterv2_t* rocksdb_compactionfilterv2_create(
    void* state,
    void (*destructor)(void*),
    void (*filter)(void*, int level, size_t num_keys,
                   const char* const* keys_list, const size_t* keys_list_sizes,
                   const char* const* existing_values_list, const size_t* existing_values_list_sizes,
                   char** new_values_list, size_t* new_values_list_sizes,
                   unsigned char* to_delete_list),
    const char* (*name)(void*)) {
  rocksdb_compactionfilterv2_t* result = new rocksdb_compactionfilterv2_t;
  result->state_ = state;
  result->destructor_ = destructor;
  result->filter_ = filter;
  result->name_ = name;
  return result;
}

void rocksdb_compactionfilterv2_destroy(rocksdb_compactionfilterv2_t* filter) {
  delete filter;
}

rocksdb_compactionfilterfactoryv2_t* rocksdb_compactionfilterfactoryv2_create(
    void* state,
    rocksdb_slicetransform_t* prefix_extractor,
    void (*destructor)(void*),
    rocksdb_compactionfilterv2_t* (*create_compaction_filter_v2)(
1746
        void* state, const rocksdb_compactionfiltercontext_t* context),
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
    const char* (*name)(void*)) {
  rocksdb_compactionfilterfactoryv2_t* result = new rocksdb_compactionfilterfactoryv2_t(prefix_extractor);
  result->state_ = state;
  result->destructor_ = destructor;
  result->create_compaction_filter_v2_ = create_compaction_filter_v2;
  result->name_ = name;
  return result;
}

void rocksdb_compactionfilterfactoryv2_destroy(rocksdb_compactionfilterfactoryv2_t* factory) {
  delete factory;
}

1760
rocksdb_comparator_t* rocksdb_comparator_create(
1761 1762 1763 1764 1765 1766 1767
    void* state,
    void (*destructor)(void*),
    int (*compare)(
        void*,
        const char* a, size_t alen,
        const char* b, size_t blen),
    const char* (*name)(void*)) {
1768
  rocksdb_comparator_t* result = new rocksdb_comparator_t;
1769 1770 1771 1772 1773 1774 1775
  result->state_ = state;
  result->destructor_ = destructor;
  result->compare_ = compare;
  result->name_ = name;
  return result;
}

1776
void rocksdb_comparator_destroy(rocksdb_comparator_t* cmp) {
1777 1778 1779
  delete cmp;
}

1780
rocksdb_filterpolicy_t* rocksdb_filterpolicy_create(
S
Sanjay Ghemawat 已提交
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
    void* state,
    void (*destructor)(void*),
    char* (*create_filter)(
        void*,
        const char* const* key_array, const size_t* key_length_array,
        int num_keys,
        size_t* filter_length),
    unsigned char (*key_may_match)(
        void*,
        const char* key, size_t length,
        const char* filter, size_t filter_length),
1792 1793 1794
    void (*delete_filter)(
        void*,
        const char* filter, size_t filter_length),
S
Sanjay Ghemawat 已提交
1795
    const char* (*name)(void*)) {
1796
  rocksdb_filterpolicy_t* result = new rocksdb_filterpolicy_t;
S
Sanjay Ghemawat 已提交
1797 1798 1799 1800
  result->state_ = state;
  result->destructor_ = destructor;
  result->create_ = create_filter;
  result->key_match_ = key_may_match;
1801
  result->delete_filter_ = delete_filter;
S
Sanjay Ghemawat 已提交
1802 1803 1804 1805
  result->name_ = name;
  return result;
}

1806
void rocksdb_filterpolicy_destroy(rocksdb_filterpolicy_t* filter) {
S
Sanjay Ghemawat 已提交
1807 1808 1809
  delete filter;
}

1810 1811
rocksdb_filterpolicy_t* rocksdb_filterpolicy_create_bloom(int bits_per_key) {
  // Make a rocksdb_filterpolicy_t, but override all of its methods so
S
Sanjay Ghemawat 已提交
1812 1813
  // they delegate to a NewBloomFilterPolicy() instead of user
  // supplied C functions.
1814
  struct Wrapper : public rocksdb_filterpolicy_t {
S
Sanjay Ghemawat 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
    const FilterPolicy* rep_;
    ~Wrapper() { delete rep_; }
    const char* Name() const { return rep_->Name(); }
    void CreateFilter(const Slice* keys, int n, std::string* dst) const {
      return rep_->CreateFilter(keys, n, dst);
    }
    bool KeyMayMatch(const Slice& key, const Slice& filter) const {
      return rep_->KeyMayMatch(key, filter);
    }
    static void DoNothing(void*) { }
  };
  Wrapper* wrapper = new Wrapper;
  wrapper->rep_ = NewBloomFilterPolicy(bits_per_key);
1828 1829
  wrapper->state_ = nullptr;
  wrapper->delete_filter_ = nullptr;
S
Sanjay Ghemawat 已提交
1830 1831 1832 1833
  wrapper->destructor_ = &Wrapper::DoNothing;
  return wrapper;
}

T
Thomas Adam 已提交
1834
rocksdb_mergeoperator_t* rocksdb_mergeoperator_create(
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
    void* state, void (*destructor)(void*),
    char* (*full_merge)(void*, const char* key, size_t key_length,
                        const char* existing_value,
                        size_t existing_value_length,
                        const char* const* operands_list,
                        const size_t* operands_list_length, int num_operands,
                        unsigned char* success, size_t* new_value_length),
    char* (*partial_merge)(void*, const char* key, size_t key_length,
                           const char* const* operands_list,
                           const size_t* operands_list_length, int num_operands,
                           unsigned char* success, size_t* new_value_length),
    void (*delete_value)(void*, const char* value, size_t value_length),
T
Thomas Adam 已提交
1847 1848 1849 1850 1851 1852
    const char* (*name)(void*)) {
  rocksdb_mergeoperator_t* result = new rocksdb_mergeoperator_t;
  result->state_ = state;
  result->destructor_ = destructor;
  result->full_merge_ = full_merge;
  result->partial_merge_ = partial_merge;
1853
  result->delete_value_ = delete_value;
T
Thomas Adam 已提交
1854 1855 1856 1857 1858 1859 1860 1861
  result->name_ = name;
  return result;
}

void rocksdb_mergeoperator_destroy(rocksdb_mergeoperator_t* merge_operator) {
  delete merge_operator;
}

1862 1863
rocksdb_readoptions_t* rocksdb_readoptions_create() {
  return new rocksdb_readoptions_t;
1864 1865
}

1866
void rocksdb_readoptions_destroy(rocksdb_readoptions_t* opt) {
1867 1868 1869
  delete opt;
}

1870 1871
void rocksdb_readoptions_set_verify_checksums(
    rocksdb_readoptions_t* opt,
1872 1873 1874 1875
    unsigned char v) {
  opt->rep.verify_checksums = v;
}

1876 1877
void rocksdb_readoptions_set_fill_cache(
    rocksdb_readoptions_t* opt, unsigned char v) {
1878 1879 1880
  opt->rep.fill_cache = v;
}

1881 1882 1883
void rocksdb_readoptions_set_snapshot(
    rocksdb_readoptions_t* opt,
    const rocksdb_snapshot_t* snap) {
1884
  opt->rep.snapshot = (snap ? snap->rep : nullptr);
1885 1886
}

1887 1888 1889 1890 1891 1892 1893
void rocksdb_readoptions_set_iterate_upper_bound(
    rocksdb_readoptions_t* opt,
    const char* key, size_t keylen) {
  Slice prefix = Slice(key, keylen);
  opt->rep.iterate_upper_bound = &prefix;
}

T
Thomas Adam 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
void rocksdb_readoptions_set_read_tier(
    rocksdb_readoptions_t* opt, int v) {
  opt->rep.read_tier = static_cast<rocksdb::ReadTier>(v);
}

void rocksdb_readoptions_set_tailing(
    rocksdb_readoptions_t* opt, unsigned char v) {
  opt->rep.tailing = v;
}

1904 1905
rocksdb_writeoptions_t* rocksdb_writeoptions_create() {
  return new rocksdb_writeoptions_t;
1906 1907
}

1908
void rocksdb_writeoptions_destroy(rocksdb_writeoptions_t* opt) {
1909 1910 1911
  delete opt;
}

1912 1913
void rocksdb_writeoptions_set_sync(
    rocksdb_writeoptions_t* opt, unsigned char v) {
1914 1915 1916
  opt->rep.sync = v;
}

1917 1918 1919 1920 1921
void rocksdb_writeoptions_disable_WAL(rocksdb_writeoptions_t* opt, int disable) {
  opt->rep.disableWAL = disable;
}


T
Thomas Adam 已提交
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
rocksdb_flushoptions_t* rocksdb_flushoptions_create() {
  return new rocksdb_flushoptions_t;
}

void rocksdb_flushoptions_destroy(rocksdb_flushoptions_t* opt) {
  delete opt;
}

void rocksdb_flushoptions_set_wait(
    rocksdb_flushoptions_t* opt, unsigned char v) {
  opt->rep.wait = v;
}

1935 1936
rocksdb_cache_t* rocksdb_cache_create_lru(size_t capacity) {
  rocksdb_cache_t* c = new rocksdb_cache_t;
1937 1938 1939 1940
  c->rep = NewLRUCache(capacity);
  return c;
}

1941
void rocksdb_cache_destroy(rocksdb_cache_t* cache) {
1942 1943 1944
  delete cache;
}

1945 1946
rocksdb_env_t* rocksdb_create_default_env() {
  rocksdb_env_t* result = new rocksdb_env_t;
1947 1948 1949 1950 1951
  result->rep = Env::Default();
  result->is_default = true;
  return result;
}

1952 1953 1954 1955
void rocksdb_env_set_background_threads(rocksdb_env_t* env, int n) {
  env->rep->SetBackgroundThreads(n);
}

1956 1957 1958 1959
void rocksdb_env_set_high_priority_background_threads(rocksdb_env_t* env, int n) {
  env->rep->SetBackgroundThreads(n, Env::HIGH);
}

1960
void rocksdb_env_destroy(rocksdb_env_t* env) {
1961 1962 1963 1964
  if (!env->is_default) delete env->rep;
  delete env;
}

T
Thomas Adam 已提交
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
rocksdb_slicetransform_t* rocksdb_slicetransform_create(
    void* state,
    void (*destructor)(void*),
    char* (*transform)(
        void*,
        const char* key, size_t length,
        size_t* dst_length),
    unsigned char (*in_domain)(
        void*,
        const char* key, size_t length),
    unsigned char (*in_range)(
        void*,
        const char* key, size_t length),
    const char* (*name)(void*)) {
  rocksdb_slicetransform_t* result = new rocksdb_slicetransform_t;
  result->state_ = state;
  result->destructor_ = destructor;
  result->transform_ = transform;
  result->in_domain_ = in_domain;
  result->in_range_ = in_range;
  result->name_ = name;
  return result;
}

void rocksdb_slicetransform_destroy(rocksdb_slicetransform_t* st) {
  delete st;
}

rocksdb_slicetransform_t* rocksdb_slicetransform_create_fixed_prefix(size_t prefixLen) {
  struct Wrapper : public rocksdb_slicetransform_t {
    const SliceTransform* rep_;
    ~Wrapper() { delete rep_; }
    const char* Name() const { return rep_->Name(); }
    Slice Transform(const Slice& src) const {
      return rep_->Transform(src);
    }
    bool InDomain(const Slice& src) const {
      return rep_->InDomain(src);
    }
    bool InRange(const Slice& src) const {
      return rep_->InRange(src);
    }
    static void DoNothing(void*) { }
  };
  Wrapper* wrapper = new Wrapper;
  wrapper->rep_ = rocksdb::NewFixedPrefixTransform(prefixLen);
2011
  wrapper->state_ = nullptr;
T
Thomas Adam 已提交
2012 2013 2014 2015
  wrapper->destructor_ = &Wrapper::DoNothing;
  return wrapper;
}

2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
rocksdb_universal_compaction_options_t* rocksdb_universal_compaction_options_create() {
  rocksdb_universal_compaction_options_t* result = new rocksdb_universal_compaction_options_t;
  result->rep = new rocksdb::CompactionOptionsUniversal;
  return result;
}

void rocksdb_universal_compaction_options_set_size_ratio(
  rocksdb_universal_compaction_options_t* uco, int ratio) {
  uco->rep->size_ratio = ratio;
}

void rocksdb_universal_compaction_options_set_min_merge_width(
  rocksdb_universal_compaction_options_t* uco, int w) {
  uco->rep->min_merge_width = w;
}

void rocksdb_universal_compaction_options_set_max_merge_width(
  rocksdb_universal_compaction_options_t* uco, int w) {
  uco->rep->max_merge_width = w;
}

void rocksdb_universal_compaction_options_set_max_size_amplification_percent(
  rocksdb_universal_compaction_options_t* uco, int p) {
  uco->rep->max_size_amplification_percent = p;
}

void rocksdb_universal_compaction_options_set_compression_size_percent(
  rocksdb_universal_compaction_options_t* uco, int p) {
  uco->rep->compression_size_percent = p;
}

void rocksdb_universal_compaction_options_set_stop_style(
  rocksdb_universal_compaction_options_t* uco, int style) {
  uco->rep->stop_style = static_cast<rocksdb::CompactionStopStyle>(style);
}

void rocksdb_universal_compaction_options_destroy(
  rocksdb_universal_compaction_options_t* uco) {
  delete uco->rep;
  delete uco;
}

2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
rocksdb_fifo_compaction_options_t* rocksdb_fifo_compaction_options_create() {
  rocksdb_fifo_compaction_options_t* result = new rocksdb_fifo_compaction_options_t;
  result->rep =  CompactionOptionsFIFO();
  return result;
}

void rocksdb_fifo_compaction_options_set_max_table_files_size(
    rocksdb_fifo_compaction_options_t* fifo_opts, uint64_t size) {
  fifo_opts->rep.max_table_files_size = size;
}

void rocksdb_fifo_compaction_options_destroy(
    rocksdb_fifo_compaction_options_t* fifo_opts) {
  delete fifo_opts;
}

A
Albert Strasheim 已提交
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
void rocksdb_options_set_min_level_to_compress(rocksdb_options_t* opt, int level) {
  if (level >= 0) {
    assert(level <= opt->rep.num_levels);
    opt->rep.compression_per_level.resize(opt->rep.num_levels);
    for (int i = 0; i < level; i++) {
      opt->rep.compression_per_level[i] = rocksdb::kNoCompression;
    }
    for (int i = level; i < opt->rep.num_levels; i++) {
      opt->rep.compression_per_level[i] = opt->rep.compression;
    }
  }
}

int rocksdb_livefiles_count(
  const rocksdb_livefiles_t* lf) {
2089
  return static_cast<int>(lf->rep.size());
A
Albert Strasheim 已提交
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
}

const char* rocksdb_livefiles_name(
  const rocksdb_livefiles_t* lf,
  int index) {
  return lf->rep[index].name.c_str();
}

int rocksdb_livefiles_level(
  const rocksdb_livefiles_t* lf,
  int index) {
  return lf->rep[index].level;
}

size_t rocksdb_livefiles_size(
  const rocksdb_livefiles_t* lf,
  int index) {
  return lf->rep[index].size;
}

const char* rocksdb_livefiles_smallestkey(
  const rocksdb_livefiles_t* lf,
  int index,
  size_t* size) {
  *size = lf->rep[index].smallestkey.size();
  return lf->rep[index].smallestkey.data();
}

const char* rocksdb_livefiles_largestkey(
  const rocksdb_livefiles_t* lf,
  int index,
  size_t* size) {
  *size = lf->rep[index].largestkey.size();
  return lf->rep[index].largestkey.data();
}

extern void rocksdb_livefiles_destroy(
  const rocksdb_livefiles_t* lf) {
  delete lf;
}

2131
}  // end extern "C"
I
Igor Canadi 已提交
2132 2133

#endif  // ROCKSDB_LITE