[Groonga-commit] droonga/droonga-engine at 15c6f69 [master] Split delete methods

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Apr 24 17:38:06 JST 2014


Kouhei Sutou	2014-04-24 17:38:06 +0900 (Thu, 24 Apr 2014)

  New Revision: 15c6f69fc135a5cbd214d75890fd9779bab25d76
  https://github.com/droonga/droonga-engine/commit/15c6f69fc135a5cbd214d75890fd9779bab25d76

  Message:
    Split delete methods

  Modified files:
    lib/droonga/plugins/groonga/delete.rb

  Modified: lib/droonga/plugins/groonga/delete.rb (+42 -34)
===================================================================
--- lib/droonga/plugins/groonga/delete.rb    2014-04-24 17:10:00 +0900 (6d34fa2)
+++ lib/droonga/plugins/groonga/delete.rb    2014-04-24 17:38:06 +0900 (d6118ec)
@@ -27,17 +27,26 @@ module Droonga
             command_class = ::Groonga::Command.find("delete")
             @command = command_class.new("delete", request)
 
-            validate_parameters
+            table_name = @command["table"]
+            key = @command["key"]
+            id = @command["id"]
+            filter = @command["filter"]
 
-            delete_record(@command["table"],
-                          :key => @command["key"],
-                          :id => @command["id"],
-                          :filter => @command["filter"])
+            validate_parameters(table_name, key, id, filter)
+
+            if key
+              delete_record_by_key(table_name, key)
+            elsif id
+              delete_record_by_id(table_name, id)
+            else
+              delete_record_by_filter(table_name, filter)
+            end
+
+            true
           end
 
           private
-          def validate_parameters
-            table_name = @command["table"]
+          def validate_parameters(table_name, key, id, filter)
             if table_name.nil? or @context[table_name].nil?
               message = "table doesn't exist: <#{table_name.to_s}>"
               raise CommandError.new(:status => Status::INVALID_ARGUMENT,
@@ -45,10 +54,6 @@ module Droonga
                                      :result => false)
             end
 
-            key = @command["key"]
-            id = @command["id"]
-            filter = @command["filter"]
-
             if key.nil? and id.nil? and filter.nil?
               message = "you must specify \"key\", \"id\", or \"filter\""
               raise CommandError.new(:status => Status::INVALID_ARGUMENT,
@@ -68,31 +73,34 @@ module Droonga
             end
           end
 
-          def delete_record(table_name, parameters={})
+          def delete_record_by_key(table_name, key)
             table = @context[table_name]
-            if parameters[:id]
-              record = table[parameters[:id].to_i]
-              record.delete if record and record.valid_id?
-            elsif parameters[:key]
-              record = table[parameters[:key]]
-              record.delete unless record.nil?
-            else
-              filter = ::Groonga::Expression.new(:context => @context)
-              begin
-                filter.define_variable(:domain => table)
-                filter.parse(parameters[:filter], :syntax => :script)
-                records = table.select(filter)
-                records.each do |record|
-                  record.key.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
+            record = table[key]
+            record.delete unless record.nil?
+          end
+
+          def delete_record_by_id(table_name, id)
+            table = @context[table_name]
+            record = table[id.to_i]
+            record.delete if record and record.valid_id?
+          end
+
+          def delete_record_by_filter(table_name, filter)
+            table = @context[table_name]
+            condition = ::Groonga::Expression.new(:context => @context)
+            condition.define_variable(:domain => table)
+            begin
+              condition.parse(filter, :syntax => :script)
+            rescue ::Groonga::SyntaxError
+              message = "syntax error in filter: <#{filter.to_s}>"
+              raise CommandError.new(:status => Status::SYNTAX_ERROR,
+                                     :message => message,
+                                     :result => false)
+            end
+            records = table.select(condition)
+            records.each do |record|
+              record.key.delete
             end
-            true
           end
         end
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index