[Groonga-commit] ranguba/rroonga at 4136693 [master] Add Object#disk_usage

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Tue Jan 30 16:02:56 JST 2018


Kouhei Sutou	2018-01-30 16:02:56 +0900 (Tue, 30 Jan 2018)

  New Revision: 4136693cb2fca444c1fd2c70ca0b29b6df49c200
  https://github.com/ranguba/rroonga/commit/4136693cb2fca444c1fd2c70ca0b29b6df49c200

  Message:
    Add Object#disk_usage

  Removed files:
    lib/groonga/statistic-measurer.rb
    lib/groonga/table.rb
    test/test-statistic-measurer.rb
  Modified files:
    ext/groonga/rb-grn-object.c
    lib/groonga.rb
    lib/groonga/column.rb
    lib/groonga/database.rb
    lib/groonga/index-column.rb
    test/test-database.rb

  Modified: ext/groonga/rb-grn-object.c (+23 -1)
===================================================================
--- ext/groonga/rb-grn-object.c    2018-01-30 15:54:26 +0900 (b8621905)
+++ ext/groonga/rb-grn-object.c    2018-01-30 16:02:56 +0900 (b3b6854b)
@@ -1,6 +1,6 @@
 /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /*
-  Copyright (C) 2009-2016  Kouhei Sutou <kou �� clear-code.com>
+  Copyright (C) 2009-2018  Kouhei Sutou <kou �� clear-code.com>
   Copyright (C) 2014-2016  Masafumi Yokoyama <yokoyama �� clear-code.com>
 
   This library is free software; you can redistribute it and/or
@@ -1989,6 +1989,25 @@ rb_grn_object_corrupt_p (VALUE self)
     return CBOOL2RVAL(is_corrupt);
 }
 
+/*
+ * @overload disk_usage
+ *   @return [Integer] The number of bytes used by this object in disk.
+ *
+ * @since 7.1.1
+ */
+static VALUE
+rb_grn_object_get_disk_usage (VALUE self)
+{
+    grn_ctx *context;
+    grn_obj *object;
+    size_t disk_usage;
+
+    rb_grn_object_deconstruct(SELF(self), &object, &context,
+                              NULL, NULL, NULL, NULL);
+    disk_usage = grn_obj_get_disk_usage(context, object);
+    return UINT2NUM(disk_usage);
+}
+
 void
 rb_grn_init_object (VALUE mGrn)
 {
@@ -2053,4 +2072,7 @@ rb_grn_init_object (VALUE mGrn)
                      rb_grn_object_dirty_p, 0);
     rb_define_method(rb_cGrnObject, "corrupt?",
                      rb_grn_object_corrupt_p, 0);
+
+    rb_define_method(rb_cGrnObject, "disk_usage",
+                     rb_grn_object_get_disk_usage, 0);
 }

  Modified: lib/groonga.rb (+0 -2)
===================================================================
--- lib/groonga.rb    2018-01-30 15:54:26 +0900 (6a5360ab)
+++ lib/groonga.rb    2018-01-30 16:02:56 +0900 (cd2cee6a)
@@ -96,9 +96,7 @@ module Groonga
 end
 
 require "groonga/context"
-require "groonga/statistic-measurer"
 require "groonga/database"
-require "groonga/table"
 require "groonga/column"
 require "groonga/patricia-trie"
 require "groonga/index-column"

  Modified: lib/groonga/column.rb (+0 -5)
===================================================================
--- lib/groonga/column.rb    2018-01-30 15:54:26 +0900 (58ef3885)
+++ lib/groonga/column.rb    2018-01-30 16:02:56 +0900 (6a9981c6)
@@ -17,11 +17,6 @@
 
 module Groonga
   class Column
-    def disk_usage
-      measurer = StatisticMeasurer.new
-      measurer.measure_disk_usage(path)
-    end
-
     # @param [Groonga::Operator] operator (Groonga::Operator::MATCH)
     # @return [Array<Groonga::IndexColumn>] Indexes on `column` which can
     #    execute `operator`.

  Modified: lib/groonga/database.rb (+0 -10)
===================================================================
--- lib/groonga/database.rb    2018-01-30 15:54:26 +0900 (7e0fc9fd)
+++ lib/groonga/database.rb    2018-01-30 16:02:56 +0900 (eec2e21b)
@@ -44,16 +44,6 @@ module Groonga
       paths
     end
 
-    def disk_usage
-      return 0 if path.nil?
-
-      usage = 0
-      measurer = StatisticMeasurer.new
-      usage += measurer.measure_disk_usage(path)
-      usage += measurer.measure_disk_usage("%s.%07X" % [path, 0])
-      usage
-    end
-
     def dump_index(output_directory)
       each do |object|
         next unless object.is_a?(Groonga::IndexColumn)

  Modified: lib/groonga/index-column.rb (+0 -10)
