|
1 | | -# Copyright 2019-2020, Intel Corporation |
| 1 | +# Copyright 2021, Intel Corporation |
2 | 2 | # |
3 | 3 | # Redistribution and use in source and binary forms, with or without |
4 | 4 | # modification, are permitted provided that the following conditions |
|
28 | 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
|
31 | | -import pmemkv |
32 | | -import json |
33 | | - |
34 | | - |
35 | | -def callback(key): |
36 | | - mem_view = memoryview(key) |
37 | | - print(mem_view.tobytes().decode()) |
38 | | - |
39 | | - |
40 | | -print("Loading config from json file") |
41 | | -config = None |
42 | | -with open("vsmap_conf.json") as f: |
43 | | - config = json.load(f) |
44 | | - |
45 | | -print(f"Starting engine with config: {config}") |
46 | | -db = pmemkv.Database("vsmap", config) |
47 | | -print("Put new key") |
48 | | -db.put("key1", "value1") |
49 | | -assert db.count_all() == 1 |
| 31 | +# Usage: |
| 32 | +# insert element: 'basic_example.py --path path_to_db --insert key value' |
| 33 | +# lookup element: 'basic_example.py --path path_to_db --lookup key' |
| 34 | +# print all elemnets: 'basic_example.py --path path_to_db --iterate' |
50 | 35 |
|
51 | | -print("Reading key back") |
52 | | -assert db.get_string("key1") == "value1" |
53 | | - |
54 | | -print("Iterating existing keys") |
55 | | -db.put("key2", "value2") |
56 | | -db.put("key3", "value3") |
57 | | -db.get_keys(lambda k: print(f"visited: {memoryview(k).tobytes().decode()}")) |
| 36 | +import pmemkv |
| 37 | +import argparse |
58 | 38 |
|
59 | | -print("Get single value") |
60 | | -db.get("key1", callback) |
| 39 | +def callback_kv(key, value): |
| 40 | + print("key: ", memoryview(key).tobytes().decode(), " value: ", memoryview(value).tobytes().decode()) |
61 | 41 |
|
62 | | -print("Get single value and key in lambda expression") |
63 | | -key = "key1" |
64 | | -db.get( |
65 | | - key, |
66 | | - lambda v, k=key: print( |
67 | | - f"key: {k} with value: " f"{memoryview(v).tobytes().decode()}" |
68 | | - ), |
69 | | -) |
| 42 | +def callback_v(value): |
| 43 | + print(memoryview(value).tobytes().decode()) |
70 | 44 |
|
71 | | -print("Get first 2 bytes of the value (without copying the entire value) and the key in a lambda expression") |
72 | | -db.get( |
73 | | - key, |
74 | | - lambda v, k=key: print( |
75 | | - f"key: {k} with first 2 bytes of the value: " f"{(memoryview(v)[0:2]).tobytes().decode()}" |
76 | | - ), |
77 | | -) |
| 45 | +parser = argparse.ArgumentParser() |
| 46 | +parser.add_argument('--path') |
78 | 47 |
|
79 | | -print("Removing existing key") |
80 | | -db.remove("key1") |
81 | | -assert not db.exists("key1") |
| 48 | +group = parser.add_mutually_exclusive_group(required=True) |
| 49 | +group.add_argument('--lookup', nargs=1, metavar=('key')) |
| 50 | +group.add_argument('--insert', nargs=2, metavar=('key', 'value')) |
| 51 | +group.add_argument('--iterate', action='store_true') |
82 | 52 |
|
83 | | -print("Stopping engine") |
84 | | -db.stop() |
| 53 | +args = parser.parse_args() |
85 | 54 |
|
86 | 55 | print("Configuring engine") |
87 | 56 | config = {} |
88 | | -config["path"] = "/dev/shm" |
89 | | -config["size"] = 1073741824 |
| 57 | +config["path"] = "/dev/shm/t1" |
| 58 | +config["size"] = 10485760 |
| 59 | +config["create_if_missing"] = 1 |
90 | 60 |
|
91 | 61 | print(f"Starting engine with config: {config}") |
92 | | -db = pmemkv.Database("vsmap", config) |
93 | | - |
94 | | -print("Put new key") |
95 | | -db.put("key1", "value1") |
96 | | - |
97 | | -print("Get single value") |
98 | | -db.get("key1", callback) |
| 62 | +db = pmemkv.Database("cmap", config) |
| 63 | + |
| 64 | +if args.lookup: |
| 65 | + db.get(args.lookup[0], callback_v) |
| 66 | +elif args.insert: |
| 67 | + db.put(args.insert[0], args.insert[1]) |
| 68 | +else: |
| 69 | + db.get_all(callback_kv) |
99 | 70 |
|
100 | 71 | print("Stopping engine") |
101 | 72 | db.stop() |
0 commit comments