Navigation Menu

Skip to content

Commit

Permalink
groonga: Accept "filter" for Groonga's delete
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 24, 2014
1 parent a9f5890 commit bae926b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
24 changes: 15 additions & 9 deletions lib/droonga/plugins/groonga/delete.rb
Expand Up @@ -66,24 +66,30 @@ def validate_parameters
:message => message,
:result => false)
end

#XXX this must be removed after it is implemented
if filter
message = "\"filter\" is not supported yet"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end
end

def delete_record(table_name, parameters={})
table = @context[table_name]
if parameters[:id]
record = table[parameters[:id].to_i]
record.delete if record and record.valid_id?
else
else if parameters[:key]
record = table[parameters[:key]]
record.delete unless record.nil?
else
filter = Groonga::Expression.new(:context => @context)
begin
filter.parse(parameters[:filter].dump, :syntax => :script)
records = table.select(filter)
records.each do |record|
record.delete
end
rescue ::Groonga::SyntaxError
message = "syntax error in filter: <#{parameters[:filter].to_s}>"
raise CommandError.new(:status => Status::SYNTAX_ERROR,
:message => message,
:result => false)
end
end
true
end
Expand Down
1 change: 1 addition & 0 deletions lib/droonga/plugins/groonga/generic_command.rb
Expand Up @@ -21,6 +21,7 @@ module Groonga
module Status
SUCCESS = 0
INVALID_ARGUMENT = -22
SYNTAX_ERROR = -63
end

class GenericCommand
Expand Down
34 changes: 19 additions & 15 deletions test/unit/plugins/groonga/test_delete.rb
Expand Up @@ -79,21 +79,6 @@ def test_duplicated_identifier(data)
)
end

def test_not_implemented_identifier
Groonga::Schema.define(:context => @context) do |schema|
schema.create_table("Books", :type => :hash)
end
message = {
"table" => "Books",
"filter" => "filter",
}
response = process(:delete, message)
assert_equal(
[NORMALIZED_HEADER_INVALID_ARGUMENT, false],
[normalize_header(response.first), response.last]
)
end

class DeleteTest < self
def test_key
Groonga::Schema.define(:context => @context) do |schema|
Expand All @@ -118,5 +103,24 @@ def test_id
table_create Ages TABLE_NO_KEY
DUMP
end

def test_filter
Groonga::Schema.define(:context => @context) do |schema|
schema.create_table("Books", :type => :hash)
end
table = Groonga::Context.default["Books"]
table.add("Groonga")
table.add("Droonga")
process(:delete,
{"table" => "Books", "filter" => '_key @^ "D"'})
assert_equal(<<-DUMP, dump)
table_create Books TABLE_HASH_KEY --key_type ShortText
load --table Books
[
["_key"],
["Groonga"]
]
DUMP
end
end
end

0 comments on commit bae926b

Please sign in to comment.