Class: Flare::Tools::Cli::Restore

Inherits:
SubCommand show all
Includes:
Flare::Tools::Common, Util::Constant, Util::Conversion
Defined in:
lib/flare/tools/cli/restore.rb

Defined Under Namespace

Classes: Restorer, TchRestorer

Constant Summary collapse

Restorers =
[]
Formats =
Restorers.map {|n| n.myname}

Constants included from Flare::Tools::Common

Flare::Tools::Common::NodeListFormat, Flare::Tools::Common::NodeListHeader

Constants included from Util::Constant

Util::Constant::DefalutBwlimit, Util::Constant::DefaultIndexServerName, Util::Constant::DefaultIndexServerPort, Util::Constant::DefaultNodePort, Util::Constant::DefaultTimeout, Util::Constant::STATUS_NG, Util::Constant::STATUS_OK

Constants inherited from SubCommand

SubCommand::S_NG, SubCommand::S_OK

Constants included from Util::Interruption

Util::Interruption::InterruptionTargets

Instance Attribute Summary

Attributes included from Option

#optp

Instance Method Summary collapse

Methods included from Flare::Tools::Common

#address_of_hostname, #fetch_cluster, #hostname_of_address, #nodekey_of, #string_of_nodelist, #user_confirmed, #wait_for_master_construction, #wait_for_servers, #wait_for_slave_construction

Methods included from Util::Logging

#debug, #error, #fatal, #info, logger, #puts, set_logger, #trace, #warn

Methods included from Util::Conversion

#short_desc_of_second

Methods inherited from SubCommand

desc, #execute_subcommand, myname, #myname, to_s, to_sym, usage

Methods included from Util::Interruption

included, #initialize_interruption, #interrupt, #interrupt_, interrupt_all, #interrupted?, #interruptible, #interruptible?

Methods included from Option

#option_init, #parse_options, #set_option_dry_run, #set_option_force, #set_option_global, #set_option_index_server

Constructor Details

#initializeRestore

Returns a new instance of Restore.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/flare/tools/cli/restore.rb', line 106

def initialize
  super
  @input = nil
  @format = nil
  @wait = 0
  @part = 0
  @partsize = 1
  @bwlimit = 0
  @include = nil
  @prefix_include = nil
  @exclude = nil
  @print_key = false
end

Instance Method Details

#execute(config, args) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/flare/tools/cli/restore.rb', line 120

def execute(config, args)
  STDERR.puts "please install tokyocabinet via gem command." unless defined? TokyoCabinet

  unless @format.nil? || Formats.include?(@format)
    STDERR.puts "unknown format: #{@format}"
    return S_NG
  end

  if @prefix_include
    if @include
      STDERR.puts "--include option is specified."
      return S_NG
    end
    @include = @prefix_include
  end

  hosts = args.map {|x| x.split(':')}
  hosts.each do |x|
    if x.size != 2
      STDERR.puts "invalid argument '#{x.join(':')}'."
      return S_NG
    end
  end

  restorer = case @format
             when TchRestorer.myname
               TchRestorer.new(@input)
             else
               raise "invalid format"
             end

  nodes = hosts.map do |hostname,port|
    Flare::Tools::Node.open(hostname, port.to_i, @timeout, @bwlimit, @bwlimit)
  end

  count = 0
  restorer.iterate do |key,data,flag,expire|
    if @include.nil? || @include =~ key
      next if @exclude && @exclude =~ key
      STDOUT.puts key if @print_key
      nodes[0].set(key, data, flag, expire) unless @dry_run
      count += 1
    end
  end
  STDERR.puts "#{count} entries have been restored."

  nodes.each do |n|
    n.close
  end

  restorer.close

  S_OK
end

#setupObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/flare/tools/cli/restore.rb', line 78

def setup
  super
  set_option_dry_run
  @optp.on('-i', '--input=FILE',             "input from file") {|v| @input = v}
  @optp.on('-f', '--format=FORMAT',          "input format [#{Formats.join(',')}]") {|v|
    @format = v
  }
  @optp.on('--bwlimit=BANDWIDTH',            "bandwidth limit (bps)") {|v| @bwlimit = v}
  @optp.on('--include=PATTERN',              "include pattern") {|v|
    begin
      @include = Regexp.new(v)
    rescue RegexpError => e
      raise "#{v} isn't a valid regular expression."
    end
  }
  @optp.on('--prefix-include=STRING',        "prefix string") {|v|
    @prefix_include = Regexp.new("^"+Regexp.escape(v))
  }
  @optp.on('--exclude=PATTERN',              "exclude pattern") {|v|
    begin
      @exclude = Regexp.new(v)
    rescue RegexpError => e
      raise "#{v} isn't a valid regular expression."
    end
  }
  @optp.on('--print-keys',                     "enables key dump to console") {@print_key = true}
end