Couchbase의 고급 버킷 액세스자는 다음과 같은 기본 제공 연산자를 사용하여 고급 키-값 저장소(KV) 기능에 액세스할 수 있게 해줍니다.
기본 버킷 접근자와 핸들러에 정의된 동일한 버킷 바인딩을 활용하지만, 더 풍부한 옵션과 연산자를 사용할 수 있습니다:
- 만료 설정 또는 검색
- CAS를 통한 경쟁 조건 해결
- 경쟁이 치열한 인기 KV 항목 조작하기
참고 기본 버킷 액세서리 는 사용하기 훨씬 쉽고, API가 간단하며, 해당 고급 버킷 접근자보다 조금 더 빠릅니다.
카우치베이스 서버 6.6.1에서는 다음과 같은 고급 버킷 접근자가 추가되었습니다:
이 7가지 새로운 버킷 액세서를 사용하면 CAS를 직접 활용하여 경합을 처리하거나 문서 만료를 설정할 수 있습니다(또는 TTL)를 통해 데이터 서비스(또는 KV)의 이벤트 를 추가하고 분산 원자 카운터 연산을 수행합니다.
예를 들어, 업서트와 같은 작업을 할 때 기본 버킷 액세서리에 맹목적으로 의존하지 말고 다음과 같이 하세요. src_bkt[id_str] = 일부_문서를 사용하면 고급 액세스자를 통해 이벤트 함수의 JavaScript 기반 로직을 사용하여 서로 다른 소스에서 동시 변이가 발생한 키의 경합(또는 경합 가능성)을 해결할 수 있습니다.
-
- 문서가 존재하지 않는 경우 다음을 사용할 수 있습니다.
couchbase.insert(src_bkt, {"id: id_str}, some_doc)를 클릭하고 반환값을 확인하여 성공 여부를 확인합니다. - 문서가 있는 경우 다음을 사용할 수 있습니다.
couchbase.replace(src_bkt, {"id: id_str, "cas": current_cas}, some_doc)를 입력하고 반환값이 성공 또는 CAS 불일치인지 확인합니다.
- 문서가 존재하지 않는 경우 다음을 사용할 수 있습니다.
각 고급 버킷 접근자에 대한 JavaScript, 입력 변이, 출력 변이 및/또는 로그 메시지를 포함한 전체 예제를 보려면 다음을 참조하세요. 스크립틀릿: 고급 액세서리 핸들러 문서 예제 섹션에서 확인할 수 있습니다.
고급 GET: 결과 = couchbase.get(바인딩, 메타)
이 작업을 통해 버킷에서 메타데이터와 함께 문서를 읽고 후속 작업을 통해 CAS를 활용하거나 다음을 확인/수정할 수 있습니다. 만료_날짜.
기본 버킷 액세서리의 경우와 대조됩니다. GET 연산은 자바스크립트 바인딩이나 맵을 노출할 뿐입니다, var adoc = src_bkt[meta.id]반환 값은 메타데이터 없이 문서만 반환합니다.
다음은 고급의 예입니다. GET 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
function OnUpdate(doc, meta) { log('input doc ', doc); log('input meta', meta); // could be the same or different var new_meta = {"id":"test_adv_get::1"}; var result = couchbase.get(src_bkt,new_meta); if (result.success) { log('success adv. get: result',result); } else { log('failure adv. get: id',new_meta.id,'result',result); } } |
몇 가지 반환 값 예시입니다:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
{ "doc": { "id": 1, "type": "test_adv_get" }, "meta": { "id": "test_adv_get::1", "cas": "1610034762747412480", "data_type": "json" }, "success": true } { "doc": { "a": 1, "random": 0.09799092443129842 }, "meta": { "id": "test_adv_insert:1", "cas": "1610140272584884224", "expiry_date": "2021-01-08T21:12:12.000Z", "data_type": "json" }, "success": true } { "error": { "code": 272, "name": "LCB_KEY_ENOENT", "desc": "The document key does not exist on the server", "key_not_found": true }, "success": false } |
고급 삽입결과 = couchbase.insert(바인딩, 메타, 문서)
이 작업을 통해 버킷에 새 문서를 만들 수 있습니다. 지정된 키를 가진 문서가 이미 존재하면 이 작업은 실패합니다. 문서에 만료 시간(또는 TTL)을 설정할 수 있습니다.
고급 버킷 액세서와 유사한 기본 버킷 액세서 작업은 없습니다. 삽입 작업 ( src_bkt[meta.id] = adoc 는 업서트에 가깝습니다).
다음은 고급의 예입니다. 삽입 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function OnUpdate(doc, meta) { log('input meta', meta); log('input doc ', doc); // could be the same or different var new_meta = {"id":"test_adv_insert:1"}; // optional set an expiry 60 seconds in the future // new_meta.expiry_date = new Date(Date.now() + 60 * 1000); var new_doc = doc; new_doc.random = Math.random(); var result = couchbase.insert(src_bkt,new_meta,new_doc); if (result.success) { log('success adv. insert: result',result); } else { log('failure adv. insert: id',new_meta.id,'result',result); } } |
몇 가지 반환 값 예시입니다:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "meta": { "id": "test_adv_insert:1", "cas": "1610041053310025728" }, "success": true } { "error": { "code": 272, "name": "LCB_KEY_EEXISTS", "desc": "The document key already exists in the server.", "key_already_exists": true }, "success": false } |
고급 UPSERT결과 = couchbase.upsert(바인딩, 메타, 문서)
이 작업을 통해 버킷에 있는 기존 문서를 업데이트하거나, 없는 경우 지정된 키로 새 문서를 만들 수 있습니다. 이 작업에서는 CAS를 지정할 수 없습니다(자동 무시됨). 또한 문서에 만료 시간(또는 TTL)을 설정할 수 있습니다.
기본 버킷 액세서리의 경우와 대조됩니다. SET 버킷 바인딩 별칭을 통해 정의된 노출된 자바스크립트 맵만 사용하는 연산입니다. src_bkt[meta.id] = adoc. 기본 SET 연산에는 반환값(상태 및 메타데이터 없음)이 없으므로 CAS 값을 확인할 방법이 없습니다.
다음은 고급의 예입니다. UPSERT 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function OnUpdate(doc, meta) { log('input meta', meta); log('input doc ', doc); // could be the same or different var new_meta = {"id":"test_adv_upsert:1"}; // CAS if supplied will be ignored // optional set an expiry 60 seconds in the future // new_meta.expiry_date = new Date(Date.now() + 60 * 1000); var new_doc = doc; new_doc.random = Math.random(); var result = couchbase.upsert(src_bkt,new_meta,new_doc); if (result.success) { log('success adv. upsert: result',result); } else { log('failure adv. upsert: id',new_meta.id,'result',result); } } |
반환 값 예시입니다:
|
1 2 3 4 5 6 7 |
{ "meta": { "id": "test_adv_upsert:1", "cas": "1610127444908376064" }, "success": true } |
고급 교체결과 = couchbase.replace(바인딩, 메타, 문서)
이 작업은 버킷에 있는 기존 문서를 대체합니다. 지정된 키를 가진 문서가 존재하지 않으면 이 작업은 실패합니다. 이 작업은 작업을 진행하기 전에 전제 조건으로 일치해야 하는 CAS 값을 지정할 수 있습니다. 또한 문서에 만료 시간(또는 TTL)을 설정할 수 있습니다.
고급 버킷 액세서와 유사한 기본 버킷 액세서 작업은 없습니다. 교체 작업 ( src_bkt[meta.id] = adoc 는 업서트에 가깝습니다).
다음은 고급의 예입니다. 교체 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
function OnUpdate(doc, meta) { log('input meta', meta); log('input doc ', doc); var mode = 3; // 1-> no CA, 2-> mismatch in CA, 3-> good CAS // Setup, make sure we have our doc to "replace", ignore any errors couchbase.insert(src_bkt,{"id":"test_adv_replace:10"},{"a:": 1}); var new_meta; if (mode === 1) { // If we pass no CAS it will succeed new_meta = {"id":"test_adv_replace:10"}; // optional set an expiry 60 seconds in the future // new_meta.expiry_date = new Date(Date.now() + 60 * 1000); } if (mode === 2) { // If we pass a non-matching CAS it will fail, so test this new_meta = {"id":"test_adv_replace:10", "cas":"1111111111111111111"}; } if (mode === 3) { // If we pass the matching or current CAS it will succeed var tmp_r = couchbase.get(src_bkt,{"id":"test_adv_replace:10"}); if (tmp_r.success) { // Here we use the current CAS just read via couchbase.get(...) new_meta = {"id":"test_adv_replace:10", "cas": tmp_r.meta.cas}; } else { log('Cannot replace non-existing key that create it and rerun',"test_adv_replace:10"); return; } } var new_doc = doc; new_doc.random = Math.random(); var result = couchbase.replace(src_bkt,new_meta,new_doc); if (result.success) { log('success adv. replace: result',result); } else { log('failure adv. replace: id',new_meta.id,'result',result); } } |
몇 가지 반환 값 예시입니다:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "meta": { "id": "test_adv_replace:10", "cas": "1610130177286144000" }, "success": true } { "error": { "code": 272, "name": "LCB_KEY_EEXISTS", "desc": "The document key exists with a CAS value different than specified", "cas_mismatch": true }, "success": false } |
고급 삭제결과 = couchbase.delete(바인딩, 메타)
이 작업을 통해 키로 지정된 버킷에서 문서를 삭제할 수 있습니다. 선택적으로 작업을 진행하기 위한 전제 조건으로 일치시킬 CAS 값을 지정할 수 있습니다.
기본 버킷 액세서리의 경우와 대조됩니다. DEL 연산은 단순히 노출된 자바스크립트 바인딩 또는 맵을 사용하는 연산입니다, 삭제 src_bkt[메타 아이디]반환값이 없는 경우(상태 및 메타데이터 없음).
다음은 고급의 예입니다. 삭제 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
function OnUpdate(doc, meta) { log('input meta', meta); log('input doc ', doc); var mode = 4; // 1-> no CA, 2-> mismatch in CA, 3-> good CAS, 4-> no such key // Setup, make sure we have our doc to "delete", ignore any errors couchbase.insert(src_bkt,{"id":"test_adv_delete:10"},{"a:": 1}); var new_meta; if (mode === 1) { // If we pass no CAS it will succeed new_meta = {"id":"test_adv_delete:10"}; // optional set an expiry 60 seconds in the future // new_meta.expiry_date = new Date(Date.now() + 60 * 1000); } if (mode === 2) { // If we pass a non-matching CAS it will fail, so test this new_meta = {"id":"test_adv_delete:10", "cas":"1111111111111111111"}; } if (mode === 3) { // If we pass the matching or current CAS it will succeed var tmp_r = couchbase.get(src_bkt,{"id":"test_adv_delete:10"}); if (tmp_r.success) { // Here we use the current CAS just read via couchbase.get(...) new_meta = {"id":"test_adv_delete:10", "cas": tmp_r.meta.cas}; } else { log('Cannot delete non-existing key that create it and rerun',"test_adv_delete:10"); return; } } if (mode === 4) { // Remove so that we have: no such key delete src_bkt["test_adv_delete:10"] new_meta = {"id":"test_adv_delete:10"}; } var result = couchbase.delete(src_bkt,new_meta); if (result.success) { log('success adv. delete: result',result); } else { log('failure adv. delete: id',new_meta.id,'result',result); } } |
몇 가지 반환 값 예시입니다:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
{ "meta": { "id": "key::10", "cas": "1609374065129816064" }, "success": true } { "error": { "code": 272, "name": "LCB_KEY_EEXISTS", "desc": "The document key exists with a CAS value different than specified", "cas_mismatch": true }, "success": false } { "error": { "code": 272, "name": "LCB_KEY_ENOENT", "desc": "The document key does not exist on the server", "key_not_found": true }, "success": false } |
고급 인크립션결과 = couchbase.increment(바인딩, 메타)
이 작업은 필드를 원자 단위로 증가시킵니다. 카운트 를 지정한 문서에 추가합니다. 문서의 구조는 아래와 같아야 합니다:
|
1 |
{"count": 23} // 23 is the current counter value |
그리고 증분 연산은 증분 후 값을 반환합니다.
지정된 카운터 문서가 존재하지 않으면 다음을 사용하여 문서가 생성됩니다. 카운트 값을 0으로 설정하고 위에서 언급한 구조를 사용합니다. 따라서 첫 번째 반환 값은 1이 됩니다.
KV 엔진 API의 제한으로 인해 현재 이 작업은 전체 문서 카운터를 조작할 수 없습니다.
고급 버킷 액세서와 유사한 기본 버킷 액세서 작업은 없습니다. 인크립션 작동합니다.
다음은 고급의 예입니다. 인크립션 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function OnUpdate(doc, meta) { log('input meta', meta); log('input doc ', doc); // if doc.count doesn't exist it will be created var ctr_meta = {"id": "my_atomic_counter:1" }; var result = couchbase.increment(src_bkt,ctr_meta); if (result.success) { log('success adv. increment: result',result); } else { log('failure adv. increment: id',ctr_meta.id,'result',result); } } |
반환 값의 예시, 다음과 같이 생성한다고 가정합니다. KEY "my_atomic_counter:1" DOC {"count": 23} 위의 이벤트 함수가 배포되면 카운트가 즉시 증가합니다:
|
1 2 3 4 5 6 7 8 9 10 |
{ "doc": { "count": 24 }, "meta": { "id": "key::1", "cas": "1609374571840471040" }, "success": true } |
고급 선언결과 = couchbase.decrement(바인딩, 메타)
이 작업은 필드를 원자 단위로 감소시킵니다. 카운트 를 지정한 문서에 추가합니다. 문서의 구조는 아래와 같아야 합니다:
|
1 |
{"count": 23} // 23 is the current counter value |
그리고 감소 연산은 감소 후 값을 반환합니다.
지정한 카운터 문서가 존재하지 않으면 카운트 값을 0으로 설정하고 위에서 언급한 구조를 사용합니다. 결과적으로 첫 번째 반환 값은 -1이 됩니다.
KV 엔진 API의 제한으로 인해 현재 이 작업은 전체 문서 카운터를 조작할 수 없습니다.
고급 버킷 액세서와 유사한 기본 버킷 액세서 작업은 없습니다. 선언 작동합니다.
다음은 고급의 예입니다. 선언 작동합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function OnUpdate(doc, meta) { log('input meta', meta); log('input doc ', doc); // if doc.count doesn't exist it will be created var ctr_meta = {"id": "my_atomic_counter:1" }; var result = couchbase.decrement(src_bkt,ctr_meta); if (result.success) { log('success adv. decrement: result',result); } else { log('failure adv. decrement: id',ctr_meta.id,'result',result); } } |
반환 값의 예시, 다음과 같이 생성한다고 가정합니다. KEY "my_atomic_counter:1" DOC {"count": 23} 위의 이벤트 함수가 배포되면 즉시 카운트가 감소합니다:
|
1 2 3 4 5 6 7 8 9 10 |
{ "doc": { "count": 22 }, "meta": { "id": "key::1", "cas": "1609374770297176064" }, "success": true } |
참조