Debian Codegraph @

tools/create-hash-table-with-duplicated-key.c

raw symbols SCIP
1 /*
2 Copyright (C) 2020 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "../lib/grn_io.h"
23 #include "../lib/grn_obj.h"
24
25 /* These implementations must be synced with the implementations in
26 * lib/hash.c. */
27 #define GRN_HASH_ENTRY_SEGMENT 1
28
29 static inline void *
30 grn_io_array_at_inline(grn_ctx *ctx, grn_io *io, uint32_t segment_id,
31 uint64_t offset, int flags)
32 {
33 return grn_io_array_at(ctx, io, segment_id, offset, &flags);
34 }
35
36 typedef struct {
37 uint32_t hash_value;
38 uint16_t flag;
39 uint16_t key_size;
40 union {
41 uint8_t buf[sizeof(uint32_t)];
42 uint32_t offset;
43 } key;
44 uint8_t value[1];
45 } grn_io_hash_entry_normal;
46
47 int
48 main(int argc, char **argv)
49 {
50 grn_rc rc = grn_init();
51 if (rc != GRN_SUCCESS) {
52 printf("failed to initialize Groonga: <%d>: %s\n",
53 rc, grn_get_global_error_message());
54 return EXIT_FAILURE;
55 }
56
57 int exit_code = EXIT_SUCCESS;
58 grn_ctx ctx;
59 grn_ctx_init(&ctx, 0);
60 if (ctx.rc != GRN_SUCCESS) {
61 goto exit;
62 }
63
64 grn_db_create(&ctx, "db", NULL);
65 if (ctx.rc != GRN_SUCCESS) {
66 goto exit;
67 }
68
69 const char *table_name = "TableWithDuplicatedKey";
70 grn_obj *table = grn_table_create(&ctx,
71 table_name, strlen(table_name),
72 NULL,
73 GRN_TABLE_HASH_KEY | GRN_OBJ_PERSISTENT,
74 grn_ctx_at(&ctx, GRN_DB_SHORT_TEXT),
75 NULL);
76 if (ctx.rc != GRN_SUCCESS) {
77 goto exit;
78 }
79
80 const char *key = "a";
81 grn_table_add(&ctx, table, key, strlen(key), NULL);
82 if (ctx.rc != GRN_SUCCESS) {
83 goto exit;
84 }
85 grn_id id_for_duplicated_entry = grn_table_add(&ctx, table, "b", 1, NULL);
86 if (ctx.rc != GRN_SUCCESS) {
87 goto exit;
88 }
89 grn_io_hash_entry_normal *entry =
90 grn_io_array_at_inline(&ctx,
91 grn_obj_get_io(&ctx, table),
92 GRN_HASH_ENTRY_SEGMENT,
93 id_for_duplicated_entry,
94 GRN_TABLE_ADD);
95 grn_memcpy(entry->key.buf, key, strlen(key));
96
97 exit :
98 if (ctx.rc != GRN_SUCCESS) {
99 printf("failed: <%d>: %s\n", ctx.rc, ctx.errbuf);
100 exit_code = EXIT_FAILURE;
101 }
102 grn_ctx_fin(&ctx);
103
104 grn_fin();
105
106 return exit_code;
107 }