def test_list_set_order(self):
        operation = list_operations.list_set_order(self.test_bin,
                                                   aerospike.LIST_ORDERED)

        self.as_connection.operate(self.test_key, [operation])
        _, _, bins = self.as_connection.get(self.test_key)
        assert bins[self.test_bin] == sorted(self.test_list)
    def setup(self, request, as_connection):
        """
        Setup Method
        """
        skip_less_than_430(self.server_version)
        self.keys = []
        # INDEXES    0, 1, 2, 3, 4, 05
        # RINDEX     5, 4, 3, 2, 1, 0
        # RANK       2, 1, 0, 3, 4, 5
        # RRANK     -4,-5,-6,-3,-2,-1
        self.test_list = [3, 1, 5, 7, 9, 11]

        self.test_key = 'test', 'demo', 'new_list_op'
        self.test_bin = 'list'
        self.as_connection.put(self.test_key, {self.test_bin: self.test_list})

        # Make sure the list is ordered, in order to get expected return order.
        ops = [list_operations.list_sort("list", 0),
               list_operations.list_set_order("list", aerospike.LIST_ORDERED)]
        self.as_connection.operate(self.test_key, ops)
        self.keys.append(self.test_key)

        yield

        for key in self.keys:
            try:
                self.as_connection.remove(key)
            except e.AerospikeError:
                pass
Exemplo n.º 3
0
    def setup(self, request, as_connection):
        """
        Setup Method
        """
        skip_less_than_430(self.server_version)
        self.keys = []
        # INDEXES    0, 1, 2, 3, 4, 05
        # RINDEX     5, 4, 3, 2, 1, 0
        # RANK       2, 1, 0, 3, 4, 5
        # RRANK     -4,-5,-6,-3,-2,-1
        self.test_list = [3, 1, 5, 7, 9, 11]

        self.test_key = 'test', 'demo', 'new_list_op'
        self.test_bin = 'list'
        self.as_connection.put(self.test_key, {self.test_bin: self.test_list})

        # Make sure the list is ordered, in order to get expected return order.
        ops = [
            list_operations.list_sort("list", 0),
            list_operations.list_set_order("list", aerospike.LIST_ORDERED)
        ]
        self.as_connection.operate(self.test_key, ops)
        self.keys.append(self.test_key)

        yield

        for key in self.keys:
            try:
                self.as_connection.remove(key)
            except e.AerospikeError:
                pass
    def test_list_set_order(self):
        operation = list_operations.list_set_order(
            self.test_bin, aerospike.LIST_ORDERED
        )

        self.as_connection.operate(self.test_key, [operation])
        _, _, bins = self.as_connection.get(self.test_key)
        assert bins[self.test_bin] == sorted(self.test_list)
    def setup(self, request, as_connection):
        """
        Setup Method
        """
        self.keys = []
        cdt_list_val = [
            [0, "a"],
            [1, "b"],
            [1, "c"],
            [1, "d", "e"],
            [2, "f"],
            [2, "two"],
            [3, "g"]
        ]
        cdt_map_val = {
            'a': [0, 'a'],
            'b': [1, 'b'],
            'c': [1, 'c'],
            'd': [1, 'd', 'e'],
            'e': [2, 'f'],
            'f': [2, 'two'],
            'g': [3, 'g']
        }

        self.cdt_key = ('test', 'cdt_values', 'wildcard')
        self.cdt_list_bin = "cdt_list_bin"
        self.cdt_map_bin = "cdt_map_bin"

        self.as_connection.put(
            self.cdt_key,
            {
                self.cdt_list_bin: cdt_list_val,
                self.cdt_map_bin: cdt_map_val
            }
        )
        # Make sure the list is ordered, in order to get expected return order.
        ops = [lo.list_sort(self.cdt_list_bin, 0),
               lo.list_set_order(self.cdt_list_bin, aerospike.LIST_ORDERED)]
        self.as_connection.operate(self.cdt_key, ops)

        self.keys.append(self.cdt_key)

        yield

        for rec_key in self.keys:
            try:
                self.as_connection.remove(rec_key)
            except e.AerospikeError:
                pass
Exemplo n.º 6
0
                listops.list_remove_by_index_range("l", 3, aerospike.LIST_RETURN_NONE),
                2,
                operations.read("l"),
            ],
        )
        print("\nremove_by_index_range('l', 3, NONE)")
        print("Remaining: {}".format(bins["l"]))
        # remove_by_index_range('l', 3, NONE)
        # Remaining: [11, 10, [7, 0]]
    except ex.OpNotApplicable as e:
        print("\nError: {0} [{1}]".format(e.msg, e.code))

    # turn into an ordered list, then remove the range of elements starting at
    # index -2. this is the same as saying removing the top two ranked elements
    ops = [
        listops.list_set_order("l", aerospike.LIST_ORDERED),
        operations.read("l"),
        listops.list_remove_by_index_range("l", -2, aerospike.LIST_RETURN_VALUE),
        operations.read("l"),
    ]
    key, metadata, bins = client.operate_ordered(key, ops)
    print(
        "\nAfter becoming an ordered list: {}\nRemoved: {}\nRemaining: {}".format(
            bins[0][1], bins[1][1], bins[2][1]
        )
    )
    # After becoming an ordered list: [10, 11, [7, 0]]
    # Removed: [11, [7, 0]]
    # Remaining: [10]

