File: create-hash-table-with-duplicated-key.c

package info (click to toggle)
groonga 15.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 163,080 kB
  • sloc: ansic: 770,564; cpp: 48,925; ruby: 40,447; javascript: 10,250; yacc: 7,045; sh: 5,602; python: 2,821; makefile: 1,672
file content (107 lines) | stat: -rw-r--r-- 3,056 bytes parent folder | download | duplicates (2)
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
/*
  Copyright (C) 2020  Sutou Kouhei <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <stdlib.h>

#include "../lib/grn_io.h"
#include "../lib/grn_obj.h"

/* These implementations must be synced with the implementations in
 * lib/hash.c. */
#define GRN_HASH_ENTRY_SEGMENT 1

static inline void *
grn_io_array_at_inline(grn_ctx *ctx, grn_io *io, uint32_t segment_id,
                       uint64_t offset, int flags)
{
  return grn_io_array_at(ctx, io, segment_id, offset, &flags);
}

typedef struct {
  uint32_t hash_value;
  uint16_t flag;
  uint16_t key_size;
  union {
    uint8_t buf[sizeof(uint32_t)];
    uint32_t offset;
  } key;
  uint8_t value[1];
} grn_io_hash_entry_normal;

int
main(int argc, char **argv)
{
  grn_rc rc = grn_init();
  if (rc != GRN_SUCCESS) {
    printf("failed to initialize Groonga: <%d>: %s\n",
           rc, grn_get_global_error_message());
    return EXIT_FAILURE;
  }

  int exit_code = EXIT_SUCCESS;
  grn_ctx ctx;
  grn_ctx_init(&ctx, 0);
  if (ctx.rc != GRN_SUCCESS) {
    goto exit;
  }

  grn_db_create(&ctx, "db", NULL);
  if (ctx.rc != GRN_SUCCESS) {
    goto exit;
  }

  const char *table_name = "TableWithDuplicatedKey";
  grn_obj *table = grn_table_create(&ctx,
                                    table_name, strlen(table_name),
                                    NULL,
                                    GRN_TABLE_HASH_KEY | GRN_OBJ_PERSISTENT,
                                    grn_ctx_at(&ctx, GRN_DB_SHORT_TEXT),
                                    NULL);
  if (ctx.rc != GRN_SUCCESS) {
    goto exit;
  }

  const char *key = "a";
  grn_table_add(&ctx, table, key, strlen(key), NULL);
  if (ctx.rc != GRN_SUCCESS) {
    goto exit;
  }
  grn_id id_for_duplicated_entry = grn_table_add(&ctx, table, "b", 1, NULL);
  if (ctx.rc != GRN_SUCCESS) {
    goto exit;
  }
  grn_io_hash_entry_normal *entry =
    grn_io_array_at_inline(&ctx,
                           grn_obj_get_io(&ctx, table),
                           GRN_HASH_ENTRY_SEGMENT,
                           id_for_duplicated_entry,
                           GRN_TABLE_ADD);
  grn_memcpy(entry->key.buf, key, strlen(key));

exit :
  if (ctx.rc != GRN_SUCCESS) {
    printf("failed: <%d>: %s\n", ctx.rc, ctx.errbuf);
    exit_code = EXIT_FAILURE;
  }
  grn_ctx_fin(&ctx);

  grn_fin();

  return exit_code;
}