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).
9 #include "rocksdb/db.h"
10 #include "rocksdb/slice.h"
11 #include "rocksdb/options.h"
13 using namespace ROCKSDB_NAMESPACE
;
15 std::string kDBPath
= "/tmp/rocksdb_simple_example";
20 // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
21 options
.IncreaseParallelism();
22 options
.OptimizeLevelStyleCompaction();
23 // create the DB if it's not already present
24 options
.create_if_missing
= true;
27 Status s
= DB::Open(options
, kDBPath
, &db
);
31 s
= db
->Put(WriteOptions(), "key1", "value");
35 s
= db
->Get(ReadOptions(), "key1", &value
);
37 assert(value
== "value");
39 // atomically apply a set of updates
43 batch
.Put("key2", value
);
44 s
= db
->Write(WriteOptions(), &batch
);
47 s
= db
->Get(ReadOptions(), "key1", &value
);
48 assert(s
.IsNotFound());
50 db
->Get(ReadOptions(), "key2", &value
);
51 assert(value
== "value");
54 PinnableSlice pinnable_val
;
55 db
->Get(ReadOptions(), db
->DefaultColumnFamily(), "key2", &pinnable_val
);
56 assert(pinnable_val
== "value");
60 std::string string_val
;
61 // If it cannot pin the value, it copies the value to its internal buffer.
62 // The intenral buffer could be set during construction.
63 PinnableSlice
pinnable_val(&string_val
);
64 db
->Get(ReadOptions(), db
->DefaultColumnFamily(), "key2", &pinnable_val
);
65 assert(pinnable_val
== "value");
66 // If the value is not pinned, the internal buffer must have the value.
67 assert(pinnable_val
.IsPinned() || string_val
== "value");
70 PinnableSlice pinnable_val
;
71 db
->Get(ReadOptions(), db
->DefaultColumnFamily(), "key1", &pinnable_val
);
72 assert(s
.IsNotFound());
73 // Reset PinnableSlice after each use and before each reuse
75 db
->Get(ReadOptions(), db
->DefaultColumnFamily(), "key2", &pinnable_val
);
76 assert(pinnable_val
== "value");
78 // The Slice pointed by pinnable_val is not valid after this point