| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 26 |
|
| 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 |
} |