sdskv-list-keys-prefix-test.cc 6.59 KB
Newer Older
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <margo.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

#include "sdskv-client.h"

static std::string gen_random_string(size_t len);

int main(int argc, char *argv[])
{
    char cli_addr_prefix[64] = {0};
    char *sdskv_svr_addr_str;
    char *db_name;
    margo_instance_id mid;
    hg_addr_t svr_addr;
    uint8_t mplex_id;
    uint32_t num_keys;
    sdskv_client_t kvcl;
    sdskv_provider_handle_t kvph;
    hg_return_t hret;
    int ret;

    if(argc != 5)
    {
        fprintf(stderr, "Usage: %s <sdskv_server_addr> <mplex_id> <db_name> <num_keys>\n", argv[0]);
        fprintf(stderr, "  Example: %s tcp://localhost:1234 1 foo 1000\n", argv[0]);
        return(-1);
    }
    sdskv_svr_addr_str = argv[1];
    mplex_id           = atoi(argv[2]);
    db_name            = argv[3];
    num_keys           = atoi(argv[4]);

    /* initialize Margo using the transport portion of the server
     * address (i.e., the part before the first : character if present)
     */
    for(unsigned i=0; (i<63 && sdskv_svr_addr_str[i] != '\0' && sdskv_svr_addr_str[i] != ':'); i++)
        cli_addr_prefix[i] = sdskv_svr_addr_str[i];

    /* start margo */
    mid = margo_init(cli_addr_prefix, MARGO_SERVER_MODE, 0, 0);
    if(mid == MARGO_INSTANCE_NULL)
    {
        fprintf(stderr, "Error: margo_init()\n");
        return(-1);
    }

    ret = sdskv_client_init(mid, &kvcl);
    if(ret != 0)
    {
        fprintf(stderr, "Error: sdskv_client_init()\n");
        margo_finalize(mid);
        return -1;
    }

    /* look up the SDSKV server address */
    hret = margo_addr_lookup(mid, sdskv_svr_addr_str, &svr_addr);
    if(hret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: margo_addr_lookup()\n");
        sdskv_client_finalize(kvcl);
        margo_finalize(mid);
        return(-1);
    }

    /* create a SDSKV provider handle */
    ret = sdskv_provider_handle_create(kvcl, svr_addr, mplex_id, &kvph);
    if(ret != 0)
    {
        fprintf(stderr, "Error: sdskv_provider_handle_create()\n");
        margo_addr_free(mid, svr_addr);
        sdskv_client_finalize(kvcl);
        margo_finalize(mid);
        return(-1);
    }

    /* open the database */
    sdskv_database_id_t db_id;
    ret = sdskv_open(kvph, db_name, &db_id);
    if(ret == 0) {
        printf("Successfuly open database %s, id is %ld\n", db_name, db_id);
    } else {
        fprintf(stderr, "Error: could not open database %s\n", db_name);
        sdskv_provider_handle_release(kvph);
        margo_addr_free(mid, svr_addr);
        sdskv_client_finalize(kvcl);
        margo_finalize(mid);
        return(-1);
    }

    /* **** put keys ***** */
    std::vector<std::string> keys;
    std::string prefix("XYZ");
    size_t max_key_size   = 16;
    size_t max_value_size = 16;

    for(unsigned i=0; i < num_keys; i++) {
        std::string k;
        if(i % 2 == 0) {
            k = prefix;
            k += gen_random_string(((max_key_size + (rand()%max_key_size))/2) - prefix.size());
        } else {
            k = gen_random_string(((max_key_size + (rand()%max_key_size))/2));
        }
        // half of the entries will be put using bulk
        auto v = gen_random_string(i*max_value_size/num_keys);
        ret = sdskv_put(kvph, db_id,
                (const void *)k.data(), k.size()+1,
                (const void *)v.data(), v.size()+1);
        if(ret != 0) {
            fprintf(stderr, "Error: sdskv_put() failed (iteration %d)\n", i);
            sdskv_shutdown_service(kvcl, svr_addr);
            sdskv_provider_handle_release(kvph);
            margo_addr_free(mid, svr_addr);
            sdskv_client_finalize(kvcl);
            margo_finalize(mid);
            return -1;
        }
        keys.push_back(k);
        std::cerr << k << "\t ===> " << v << std::endl;
    }
    printf("Successfuly inserted %d keys\n", num_keys);

    /* **** list keys **** */
    std::sort(keys.begin(), keys.end());

    auto i1 = keys.size()/4;
    auto i2 = 3*keys.size()/4;
    auto keys_after = keys[i1-1];

    hg_size_t max_keys = i2-i1;
    std::vector<std::vector<char>> result_strings(max_keys, std::vector<char>(max_key_size+1));
    std::vector<void*> list_result(max_keys);
    std::vector<hg_size_t> ksizes(max_keys, max_key_size+1);

    for(unsigned i=0; i<max_keys; i++) {
        list_result[i] = (void*)result_strings[i].data();
    }

    std::cout << "Expecting " << max_keys << " keys after " << keys_after << std::endl;

    ret = sdskv_list_keys_with_prefix(kvph, db_id, 
                (const void*)keys_after.c_str(), keys_after.size()+1,
                prefix.data(), prefix.size(),
                list_result.data(), ksizes.data(), &max_keys);

    if(ret != 0) {
        fprintf(stderr, "Error: sdskv_list_keys() failed\n");
        sdskv_shutdown_service(kvcl, svr_addr);
        sdskv_provider_handle_release(kvph);
        margo_addr_free(mid, svr_addr);
        sdskv_client_finalize(kvcl);
        margo_finalize(mid);
        return -1;
    }

    /* put the returned strings in an array */
    std::vector<std::string> res;
    for(auto ptr : list_result) {
        res.push_back(std::string((const char*)ptr));
        std::cout << *res.rbegin() << std::endl;
    }

    /* check that the returned keys are correct */
    for(unsigned i=0; i < max_keys; i++) {
        fprintf(stderr, "Returned key %d is %s\n", i, res[i].c_str());
        if(strncmp(res[i].c_str(), prefix.c_str(), prefix.size()) != 0) {
            fprintf(stderr, "Error: returned key doesn't match prefix\n");
            fprintf(stderr, "       key received: %s\n", res[i].c_str());
            fprintf(stderr, "       expected prefix: %s\n", prefix.c_str());
            sdskv_shutdown_service(kvcl, svr_addr);
            sdskv_provider_handle_release(kvph);
            margo_addr_free(mid, svr_addr);
            sdskv_client_finalize(kvcl);
            margo_finalize(mid);
            return -1;
        }
    }

    /* shutdown the server */
    ret = sdskv_shutdown_service(kvcl, svr_addr);

    /**** cleanup ****/
    sdskv_provider_handle_release(kvph);
    margo_addr_free(mid, svr_addr);
    sdskv_client_finalize(kvcl);
    margo_finalize(mid);
    return(ret);
}

static std::string gen_random_string(size_t len) {
    static const char alphanum[] =
                "0123456789"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "abcdefghijklmnopqrstuvwxyz";
    std::string s(len, ' ');
    for (unsigned i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }
    return s;
}