Skip to content

Commit 5e6ae4f

Browse files
committed
Add support for create patricia trie table without database
1 parent 56fe62d commit 5e6ae4f

4 files changed

Lines changed: 147 additions & 4 deletions

File tree

ext/groonga/rb-grn-context.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
Copyright (C) 2010-2021 Sutou Kouhei <kou@clear-code.com>
44
Copyright (C) 2016 Masafumi Yokoyama <yokoyama@clear-code.com>
5-
Copyright (C) 2019 Horimoto Yasuhiro <horimoto@clear-code.com>
5+
Copyright (C) 2019-2022 Horimoto Yasuhiro <horimoto@clear-code.com>
66
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
@@ -326,6 +326,26 @@ rb_grn_context_ensure (VALUE *context)
326326
return SELF(*context);
327327
}
328328

329+
/*
330+
* If context has already implemented database, it returns +true+,
331+
* otherwise it returns +false+.
332+
*
333+
* @return [Boolean] `true` if context has already implemented database.
334+
* `false` if context does not implement database.
335+
*/
336+
VALUE
337+
rb_grn_context_implement_db (grn_ctx *context)
338+
{
339+
grn_obj *database;
340+
341+
database = grn_ctx_db(context);
342+
if (database) {
343+
return Qtrue;
344+
} else {
345+
return Qfalse;
346+
}
347+
}
348+
329349
VALUE
330350
rb_grn_context_rb_string_new (grn_ctx *context, const char *string, long length)
331351
{

ext/groonga/rb-grn-patricia-trie.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ rb_grn_patricia_trie_s_create (int argc, VALUE *argv, VALUE klass)
166166
grn_obj *key_type = NULL, *value_type = NULL, *table;
167167
const char *name = NULL, *path = NULL;
168168
unsigned name_size = 0;
169+
unsigned int key_size = 0;
170+
unsigned int value_size = 0;
169171
grn_table_flags flags = GRN_OBJ_TABLE_PAT_KEY;
170172
VALUE rb_table;
171173
VALUE options, rb_context, rb_name, rb_path, rb_persistent;
172-
VALUE rb_key_normalize, rb_key_with_sis, rb_key_type;
174+
VALUE rb_key_normalize, rb_key_with_sis, rb_key_type, rb_key_size, rb_key_var_size;
173175
VALUE rb_value_type;
174176
VALUE rb_default_tokenizer;
175177
VALUE rb_token_filters;
@@ -186,6 +188,8 @@ rb_grn_patricia_trie_s_create (int argc, VALUE *argv, VALUE klass)
186188
"key_normalize", &rb_key_normalize,
187189
"key_with_sis", &rb_key_with_sis,
188190
"key_type", &rb_key_type,
191+
"key_size", &rb_key_size,
192+
"key_var_size", &rb_key_var_size,
189193
"value_type", &rb_value_type,
190194
"default_tokenizer", &rb_default_tokenizer,
191195
"token_filters", &rb_token_filters,
@@ -227,8 +231,17 @@ rb_grn_patricia_trie_s_create (int argc, VALUE *argv, VALUE klass)
227231
if (RVAL2CBOOL(rb_sub_records))
228232
flags |= GRN_OBJ_WITH_SUBREC;
229233

230-
table = grn_table_create(context, name, name_size, path,
231-
flags, key_type, value_type);
234+
if (rb_grn_context_implement_db(context)) {
235+
table = grn_table_create(context, name, name_size, path,
236+
flags, key_type, value_type);
237+
} else {
238+
if (RVAL2CBOOL(rb_key_var_size))
239+
flags |= GRN_OBJ_KEY_VAR_SIZE;
240+
if (!NIL_P(rb_key_size))
241+
key_size = NUM2UINT(rb_key_size);
242+
value_size = 0;
243+
table = (grn_obj *)grn_pat_create(context, path, key_size, value_size, flags);
244+
}
232245
if (!table)
233246
rb_grn_context_check(context, rb_ary_new_from_values(argc, argv));
234247
rb_table = GRNOBJECT2RVAL(klass, context, table, GRN_TRUE);

ext/groonga/rb-grn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ void rb_grn_context_reset_floating_objects(RbGrnContext *rb_grn_contex
393393
void rb_grn_context_mark_grn_id (grn_ctx *context,
394394
grn_id id);
395395
grn_ctx *rb_grn_context_ensure (VALUE *context);
396+
VALUE rb_grn_context_implement_db (grn_ctx *context);
396397
VALUE rb_grn_context_get_default (void);
397398
VALUE rb_grn_context_to_exception (grn_ctx *context,
398399
VALUE related_object);

test/test-patricia-trie-no-db.rb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2022 Horimoto Yasuhiro <horimoto@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+
class PatriciaTrieTestNoDB < Test::Unit::TestCase
19+
include GroongaTestUtils
20+
include ERB::Util
21+
22+
23+
def test_support_key?
24+
assert_predicate(Groonga::PatriciaTrie.create(key_size: 4096,
25+
key_var_size: true),
26+
:support_key?)
27+
end
28+
29+
def test_add
30+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
31+
users.define_column("address", "ShortText")
32+
me = users.add("me", :address => "me@example.com")
33+
assert_equal("me@example.com", me[:address])
34+
end
35+
36+
def test_key?
37+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
38+
assert_false(users.key?("morita"))
39+
users.add("morita")
40+
assert_true(users.key?("morita"))
41+
end
42+
43+
def test_has_key?
44+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
45+
assert_false(users.has_key?("morita"))
46+
users.add("morita")
47+
assert_true(users.has_key?("morita"))
48+
end
49+
50+
def test_scan
51+
Groonga::Context.default_options = {:encoding => "utf-8"}
52+
words = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
53+
words.add("リンク")
54+
arupaka = words.add("アルパカ")
55+
words.add("アルパカ(生物)")
56+
adventure_of_link = words.add('リンクの冒険')
57+
words.add('冒険')
58+
gaxtu = words.add('ガッ')
59+
muteki = words.add('MUTEKI')
60+
assert_equal([[muteki, "MUTEKI", 0, 6],
61+
[adventure_of_link, "リンクの冒険", 7, 18],
62+
[arupaka, "アルパカ", 42, 12],
63+
[gaxtu, "ガッ", 55, 6]],
64+
words.scan('MUTEKI リンクの冒険 ミリバール アルパカ ガッ'))
65+
end
66+
67+
def test_added?
68+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
69+
bob = users.add("bob")
70+
assert_predicate(bob, :added?)
71+
bob_again = users.add("bob")
72+
assert_not_predicate(bob_again, :added?)
73+
end
74+
75+
def test_rename
76+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
77+
name = users.define_column("name", "ShortText")
78+
address = users.define_column("address", "ShortText")
79+
80+
users.rename("People")
81+
assert_equal(["People", "People.name", "People.address"],
82+
[users.name, name.name, address.name])
83+
end
84+
85+
def test_each
86+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
87+
users.add("Alice")
88+
users.add("Bob")
89+
users.add("Carl")
90+
91+
user_names = []
92+
users.each do |user|
93+
user_names << user.key
94+
end
95+
assert_equal(["Alice", "Bob", "Carl"], user_names)
96+
end
97+
98+
def test_truncate
99+
users = Groonga::PatriciaTrie.create(key_size: 4096, key_var_size: true)
100+
users.add("Alice")
101+
users.add("Bob")
102+
users.add("Carl")
103+
assert_equal(3, users.size)
104+
assert_nothing_raised do
105+
users.truncate
106+
end
107+
assert_equal(0, users.size)
108+
end
109+
end

0 commit comments

Comments
 (0)