mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Squashed 'src/rocksdb/' content from commit 78b8a7d
git-subtree-dir: src/rocksdb git-subtree-split: 78b8a7d908d7e67cc6664398e39560b76c965773
This commit is contained in:
2
examples/.gitignore
vendored
Normal file
2
examples/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
column_families_example
|
||||
simple_example
|
||||
9
examples/Makefile
Normal file
9
examples/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
include ../build_config.mk
|
||||
|
||||
all: simple_example column_families_example
|
||||
|
||||
simple_example: simple_example.cc
|
||||
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../librocksdb.a -I../include -O2 -std=c++11 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
|
||||
|
||||
column_families_example: column_families_example.cc
|
||||
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../librocksdb.a -I../include -O2 -std=c++11 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
|
||||
1
examples/README.md
Normal file
1
examples/README.md
Normal file
@@ -0,0 +1 @@
|
||||
Compile RocksDB first by executing `make static_lib` in parent dir
|
||||
72
examples/column_families_example.cc
Normal file
72
examples/column_families_example.cc
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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.
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "rocksdb/db.h"
|
||||
#include "rocksdb/slice.h"
|
||||
#include "rocksdb/options.h"
|
||||
|
||||
using namespace rocksdb;
|
||||
|
||||
std::string kDBPath = "/tmp/rocksdb_column_families_example";
|
||||
|
||||
int main() {
|
||||
// open DB
|
||||
Options options;
|
||||
options.create_if_missing = true;
|
||||
DB* db;
|
||||
Status s = DB::Open(options, kDBPath, &db);
|
||||
assert(s.ok());
|
||||
|
||||
// create column family
|
||||
ColumnFamilyHandle* cf;
|
||||
s = db->CreateColumnFamily(ColumnFamilyOptions(), "new_cf", &cf);
|
||||
assert(s.ok());
|
||||
|
||||
// close DB
|
||||
delete cf;
|
||||
delete db;
|
||||
|
||||
// open DB with two column families
|
||||
std::vector<ColumnFamilyDescriptor> column_families;
|
||||
// have to open default column familiy
|
||||
column_families.push_back(ColumnFamilyDescriptor(
|
||||
kDefaultColumnFamilyName, ColumnFamilyOptions()));
|
||||
// open the new one, too
|
||||
column_families.push_back(ColumnFamilyDescriptor(
|
||||
"new_cf", ColumnFamilyOptions()));
|
||||
std::vector<ColumnFamilyHandle*> handles;
|
||||
s = DB::Open(DBOptions(), kDBPath, column_families, &handles, &db);
|
||||
assert(s.ok());
|
||||
|
||||
// put and get from non-default column family
|
||||
s = db->Put(WriteOptions(), handles[1], Slice("key"), Slice("value"));
|
||||
assert(s.ok());
|
||||
std::string value;
|
||||
s = db->Get(ReadOptions(), handles[1], Slice("key"), &value);
|
||||
assert(s.ok());
|
||||
|
||||
// atomic write
|
||||
WriteBatch batch;
|
||||
batch.Put(handles[0], Slice("key2"), Slice("value2"));
|
||||
batch.Put(handles[1], Slice("key3"), Slice("value3"));
|
||||
batch.Delete(handles[0], Slice("key"));
|
||||
s = db->Write(WriteOptions(), &batch);
|
||||
assert(s.ok());
|
||||
|
||||
// drop column family
|
||||
s = db->DropColumnFamily(handles[1]);
|
||||
assert(s.ok());
|
||||
|
||||
// close db
|
||||
for (auto handle : handles) {
|
||||
delete handle;
|
||||
}
|
||||
delete db;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
examples/simple_example.cc
Normal file
41
examples/simple_example.cc
Normal file
@@ -0,0 +1,41 @@
|
||||
// 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.
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include "rocksdb/db.h"
|
||||
#include "rocksdb/slice.h"
|
||||
#include "rocksdb/options.h"
|
||||
|
||||
using namespace rocksdb;
|
||||
|
||||
std::string kDBPath = "/tmp/rocksdb_simple_example";
|
||||
|
||||
int main() {
|
||||
DB* db;
|
||||
Options options;
|
||||
// Optimize RocksDB. This is the easiest way to get RocksDB to perform well
|
||||
options.IncreaseParallelism();
|
||||
options.OptimizeLevelStyleCompaction();
|
||||
// create the DB if it's not already present
|
||||
options.create_if_missing = true;
|
||||
|
||||
// open DB
|
||||
Status s = DB::Open(options, kDBPath, &db);
|
||||
assert(s.ok());
|
||||
|
||||
// Put key-value
|
||||
s = db->Put(WriteOptions(), "key", "value");
|
||||
assert(s.ok());
|
||||
std::string value;
|
||||
// get value
|
||||
s = db->Get(ReadOptions(), "key", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "value");
|
||||
|
||||
delete db;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user