Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 : : /*
3 : : * Copyright 2010 Couchbase, Inc
4 : : *
5 : : * Licensed under the Apache License, Version 2.0 (the "License");
6 : : * you may not use this file except in compliance with the License.
7 : : * You may obtain a copy of the License at
8 : : *
9 : : * http://www.apache.org/licenses/LICENSE-2.0
10 : : *
11 : : * Unless required by applicable law or agreed to in writing, software
12 : : * distributed under the License is distributed on an "AS IS" BASIS,
13 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : : * See the License for the specific language governing permissions and
15 : : * limitations under the License.
16 : : */
17 : :
18 : : #include <stdio.h>
19 : : #include <stdlib.h>
20 : : #include <string.h>
21 : : #include <fcntl.h>
22 : :
23 : : #include "libforestdb/forestdb.h"
24 : : #include "common.h"
25 : : #include "internal_types.h"
26 : : #include "btree_var_kv_ops.h"
27 : :
28 : : #include "memleak.h"
29 : :
30 : : #ifdef __DEBUG
31 : : #ifndef __DEBUG_FDB
32 : : #undef DBG
33 : : #undef DBGCMD
34 : : #undef DBGSW
35 : : #define DBG(...)
36 : : #define DBGCMD(...)
37 : : #define DBGSW(n, ...)
38 : : #endif
39 : : #endif
40 : :
41 : : LIBFDB_API
42 : 13474 : fdb_status fdb_get_kv(fdb_kvs_handle *handle,
43 : : void *key, size_t keylen,
44 : : void **value_out, size_t *valuelen_out)
45 : : {
46 : 13474 : fdb_doc *doc = NULL;
47 : : fdb_status fs;
48 : :
49 [ + + ][ + - ]: 13474 : if (key == NULL || keylen == 0 || keylen > FDB_MAX_KEYLEN ||
[ + - ][ + - ]
[ + + ][ - + ]
50 : : keylen > handle->config.blocksize - 256 ||
51 : : value_out == NULL || valuelen_out == NULL) {
52 : 2 : return FDB_RESULT_INVALID_ARGS;
53 : : }
54 : :
55 : 13472 : fs = fdb_doc_create(&doc, key, keylen, NULL, 0, NULL, 0);
56 [ - + ]: 13472 : if (fs != FDB_RESULT_SUCCESS) {
57 : : if (doc) { // LCOV_EXCL_START
58 : : fdb_doc_free(doc);
59 : : }
60 : : return fs;
61 : : } // LCOV_EXCL_STOP
62 : :
63 : 13472 : fs = fdb_get(handle, doc);
64 [ + + ]: 13472 : if (fs != FDB_RESULT_SUCCESS) {
65 [ + - ]: 403 : if (doc) {
66 : 403 : fdb_doc_free(doc);
67 : : }
68 : 403 : return fs;
69 : : }
70 : :
71 : 13069 : *value_out = doc->body;
72 : 13069 : *valuelen_out = doc->bodylen;
73 [ + - ]: 13069 : if (doc->key) free(doc->key);
74 [ + + ]: 13069 : if (doc->meta) free(doc->meta);
75 : 13069 : free(doc);
76 : :
77 : 13474 : return fs;
78 : : }
79 : :
80 : : LIBFDB_API
81 : 9358 : fdb_status fdb_set_kv(fdb_kvs_handle *handle,
82 : : void *key, size_t keylen,
83 : : void *value, size_t valuelen)
84 : : {
85 : : fdb_doc *doc;
86 : : fdb_status fs;
87 : :
88 [ + + ][ + - ]: 9358 : if (key == NULL || keylen == 0 || keylen > FDB_MAX_KEYLEN ||
[ + - ][ - + ]
89 : : keylen > handle->config.blocksize - 256) {
90 : 1 : return FDB_RESULT_INVALID_ARGS;
91 : : }
92 : :
93 : 9357 : fs = fdb_doc_create(&doc, key, keylen, NULL, 0, value, valuelen);
94 [ - + ]: 9357 : if (fs != FDB_RESULT_SUCCESS) {
95 : : if (doc) { // LCOV_EXCL_START
96 : : fdb_doc_free(doc);
97 : : }
98 : : return fs;
99 : : } // LCOV_EXCL_STOP
100 : :
101 : 9357 : fs = fdb_set(handle, doc);
102 [ - + ]: 9357 : if (fs != FDB_RESULT_SUCCESS) {
103 [ # # ]: 0 : if (doc) {
104 : 0 : fdb_doc_free(doc);
105 : : }
106 : 0 : return fs;
107 : : }
108 : 9357 : fdb_doc_free(doc);
109 : :
110 : 9358 : return fs;
111 : : }
112 : :
113 : : LIBFDB_API
114 : 19 : fdb_status fdb_del_kv(fdb_kvs_handle *handle,
115 : : void *key, size_t keylen)
116 : : {
117 : : fdb_doc *doc;
118 : : fdb_status fs;
119 : :
120 [ + + ][ + - ]: 19 : if (key == NULL || keylen == 0 || keylen > FDB_MAX_KEYLEN ||
[ + - ][ - + ]
121 : : keylen > handle->config.blocksize - 256) {
122 : 1 : return FDB_RESULT_INVALID_ARGS;
123 : : }
124 : :
125 : 18 : fs = fdb_doc_create(&doc, key, keylen, NULL, 0, NULL, 0);
126 [ - + ]: 18 : if (fs != FDB_RESULT_SUCCESS) {
127 : : if (doc) { // LCOV_EXCL_START
128 : : fdb_doc_free(doc);
129 : : }
130 : : return fs;
131 : : } // LCOV_EXCL_STOP
132 : :
133 : 18 : fs = fdb_del(handle, doc);
134 [ - + ]: 18 : if (fs != FDB_RESULT_SUCCESS) {
135 : 0 : fdb_doc_free(doc);
136 : 0 : return fs;
137 : : }
138 : 18 : fdb_doc_free(doc);
139 : :
140 : 19 : return fs;
141 : : }
142 : :
|