summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/csuite/wt2999_join_extractor/main.c
blob: 2aec304605916b757c62858b3a6980a18a8adb88 (plain)
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
/*-
 * Public Domain 2014-present MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
#include "test_util.h"

/*
 * JIRA ticket reference: WT-2999
 *
 * Test case description: Create a table that stores ~4K size blobs;
 * two indices are defined using a pair of custom extractors
 * that pull the first and second 32-bit integers from the blob.
 * A simple join is created using the two indices, and iterated.
 *
 * Failure mode: When a custom extractor is used with cursor
 * joins, there are memory leaks at the point where the extractor
 * sets the key.
 */
static int
custom_extract1(WT_EXTRACTOR *extractor, WT_SESSION *session, const WT_ITEM *key,
  const WT_ITEM *value, WT_CURSOR *result_cursor)
{
    WT_ITEM item;
    int32_t v1;

    (void)extractor;
    (void)key;
    testutil_check(wiredtiger_struct_unpack(session, value->data, value->size, "u", &item));

    v1 = ((int *)item.data)[0];
    item.data = &v1;
    item.size = sizeof(v1);

    result_cursor->set_key(result_cursor, &item);
    return (result_cursor->insert(result_cursor));
}

static int
custom_extract2(WT_EXTRACTOR *extractor, WT_SESSION *session, const WT_ITEM *key,
  const WT_ITEM *value, WT_CURSOR *result_cursor)
{
    WT_ITEM item;
    int32_t v2;

    (void)extractor;
    (void)key;
    testutil_check(wiredtiger_struct_unpack(session, value->data, value->size, "u", &item));

    v2 = ((int *)item.data)[1];
    item.data = &v2;
    item.size = sizeof(v2);

    result_cursor->set_key(result_cursor, &item);
    return (result_cursor->insert(result_cursor));
}

static WT_EXTRACTOR custom_extractor1 = {custom_extract1, NULL, NULL};
static WT_EXTRACTOR custom_extractor2 = {custom_extract2, NULL, NULL};

int
main(int argc, char *argv[])
{
    TEST_OPTS *opts, _opts;
    WT_CONNECTION *conn;
    WT_CURSOR *cursor1, *cursor2, *jcursor;
    WT_ITEM k, v;
    WT_SESSION *session;
    int32_t key, val[2];
    int i, ret;

    opts = &_opts;
    memset(opts, 0, sizeof(*opts));
    testutil_check(testutil_parse_opts(argc, argv, opts));
    testutil_make_work_dir(opts->home);

    testutil_check(wiredtiger_open(opts->home, NULL, "create", &conn));
    opts->conn = conn;
    testutil_check(conn->open_session(conn, NULL, NULL, &session));

    testutil_check(conn->add_extractor(conn, "custom_extractor1", &custom_extractor1, NULL));
    testutil_check(conn->add_extractor(conn, "custom_extractor2", &custom_extractor2, NULL));

    testutil_check(
      session->create(session, "table:main", "key_format=u,value_format=u,columns=(k,v)"));
    testutil_check(
      session->create(session, "index:main:index1", "key_format=u,extractor=custom_extractor1"));
    testutil_check(
      session->create(session, "index:main:index2", "key_format=u,extractor=custom_extractor2"));

    testutil_check(session->open_cursor(session, "table:main", NULL, NULL, &cursor1));

    v.data = val;
    v.size = sizeof(val);
    k.data = &key;
    k.size = sizeof(key);

    key = 10;
    val[0] = 20;
    val[1] = 30;
    for (i = 0; i < 100000; ++i) {
        key += i;
        val[0] += i;
        val[1] += i;
        cursor1->set_key(cursor1, &k);
        cursor1->set_value(cursor1, &v);
        testutil_check(cursor1->insert(cursor1));
    }

    testutil_check(cursor1->close(cursor1));

    testutil_check(session->open_cursor(session, "index:main:index1", NULL, NULL, &cursor1));
    key = 20;
    cursor1->set_key(cursor1, &k);
    testutil_check(cursor1->search(cursor1));

    testutil_check(session->open_cursor(session, "index:main:index2", NULL, NULL, &cursor2));
    key = 30;
    cursor2->set_key(cursor2, &k);
    testutil_check(cursor2->search(cursor2));

    testutil_check(session->open_cursor(session, "join:table:main", NULL, NULL, &jcursor));
    testutil_check(session->join(session, jcursor, cursor1, "compare=gt"));
    testutil_check(session->join(session, jcursor, cursor2, "compare=gt"));

    while ((ret = jcursor->next(jcursor)) == 0) // leak
        ;
    testutil_assert(ret == WT_NOTFOUND);

    testutil_check(jcursor->close(jcursor));
    testutil_check(cursor1->close(cursor1));
    testutil_check(cursor2->close(cursor2));

    testutil_check(opts->conn->close(opts->conn, NULL));
    opts->conn = NULL;
    testutil_cleanup(opts);

    return (EXIT_SUCCESS);
}