Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/smoke-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}
Expand Down
40 changes: 40 additions & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -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__()
48 changes: 1 addition & 47 deletions examples/client/aggregate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

##########################################################################
# Copyright 2013-2021 Aerospike, Inc.
#
Expand All @@ -16,7 +16,6 @@
##########################################################################


from __future__ import print_function
import aerospike
import json
import re
Expand All @@ -34,56 +33,18 @@

optparser = OptionParser(usage=usage, add_help_option=False)

optparser.add_option(
"-U", "--username", dest="username", type="string", metavar="<USERNAME>",
help="Username to connect to database.")

optparser.add_option(
"-P", "--password", dest="password", type="string", metavar="<PASSWORD>",
help="Password to connect to database.")

optparser.add_option(
"-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="<ADDRESS>",
help="Address of Aerospike server.")

optparser.add_option(
"-p", "--port", dest="port", type="int", default=3000, metavar="<PORT>",
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="<NS>",
help="Port of the Aerospike server.")

optparser.add_option(
"-s", "--set", dest="set", type="string", default="demo", metavar="<SET>",
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__)
}
Expand All @@ -104,13 +65,6 @@ def parse_arg(s):

try:

# ----------------------------------------------------------------------------
# Connect to Cluster
# ----------------------------------------------------------------------------

client = aerospike.client(config).connect(
options.username, options.password)

# ----------------------------------------------------------------------------
# Perform Operation
# ----------------------------------------------------------------------------
Expand Down
150 changes: 15 additions & 135 deletions examples/client/append.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

##########################################################################
# Copyright 2013-2021 Aerospike, Inc.
#
Expand All @@ -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="<USERNAME>",
help="Username to connect to database.")

optparser.add_option(
"-P", "--password", dest="password", type="string", metavar="<PASSWORD>",
help="Password to connect to database.")

optparser.add_option(
"-h", "--host", dest="host", type="string", default="127.0.0.1", metavar="<ADDRESS>",
help="Address of Aerospike server.")

optparser.add_option(
"-p", "--port", dest="port", type="int", default=3000, metavar="<PORT>",
help="Port of the Aerospike server.")
from .. import Example

optparser.add_option(
"-n", "--namespace", dest="namespace", type="string", default="test", metavar="<NS>",
help="Port of the Aerospike server.")

optparser.add_option(
"-s", "--set", dest="set", type="string", default="demo", metavar="<SET>",
help="Port of the Aerospike server.")

optparser.add_option(
"--gen", dest="gen", type="int", default=10, metavar="<GEN>",
help="Generation of the record being written.")

optparser.add_option(
"--ttl", dest="ttl", type="int", default=1000, metavar="<TTL>",
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)
Loading
Loading