Navigation Menu

Skip to content

Commit

Permalink
Extract codes to scan table related objects in Groonga database
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 21, 2015
1 parent 574b8fd commit 6573fca
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 58 deletions.
80 changes: 80 additions & 0 deletions lib/droonga/database_scanner.rb
@@ -0,0 +1,80 @@
# Copyright (C) 2015 Droonga Project
#
# 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

require "groonga"

module Droonga
module DatabaseScanner
def n_all_objects
n_tables = 0
n_columns = 0
n_records = 0
each_table do |table|
n_tables += 1
n_columns += table.columns.size
unless index_only_table?(table)
n_records += table.size
end
end
n_tables + n_columns + n_records
end

def each_table(&block)
options = {
:ignore_missing_object => true,
:order_by => :key,
}
reference_tables = []
@context.database.each(options) do |object|
next unless table?(object)
if reference_table?(object)
reference_tables << object
next
end
yield(object)
end
reference_tables.each do |reference_table|
yield(object)
end
end

def table?(object)
object.is_a?(::Groonga::Table)
end

def reference_table?(table)
table.support_key? and table?(table.domain)
end

def index_only_table?(table)
return false if table.columns.empty?
table.columns.all? do |column|
index_column?(column)
end
end

def index_column?(column)
column.is_a?(::Groonga::IndexColumn)
end

def each_index_columns(&block)
each_table do |table|
table.columns.each do |column|
yield(column) if index_column?(column)
end
end
end
end
end
62 changes: 4 additions & 58 deletions lib/droonga/plugins/dump.rb
Expand Up @@ -18,6 +18,7 @@
require "droonga/plugin"
require "droonga/plugin/async_command"
require "droonga/error_messages"
require "droonga/database_scanner"

module Droonga
module Plugins
Expand Down Expand Up @@ -62,6 +63,8 @@ def start(request)
end

class Dumper < AsyncCommand::AsyncHandler
include DatabaseScanner

def initialize(context, loop, messenger, request)
@context = context
super(loop, messenger, request)
Expand Down Expand Up @@ -116,31 +119,12 @@ def error_message
end

def forecast
n_tables = 0
n_columns = 0
n_records = 0
each_table do |table|
n_tables += 1
n_columns += table.columns.size
unless index_only_table?(table)
n_records += table.size
end
end
n_dump_messages = n_tables + n_columns + n_records
forward("#{prefix}.forecast", "nMessages" => n_dump_messages)
forward("#{prefix}.forecast", "nMessages" => n_all_objects)
end

def dump_schema
reference_tables = []
each_table do |table|
if reference_table?(table)
reference_tables << table
next
end
dump_table(table)
end

reference_tables.each do |table|
dump_table(table)
end
end
Expand Down Expand Up @@ -268,44 +252,6 @@ def dump_indexes
end
end

def each_table
options = {
:ignore_missing_object => true,
:order_by => :key,
}
@context.database.each(options) do |object|
next unless table?(object)
yield(object)
end
end

def table?(object)
object.is_a?(::Groonga::Table)
end

def index_only_table?(table)
return false if table.columns.empty?
table.columns.all? do |column|
index_column?(column)
end
end

def reference_table?(table)
table.support_key? and table?(table.domain)
end

def index_column?(column)
column.is_a?(::Groonga::IndexColumn)
end

def each_index_columns
each_table do |table|
table.columns.each do |column|
yield(column) if index_column?(column)
end
end
end

def setup_forward_data
super
@n_forwarded_messages = 0
Expand Down

0 comments on commit 6573fca

Please sign in to comment.