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.
Operation | Method Name | Description | Conditions |
---|---|---|---|
write | Operation.write | Write a value to a bin. | |
read | Operation.read | Read the value of a bin. | |
read_header | Operation.read_header | Read only the record metadata (generation and time-to-live). | |
add | Operation.add | Add an integer to the existing bin value. | Integer only. |
append | Operation.append | append string to the existing bin value | String only. |
prepend | Operation.prepend | Prepend string to the existing bin value. | String only. |
touch | Operation.touch | Rewrite 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:
Operation | Description |
---|---|
append | Adds one or more elements to the end of the list. |
insert | Inserts one or more elements at the specified index. |
pop | Removes and returns the list element at the specified index. |
pop_range | Removes and returns the list elements at the specified range. |
remove | Removes the list element at the specified index. |
remove_range | Removes the list elements at the specified range. |
set | Sets a list element at the specified index to a new value. |
trim | Removes all list elements not within the specified range. |
clear | Removes all elements from the list. |
size | Returns the element count of the list. |
get | Returns the list element at the specified index. |
get_range | Returns 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.newts = Time.now.to_iaddr = request.remote_ipuser = current_user.id
# Array of operations to be performed on the recordops = [ 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 performedkey = Key.new("app", "pageviews", uid)
# operate on the recordresult = client.operate(key, ops)views = result.bins['views'] # post-increment value
Operations Available on Maps
The following operations are supported on Map values:
Operation | Description |
---|---|
set_policy | Sets the map policy (sort order) |
put | Updates a map entry to a new value or creates a new map entry. |
put_items | Updates/creates multiple map entries. |
increment | Increments the numeric value of an existing map entry. |
decrement | Decrements the numeric value of an existing map entry. |
clear | Removes all elements from the map. |
remove_keys | Removes on or more keys from a map; optionally returns the removed entries. |
remove_key_range | Removes a range of keys from a map; optionally returns the removed entries. |
remove_values | Removes one or more map entries identified by value; optionally returns the removed entries. |
remove_value_range | Removes a range of map entries identified by value; optionally returns the removed entries. |
remove_index | Removes the map entry identified by index; optionally returns the removed entry. |
remove_index_range | Removes a range of map entries identified by index; optionally returns the removed entries. |
remove_by_rank | Removes the map entry identified by rank; optionally returns the removed entry. |
remove_by_rank_range | Removes a range of map entries identified by rank; optionally returns the removed entries. |
size | Returns the number of entries in the map. |
get_key | Returns the map entry identified by key. |
get_key_range | Returns a range of map entries identified by key. |
get_value | Returns the map entries identified by value. |
get_value_range | Returns a range of map entries identified by value. |
get_index | Returns the map entry identified by index. |
get_index_range | Returns a range of map entries identified by index. |
get_by_rank | Returns a map entry identified by rank. |
get_by_rank_range | Returns a range of map entries identified by rank. |