diff --git a/.github/workflows/smoke-tests.yml b/.github/workflows/smoke-tests.yml index 1de80c89bb..e3031a8d0d 100644 --- a/.github/workflows/smoke-tests.yml +++ b/.github/workflows/smoke-tests.yml @@ -275,8 +275,7 @@ jobs: # We cannot run this module with the cwd being string_ops # Otherwise we will get relative import errors # https://stackoverflow.com/a/47030746 - run: python3 -m string_ops.run_all_examples - working-directory: examples + run: python3 -m examples.run_all_examples - name: Install test dependencies if: ${{ matrix.test == 'doctest' }} diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 0000000000..2036629bb2 --- /dev/null +++ b/examples/__init__.py @@ -0,0 +1,40 @@ +import aerospike + + +class Example: + def __init__( + self, + host: str = "127.0.0.1", + port: int = 3000, + user: str = None, + password: str = None, + namespace: str = "test", + set_name: str = "demo" + ): + config = { + "hosts": [(host, port)], + "user": user, + "password": password + } + client = aerospike.client(config) + + self.client = client + self.namespace = namespace + self.set_name = set_name + self.key = (self.namespace, self.set_name, "docreadkey") + + def __del__(self): + self.client.close() + +# TODO: I'm wondering if pytest can be used since +# it has fixtures as a built-in feature +class ExampleWithRecord(Example): + def __init__(self): + super().__init__() + + self.client.put(self.key, bins={"a": 1}) + + def __del__(self): + self.client.remove(self.key) + + super().__del__() diff --git a/examples/client/aggregate.py b/examples/client/aggregate.py index f5a74bcd89..2bfc172637 100644 --- a/examples/client/aggregate.py +++ b/examples/client/aggregate.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import json import re @@ -34,56 +33,18 @@ optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-b", "--bins", dest="bins", type="string", action="append", help="Bins to select from each record.") (options, args) = optparser.parse_args() -if options.help: - optparser.print_help() - print() - sys.exit(1) - if len(args) < 3: optparser.print_help() print() sys.exit(1) -########################################################################## -# Client Configuration -########################################################################## - config = { - 'hosts': [(options.host, options.port)], 'lua': { 'user_path': os.path.dirname(__file__) } @@ -104,13 +65,6 @@ def parse_arg(s): try: - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - # ---------------------------------------------------------------------------- # Perform Operation # ---------------------------------------------------------------------------- diff --git a/examples/client/append.py b/examples/client/append.py index f76144b620..796aa4c277 100644 --- a/examples/client/append.py +++ b/examples/client/append.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,146 +16,26 @@ ########################################################################## -from __future__ import print_function -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Option Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") +from .. import Example -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--gen", dest="gen", type="int", default=10, metavar="", - help="Generation of the record being written.") - -optparser.add_option( - "--ttl", dest="ttl", type="int", default=1000, metavar="", - help="TTL of the record being written.") - - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - key = args.pop() +class Append(Example): + def run(self): record = { 'example_name': 'John', 'example_age': 1 } - meta = {} - if (options.gen): - meta['gen'] = options.gen - if (options.ttl): - meta['ttl'] = options.ttl - policy = None - - # invoke operation - - client.put((namespace, set, key), record, meta, policy) - - print(record) - print("---") - print("OK, 1 record written.") - - client.append( - (namespace, set, key), "example_name", " Smith", meta, policy) - (key, meta, bins) = client.get((namespace, set, key)) + # TODO meta gen/ttl should be options? + meta = { + 'gen': 10 + } + policy = { + 'ttl': 1000 + } + self.client.put(self.key, record, meta, policy) + self.client.append( + self.key, "example_name", " Smith", meta, policy) + (key, meta, bins) = self.client.get(self.key) print(bins) - print("---") - print("OK, 1 record appended.") - - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) diff --git a/examples/client/apply.py b/examples/client/apply.py index e497a5fe7a..b9d56cc09e 100644 --- a/examples/client/apply.py +++ b/examples/client/apply.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,139 +16,14 @@ ########################################################################## -from __future__ import print_function -import aerospike -import json -import sys - -from optparse import OptionParser - -########################################################################## -# Option Parsing -########################################################################## - -usage = "usage: %prog [options] key module function [args...]" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--gen", dest="gen", type="int", default=None, metavar="", - help="Generation of the record being written.") - -optparser.add_option( - "--ttl", dest="ttl", type="int", default=None, metavar="", - help="TTL of the record being written.") - - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) < 3: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - - -def parse_arg(s): - try: - return json.loads(s) - except ValueError: - return s - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) +from .. import ExampleWithRecord - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - try: - - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - - args.reverse() - key = args.pop() - module = args.pop() - function = args.pop() - - # invoke operation - args.reverse() - argl = list(map(parse_arg, args)) - res = client.apply((namespace, set, key), module, function, argl) +class Apply(ExampleWithRecord): + def run(self): + module = "module" + function = "a" + args = [] + res = self.client.apply(self.key, module, function, args) print(res) - print("---") - print("OK, 1 UDF applied.") - - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) diff --git a/examples/client/bin_ops.py b/examples/client/bin_ops.py index 35826bfb3d..6faace60e1 100644 --- a/examples/client/bin_ops.py +++ b/examples/client/bin_ops.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -14,124 +14,59 @@ # See the License for the specific language governing permissions and # limitations under the License. ########################################################################## +from .. import Example -from __future__ import print_function import aerospike +from aerospike_helpers.operations import operations import pprint -import sys - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options]" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Application -########################################################################## -exitCode = 0 -try: - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - config = {'hosts': [(options.host, options.port)]} - client = aerospike.client(config).connect( - options.username, options.password) - # ---------------------------------------------------------------------------- - # Perform Operations - # ---------------------------------------------------------------------------- - - try: +class BinOps(Example): + def run(self): pp = pprint.PrettyPrinter(indent=2) - client.put(('test', 'cats', 'mr. peppy'), {'breed': 'persian'}, + key = ('test', 'cats', 'mr. peppy') + + self.client.put(key, {'breed': 'persian'}, policy={'exists': aerospike.POLICY_EXISTS_CREATE_OR_REPLACE, - 'key': aerospike.POLICY_KEY_DIGEST}, - meta={'ttl': 120}) - (key, meta, bins) = client.get(('test', 'cats', 'mr. peppy')) + 'key': aerospike.POLICY_KEY_DIGEST, 'ttl': 120}) + (key, meta, bins) = self.client.get(key) + print("Before:", bins) - client.increment( - key, 'lives', -1, {'gen': 2, 'ttl': 1000}, policy={'total_timeout': 1500}) - (key, meta, bins) = client.get(key) + self.client.increment( + key, 'lives', -1, {'gen': 2}, policy={'total_timeout': 1500, 'ttl': 1000}) + (key, meta, bins) = self.client.get(key) + print("After:", bins) # the key we got back when we fetched the record with get() is useable # as-is because it contains the record's digest - client.increment(key, 'lives', -1) - (key, meta, bins) = client.get(key) + self.client.increment(key, 'lives', -1) + (key, meta, bins) = self.client.get(key) + # kitty lost a life, unfortunately print("Poor Kitty:", bins) - client.put(key, {'owner': 'Fry'}) - client.prepend(key, 'owner', 'Philip J. ') - client.append(key, 'owner', ' Esq.') + self.client.put(key, {'owner': 'Fry'}) + self.client.prepend(key, 'owner', 'Philip J. ') + self.client.append(key, 'owner', ' Esq.') + # kitty loses another life, gains a color, all as part of a record # multi-op - ops = [{'bin': 'color', 'op': aerospike.OPERATOR_WRITE, 'val': 'smoke'}, - {'bin': 'lives', 'op': aerospike.OPERATOR_INCR, 'val': -1}, - {'bin': 'ailments', 'op': aerospike.OPERATOR_READ}, - {'bin': 'lives', 'op': aerospike.OPERATOR_READ}] - (key, meta, bins) = client.operate(key, ops) + ops = [ + operations.write(bin="color", write_item="smoke"), + operations.increment(bin_name="lives", amount=-1), + operations.read("ailments"), + operations.read("lives") + ] + (key, meta, bins) = self.client.operate(key, ops) print("After calling operate(), kitty is down to", bins['lives'], "lives") pp.pprint(bins) # display the record as it is after all the operations - (key, meta, bins) = client.get(key) + (key, meta, bins) = self.client.get(key) print("\nRecord\n======\nKey\n---") pp.pprint(key) print("Meta\n----") pp.pprint(meta) print("Bins\n----") pp.pprint(bins) - - except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) diff --git a/examples/client/client_big_list.py b/examples/client/client_big_list.py index c5fc2f7e6f..b173e436a5 100644 --- a/examples/client/client_big_list.py +++ b/examples/client/client_big_list.py @@ -20,6 +20,7 @@ import aerospike from aerospike import exception as as_exceptions +from aerospike_helpers.operations import list_operations, operations ''' This provides a rough implementation of an expandable list for Aerospike It utilizes a metadata record to provide information about associated subrecords. @@ -172,7 +173,7 @@ def get_all_entries(self, extended_search=False): ) keys.append(key) - subrecords = self.client.get_many(keys) + subrecords = self.client.batch_read(keys) entries = self._get_items_from_subrecords(subrecords) # Try to get subrecords beyond the listed amount. @@ -243,8 +244,11 @@ def _create_or_update_subrecord(self, item, subrecord_number, generation, retrie subrecord_userkey = self._make_user_key(subrecord_number) subrecord_record_key = (self.ns, self.set, subrecord_userkey) try: - self.client.list_append( - subrecord_record_key, self.subrecord_list_bin, item) + ops = [ + list_operations.list_append(self.subrecord_list_bin, item) + ] + self.client.operate( + subrecord_record_key, ops) except as_exceptions.RecordTooBig as e: if retries_remaining == 0: raise e @@ -260,11 +264,15 @@ def _update_metadata_record(self, generation): update_policy = {'gen': aerospike.POLICY_GEN_EQ} meta = {'gen': generation} try: - self.client.increment(self.metadata_key, self.subrecourd_count_name, 1, meta=meta, policy=update_policy) + ops = [ + operations.increment(self.subrecourd_count_name, 1) + ] + self.client.operate(self.metadata_key, ops, meta=meta, policy=update_policy) except as_exceptions.RecordTooBig: raise ASMetadataRecordTooLarge except as_exceptions.RecordGenerationError: # This means that somebody else has updated the record count already. Don't risk updating again. + # TODO: if this happens then there's no way to further update the list from this client? pass def _get_items_from_subrecords(self, subrecords): @@ -286,7 +294,8 @@ def _get_items_from_subrecords(self, subrecords): entries = [] # If a subrecord was included in the header of the top level record, but the matching subrecord # was not found, ignore it. - for _, _, sr_bins in subrecords: + for br in subrecords.batch_records: + sr_bins = br.record[2] if sr_bins: entries.extend(sr_bins[self.subrecord_list_bin]) return entries diff --git a/examples/client/delete.py b/examples/client/delete.py index 3bb9677a4d..661656e17e 100644 --- a/examples/client/delete.py +++ b/examples/client/delete.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -30,49 +30,14 @@ optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", default="ram", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", default="ram", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="as-s1.as-network.com", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "--timeout", dest="timeout", type="int", default=1000, metavar="", help="Client timeout") -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Namespace of database.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Set to use within namespace of database.") - optparser.add_option( "-c", "--test_count", dest="test_count", type="int", default=128, metavar="", help="Number of test cases to run.") -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - ########################################################################## # Client Configuration ########################################################################## diff --git a/examples/client/exists.py b/examples/client/exists.py index 9dfd67d8e1..83ace5fade 100644 --- a/examples/client/exists.py +++ b/examples/client/exists.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,117 +15,12 @@ # limitations under the License. ########################################################################## -from __future__ import print_function - -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: +from .. import ExampleWithRecord - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - client = aerospike.client(config).connect( - options.username, options.password) - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - key = args.pop() - (key, metadata) = client.exists((namespace, set, key)) - if metadata is not None: - print(key, metadata) - print("---") - print("OK, 1 record found.") - else: - print('error: Not Found.', file=sys.stderr) - exitCode = 1 - - except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## +class Exists(ExampleWithRecord): + def run(self): + (key, metadata) = self.client.exists(self.key) + print(key, metadata) -sys.exit(exitCode) + # TODO: missing negative path example (e.g where metadata is None) diff --git a/examples/client/exists_many.py b/examples/client/exists_many.py index 5f597b3afb..70fc7555e2 100644 --- a/examples/client/exists_many.py +++ b/examples/client/exists_many.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,125 +16,17 @@ ########################################################################## -from __future__ import print_function -import aerospike -import sys - -from optparse import OptionParser - - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options]" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-k", "--keys", dest="keys", type="string", default="", metavar="", - help="Keys to be accessed in the database server. Should be specified as 'name','name1','name2' etc") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- +from .. import ExampleWithRecord - try: - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - # args.pop() - - keys = options.keys.split(',') - keylist = [] - for key in keys: - individualkey = (namespace, set, key) - keylist.append(individualkey) - - records = client.exists_many(keylist) +# TODO: should use fixture with multiple records +class ExistsMany(ExampleWithRecord): + def run(self): + keys = [f"key{i}" for i in range(5)] + records = self.client.exists_many(keys) if records != None: + print(f"{len(records)} records were found") print(records) - print("---") - print("OK, %d records found." % len(records)) else: - print('error: Not Found.', file=sys.stderr) - exitCode = 1 - - except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) + print('error: Not Found.') diff --git a/examples/client/get.py b/examples/client/get.py index c4ae106efb..1b7c742d55 100644 --- a/examples/client/get.py +++ b/examples/client/get.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,148 +15,13 @@ # limitations under the License. ########################################################################## -from __future__ import print_function - -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") +from .. import ExampleWithRecord -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--timeout", dest="timeout", type="int", default=1000, metavar="", - help="Client timeout") - -optparser.add_option( - "--read-timeout", dest="read_timeout", type="int", default=1000, metavar="", - help="Client read timeout") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--no-key", dest="nokey", action="store_true", - help="Do not return the key") - -optparser.add_option( - "--no-metadata", dest="nometadata", action="store_true", - help="Do not return the metadata") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)], - 'policies': { - 'total_timeout': options.timeout - } -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - key = args.pop() +class Get(ExampleWithRecord): + def run(self): policy = { - 'total_timeout': options.read_timeout + 'total_timeout': 300 } - - (key, metadata, record) = client.get((namespace, set, key), policy) - - if metadata is not None: - if options.nometadata and options.nokey: - print(record) - elif options.nometadata: - print(key, record) - elif options.nokey: - print(metadata, record) - else: - print(key, metadata, record) - print("---") - print("OK, 1 record found.") - else: - print('error: Not Found.', file=sys.stderr) - exitCode = 1 - - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) + record = self.client.get(self.key, policy) + print(record) diff --git a/examples/client/get_async.py b/examples/client/get_async.py deleted file mode 100644 index 693496a98f..0000000000 --- a/examples/client/get_async.py +++ /dev/null @@ -1,159 +0,0 @@ -# -*- coding: utf-8 -*- -########################################################################## -# Copyright 2013-2021 Aerospike, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -########################################################################## - -import asyncio -import sys -import aerospike -import time -from aerospike_helpers.awaitable import io - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options]" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", default="ram", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", default="ram", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="as-s1.as-network.com", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--timeout", dest="timeout", type="int", default=1000, metavar="", - help="Client timeout") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Namespace of database.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Set to use within namespace of database.") - -optparser.add_option( - "-c", "--test_count", dest="test_count", type="int", default=128, metavar="", - help="Number of async IO to spawn.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)], - 'policies': { - 'total_timeout': options.timeout - } -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - print(f"Connecting to {options.host}:{options.port} with {options.username}:{options.password}") - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - io_results = {} - test_count = options.test_count - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - policy = { - 'total_timeout': options.timeout - } - meta = None - - print(f"IO async test count:{test_count}") - - async def async_io(namespace, set, i): - key = (namespace, \ - set, \ - str(i), \ - client.get_key_digest(namespace, set, str(i))) - context = {'result': {}} - io_results[key[2]] = context - result = None - try: - result = await io.get(client, key, policy) - except Exception as eargs: - print(f"error: {eargs.code}, {eargs.msg}, {eargs.file}, {eargs.line}") - print(result) - io_results[key[2]]['result'] = result - async def main(): - func_list = [] - for i in range(test_count): - func_list.append(async_io(namespace, set, i)) - await asyncio.gather(*func_list) - asyncio.get_event_loop().run_until_complete(main()) - print(io_results) - print(f"get_async completed with returning {len(io_results)} records") - except Exception as e: - print(f"error: {0} ".format(e), file=sys.stderr) - rc = 1 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) diff --git a/examples/client/get_key_digest.py b/examples/client/get_key_digest.py index 0153092d61..f5421c299f 100644 --- a/examples/client/get_key_digest.py +++ b/examples/client/get_key_digest.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -29,46 +28,13 @@ usage = "usage: %prog [options] key" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") optparser.add_option( "--timeout", dest="timeout", type="int", default=1000, metavar="", help="Client timeout") -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - (options, args) = optparser.parse_args() -if options.help: - optparser.print_help() - print() - sys.exit(1) if len(args) != 1: optparser.print_help() diff --git a/examples/client/get_many.py b/examples/client/get_many.py index c2a1f8f512..76bc97a289 100644 --- a/examples/client/get_many.py +++ b/examples/client/get_many.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -30,47 +29,10 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-k", "--keys", dest="keys", type="string", default="", metavar="", help="Keys to be accessed in the database server. Should be specified as 'name','name1','name2' etc") -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - ########################################################################## # Client Configuration ########################################################################## diff --git a/examples/client/get_nodes.py b/examples/client/get_nodes.py index 6171e5bc2c..2b67240075 100644 --- a/examples/client/get_nodes.py +++ b/examples/client/get_nodes.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -30,35 +29,6 @@ usage = "usage: %prog" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - ########################################################################## # Client Configuration ########################################################################## diff --git a/examples/client/increment.py b/examples/client/increment.py index 303fa11615..ee91e05da4 100644 --- a/examples/client/increment.py +++ b/examples/client/increment.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -29,36 +28,6 @@ usage = "usage: %prog [options] key" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "--gen", dest="gen", type="int", default=10, metavar="", help="Generation of the record being written.") @@ -68,13 +37,6 @@ help="TTL of the record being written.") -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - if len(args) != 1: optparser.print_help() print() diff --git a/examples/client/index_create.py b/examples/client/index_create.py index 8968a8ba9c..84a8e03edb 100644 --- a/examples/client/index_create.py +++ b/examples/client/index_create.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -29,48 +28,12 @@ usage = "usage: %prog [options] bin index_name" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") optparser.add_option( "-t", "--type", dest="type", type="string", default="string", metavar="", help="The type of index to create") -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - if len(args) != 2: optparser.print_help() print() diff --git a/examples/client/index_remove.py b/examples/client/index_remove.py index 8f245adb82..b2907106da 100644 --- a/examples/client/index_remove.py +++ b/examples/client/index_remove.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -29,39 +28,6 @@ usage = "usage: %prog [options] bin index_name" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - if len(args) != 1: optparser.print_help() print() diff --git a/examples/client/info.py b/examples/client/info.py index 52acf0f8de..3128902216 100644 --- a/examples/client/info.py +++ b/examples/client/info.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -29,27 +28,6 @@ usage = "usage: %prog [options] [REQUEST]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") (options, args) = optparser.parse_args() diff --git a/examples/client/is_connected.py b/examples/client/is_connected.py index 2991c83117..ea0317b549 100644 --- a/examples/client/is_connected.py +++ b/examples/client/is_connected.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -31,40 +30,6 @@ optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) if len(args) != 0: optparser.print_help() diff --git a/examples/client/kvs.py b/examples/client/kvs.py index 251c944ff9..bde2c26969 100644 --- a/examples/client/kvs.py +++ b/examples/client/kvs.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -29,35 +28,6 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - ########################################################################## # Application ########################################################################## diff --git a/examples/client/multi_thread.py b/examples/client/multi_thread.py index 43f3885687..08b02f0d4f 100644 --- a/examples/client/multi_thread.py +++ b/examples/client/multi_thread.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -32,35 +31,6 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", - metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) ########################################################################## # Client Configuration diff --git a/examples/client/operate.py b/examples/client/operate.py index 999280ddda..ec37158caf 100644 --- a/examples/client/operate.py +++ b/examples/client/operate.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,160 +15,32 @@ # limitations under the License. ########################################################################## -from __future__ import print_function -import sys -from optparse import OptionParser +from .. import Example -import aerospike from aerospike_helpers.operations import operations as op_helpers -########################################################################## -# Option Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--gen", dest="gen", type="int", default=10, metavar="", - help="Generation of the record being written.") - -optparser.add_option( - "--ttl", dest="ttl", type="int", default=1000, metavar="", - help="TTL of the record being written.") - - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - setname = options.set if options.set and options.set != 'None' else None - key = args.pop() - record_key = (namespace, setname, key) - +class Operate(Example): + def run(self): record = { 'example_name': 'John', 'example_age': 1 } - meta = {'ttl': options.ttl, 'gen': options.gen} + meta = {'ttl': 1000, 'gen': 10} policy = None + self.client.put(self.key, record, meta, policy) - # invoke operation - - client.put(record_key, record, meta, policy) - - print("---") - print("OK, 1 record written.") - - _, _, bins = client.get(record_key) + _, _, bins = self.client.get(self.key) + print("Before operation:", bins) - print("---") - print("Before operate operation") - print(bins) - - operation_list = [ + ops = [ op_helpers.prepend("example_name", "Mr "), op_helpers.increment("example_age", 3), op_helpers.read("example_name") ] + _, _, bins = self.client.operate(self.key, ops, meta, policy) + print("Record returned by operate():", bins) - _, _, bins = client.operate( - record_key, operation_list, meta, policy) - print("---") - print("Record returned on operate completion") - print(bins) - - _, _, bins = client.get(record_key) - - print("---") - print("After operate operation") - print(bins) - - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) + _, _, bins = self.client.get(self.key) + print("After operation:", bins) diff --git a/examples/client/prepend.py b/examples/client/prepend.py index cb14c0265e..20707a221c 100644 --- a/examples/client/prepend.py +++ b/examples/client/prepend.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,143 +15,23 @@ # limitations under the License. ########################################################################## -from __future__ import print_function - -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Option Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") +from .. import Example -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--gen", dest="gen", type="int", default=10, metavar="", - help="Generation of the record being written.") - -optparser.add_option( - "--ttl", dest="ttl", type="int", default=1000, metavar="", - help="TTL of the record being written.") - - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - key = args.pop() +class Prepend(Example): + def run(self): + # TODO: can share this in a fixture class? record = { 'example_name': 'John', 'example_age': 1 } - meta = {'ttl': options.ttl, 'gen': options.gen} + # TODO: should this be configurable? + meta = {'ttl': 1000, 'gen': 10} policy = None - # invoke operation - - client.put((namespace, set, key), record, meta, policy) - - print(record) - print("---") - print("OK, 1 record written.") - - client.prepend( - (namespace, set, key), "example_name", "Mr ", meta, policy) - (key, meta, bins) = client.get((namespace, set, key)) + self.client.put(self.key, record, meta, policy) + self.client.prepend(self.key, "example_name", "Mr ", meta, policy) + (key, meta, bins) = self.client.get(self.key) print(bins) - print("---") - print("OK, 1 record prepended.") - - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) diff --git a/examples/client/put.py b/examples/client/put.py index 17eea90085..c4eefe7a8e 100644 --- a/examples/client/put.py +++ b/examples/client/put.py @@ -1,4 +1,6 @@ -# -*- coding: utf-8 -*- +from .. import Example + + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,99 +18,8 @@ ########################################################################## -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Option Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--gen", dest="gen", type="int", default=5, metavar="", - help="Generation of the record being written.") - -optparser.add_option( - "--ttl", dest="ttl", type="int", default=1000, metavar="", - help="TTL of the record being written.") - - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) == 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - try: - - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - key = args.pop() - +class Put(Example): + def run(self): record = { 'i': 123, 'f': 3.1415, @@ -120,32 +31,7 @@ 'l': [123, 'abc', '안녕하세요', ['x', 'y', 'z'], {'x': 1, 'y': 2, 'z': 3}], 'm': {'i': 123, 's': 'abc', 'u': '안녕하세요', 'l': ['x', 'y', 'z'], 'd': {'x': 1, 'y': 2, 'z': 3}} } - - meta = {'ttl': options.ttl, 'gen': options.gen} + # TODO: should TTL and gen be configurable? + meta = {'ttl': 1000, 'gen': 5} policy = None - # invoke operation - client.put((namespace, set, key), record, meta, policy) - - print(record) - print("---") - print("OK, 1 record written.") - - except Exception as e: - #print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - #print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) + self.client.put(self.key, record, meta, policy) diff --git a/examples/client/put_async.py b/examples/client/put_async.py deleted file mode 100644 index 907ba80598..0000000000 --- a/examples/client/put_async.py +++ /dev/null @@ -1,169 +0,0 @@ -# -*- coding: utf-8 -*- -########################################################################## -# Copyright 2013-2021 Aerospike, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -########################################################################## - -import asyncio -import sys -import aerospike -import time -from aerospike_helpers.awaitable import io - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options]" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", default="ram", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", default="ram", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="as-s1.as-network.com", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--timeout", dest="timeout", type="int", default=1000, metavar="", - help="Client timeout") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Namespace of database.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Set to use within namespace of database.") - -optparser.add_option( - "-c", "--test_count", dest="test_count", type="int", default=128, metavar="", - help="Number of async IO to spawn.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - -config = { - 'hosts': [(options.host, options.port)], - 'policies': { - 'total_timeout': options.timeout - } -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - print(f"Connecting to {options.host}:{options.port} with {options.username}:{options.password}") - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - io_results = {} - test_count = options.test_count - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - policy = { - 'total_timeout': options.timeout - } - meta = None - - print(f"IO async test count:{test_count}") - - async def async_io(namespace, set, i): - futures = [] - key = (namespace, \ - set, \ - str(i), \ - client.get_key_digest(namespace, set, str(i))) - record = { - 'i': i, - 'f': 3.1415, - 's': 'abc', - 'u': '안녕하세요', - #'b': bytearray(['d','e','f']), - 'l': [i, 'abc', 'வணக்கம்', ['x', 'y', 'z'], {'x': 1, 'y': 2, 'z': 3}], - 'm': {'i': i, 's': 'abc', 'u': 'ஊத்தாப்பம்', 'l': ['x', 'y', 'z'], 'd': {'x': 1, 'y': 2, 'z': 3}} - } - context = {'state': 0, 'result': {}} - io_results[key[2]] = context - result = None - try: - result = await io.put(client, key, record, meta, policy) - except Exception as eargs: - print(f"error: {eargs.code}, {eargs.msg}, {eargs.file}, {eargs.line}") - pass - io_results[key[2]]['result'] = result - async def main(): - func_list = [] - for i in range(test_count): - func_list.append(async_io(namespace, set, i)) - await asyncio.gather(*func_list) - asyncio.get_event_loop().run_until_complete(main()) - print(f"put_async completed with returning {len(io_results)} records") - #print(io_results) - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - rc = 1 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## - -sys.exit(exitCode) diff --git a/examples/client/query.py b/examples/client/query.py index 42730d5fda..a1bbae5087 100644 --- a/examples/client/query.py +++ b/examples/client/query.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import re @@ -29,38 +28,6 @@ # Option Parsing ########################################################################## -usage = "usage: %prog [options] [where]" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-m", "--module", dest="module", type="string", help="UDF Module.") @@ -85,25 +52,7 @@ "--show-meta", dest="show_meta", action="store_true", help="If set, displays the metadata.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) > 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## - config = { - 'hosts': [(options.host, options.port)], 'lua': { 'user_path': os.path.dirname(__file__) } diff --git a/examples/client/query_apply.py b/examples/client/query_apply.py index 1febe7127c..ad9747734f 100644 --- a/examples/client/query_apply.py +++ b/examples/client/query_apply.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import json @@ -36,28 +35,6 @@ def query_callback(option, opt, value, parser): setattr(parser.values, option.dest, value.split(',')) -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-m", "--module", dest="module", type="string", help="UDF Module.") diff --git a/examples/client/remove.py b/examples/client/remove.py index d6efe0eab6..654f8b1e60 100644 --- a/examples/client/remove.py +++ b/examples/client/remove.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,119 +15,11 @@ # limitations under the License. ########################################################################## -from __future__ import print_function - -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options] key" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) +from .. import ExampleWithRecord -############################################################################### -# Client Configuration -############################################################################### - -config = { - 'hosts': [(options.host, options.port)] -} - - -############################################################################### -# Application -############################################################################### - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - namespace = options.namespace if options.namespace and options.namespace != 'None' else None - set = options.set if options.set and options.set != 'None' else None - key = args.pop() - - client.remove((namespace, set, key)) - - print("OK, 1 record removed.") - - except Exception as eargs: - (code, msg, file, line) = eargs - if code == 602: - print("error: Record not found") - else: - print( - "error: {0}".format((code, msg, file, line)), file=sys.stderr) - rc = 1 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## -sys.exit(exitCode) +class Remove(ExampleWithRecord): + def run(self): + # TODO: should demonstrate the negative path + # since key is an input for the old example. + self.client.remove(self.key) diff --git a/examples/client/remove_bin.py b/examples/client/remove_bin.py index 4adc8dd090..85ed8c94cd 100644 --- a/examples/client/remove_bin.py +++ b/examples/client/remove_bin.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys diff --git a/examples/client/scan.py b/examples/client/scan.py index 146a08d6e8..b8e4862ef3 100644 --- a/examples/client/scan.py +++ b/examples/client/scan.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -28,36 +27,6 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-b", "--bins", dest="bins", type="string", action="append", help="Bins to select from each record.") diff --git a/examples/client/scan_apply.py b/examples/client/scan_apply.py index fc7151d937..39e750739b 100644 --- a/examples/client/scan_apply.py +++ b/examples/client/scan_apply.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import json @@ -35,33 +34,6 @@ def scan_callback(option, opt, value, parser): optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") optparser.add_option( "-m", "--module", dest="module", type="string", diff --git a/examples/client/scan_partition.py b/examples/client/scan_partition.py index 00870b9509..7405cf6315 100644 --- a/examples/client/scan_partition.py +++ b/examples/client/scan_partition.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -28,36 +27,6 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-i", "--partition_id", dest="partition", type="int", default=0, help="Partition id from where to scan.") diff --git a/examples/client/select_many.py b/examples/client/select_many.py index 63eef3814c..4b9e2e97ad 100644 --- a/examples/client/select_many.py +++ b/examples/client/select_many.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -30,36 +29,6 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "-k", "--keys", dest="keys", type="string", default="", metavar="", help="Keys to be accessed in the database server. Should be specified as 'name','name1','name2' etc") diff --git a/examples/client/select_record.py b/examples/client/select_record.py index aab0780369..29abeded74 100644 --- a/examples/client/select_record.py +++ b/examples/client/select_record.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -30,34 +29,6 @@ optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "--no-key", dest="nokey", action="store_true", help="Do not return the key") diff --git a/examples/client/touch.py b/examples/client/touch.py index ae16eb1834..beae74a229 100644 --- a/examples/client/touch.py +++ b/examples/client/touch.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -28,36 +27,6 @@ usage = "usage: %prog [options] key" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - optparser.add_option( "--gen", dest="gen", type="int", default=10, metavar="", help="Generation of the record being written.") diff --git a/examples/client/ttl.py b/examples/client/ttl.py index 1e2638c118..941612885b 100644 --- a/examples/client/ttl.py +++ b/examples/client/ttl.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -25,7 +25,6 @@ # This test is meant to run on an Aerospike 2.x or 3.x server, so the # records that it writes have only primitive types for bin values. -from __future__ import print_function import aerospike import sys @@ -44,34 +43,6 @@ optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - (options, args) = optparser.parse_args() if options.help: diff --git a/examples/client/udf_get.py b/examples/client/udf_get.py index 684bb8db71..da22c6cdd9 100644 --- a/examples/client/udf_get.py +++ b/examples/client/udf_get.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -28,35 +27,8 @@ usage = "usage: %prog [options] module" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - (options, args) = optparser.parse_args() -if options.help: - optparser.print_help() - print() - sys.exit(1) - if len(args) != 1: optparser.print_help() print() diff --git a/examples/client/udf_list.py b/examples/client/udf_list.py index fc2f8b0fff..0f65661de3 100644 --- a/examples/client/udf_list.py +++ b/examples/client/udf_list.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -28,34 +27,6 @@ usage = "usage: %prog [options]" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) ########################################################################## # Client Configuration diff --git a/examples/client/udf_put.py b/examples/client/udf_put.py index c2d86530c0..3b95fa4661 100644 --- a/examples/client/udf_put.py +++ b/examples/client/udf_put.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,7 +15,6 @@ # limitations under the License. ########################################################################## -from __future__ import print_function import aerospike import sys @@ -28,34 +27,6 @@ usage = "usage: %prog [options] filename" -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) if len(args) != 1: optparser.print_help() diff --git a/examples/client/udf_remove.py b/examples/client/udf_remove.py index 18ef2e513d..daeb4c08c9 100644 --- a/examples/client/udf_remove.py +++ b/examples/client/udf_remove.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -15,103 +15,11 @@ # limitations under the License. ########################################################################## -from __future__ import print_function - -import aerospike -import sys - -from optparse import OptionParser - -########################################################################## -# Options Parsing -########################################################################## - -usage = "usage: %prog [options] module" - -optparser = OptionParser(usage=usage, add_help_option=False) - -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) - -if len(args) != 1: - optparser.print_help() - print() - sys.exit(1) - -########################################################################## -# Client Configuration -########################################################################## -config = { - 'hosts': [(options.host, options.port)] -} - -########################################################################## -# Application -########################################################################## - -exitCode = 0 - -try: - - # ---------------------------------------------------------------------------- - # Connect to Cluster - # ---------------------------------------------------------------------------- - - client = aerospike.client(config).connect( - options.username, options.password) - - # ---------------------------------------------------------------------------- - # Perform Operation - # ---------------------------------------------------------------------------- - - try: - - module = args.pop() - - client.udf_remove(module) - print("OK, 1 UDF de-registered") - - except Exception as e: - print("error: {0}".format(e), file=sys.stderr) - exitCode = 2 - - # ---------------------------------------------------------------------------- - # Close Connection to Cluster - # ---------------------------------------------------------------------------- - - client.close() - -except Exception as eargs: - print("error: {0}".format(eargs), file=sys.stderr) - exitCode = 3 - -########################################################################## -# Exit -########################################################################## +from .. import Example -sys.exit(exitCode) +class UDFRemove(Example): + def run(self): + # TODO: need negative path + module = "example.lua" + self.client.udf_remove(module) diff --git a/examples/client/unicode_smiles.py b/examples/client/unicode_smiles.py index bcc13959ef..2706ec6b29 100644 --- a/examples/client/unicode_smiles.py +++ b/examples/client/unicode_smiles.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + ########################################################################## # Copyright 2013-2021 Aerospike, Inc. # @@ -16,7 +16,6 @@ ########################################################################## -from __future__ import print_function import aerospike import sys @@ -32,25 +31,6 @@ optparser = OptionParser(usage=usage, add_help_option=False) -optparser.add_option( - "--help", dest="help", action="store_true", - help="Displays this message.") - -optparser.add_option( - "-U", "--username", dest="username", type="string", metavar="", - help="Username to connect to database.") - -optparser.add_option( - "-P", "--password", dest="password", type="string", metavar="", - help="Password to connect to database.") - -optparser.add_option( - "-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="
", - help="Address of Aerospike server.") - -optparser.add_option( - "-p", "--port", dest="port", type="int", default=3000, metavar="", - help="Port of the Aerospike server.") optparser.add_option( "--timeout", dest="timeout", type="int", default=1000, metavar="", @@ -60,20 +40,6 @@ "--read-timeout", dest="read_timeout", type="int", default=1000, metavar="", help="Client read timeout") -optparser.add_option( - "-n", "--namespace", dest="namespace", type="string", default="test", metavar="", - help="Port of the Aerospike server.") - -optparser.add_option( - "-s", "--set", dest="set", type="string", default="demo", metavar="", - help="Port of the Aerospike server.") - -(options, args) = optparser.parse_args() - -if options.help: - optparser.print_help() - print() - sys.exit(1) ########################################################################## # Application diff --git a/examples/run_all_examples.py b/examples/run_all_examples.py new file mode 100644 index 0000000000..a3ffbd1335 --- /dev/null +++ b/examples/run_all_examples.py @@ -0,0 +1,20 @@ +from .client.get import Get +from .string_ops.customer_experience.email_normalization_old import EmailNormalizationOld +from .string_ops.customer_experience.email_normalization_new import EmailNormalizationNew +from .string_ops.customer_experience.partial_extraction_old import PartialExtractionOld +from .string_ops.customer_experience.partial_extraction_new import PartialExtractionNew +from .string_ops.quickstart.string_expressions import StringExpressions +from .string_ops.quickstart.string_ops import StringOps + +example_classes = [ + Get, + EmailNormalizationOld, + EmailNormalizationNew, + PartialExtractionNew, + PartialExtractionOld, + StringExpressions, + StringOps, +] + +for cls in example_classes: + example = cls().run() diff --git a/examples/string_ops/__init__.py b/examples/string_ops/__init__.py index 89b3d4f7d7..e69de29bb2 100644 --- a/examples/string_ops/__init__.py +++ b/examples/string_ops/__init__.py @@ -1,14 +0,0 @@ -import aerospike - - -class Example: - def __init__(self): - config = { - "hosts": [("127.0.0.1", 3000)] - } - client = aerospike.client(config) - - self.client = client - - def __del__(self): - self.client.close() diff --git a/examples/string_ops/customer_experience/__init__.py b/examples/string_ops/customer_experience/__init__.py index 693114a7c3..64cb347632 100644 --- a/examples/string_ops/customer_experience/__init__.py +++ b/examples/string_ops/customer_experience/__init__.py @@ -1,4 +1,4 @@ -from .. import Example +from ... import Example class CustomerExperienceExample(Example): def __init__(self): diff --git a/examples/string_ops/quickstart/string_expressions.py b/examples/string_ops/quickstart/string_expressions.py index 1e9a3891aa..833ed81920 100644 --- a/examples/string_ops/quickstart/string_expressions.py +++ b/examples/string_ops/quickstart/string_expressions.py @@ -1,4 +1,4 @@ -from .. import Example +from ... import Example from aerospike_helpers import expressions as exp from aerospike_helpers.operations import expression_operations as expr_ops diff --git a/examples/string_ops/quickstart/string_ops.py b/examples/string_ops/quickstart/string_ops.py index 37afbf3de9..143881cdba 100644 --- a/examples/string_ops/quickstart/string_ops.py +++ b/examples/string_ops/quickstart/string_ops.py @@ -1,4 +1,4 @@ -from .. import Example +from ... import Example from aerospike_helpers.operations import string_operations as so class StringOps(Example): diff --git a/examples/string_ops/run_all_examples.py b/examples/string_ops/run_all_examples.py deleted file mode 100644 index 2a3ddaa65f..0000000000 --- a/examples/string_ops/run_all_examples.py +++ /dev/null @@ -1,18 +0,0 @@ -from .customer_experience.email_normalization_old import EmailNormalizationOld -from .customer_experience.email_normalization_new import EmailNormalizationNew -from .customer_experience.partial_extraction_old import PartialExtractionOld -from .customer_experience.partial_extraction_new import PartialExtractionNew -from .quickstart.string_expressions import StringExpressions -from .quickstart.string_ops import StringOps - -example_classes = [ - EmailNormalizationOld, - EmailNormalizationNew, - PartialExtractionNew, - PartialExtractionOld, - StringExpressions, - StringOps -] - -for cls in example_classes: - example = cls().run()