Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-stress-patricia-trie.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2008-2009 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
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 <pat.h>
20 #include <gcutter.h>
21 #include "../lib/grn-assertions.h"
22 
23 void data_read_write(void);
24 void test_read_write(gconstpointer *data);
25 
26 #define N_THREADS 10
27 
28 static grn_ctx *contexts[N_THREADS];
29 static grn_pat *tries[N_THREADS];
30 
31 void
33 {
34  int i;
35 
36  for (i = 0; i < N_THREADS; i++) {
37  contexts[i] = NULL;
38  }
39 }
40 
41 void
43 {
44  int i;
45 
46  for (i = 0; i < N_THREADS; i++) {
48 
49  context = contexts[i];
50  if (context) {
51  grn_pat *trie;
52 
53  trie = tries[i];
54  if (trie)
55  grn_pat_close(context, trie);
56  grn_ctx_fin(context);
57  }
58  }
59 }
60 
61 
62 void
63 data_read_write(void)
64 {
65  gint i;
66  const gchar *n_processes, *process_number, *thread_type;
67 
68  n_processes = g_getenv(GRN_TEST_ENV_N_PROCESSES);
69  if (!n_processes)
70  n_processes = "?";
71 
72  process_number = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
73  if (!process_number)
74  process_number = "?";
75 
76  if (g_getenv(GRN_TEST_ENV_MULTI_THREAD))
77  thread_type = "multi thread";
78  else
79  thread_type = "single thread";
80 
81  for (i = 0; i < N_THREADS; i++) {
82  cut_add_data(cut_take_printf("%s process(es)[%s] - %s[%d]",
83  n_processes, process_number, thread_type, i),
84  GINT_TO_POINTER(i), NULL);
85  }
86 }
87 
88 void
89 test_read_write(gconstpointer *data)
90 {
91  gint i;
92  const gchar *key;
94  grn_pat *trie;
95  const gchar *path;
96  const gchar *value_string;
97  gint process_number = 0;
98  const gchar *process_number_string;
99  void *value;
100  int added;
101  grn_id id = GRN_ID_NIL;
102  grn_rc rc;
103 
104  i = GPOINTER_TO_INT(data);
105  process_number_string = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
106  if (process_number_string)
107  process_number = atoi(process_number_string);
108 
109  key = cut_take_printf("key: %d (%d:%d)", i, process_number, N_THREADS);
110 
111  rc = grn_ctx_init(contexts[i], GRN_CTX_USE_QL);
112  grn_test_assert(rc, cut_message("context: %d (%d)", i, process_number));
113  context = contexts[i];
114 
115  path = g_getenv(GRN_TEST_ENV_PATRICIA_TRIE_PATH);
116  cut_assert_not_null(path);
117  tries[i] = grn_pat_open(context, path);
118  cut_assert_not_null(tries[i],
119  cut_message("patricia trie: %d (%d)", i, process_number));
120  trie = tries[i];
121 
123  grn_pat_get(context, trie, key, strlen(key), &value),
124  cut_message("lookup - fail: %s (%d:%d)", key, i, process_number));
125 
126  value_string = cut_take_printf("value: [%s] (%d:%d)", key, i, process_number);
127  rc = grn_io_lock(context, trie->io, -1);
128  if (rc != GRN_SUCCESS)
129  grn_test_assert(rc);
130  added = 0;
131  id = grn_pat_add(context, trie, key, strlen(key), &value, &added);
132  grn_io_unlock(trie->io);
134  cut_assert_equal_uint(1, added);
135  strcpy(value, value_string);
136 
137  value = NULL;
138  id = grn_pat_get(context, trie, key, strlen(key), &value);
140  id,
141  cut_message("lookup - success: %s (%d:%d)", key, i, process_number));
142  cut_assert_equal_string(value_string, value);
143 
144  tries[i] = NULL;
145  grn_test_assert(grn_pat_close(context, trie));
146 
147  contexts[i] = NULL;
148  grn_test_assert(grn_ctx_fin(context));
149 }