]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | #include <cstdio> |
6 | #include <string> | |
7 | #include <vector> | |
8 | ||
9 | #include "rocksdb/db.h" | |
10 | #include "rocksdb/slice.h" | |
11 | #include "rocksdb/options.h" | |
12 | ||
13 | using namespace rocksdb; | |
14 | ||
15 | std::string kDBPath = "/tmp/rocksdb_column_families_example"; | |
16 | ||
17 | int main() { | |
18 | // open DB | |
19 | Options options; | |
20 | options.create_if_missing = true; | |
21 | DB* db; | |
22 | Status s = DB::Open(options, kDBPath, &db); | |
23 | assert(s.ok()); | |
24 | ||
25 | // create column family | |
26 | ColumnFamilyHandle* cf; | |
27 | s = db->CreateColumnFamily(ColumnFamilyOptions(), "new_cf", &cf); | |
28 | assert(s.ok()); | |
29 | ||
30 | // close DB | |
31 | delete cf; | |
32 | delete db; | |
33 | ||
34 | // open DB with two column families | |
35 | std::vector<ColumnFamilyDescriptor> column_families; | |
36 | // have to open default column family | |
37 | column_families.push_back(ColumnFamilyDescriptor( | |
38 | kDefaultColumnFamilyName, ColumnFamilyOptions())); | |
39 | // open the new one, too | |
40 | column_families.push_back(ColumnFamilyDescriptor( | |
41 | "new_cf", ColumnFamilyOptions())); | |
42 | std::vector<ColumnFamilyHandle*> handles; | |
43 | s = DB::Open(DBOptions(), kDBPath, column_families, &handles, &db); | |
44 | assert(s.ok()); | |
45 | ||
46 | // put and get from non-default column family | |
47 | s = db->Put(WriteOptions(), handles[1], Slice("key"), Slice("value")); | |
48 | assert(s.ok()); | |
49 | std::string value; | |
50 | s = db->Get(ReadOptions(), handles[1], Slice("key"), &value); | |
51 | assert(s.ok()); | |
52 | ||
53 | // atomic write | |
54 | WriteBatch batch; | |
55 | batch.Put(handles[0], Slice("key2"), Slice("value2")); | |
56 | batch.Put(handles[1], Slice("key3"), Slice("value3")); | |
57 | batch.Delete(handles[0], Slice("key")); | |
58 | s = db->Write(WriteOptions(), &batch); | |
59 | assert(s.ok()); | |
60 | ||
61 | // drop column family | |
62 | s = db->DropColumnFamily(handles[1]); | |
63 | assert(s.ok()); | |
64 | ||
65 | // close db | |
66 | for (auto handle : handles) { | |
67 | delete handle; | |
68 | } | |
69 | delete db; | |
70 | ||
71 | return 0; | |
72 | } |