===================================================================
--- lib/groonga/index-column.rb    2018-01-30 15:54:26 +0900 (a8f80875)
+++ lib/groonga/index-column.rb    2018-01-30 16:02:56 +0900 (86adb4f1)
@@ -24,16 +24,6 @@ module Groonga
       dumper = IndexColumnDumper.new(self, output_directory)
       dumper.dump
     end
-
-    def disk_usage
-      return 0 if path.nil?
-
-      usage = super
-      chunk_path = "#{path}.c"
-      measurer = StatisticMeasurer.new
-      usage += measurer.measure_disk_usage(chunk_path)
-      usage
-    end
   end
 
   class IndexColumnDumper

  Deleted: lib/groonga/statistic-measurer.rb (+0 -37) 100644
===================================================================
--- lib/groonga/statistic-measurer.rb    2018-01-30 15:54:26 +0900 (be42159f)
+++ /dev/null
@@ -1,37 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright (C) 2013  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
-# License version 2.1 as published by the Free Software Foundation.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-
-module Groonga
-  # Measures statistic.
-  class StatisticMeasurer
-    MAX_N_ADDITIONAL_PATHS = 4096
-
-    # @param path [String, nil] Measures disk usage of the path.
-    # @return [Integer] 0 if path is @nil@, disk usage of the path otherwise.
-    def measure_disk_usage(path)
-      return 0 if path.nil?
-
-      usage = File.size(path)
-      1.step(MAX_N_ADDITIONAL_PATHS) do |i|
-        additional_path = "%s.%03X" % [path, i]
-        break unless File.exist?(additional_path)
-        usage += File.size(additional_path)
-      end
-      usage
-    end
-  end
-end

  Deleted: lib/groonga/table.rb (+0 -25) 100644
===================================================================
--- lib/groonga/table.rb    2018-01-30 15:54:26 +0900 (8a43ed4b)
+++ /dev/null
@@ -1,25 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright (C) 2013  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
-# License version 2.1 as published by the Free Software Foundation.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-
-module Groonga
-  class Table
-    def disk_usage
-      measurer = StatisticMeasurer.new
-      measurer.measure_disk_usage(path)
-    end
-  end
-end

  Modified: test/test-database.rb (+9 -0)
===================================================================
--- test/test-database.rb    2018-01-30 15:54:26 +0900 (45e71ee1)
+++ test/test-database.rb    2018-01-30 16:02:56 +0900 (a2cfc825)
@@ -376,6 +376,7 @@ class DatabaseTest < Test::Unit::TestCase
     def test_empty
       paths = [
         @database.path,
+        "#{@database.path}.conf",
         "#{@database.path}.001",
         "#{@database.path}.0000000",
       ]
@@ -395,6 +396,10 @@ class DatabaseTest < Test::Unit::TestCase
       @database.touch
       assert_equal(Time.now.sec, @database.last_modified.sec)
     end
+  end
+
+  class DirtyTest < self
+    setup :setup_database
 
     def test_dirty?
       assert do
@@ -405,6 +410,10 @@ class DatabaseTest < Test::Unit::TestCase
         @database.dirty?
       end
     end
+  end
+
+  class CorruptTest < self
+    setup :setup_database
 
     def test_corrupt?
       assert do

  Deleted: test/test-statistic-measurer.rb (+0 -55) 100644
===================================================================
--- test/test-statistic-measurer.rb    2018-01-30 15:54:26 +0900 (e2ff1158)
+++ /dev/null
@@ -1,55 +0,0 @@
-# Copyright (C) 2013  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
-# License version 2.1 as published by the Free Software Foundation.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-
-class StatisticMeasurerTest < Test::Unit::TestCase
-  include GroongaTestUtils
-
-  def setup
-    @measurer = Groonga::StatisticMeasurer.new
-  end
-
-  class DiskUsageTest < self
-    def test_nil
-      assert_equal(0, disk_usage(nil))
-    end
-
-    def test_path_only
-      size = 5
-      path = File.join(@tmp_dir, "db")
-      write(path, "X" * size)
-      assert_equal(size, disk_usage(path))
-    end
-
-    def test_additional_path
-      size_per_file = 5
-      path = File.join(@tmp_dir, "db")
-      write(path, "X" * size_per_file)
-      write("#{path}.001", "X" * size_per_file)
-      write("#{path}.002", "X" * size_per_file)
-      assert_equal(3 * size_per_file, disk_usage(path))
-    end
-
-    private
-    def disk_usage(path)
-      @measurer.measure_disk_usage(path)
-    end
-
-    def write(path, content)
-      File.open(path, "w") do |file|
-        file.write(content)
-      end
-    end
-  end
-end
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180130/7a6a6bb5/attachment-0001.htm 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る