groonga-client 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,5 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
1
  # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
4
- # Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com>
2
+ # Copyright (C) 2013-2017 Kouhei Sutou <kou@clear-code.com>
5
3
  #
6
4
  # This library is free software; you can redistribute it and/or
7
5
  # modify it under the terms of the GNU Lesser General Public
@@ -66,6 +64,37 @@ module Groonga
66
64
  :domain,
67
65
  :range,
68
66
  :source)
67
+ # @return [::Array<String>]
68
+ # The flag names of the column.
69
+ #
70
+ # @since 0.5.3
71
+ def flags
72
+ (super || "").split("|")
73
+ end
74
+
75
+ # @return [Boolean]
76
+ # `true` if the column is a scalar column, `false` otherwise.
77
+ #
78
+ # @since 0.5.3
79
+ def scalar?
80
+ flags.include?("COLUMN_SCALAR")
81
+ end
82
+
83
+ # @return [Boolean]
84
+ # `true` if the column is a vector column, `false` otherwise.
85
+ #
86
+ # @since 0.5.3
87
+ def vector?
88
+ flags.include?("COLUMN_VECTOR")
89
+ end
90
+
91
+ # @return [Boolean]
92
+ # `true` if the column is an index column, `false` otherwise.
93
+ #
94
+ # @since 0.5.3
95
+ def index?
96
+ flags.include?("COLUMN_INDEX")
97
+ end
69
98
  end
70
99
  end
71
100
  end
@@ -96,8 +96,25 @@ module Groonga
96
96
  @tables
97
97
  end
98
98
 
99
- private
100
- def coerce_tables
99
+ # @param name [String] The object name to be retrieved.
100
+ #
101
+ # @return [Plugin, Type, Tokenizer, Normalizer, TokenFilter, Table, Column]
102
+ # The object named `name`.
103
+ #
104
+ # @since 0.5.3
105
+ def [](name)
106
+ name = name.to_s if name.is_a?(Symbol)
107
+ if name.include?(".")
108
+ table_name, column_name = name.split(".", 2)
109
+ tables[table_name].columns[column_name]
110
+ else
111
+ tables[name] ||
112
+ types[name] ||
113
+ tokenizers[name] ||
114
+ normalizers[name] ||
115
+ token_filters[name] ||
116
+ plugins[name]
117
+ end
101
118
  end
102
119
 
103
120
  module HashValueConverter
@@ -147,6 +164,11 @@ module Groonga
147
164
  include Hashie::Extensions::MethodAccess
148
165
  end
149
166
 
167
+ class Command < ::Hash
168
+ include Hashie::Extensions::MergeInitializer
169
+ include Hashie::Extensions::MethodAccess
170
+ end
171
+
150
172
  class Index < ::Hash
151
173
  include Hashie::Extensions::MethodAccess
152
174
 
@@ -204,6 +226,8 @@ module Groonga
204
226
  when :value_type
205
227
  value = ValueType.new(value) unless value.nil?
206
228
  super(key, value)
229
+ when :command
230
+ super(key, Command.new(value))
207
231
  else
208
232
  super
209
233
  end
@@ -243,6 +267,8 @@ module Groonga
243
267
  super(key, coerce_columns(value))
244
268
  when :indexes
245
269
  super(key, coerce_indexes(value))
270
+ when :command
271
+ super(key, Command.new(value))
246
272
  else
247
273
  super
248
274
  end
@@ -16,6 +16,6 @@
16
16
 
17
17
  module Groonga
18
18
  class Client
19
- VERSION = "0.5.2"
19
+ VERSION = "0.5.3"
20
20
  end
21
21
  end
