Class: Mkgrendb::Mkgrendb
- Inherits:
-
Object
- Object
- Mkgrendb::Mkgrendb
- Defined in:
- lib/mkgrendb/mkgrendb.rb
Instance Method Summary collapse
- #db_add_file(stdout, filename, shortpath) ⇒ Object
- #delete ⇒ Object
- #dump ⇒ Object
- #full ⇒ Object
-
#initialize(input) ⇒ Mkgrendb
constructor
A new instance of Mkgrendb.
- #print_result ⇒ Object
- #searchDirectory(stdout, dir, shortdir, depth) ⇒ Object
- #time ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(input) ⇒ Mkgrendb
Returns a new instance of Mkgrendb.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mkgrendb/mkgrendb.rb', line 14 def initialize(input) input = File.(input) @input_yaml = input.sub(/\.db$/, ".yaml") @output_db = input.sub(/\.yaml$/, ".db") puts "input_yaml : #{@input_yaml} found." @src = YAML.load(open(@input_yaml).read()) @file_count = 0 @add_count = 0 @update_count = 0 @start_time = Time.now end |
Instance Method Details
#db_add_file(stdout, filename, shortpath) ⇒ Object
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mkgrendb/mkgrendb.rb', line 141 def db_add_file(stdout, filename, shortpath) # 格納するデータ values = { :path => filename, :shortpath => shortpath, :content => nil, :timestamp => File.mtime(filename), :suffix => File::extname(filename), } # 検索するデータベース documents = Groonga::Context.default["documents"] # 既に登録されているファイルならばそれを上書き、そうでなければ新規レコードを作成 _documents = documents.select do |record| record["path"] == values[:path] end isNewFile = false if _documents.size.zero? document = documents.add isNewFile = true else document = _documents.to_a[0].key end # タイムスタンプが新しければデータベースに格納 if (document[:timestamp] < values[:timestamp]) # 実際に使うタイミングでファイルの内容を読み込み values[:content] = open(filename).read # データベースに格納 values.each do |key, value| if (key == :path) if (isNewFile) @add_count += 1 puts "add_file : #{value}" else @update_count += 1 puts "update : #{value}" end end document[key] = value end end end |
#delete ⇒ Object
44 45 46 |
# File 'lib/mkgrendb/mkgrendb.rb', line 44 def delete db_delete(@output_db) end |
#dump ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mkgrendb/mkgrendb.rb', line 66 def dump() db_open(@output_db) documents = Groonga::Context.default["documents"] records = documents.select records.each do |record| p record puts "path : #{record.path}" puts "shortpath : #{record.shortpath}" puts "suffix : #{record.suffix}" puts "timestamp : #{record..strftime('%Y/%m/%d %H:%M:%S')}" puts "content :", record.content ? record.content[0..64] : nil puts end end |
#full ⇒ Object
48 49 50 51 |
# File 'lib/mkgrendb/mkgrendb.rb', line 48 def full delete update end |
#print_result ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/mkgrendb/mkgrendb.rb', line 57 def print_result puts puts "input_yaml : #{@input_yaml} (#{Util::time_s(time)})" puts "output_db : #{@output_db}*" puts "files : #{@file_count}" puts "add : #{@add_count}" puts "update : #{@update_count}" end |
#searchDirectory(stdout, dir, shortdir, depth) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/mkgrendb/mkgrendb.rb', line 190 def searchDirectory(stdout, dir, shortdir, depth) Dir.foreach(dir) do |name| next if (name == '.' || name == '..') fpath = File.join(dir,name) shortpath = File.join(shortdir,name) # 除外ディレクトリならばパス next if ignoreDir?(fpath) # 読み込み不可ならばパス next unless FileTest.readable?(fpath) # ファイルならば中身を探索、ディレクトリならば再帰 case File.ftype(fpath) when "directory" searchDirectory(stdout, fpath, shortpath, depth + 1) when "file" unless ignoreFile?(fpath) db_add_file(stdout, fpath, shortpath) @file_count += 1 puts "file_count : #{@file_count}" if (@file_count % 100 == 0) end end end end |
#time ⇒ Object
53 54 55 |
# File 'lib/mkgrendb/mkgrendb.rb', line 53 def time @end_time - @start_time end |
#update ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mkgrendb/mkgrendb.rb', line 26 def update db_create(@output_db) db_open(@output_db) @src["directory"].each do |dir| dir = File.(dir) if (!FileTest.exist?(dir)) puts "[WARNING] : #{dir} (Not found, skip)" elsif (FileTest.directory? dir) db_add_dir(dir) else db_add_file(STDOUT, dir, File.basename(dir)) end end @end_time = Time.now print_result end |