null+****@clear*****
null+****@clear*****
Wed Jan 25 14:23:38 JST 2012
SUZUKI Miho 2012-01-25 14:23:38 +0900 (Wed, 25 Jan 2012) New Revision: 278999a4e7a3792d11a83d9a10ad78d8a8cda881 Merged 8cd0856: Merge pull request #24 from logaling/improve-index Log: [#19] fix to deindex if glossary file remeved Modified files: lib/logaling/glossary_db.rb lib/logaling/repository.rb Modified: lib/logaling/glossary_db.rb (+13 -1) =================================================================== --- lib/logaling/glossary_db.rb 2012-01-23 18:04:45 +0900 (1344d45) +++ lib/logaling/glossary_db.rb 2012-01-25 14:23:38 +0900 (44f8ed7) @@ -62,10 +62,14 @@ module Logaling @database = nil end - def index_glossary(glossary, glossary_name, glossary_source, source_language, target_language, indexed_at) + def deindex_glossary(glossary_name, glossary_source) delete_translations_by_glossary_source(glossary_source) delete_glossary(glossary_name) delete_glossary_source(glossary_source) + end + + def index_glossary(glossary, glossary_name, glossary_source, source_language, target_language, indexed_at) + deindex_glossary(glossary_name, glossary_source) add_glossary_source(glossary_source, indexed_at) add_glossary(glossary_name) @@ -193,6 +197,14 @@ module Logaling !glossary.size.zero? end + def get_all_glossary + glossaries = [] + Groonga["glossary_sources"].each do |record| + glossaries << record.key + end + glossaries + end + private def delete_glossary_source(glossary_source) records = Groonga["glossary_sources"].select do |record| Modified: lib/logaling/repository.rb (+19 -20) =================================================================== --- lib/logaling/repository.rb 2012-01-23 18:04:45 +0900 (028745b) +++ lib/logaling/repository.rb 2012-01-25 14:23:38 +0900 (72392e4) @@ -71,22 +71,27 @@ module Logaling end def index - projects = Dir[File.join(@path, "projects", "*")] + project_glossaries = Dir[File.join(@path, "projects", "*")].map do |project| + Dir.glob(get_all_glossary_sources(File.join(project, "glossary"))) + end + imported_glossaries = Dir.glob(get_all_glossary_sources(cache_path)) + all_glossaries = project_glossaries.flatten + imported_glossaries Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db| db.recreate_table - projects.each do |project| - get_glossaries_from_project(project).each do |glossary_path, glossary_name, glossary_source, source_language, target_language, indexed_at| - unless db.glossary_source_exist?(glossary_source, indexed_at) - db.index_glossary(Glossary.load(glossary_path), glossary_name, glossary_source, source_language, target_language, indexed_at) - end - end - end - get_glossaries(cache_path).each do |glossary_path, glossary_name, glossary_source, source_language, target_language, indexed_at| + all_glossaries.each do |glossary_source| + indexed_at = File.mtime(glossary_source) unless db.glossary_source_exist?(glossary_source, indexed_at) - db.index_glossary(Glossary.load(glossary_path), glossary_name, glossary_source, source_language, target_language, indexed_at) + glossary_name, source_language, target_language = get_glossary(glossary_source) + puts "now index #{glossary_name}..." + db.index_glossary(Glossary.load(glossary_source), glossary_name, glossary_source, source_language, target_language, indexed_at) end end + (db.get_all_glossary - all_glossaries).each do |glossary_source| + glossary_name, source_language, target_language = get_glossary(glossary_source) + puts "now deindex #{glossary_name}..." + db.deindex_glossary(glossary_name, glossary_source) + end end end @@ -130,18 +135,12 @@ module Logaling end private - def get_glossaries(path) - Dir.glob(get_all_glossary_paths(path)).map do |file| - glossary_name, source_language, target_language = File::basename(file, ".*").split(".") - [file, glossary_name, File::basename(file), source_language, target_language, File.mtime(file)] - end - end - - def get_glossaries_from_project(path) - get_glossaries(File.join(path, "glossary")) + def get_glossary(path) + glossary_name, source_language, target_language = File::basename(path, ".*").split(".") + [glossary_name, source_language, target_language] end - def get_all_glossary_paths(path) + def get_all_glossary_sources(path) %w(yml tsv csv).map{|type| File.join(path, "*.#{type}") } end