Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Bin operations

Use the Aerospike Ruby client APIs to modify and read multiple record bins in a single command. This atomic modification returns the result.

Use the Aerospike Ruby APIs to perform the following operations.

OperationMethod NameDescriptionConditions
writeOperation.writeWrite a value to a bin.
readOperation.readRead the value of a bin.
read_headerOperation.read_headerRead only the record metadata (generation and time-to-live).
addOperation.addAdd an integer to the existing bin value.Integer only.
appendOperation.appendappend string to the existing bin valueString only.
prependOperation.prependPrepend string to the existing bin value.String only.
touchOperation.touchRewrite the same record.Metadata (generation and time-to-live) updates.

Operation specification

To specify operations on different bins in the same transaction:

  • Create bins with values to apply:
key = Key.new('test', "demoset", "opkey")
bin1 = Bin.new("opt_int_bin", 7)
bin2 = Bin.new("opt_string_bin", "string value")
client.put(key, [bin1, bin2])
  • Create the appropriate operations to use the bins and supply them to Operate():
bin3 = Bin.new(bin1.name, 4)
bin4 = Bin.new(bin2.name, "new string")
record = client.operate(
key,
Operation.add(bin3),
Operation.put(bin4),
Operation.get
)

The result returns in the Record object.

Collection operations

Use the Aerospike Ruby client to perform operations on Lists and Maps in the Aerospike database.

In addition to the basic data types such as integers or strings, the Aerospike database supports several collection data types (CDTs). Two of these CDTs are Lists and Maps. Using the operations described in the following sections, you can manipulate lists and maps on the server. For example, you can add or remove an item without the need to read and replace the whole bin value where the list or map is stored.

Operations Available on Lists

The following operations are supported on List values:

OperationDescription
appendAdds one or more elements to the end of the list.
insertInserts one or more elements at the specified index.
popRemoves and returns the list element at the specified index.
pop_rangeRemoves and returns the list elements at the specified range.
removeRemoves the list element at the specified index.
remove_rangeRemoves the list elements at the specified range.
setSets a list element at the specified index to a new value.
trimRemoves all list elements not within the specified range.
clearRemoves all elements from the list.
sizeReturns the element count of the list.
getReturns the list element at the specified index.
get_rangeReturns the list of elements at the specified range.

Examples

These examples illustrate common operations on collections.

Tracking Page Views

This example application tracks the page views of a website. The key is the URL for a page. The record contains the following bins:

  • last-updated — (integer) The POSIX time this record was last updated.
  • views — (integer) The number of page view entries.
  • addr — (list) A list of IP address strings.
  • user — (list) A list of user ID strings.
  • time — (list) A list of timestamps.

The addr, user, and time bins are Lists of scalar values such as Strings and Integers. Their entries are in sync in that a single page-view value is at the same index of each bin.

require "aerospike"
include Aerospike
client = Client.new
ts = Time.now.to_i
addr = request.remote_ip
user = current_user.id
# Array of operations to be performed on the record
ops = [
Operation.put(Bin.new('last-updated', ts)),
Operation.add(Bin.new('views', 1)),
Operation.get('views'),
CDT::ListOperation.append("addr", addr),
CDT::ListOperation.append("user", user),
CDT::ListOperation.append("time", ts)
]
# Key of the record on which the operations have to be performed
key = Key.new("app", "pageviews", uid)
# operate on the record
result = client.operate(key, ops)
views = result.bins['views'] # post-increment value

Operations Available on Maps

The following operations are supported on Map values:

OperationDescription
set_policySets the map policy (sort order)
putUpdates a map entry to a new value or creates a new map entry.
put_itemsUpdates/creates multiple map entries.
incrementIncrements the numeric value of an existing map entry.
decrementDecrements the numeric value of an existing map entry.
clearRemoves all elements from the map.
remove_keysRemoves on or more keys from a map; optionally returns the removed entries.
remove_key_rangeRemoves a range of keys from a map; optionally returns the removed entries.
remove_valuesRemoves one or more map entries identified by value; optionally returns the removed entries.
remove_value_rangeRemoves a range of map entries identified by value; optionally returns the removed entries.
remove_indexRemoves the map entry identified by index; optionally returns the removed entry.
remove_index_rangeRemoves a range of map entries identified by index; optionally returns the removed entries.
remove_by_rankRemoves the map entry identified by rank; optionally returns the removed entry.
remove_by_rank_rangeRemoves a range of map entries identified by rank; optionally returns the removed entries.
sizeReturns the number of entries in the map.
get_keyReturns the map entry identified by key.
get_key_rangeReturns a range of map entries identified by key.
get_valueReturns the map entries identified by value.
get_value_rangeReturns a range of map entries identified by value.
get_indexReturns the map entry identified by index.
get_index_rangeReturns a range of map entries identified by index.
get_by_rankReturns a map entry identified by rank.
get_by_rank_rangeReturns a range of map entries identified by rank.
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?