Skip to content

Examples for List operations

Use Aerospike lists to implement use cases when working with any of the following:

  • Queues
  • Unique ordered data
  • Time series containers
  • Leaderboards
  • Membership lists for user profiles

Each one of the list operations has a detailed description with code examples.

Modeling concepts

An Aerospike list can be used to express a many-to-one relationship in a single record. By comparison in a relational database, such a relationship would span multiple rows in two tables.

For example, a user and her vehicles would likely be expressed in a relational database as a users table and a vehicles table, with a vehicles.user_id column indexed and declared to be a foreign key to users.

In Aerospike a Users set would contain user records. Each user’s vehicles could be expressed as a list of map elements, with each map element containing the information of one vehicle. Users without any vehicles would not have a vehicles bin at all. In Aerospike, sets have no schema, and records can vary from one another in their structure. There is no necessity to place-hold vehicles with something like a NULL in a relational database.

Examples

Lists as queues

Starting from an unordered list in the lqueue bin:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]

Enqueue two elements

Enqueue two new elements at the same time, using a single atomic operate call with two list_append operations. After this call, the list has 16 elements.

Record record = client.operate(null, key,
ListOperation.append("lqueue", Value.get(377)),
ListOperation.append("lqueue", Value.get(477))
);
// lqueue: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 477]

Dequeue two elements

Dequeue the first two values with one atomic list operation. The remove_by_index_range operation removes elements starting at index 0, with a count of 2.

Record record = client.operate(null, key,
ListOperation.removeByIndexRange("lqueue", 0, 2, ListReturnType.VALUE)
);
// Removed: [0, 1]
// lqueue: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 477]

Enqueue and dequeue atomically

Use a single operate call to enqueue a new element while dequeuing the first element in the queue.

Record record = client.operate(null, key,
ListOperation.append("lqueue", Value.get(125)),
ListOperation.removeByIndexRange("lqueue", 0, 1, ListReturnType.VALUE)
);
// Removed: [1]
// lqueue: [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 477, 125]

Lists as containers for time series data

A record contains a list of [timestamp, value] pairs in the pairs bin:

[
[1523474230000, 39.04],
[1523474231001, 39.78],
[1523474236006, 40.07],
[1523474235005, 41.18],
[1523474233003, 40.89],
[1523474234004, 40.93]
]

First, insert the record:

List<List<Object>> pairs = Arrays.asList(
Arrays.asList(1523474230000L, 39.04),
Arrays.asList(1523474231001L, 39.78),
Arrays.asList(1523474236006L, 40.07),
Arrays.asList(1523474235005L, 41.18),
Arrays.asList(1523474233003L, 40.89),
Arrays.asList(1523474234004L, 40.93)
);
client.put(null, key, new Bin("pairs", pairs));

Then retrieve the pairs within a specified range of timestamp values using get_by_value_range. The range lower bound is [1523474231000] and upper bound is [1523474234000]. Pairs whose first element falls within this range are returned.

Record record = client.operate(null, key,
ListOperation.getByValueRange("pairs",
Value.get(Arrays.asList(1523474231000L)),
Value.get(Arrays.asList(1523474234000L)),
ListReturnType.VALUE)
);
List<?> list = record.getList("pairs");
for (Object value : list) {
System.out.println(value);
}
// [1523474231001, 39.78]
// [1523474233003, 40.89]
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?