AerospikeC client manual --- key-value storage-record advanced operations

Source: Internet
Author: User

AerospikeC client manual --- key-value storage-record advanced operations

  •  
  • Operation
  • Description
  • Condition
  • Write
  • Write value to bin
  •  
  • Read
  • Read bin Value
  •  
  • Increment
  • Add an integer value of bin.
  • Must be an integer value
  • Append
  • Add bin content
  • The type of the appended content must be the same as that of the bin original value. The value type can only be bytes or string)
  • Prepend
  • Add bin content before
  • The type of the appended content must be the same as that of the bin original value. The value type can only be bytes or string)
  • Touch
  • Update the generation number of the modification record
  •  
Record Advanced Operations Aerospike provides the ability to perform separate modification operations on multiple bins in a single transaction. This feature allows you to modify and read data from multiple bins of the same record to the client in a single transaction. That is, allow the application to perform atomic modifications and return the modified results.
The following is a list of operations that can be performed on a record.
The read operation is executed after all other operations are completed. The execution sequence of non-read operations is defined by the application. The following is an example of an application that tracks the number of page views of a website. The record key is the URL of the page, including the following bin:
Last-updated-integer value. The last update time of the record.
Views-integer value. Page views.
Addr-byte array. IP Address Sequence, separated by NULL characters.
User-byte array. User ID sequence, separated by NULL characters.
Time-byte array. Timestamp string sequence, separated by NULL.

 

The address, user, and time bin are all strings separated by NULL characters. The address, user, and time are synchronized, so that the values related to page browsing are the same index locations in each bin.

as_operations ops;as_operations_inita(&ops, 5);as_operations_add_write_int64(&ops, "last-updated", timestamp);as_operations_add_incr(&ops, "views", 1);as_operations_add_append_raw(&ops, "addr", (uint8_t*)addr, strlen(addr) + 1);as_operations_add_append_raw(&ops, "user", (uint8_t*)user, strlen(user) + 1);as_operations_add_append_raw(&ops, "time", (uint8_t*)time, strlen(time) + 1);as_key key;as_key_init(&key, "app", "pages", url);if (aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec) != AEROSPIKE_OK) {    fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}as_operations_destroy(&ops);
Add and read

Adding and reading are common operation orders. Allow the application to use a counter and read the value after each increase.

The following is a page browsing counter. The record key is a URL with the counter bin name "views ".

To perform a read operation, you must provide a record object to fill in the read record values.

as_operations ops;as_operations_inita(&ops, 2);as_operations_add_incr(&ops, "views", 1);as_operations_add_read(&ops, "views");as_record _rec;as_record *rec = as_record_inita(&_rec, 1);as_key key;as_key_init(&key, "app", "pages", url);if (aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}else { printf("views = %ld\n", as_record_get_int64(rec, "views", 0));}as_record_destroy(rec);as_operations_destroy(&ops);
Touching record

Each record contains metadata, such as the generation and TTL of the record ). The generation number can be considered as the version number of the record. Each update of the record will increase. The life time is the expiration time of the record failure. These values are not modified when reading records. If a record lasts for 5 minutes, it is no longer available after 5 minutes, even if it is continuously read. To ensure that the record does not expire, you can use touch.

In the following example, read Three bins and touch a record so that they do not expire. We want to read these bins from the database, so we initialized a record object containing three bins.

as_operations ops;as_operations_inita(&ops, 4);as_operations_add_touch(&ops);as_operations_add_read(&ops, "x");as_operations_add_read(&ops, "y");as_operations_add_read(&ops, "z");as_record _rec;as_record *rec = as_record_inita(&_rec, 3);as_key key;as_key_init(&key, "app", "pages", url);if (aerospike_key_operate(&as, &err, NULL, &key, &ops, &rec) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}else { printf("x = %ld\n", as_record_get_int64(rec, "x", 0)); printf("y = %ld\n", as_record_get_int64(rec, "y", 0)); printf("z = %ld\n", as_record_get_int64(rec, "z", 0));}as_record_destroy(rec);as_operations_destroy(&ops);
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.