@@ -0,0 +1,369 @@
1
+ # Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+
17
+ require "time"
18
+
19
+ require "groonga/command/parser"
20
+
21
+ require "groonga/client"
22
+ require "groonga/client/test-helper"
23
+ require "groonga/client/command-line/groonga-client-index-recreate"
24
+
25
+ class TestCommandLineIndexRecreate < Test::Unit::TestCase
26
+ include Groonga::Client::TestHelper
27
+
28
+ def setup
29
+ @now = Time.parse("2017-10-25T17:22:00+0900")
30
+ stub(Time).now {@now}
31
+ end
32
+
33
+ def groonga_url
34
+ @groonga_server_runner.url.to_s
35
+ end
36
+
37
+ def open_client
38
+ Groonga::Client.open(:url => groonga_url) do |client|
39
+ yield(client)
40
+ end
41
+ end
42
+
43
+ def restore(commands)
44
+ open_client do |client|
45
+ values = nil
46
+ Groonga::Command::Parser.parse(commands) do |event, *args|
47
+ case event
48
+ when :on_command
49
+ command, = args
50
+ response = client.execute(command)
51
+ unless response.success?
52
+ raise Groonga::Client::Request::ErrorResponse.new(response)
53
+ end
54
+ when :on_load_start
55
+ command, = args
56
+ values = []
57
+ when :on_load_columns
58
+ command, columns = args
59
+ command[:columns] ||= columns.join(",")
60
+ when :on_load_value
61
+ command, value = args
62
+ values << value
63
+ when :on_load_complete
64
+ command, = args
65
+ command[:values] ||= JSON.generate(values)
66
+ response = client.execute(command)
67
+ unless response.success?
68
+ raise Groonga::Client::Request::ErrorResponse.new(response)
69
+ end
70
+ else
71
+ p [:unhandled_event, event, *args]
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def dump
78
+ open_client do |client|
79
+ client.dump.body
80
+ end
81
+ end
82
+
83
+ def index_recreate(*arguments)
84
+ command_line = Groonga::Client::CommandLine::GroongaClientIndexRecreate.new
85
+ begin
86
+ stdout, $stdout = $stdout, StringIO.new
87
+ stderr, $stderr = $stderr, StringIO.new
88
+ [
89
+ command_line.run(["--url", groonga_url, *arguments]),
90
+ $stdout.string,
91
+ $stderr.string,
92
+ ]
93
+ ensure
94
+ $stdout, $stderr = stdout, stderr
95
+ end
96
+ end
97
+
98
+ def test_no_alias_column
99
+ index_recreate
100
+ assert_equal(<<-DUMP.chomp, dump)
101
+ config_set alias.column Aliases.real_name
102
+
103
+ table_create Aliases TABLE_HASH_KEY ShortText
104
+ column_create Aliases real_name COLUMN_SCALAR ShortText
105
+ DUMP
106
+ end
107
+
108
+ def test_real_index
109
+ restore(<<-COMMANDS)
110
+ table_create Memos TABLE_HASH_KEY ShortText
111
+ column_create Memos content COLUMN_SCALAR Text
112
+
113
+ table_create Terms TABLE_PAT_KEY ShortText \
114
+ --normalizer NormalizerAuto \
115
+ --default_tokenizer TokenBigram
116
+ column_create Terms memos_content \
117
+ COLUMN_INDEX|WITH_POSITION \
118
+ Memos content
119
+ COMMANDS
120
+
121
+ assert_equal([true, "", ""],
122
+ index_recreate("Terms.memos_content"))
123
+
124
+ assert_equal(<<-DUMP.chomp, dump)
125
+ config_set alias.column Aliases.real_name
126
+
127
+ table_create Aliases TABLE_HASH_KEY ShortText
128
+ column_create Aliases real_name COLUMN_SCALAR ShortText
129
+
130
+ table_create Memos TABLE_HASH_KEY ShortText
131
+ column_create Memos content COLUMN_SCALAR Text
132
+
133
+ table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
134
+
135
+ load --table Aliases
136
+ [
137
+ ["_key","real_name"],
138
+ ["Terms.memos_content","Terms.memos_content_20171025"]
139
+ ]
140
+
141
+ column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
142
+ DUMP
143
+ end
144
+
145
+ def test_old_index
146
+ restore(<<-COMMANDS)
147
+ config_set alias.column CustomAliases.name
148
+
149
+ table_create CustomAliases TABLE_HASH_KEY ShortText
150
+ column_create CustomAliases name COLUMN_SCALAR ShortText
151
+
152
+ table_create Memos TABLE_HASH_KEY ShortText
153
+ column_create Memos content COLUMN_SCALAR Text
154
+
155
+ table_create Terms TABLE_PAT_KEY ShortText \
156
+ --normalizer NormalizerAuto \
157
+ --default_tokenizer TokenBigram
158
+ column_create Terms memos_content_20171024 \
159
+ COLUMN_INDEX|WITH_POSITION \
160
+ Memos content
161
+
162
+ load --table CustomAliases
163
+ [
164
+ ["_key","name"],
165
+ ["Terms.memos_content","Terms.memos_content_20171024"]
166
+ ]
167
+ COMMANDS
168
+
169
+ assert_equal([true, "", ""],
170
+ index_recreate("Terms.memos_content"))
171
+
172
+ assert_equal(<<-DUMP.chomp, dump)
173
+ config_set alias.column CustomAliases.name
174
+
175
+ table_create CustomAliases TABLE_HASH_KEY ShortText
176
+ column_create CustomAliases name COLUMN_SCALAR ShortText
177
+
178
+ table_create Memos TABLE_HASH_KEY ShortText
179
+ column_create Memos content COLUMN_SCALAR Text
180
+
181
+ table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
182
+
183
+ load --table CustomAliases
184
+ [
185
+ ["_key","name"],
186
+ ["Terms.memos_content","Terms.memos_content_20171025"]
187
+ ]
188
+
189
+ column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
190
+ column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
191
+ DUMP
192
+ end
193
+
194
+ def test_old_indexes
195
+ restore(<<-COMMANDS)
196
+ config_set alias.column CustomAliases.name
197
+
198
+ table_create CustomAliases TABLE_HASH_KEY ShortText
199
+ column_create CustomAliases name COLUMN_SCALAR ShortText
200
+
201
+ table_create Memos TABLE_HASH_KEY ShortText
202
+ column_create Memos content COLUMN_SCALAR Text
203
+
204
+ table_create Terms TABLE_PAT_KEY ShortText \
205
+ --normalizer NormalizerAuto \
206
+ --default_tokenizer TokenBigram
207
+ column_create Terms memos_content_20171022 \
208
+ COLUMN_INDEX|WITH_POSITION \
209
+ Memos content
210
+ column_create Terms memos_content_20171023 \
211
+ COLUMN_INDEX|WITH_POSITION \
212
+ Memos content
213
+ column_create Terms memos_content_20171024 \
214
+ COLUMN_INDEX|WITH_POSITION \
215
+ Memos content
216
+
217
+ load --table CustomAliases
218
+ [
219
+ ["_key","name"],
220
+ ["Terms.memos_content","Terms.memos_content_20171024"]
221
+ ]
222
+ COMMANDS
223
+
224
+ assert_equal([true, "", ""],
225
+ index_recreate("Terms.memos_content"))
226
+
227
+ assert_equal(<<-DUMP.chomp, dump)
228
+ config_set alias.column CustomAliases.name
229
+
230
+ table_create CustomAliases TABLE_HASH_KEY ShortText
231
+ column_create CustomAliases name COLUMN_SCALAR ShortText
232
+
233
+ table_create Memos TABLE_HASH_KEY ShortText
234
+ column_create Memos content COLUMN_SCALAR Text
235
+
236
+ table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
237
+
238
+ load --table CustomAliases
239
+ [
240
+ ["_key","name"],
241
+ ["Terms.memos_content","Terms.memos_content_20171025"]
242
+ ]
243
+
244
+ column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
245
+ column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
246
+ DUMP
247
+ end
248
+
249
+ def test_already_latest
250
+ restore(<<-COMMANDS)
251
+ config_set alias.column CustomAliases.name
252
+
253
+ table_create CustomAliases TABLE_HASH_KEY ShortText
254
+ column_create CustomAliases name COLUMN_SCALAR ShortText
255
+
256
+ table_create Memos TABLE_HASH_KEY ShortText
257
+ column_create Memos content COLUMN_SCALAR Text
258
+
259
+ table_create Terms TABLE_PAT_KEY ShortText \
260
+ --normalizer NormalizerAuto \
261
+ --default_tokenizer TokenBigram
262
+ column_create Terms memos_content_20171022 \
263
+ COLUMN_INDEX|WITH_POSITION \
264
+ Memos content
265
+ column_create Terms memos_content_20171023 \
266
+ COLUMN_INDEX|WITH_POSITION \
267
+ Memos content
268
+ column_create Terms memos_content_20171024 \
269
+ COLUMN_INDEX|WITH_POSITION \
270
+ Memos content
271
+ column_create Terms memos_content_20171025 \
272
+ COLUMN_INDEX|WITH_POSITION \
273
+ Memos content
274
+
275
+ load --table CustomAliases
276
+ [
277
+ ["_key","name"],
278
+ ["Terms.memos_content","Terms.memos_content_20171025"]
279
+ ]
280
+ COMMANDS
281
+
282
+ assert_equal([true, "", ""],
283
+ index_recreate("Terms.memos_content"))
284
+
285
+ assert_equal(<<-DUMP.chomp, dump)
286
+ config_set alias.column CustomAliases.name
287
+
288
+ table_create CustomAliases TABLE_HASH_KEY ShortText
289
+ column_create CustomAliases name COLUMN_SCALAR ShortText
290
+
291
+ table_create Memos TABLE_HASH_KEY ShortText
292
+ column_create Memos content COLUMN_SCALAR Text
293
+
294
+ table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
295
+
296
+ load --table CustomAliases
297
+ [
298
+ ["_key","name"],
299
+ ["Terms.memos_content","Terms.memos_content_20171025"]
300
+ ]
301
+
302
+ column_create Terms memos_content_20171022 COLUMN_INDEX|WITH_POSITION Memos content
303
+ column_create Terms memos_content_20171023 COLUMN_INDEX|WITH_POSITION Memos content
304
+ column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
305
+ column_create Terms memos_content_20171025 COLUMN_INDEX|WITH_POSITION Memos content
306
+ DUMP
307
+ end
308
+
309
+ def test_latest_alias_but_not_exist
310
+ restore(<<-COMMANDS)
311
+ config_set alias.column CustomAliases.name
312
+
313
+ table_create CustomAliases TABLE_HASH_KEY ShortText
314
+ column_create CustomAliases name COLUMN_SCALAR ShortText
315
+
316
+ table_create Memos TABLE_HASH_KEY ShortText
317
+ column_create Memos content COLUMN_SCALAR Text
318
+
319
+ table_create Terms TABLE_PAT_KEY ShortText \
320
+ --normalizer NormalizerAuto \
321
+ --default_tokenizer TokenBigram
322
+ column_create Terms memos_content_20171022 \
323
+ COLUMN_INDEX|WITH_POSITION \
324
+ Memos content
325
+ column_create Terms memos_content_20171023 \
326
+ COLUMN_INDEX|WITH_POSITION \
327
+ Memos content
328
+ column_create Terms memos_content_20171024 \
329
+ COLUMN_INDEX|WITH_POSITION \
330
+ Memos content
331
+
332
+ load --table CustomAliases
333
+ [
334
+ ["_key","name"],
335
+ ["Terms.memos_content","Terms.memos_content_20171025"]
336
+ ]
337
+ COMMANDS
338
+
339
+ assert_equal([
340
+ false,
341
+ "",
342
+ "Alias doesn't specify real index column: " +
343
+ "<Terms.memos_content_20171025>\n",
344
+ ],
345
+ index_recreate("Terms.memos_content"))
346
+
347
+ assert_equal(<<-DUMP.chomp, dump)
348
+ config_set alias.column CustomAliases.name
349
+
350
+ table_create CustomAliases TABLE_HASH_KEY ShortText
351
+ column_create CustomAliases name COLUMN_SCALAR ShortText
352
+
353
+ table_create Memos TABLE_HASH_KEY ShortText
354
+ column_create Memos content COLUMN_SCALAR Text
355
+
356
+ table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
357
+
358
+ load --table CustomAliases
359
+ [
360
+ ["_key","name"],
361
+ ["Terms.memos_content","Terms.memos_content_20171025"]
362
+ ]
363
+
364
+ column_create Terms memos_content_20171022 COLUMN_INDEX|WITH_POSITION Memos content
365
+ column_create Terms memos_content_20171023 COLUMN_INDEX|WITH_POSITION Memos content
366
+ column_create Terms memos_content_20171024 COLUMN_INDEX|WITH_POSITION Memos content
367
+ DUMP
368
+ end
369
+ end