Aerospike C客戶端手冊———鍵-值存儲—記錄高級操作

記錄高級操作

Aerospike提供在單個事務中對多個bin執行分開的單獨修改操作的能力。此特性允許對同一條記錄多個bin的修改與讀取數據到客戶端在單個事務中完成,即請允許應用執行原子性的修改且返回修改後的結果。

下面是可在一條記錄上執行的操作列表。

操作 描述 條件
write 寫入值到bin  
read 讀取bin值  
increment 增加一個bin的整型數值 必須是整型數值
append 追加bin內容 追加內容的類型必須與bin原值的類型一致。數值類型只能是字節(bytes)或字符串(string)
prepend 前加bin內容 追加內容的類型必須與bin原值的類型一致。數值類型只能是字節(bytes)或字符串(string)
touch 更新修改記錄的分代編號  

讀取操作在所有其它操作完成後執行。非讀取操作的執行順序由應用定義。

跟蹤頁面瀏覽數

下面是一個跟蹤網站頁面瀏覽數的應用舉例。記錄鍵是頁面的URL,包含如下的bin:

  • last-updated —  整型值。記錄的最後更新時間。
  • views — 整型值。頁面瀏覽次數。
  • addr — 字節數組。IP地址序列,以空字符(NULL)分割。
  • user —  字節數組。用戶ID序列,以空字符(NULL)分割。
  • time — 字節數組。時間戳字符串序列 ,以空字符(NULL)分割。

地址、用戶和時間bin都是以空字符(NULL)分割的字符串。地址、用戶和時間是同步的,這樣一個頁面瀏覽相關的值在各個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);

增加與讀取

增加與讀取是常見的操作順序。允許應用使用一個計數器並在每次增加後讀取值。

下面是一個頁面瀏覽計數器。記錄的鍵是URL,包含計數器的bin名稱爲“views”。

由於要執行讀操作,所以需要提供一個記錄對象用於填充讀取的記錄數值。

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記錄 

每條記錄都包含元數據,比如:記錄的分代編號(generation )和生存時間(TTL)。分代編號可被認爲是記錄的版本號,記錄每次更新時會增加。生存時間是記錄失效的到期時間。讀取記錄時,這些值都不會被修改。若一條記錄的生存時間是5分鐘,即使是不斷地讀取,5分鐘過後它也不再可用。爲了保證記錄不失效,可使用touch操作。

下面的例子,讀取3個bin並touch一條記錄,使其不至失效。我們要從數據庫中讀取這些bin,所以初始化了一個容納3個bin的記錄對象。

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);

附加的示例

請參見目錄【examples/basic_examples/touch】下的更多使用aerospike_key_operate()的示例


譯        者: 歪脖大肚子Q

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章