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 
22 #include "docio.h"
23 #include "filemgr.h"
24 #include "filemgr_ops.h"
25 #include "test.h"
26 
_set_doc(struct docio_object *doc, char *key, char *meta, char *body)27 uint32_t _set_doc(struct docio_object *doc, char *key, char *meta, char *body)
28 {
29     strcpy((char*)doc->key, key);
30     doc->length.keylen = strlen((char*)doc->key) + 1;
31     strcpy((char*)doc->meta, meta);
32     doc->length.metalen = strlen((char*)doc->meta) + 1;
33     strcpy((char*)doc->body, body);
34     doc->length.bodylen = strlen((char*)doc->body) + 1;
35 
36     return sizeof(struct docio_length) + doc->length.keylen + doc->length.metalen + doc->length.bodylen;
37 }
38 
basic_test()39 void basic_test()
40 {
41     TEST_INIT();
42 
43     uint64_t offset;
44     uint32_t docsize;
45     int r;
46     int blocksize = 128;
47     struct docio_handle handle;
48     struct filemgr *file;
49     char keybuf[1024];
50     char metabuf[1024];
51     char bodybuf[4096];
52     struct docio_object doc;
53     struct filemgr_config config;
54     char *fname = (char *) "./docio_testfile";
55 
56     handle.log_callback = NULL;
57 
58     doc.key = (void*)keybuf;
59     doc.meta = (void*)metabuf;
60     doc.body = (void*)bodybuf;
61 
62     memset(&config, 0, sizeof(config));
63     config.blocksize = blocksize;
64     config.ncacheblock = 1024;
65     config.options = FILEMGR_CREATE;
66     config.num_wal_shards = 8;
67     r = system(SHELL_DEL " docio_testfile");
68     (void)r;
69     filemgr_open_result result = filemgr_open(fname, get_filemgr_ops(), &config, NULL);
70     file = result.file;
71     docio_init(&handle, file, false);
72 
73     docsize = _set_doc(&doc, (char *) "this_is_key", (char *) "this_is_metadata",
74                        (char *) "this_is_body_lawiefjaawleif");
75     (void)docsize;
76     offset = docio_append_doc(&handle, &doc, 0, 0);
77     (void)offset;
78     DBG("docsize %d written at %" _F64 "\n", docsize, offset);
79 
80     docsize = _set_doc(&doc, (char *) "this_is_key2", (char *) "this_is_metadata2",
81                        (char *) "hello_world");
82     (void)docsize;
83     offset = docio_append_doc(&handle, &doc, 0, 0);
84     (void)offset;
85     DBG("docsize %d written at %" _F64 "\n", docsize, offset);
86 
87     docsize = _set_doc(&doc, (char *) "key3", (char *) "a", (char *) "b");
88     (void)docsize;
89     offset = docio_append_doc(&handle, &doc, 0, 0);
90     (void)offset;
91     DBG("docsize %d written at %" _F64 "\n", docsize, offset);
92 
93     docsize = _set_doc(&doc, (char *) "key4", (char *) "a", (char *) "b");
94     (void)docsize;
95     offset = docio_append_doc(&handle, &doc, 0, 0);
96     (void)offset;
97     DBG("docsize %d written at %" _F64 "\n", docsize, offset);
98 
99     docsize = _set_doc(&doc, (char *) "key5", (char *) "a", (char *) "b");
100     (void)docsize;
101     offset = docio_append_doc(&handle, &doc, 0, 0);
102     (void)offset;
103     DBG("docsize %d written at %" _F64 "\n", docsize, offset);
104 
105     doc.length.keylen = 1;
106     doc.length.metalen = 1;
107     doc.length.bodylen = 190;
108     docsize = 12 + 182;
109     (void)docsize;
110     offset = docio_append_doc(&handle, &doc, 0, 0);
111     (void)offset;
112     DBG("docsize %d written at %" _F64 "\n", docsize, offset);
113 
114     keylen_t keylen;
115     docio_read_doc_key(&handle, 81, &keylen, (void*)keybuf);
116     DBG("keylen %d %s\n", keylen, keybuf);
117 
118     filemgr_commit(file, true, NULL);
119     filemgr_close(file, true, NULL, NULL);
120 
121     TEST_RESULT("basic test");
122 }
123 
main()124 int main()
125 {
126     #ifdef _MEMPOOL
127         mempool_init();
128     #endif
129 
130 
131     basic_test();
132 
133     return 0;
134 }
135