except ex.ClientError as e:
Exemplo n.º 7
0
    client.remove(key)
except ex.RecordError as e:
    pass

try:
    print("\nset_order(bin, order[, context])\n")
    # create a record with an unordered list
    client.put(key, {"l": [4, 5, 8, 1, 2, [3, 2], 9, 6]})
    key, metadata, bins = client.get(key)
    print("{}".format(bins["l"]))
    # [4, 5, 8, 1, 2, [3, 2], 9, 6]

    # set the inner list (at index 5) to ORDERED
    ctx = [cdt_ctx.cdt_ctx_list_index(5)]
    client.operate(key,
                   [listops.list_set_order("l", aerospike.LIST_ORDERED, ctx)])
    key, metadata, bins = client.get(key)
    print("\nset_order('l', ORDERED, BY_LIST_INDEX(5))\n{}".format(bins["l"]))
    # set_order('l', ORDERED, BY_LIST_INDEX(5))
    # [4, 5, 8, 1, 2, [2, 3], 9, 6]

    # set the outer list to ORDERED
    client.operate(key, [listops.list_set_order("l", aerospike.LIST_ORDERED)])
    key, metadata, bins = client.get(key)
    print("\nset_order('l', ORDERED)\n{} ".format(bins["l"]))
    # [1, 2, 4, 5, 6, 8, 9, [2, 3]]
    # note that list ordering puts integers elements before list elements
    # see https://www.aerospike.com/docs/guide/cdt-ordering.html
except ex.ClientError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
    [9.84, "Donovan Bailey", "Atlanta, USA", "July 27, 1996"],
    [9.79, "Maurice Greene", "Athens, Greece", "June 16, 1999"],
    [9.77, "Asafa Powell", "Athens, Greece", "June 14, 2005"],
    [9.77, "Asafa Powell", "Gateshead, England", "June 11, 2006"],
    [9.77, "Asafa Powell", "Zurich, Switzerland", "August 18, 2006"],
    [9.74, "Asafa Powell", "Rieti, Italy", "September 9, 2007"],
    [9.72, "Usain Bolt", "New York, USA", "May 31, 2008"],
    [9.69, "Usain Bolt", "Beijing, China", "August 16, 2008"],
    [9.58, "Usain Bolt", "Berlin, Germany", "August 16, 2009"],
]
bins = {"scores": wr_100m_1}

try:
    client.put(key, bins, {"ttl": aerospike.TTL_NEVER_EXPIRE})
    # client.list_set_order(key, 'scores', list_policy)
    ops = [lh.list_set_order("scores", aerospike.LIST_ORDERED)]
    client.operate(key, ops)
except e.RecordError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
    sys.exit(3)

pp = pprint.PrettyPrinter(indent=2)
try:
    list_policy = {
        "list_order":
        aerospike.LIST_ORDERED,
        "write_flags": (aerospike.LIST_WRITE_ADD_UNIQUE
                        | aerospike.LIST_WRITE_PARTIAL
                        | aerospike.LIST_WRITE_NO_FAIL),
    }
    ops = [lh.list_append_items("scores", wr_100m_2, list_policy)]
Exemplo n.º 9
0
def main(filename, hostname='localhost', port=3000):
    '''
    This initializes the data for our main app
    Args:
        filename string name of file.
    '''
    client = aerospike.client({'hosts': [(hostname, port)]}).connect()

    main_record_key = 'test', 'stocks', 'symbols'
    symbol_to_dates = {}
    latest_price = {}

    with open(filename) as inf:
        count = 0
        #  Write daily stock performance records.
        for row in csv.DictReader(inf):
            count += 1
            if count % 20000 == 0:
                print('{} records loaded so far.'.format(count))
            symbol = row[NAME_COLUMN]
            closing_price = row[CLOSE_COLUMN]
            opening_price = row[OPEN_COLUMN]
            date = row[DATE_COLUMN]
            if symbol not in symbol_to_dates:
                symbol_to_dates[symbol] = [date]
            else:
                symbol_to_dates[symbol].append(date)

            latest_price[symbol] = closing_price

            write_daily_record(client, symbol, date, opening_price,
                               closing_price)

    #  Write a record containing all of the stock symbols in alphabetical order.
    symbols = list(sorted([symbol for symbol in symbol_to_dates]))
    main_record_rec = {'symbols': symbols}
    client.put(main_record_key, main_record_rec)

    #  For each symbol, write a record containing the data points available for it.
    sort_op = lo.list_set_order('dates', aerospike.LIST_ORDERED)
    for symbol in symbol_to_dates:
        key = 'test', 'stocks', symbol
        rec = {
            'symbol': symbol,
            'dates': list(sorted(symbol_to_dates[symbol])),
            'price': latest_price[symbol]
        }
        client.put(key, rec)
        client.operate(key, [sort_op])

    #  Create some sample portfolios.
    for portfolio_name in PORTFOLIOS:
        key = 'test', PORTFOLIO_SET, portfolio_name
        rec = {
            'name': portfolio_name,
            'id': portfolio_name,
            'stocks': PORTFOLIOS[portfolio_name]
        }
        client.put(key, rec)

    #  Now Enter the preloaded portfolios into the list.
    portfolio_list_key = 'test', 'stocks', RECENT_PORTFOLIOS_KEY
    recent_portfolios = [portfolio_name for portfolio_name in PORTFOLIOS]
    client.put(portfolio_list_key, {'portfolios': recent_portfolios})