[Groonga-commit] ranguba/rroonga at bf37160 [master] grndump: support forward index

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Feb 24 18:24:57 JST 2014


Kouhei Sutou	2014-02-24 18:24:57 +0900 (Mon, 24 Feb 2014)

  New Revision: bf371601031d5dc1035fab429133715fe16ee556
  https://github.com/ranguba/rroonga/commit/bf371601031d5dc1035fab429133715fe16ee556

  Message:
    grndump: support forward index

  Modified files:
    lib/groonga/dumper.rb
    test/test-table-dumper.rb

  Modified: lib/groonga/dumper.rb (+23 -6)
===================================================================
--- lib/groonga/dumper.rb    2014-02-24 18:08:18 +0900 (4b573d0)
+++ lib/groonga/dumper.rb    2014-02-24 18:24:57 +0900 (15ab7e8)
@@ -97,7 +97,7 @@ module Groonga
       options[:database].each(each_options(:order_by => :key)) do |object|
         next unless object.is_a?(Groonga::Table)
         next if object.size.zero?
-        next if index_only_table?(object)
+        next if inverted_index_only_table?(object)
         next if target_table?(options[:exclude_tables], object, false)
         next unless target_table?(options[:tables], object, true)
         options[:output].write("\n") if !first_table or options[:dump_schema]
@@ -120,10 +120,10 @@ module Groonga
       output.write("register #{plugin_name}\n")
     end
 
-    def index_only_table?(table)
+    def inverted_index_only_table?(table)
       return false if table.columns.empty?
       table.columns.all? do |column|
-        column.index?
+        column.index? and column.inverted?
       end
     end
 
@@ -700,8 +700,12 @@ module Groonga
     def resolve_value(record, column, value)
       case value
       when ::Array
-        value.collect do |v|
-          resolve_value(record, column, v)
+        if column.index?
+          resolve_forward_index_value(record, column, value)
+        else
+          value.collect do |v|
+            resolve_value(record, column, v)
+          end
         end
       when Groonga::Record
         if value.support_key?
@@ -739,6 +743,19 @@ module Groonga
       end
     end
 
+    def resolve_forward_index_value(record, column, entries)
+      resolved_forward_index_entries = {}
+      sorted_entries = entries.sort_by do |entry|
+        entry[:value]
+      end
+      sorted_entries.each do |entry|
+        resolved_value = resolve_value(record, column, entry[:value])
+        resolved_weight = resolve_value(record, column, entry[:weight])
+        resolved_forward_index_entries[resolved_value] = resolved_weight
+      end
+      resolved_forward_index_entries
+    end
+
     def fix_encoding(value)
       if value.encoding == ::Encoding::ASCII_8BIT
         value.force_encoding(@table.context.ruby_encoding)
@@ -762,7 +779,7 @@ module Groonga
       end
       columns << @table.column("_value") unles****@table*****?
       data_columns =****@table***** do |column|
-        column.index?
+        column.index? and column.inverted?
       end
       sorted_columns = data_columns.sort_by do |column|
         column.local_name

  Modified: test/test-table-dumper.rb (+51 -1)
===================================================================
--- test/test-table-dumper.rb    2014-02-24 18:08:18 +0900 (05fcec0)
+++ test/test-table-dumper.rb    2014-02-24 18:24:57 +0900 (b8c766d)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2011-2012  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2011-2014  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -350,4 +350,54 @@ load --table Users
 EOS
     end
   end
+
+  class ForwardIndexTest < self
+    def setup
+      Groonga::Schema.define do |schema|
+        schema.create_table("Tags",
+                            :type => :hash,
+                            :key_type => "ShortText") do |table|
+        end
+
+        schema.create_table("Products",
+                            :type => :patricia_trie,
+                            :key_type => "ShortText") do |table|
+          table.index("Tags",
+                      :name => "tags",
+                      :with_weight => true)
+        end
+      end
+    end
+
+    def products
+      Groonga["Products"]
+    end
+
+    def test_weight
+      products.add("Groonga", :tags => [
+                     {
+                       :value  => "groonga",
+                       :weight => 100,
+                     },
+                   ])
+      products.add("Mroonga", :tags => [
+                     {
+                       :value  => "mroonga",
+                       :weight => 100,
+                     },
+                     {
+                       :value  => "groonga",
+                       :weight => 10,
+                     },
+                   ])
+      assert_equal(<<-COMMAND, dump("Products"))
+load --table Products
+[
+[\"_key\",\"tags\"],
+[\"Groonga\",{\"groonga\":100}],
+[\"Mroonga\",{\"groonga\":10,\"mroonga\":100}]
+]
+      COMMAND
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



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