|
| 1 | +# Copyright 2021, Intel Corporation |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in |
| 12 | +# the documentation and/or other materials provided with the |
| 13 | +# distribution. |
| 14 | +# |
| 15 | +# * Neither the name of the copyright holder nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived |
| 17 | +# from this software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +# Usage: |
| 32 | +# insert element: 'interactive_example.py --path path_to_db --insert key value' |
| 33 | +# lookup element: 'interactive_example.py --path path_to_db --lookup key' |
| 34 | +# print all elements: 'interactive_example.py --path path_to_db --iterate' |
| 35 | + |
| 36 | +import pmemkv |
| 37 | +import argparse |
| 38 | + |
| 39 | +def callback_kv(key, value): |
| 40 | + print("key: ", memoryview(key).tobytes().decode(), " value: ", memoryview(value).tobytes().decode()) |
| 41 | + |
| 42 | +def callback_v(value): |
| 43 | + print(memoryview(value).tobytes().decode()) |
| 44 | + |
| 45 | +parser = argparse.ArgumentParser() |
| 46 | +parser.add_argument('--path', help='Path to the database') |
| 47 | + |
| 48 | +group = parser.add_mutually_exclusive_group(required=True) |
| 49 | +group.add_argument('--lookup', nargs=1, metavar=('key'), |
| 50 | + help='Print element with specified key') |
| 51 | +group.add_argument('--insert', nargs=2, metavar=('key', 'value'), |
| 52 | + help='Insert element with specified key and value') |
| 53 | +group.add_argument('--iterate', action='store_true', |
| 54 | + help='Print all elements) |
| 55 | + |
| 56 | +args = parser.parse_args() |
| 57 | + |
| 58 | +print("Configuring engine") |
| 59 | +config = {} |
| 60 | +config["path"] = args.path |
| 61 | +config["size"] = 10485760 |
| 62 | +config["create_if_missing"] = 1 |
| 63 | + |
| 64 | +print(f"Starting engine with config: {config}") |
| 65 | +db = pmemkv.Database("cmap", config) |
| 66 | + |
| 67 | +if args.lookup: |
| 68 | + db.get(args.lookup[0], callback_v) |
| 69 | +elif args.insert: |
| 70 | + db.put(args.insert[0], args.insert[1]) |
| 71 | +elif args.iterate: |
| 72 | + db.get_all(callback_kv) |
| 73 | + |
| 74 | +print("Stopping engine") |
| 75 | +db.stop() |
0 commit comments