[Groonga-commit] droonga/fluent-plugin-droonga at 540d650 [master] Validate datasets, datasets.*.number_of_partitions, and datasets.*.ring

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Feb 12 14:04:25 JST 2014


YUKI Hiroshi	2014-02-12 14:04:25 +0900 (Wed, 12 Feb 2014)

  New Revision: 540d65091392f8d4b91f836f9e37fe5627168764
  https://github.com/droonga/fluent-plugin-droonga/commit/540d65091392f8d4b91f836f9e37fe5627168764

  Message:
    Validate datasets, datasets.*.number_of_partitions, and datasets.*.ring

  Modified files:
    lib/droonga/catalog.rb
    lib/droonga/catalog/base.rb
    lib/droonga/catalog_loader.rb

  Modified: lib/droonga/catalog.rb (+0 -4)
===================================================================
--- lib/droonga/catalog.rb    2014-02-12 13:40:20 +0900 (826f058)
+++ lib/droonga/catalog.rb    2014-02-12 14:04:25 +0900 (7fef29b)
@@ -16,13 +16,9 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 require "droonga/catalog_loader"
-require "droonga/error"
 
 module Droonga
   class << self
     attr_accessor :catalog
   end
-
-  class InvalidCatalog < Error
-  end
 end

  Modified: lib/droonga/catalog/base.rb (+81 -0)
===================================================================
--- lib/droonga/catalog/base.rb    2014-02-12 13:40:20 +0900 (e30ae39)
+++ lib/droonga/catalog/base.rb    2014-02-12 14:04:25 +0900 (a303e81)
@@ -19,6 +19,40 @@ require "droonga/message_processing_error"
 
 module Droonga
   module Catalog
+    class ValidationError < Error
+      def initialize(message, path)
+        if path
+          super("Validation error in #{path}: #{message}")
+        else
+          super(message)
+        end
+      end
+    end
+
+    class MissingRequiredParameter < Error
+      def initialize(name, path)
+        super("You must specify \"#{name}\".", path)
+      end
+    end
+
+    class MismatchedParameterType < Error
+      def initialize(name, expected, actual, path)
+        super("\"#{name}\" must be a #{expected}, but a #{actual}.", path)
+      end
+    end
+
+    class NegativeNumber < Error
+      def initialize(name, actual, path)
+        super("\"#{name}\" must be a positive number, but #{actual}.", path)
+      end
+    end
+
+    class SmallerThanOne < Error
+      def initialize(name, actual, path)
+        super("\"#{name}\" must be 1 or larger number, but #{actual}.", path)
+      end
+    end
+
     class Base
       attr_reader :path, :base_path
       def initialize(data, path)
@@ -26,12 +60,16 @@ module Droonga
         @path = path
         @base_path = File.dirname(base_path)
 
+        validate_datasets
+
         @data["datasets"].each do |name, dataset|
+          validate_dataset(dataset, "datasets.#{name}")
           number_of_partitions = dataset["number_of_partitions"]
           next if number_of_partitions < 2
           total_weight = compute_total_weight(dataset)
           continuum = []
           dataset["ring"].each do |key, value|
+            validate_ring(value, "datasets.ring.#{key}")
             points = number_of_partitions * 160 * value["weight"] / total_weight
             points.times do |point|
               hash = Digest::SHA1.hexdigest("#{key}:#{point}")
@@ -141,6 +179,49 @@ module Droonga
           result + zone[1]["weight"]
         end
       end
+
+      def validate_parameter_type(value, name, expected)
+        unless datasets.is_a?(expected)
+          raise MismatchedParameterType.new(name,
+                                            expected,
+                                            value.class,
+                                            @path)
+        end
+      end
+
+      def validate_datasets
+        datasets = @data["datasets"]
+
+        raise MissingRequiredParameter.new("datasets", @path) unless datasets
+
+        validate_parameter_type(datasets, "datasets", Hash)
+      end
+
+      def validate_dataset(dataset, name)
+        validate_parameter_type(dataset, name, Hash)
+
+        n_partitions = dataset["number_of_partitions"]
+        validate_parameter_type(n_partitions,
+                                "#{name}.number_of_partitions",
+                                Integer)
+        if n_partitions < 1
+          raise SmallerThanOne.new("#{name}.number_of_partitions", n_partitions, @path)
+        end
+
+        validate_parameter_type(dataset["ring"],
+                                "#{name}.ring",
+                                Hash)
+      end
+
+      def validate_ring(ring, name)
+        validate_parameter_type(ring, name, Hash)
+
+        weight = ring["weight"]
+        validate_parameter_type(weight, "#{name}.weight", Numeric)
+        if weight < 0
+          raise NegativeNumber.new("#{name}.weight", weight, @path)
+        end
+      end
     end
   end
 end

  Modified: lib/droonga/catalog_loader.rb (+1 -1)
===================================================================
--- lib/droonga/catalog_loader.rb    2014-02-12 13:40:20 +0900 (3ee85ac)
+++ lib/droonga/catalog_loader.rb    2014-02-12 14:04:25 +0900 (de5c417)
@@ -29,7 +29,7 @@ module Droonga
       end
       Catalog::Version1.new(data, @path)
     rescue JSON::ParserError => error
-      raise InvalidCatalog.new("Syntax error in #{@path}\n#{error.to_s}")
+      raise Error.new("Syntax error in #{@path}:\n#{error.to_s}")
     end
   end
 end
-------------- next part --------------
HTML����������������������������...
Descargar 



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