null+****@clear*****
null+****@clear*****
Mon Jan 23 17:02:37 JST 2012
SUZUKI Miho 2012-01-23 17:02:37 +0900 (Mon, 23 Jan 2012) New Revision: e1b04cf0138ba6365b2582d1d1c58e077666c655 Merged 8cd0856: Merge pull request #24 from logaling/improve-index Log: [#19] improve index - add glossary_source table - add glossary existence check - fix to load glossary after checking existence of glossary in table Modified files: lib/logaling/glossary_db.rb lib/logaling/repository.rb Modified: lib/logaling/glossary_db.rb (+67 -14) =================================================================== --- lib/logaling/glossary_db.rb 2012-01-23 11:21:23 +0900 (8e72ef5) +++ lib/logaling/glossary_db.rb 2012-01-23 17:02:37 +0900 (28fd175) @@ -62,13 +62,18 @@ module Logaling @database = nil end - def index_glossary(glossary, glossary_name, source_language, target_language) + def index_glossary(glossary, glossary_name, glossary_source, source_language, target_language, indexed_at) + delete_translations_by_glossary_source(glossary_source) + delete_glossary(glossary_name) + delete_glossary_source(glossary_source) + + add_glossary_source(glossary_source, indexed_at) add_glossary(glossary_name) glossary.each do |term| source_term = term['source_term'] target_term = term['target_term'] note = term['note'] - add_translations(glossary_name, source_language, target_language, source_term, target_term, note) + add_translation(glossary_name, glossary_source, source_language, target_language, source_term, target_term, note) end end @@ -178,23 +183,64 @@ module Logaling end end + def glossary_source_exist?(glossary_source, indexed_at) + glossary = Groonga["glossary_sources"].select do |record| + [ + record.key == glossary_source, + record.indexed_at == indexed_at + ] + end + !glossary.size.zero? + end + private + def delete_glossary_source(glossary_source) + records = Groonga["glossary_sources"].select do |record| + record.key == glossary_source + end + + records.each do |record| + record.key.delete + end + end + + def add_glossary_source(glossary_source, indexed_at) + Groonga["glossary_sources"].add(glossary_source, :indexed_at => indexed_at) + end + + def delete_glossary(glossary_name) + records = Groonga["glossaries"].select do |record| + record.key == glossary_name + end + + records.each do |record| + record.key.delete + end + end + def add_glossary(glossary_name) - name = Groonga["glossaries"].select{|record| record.key == glossary_name} - if name.size.zero? - now = Time.now - Groonga["glossaries"].add(glossary_name, :indexed_at => now) + Groonga["glossaries"].add(glossary_name) + end + + def delete_translations_by_glossary_source(glossary_source) + records = Groonga["translations"].select do |record| + record.glossary_source == glossary_source + end + + records.each do |record| + record.key.delete end end - def add_translations(glossary_name, source_language, target_language, source_term, target_term, note) + def add_translation(glossary_name, glossary_source, source_language, target_language, source_term, target_term, note) Groonga["translations"].add(:glossary => glossary_name, - :source_language => source_language, - :target_language => target_language, - :source_term => source_term, - :target_term => target_term, - :note => note, - ) + :glossary_source => glossary_source, + :source_language => source_language, + :target_language => target_language, + :source_term => source_term, + :target_term => target_term, + :note => note, + ) end def reset_context(encoding) @@ -213,14 +259,20 @@ module Logaling table.text("conf_value") end - schema.create_table("glossaries", + schema.create_table("glossary_sources", :type => :hash, :key_type => "ShortText") do |table| table.time("indexed_at") end + schema.create_table("glossaries", + :type => :hash, + :key_type => "ShortText") do |table| + end + schema.create_table("translations") do |table| table.reference("glossary", "glossaries") + table.reference("glossary_source", "glossary_sources") table.short_text("source_language") table.short_text("target_language") table.short_text("source_term") @@ -243,6 +295,7 @@ module Logaling schema.remove_table("configurations") if Groonga["configurations"] schema.remove_table("translations") if Groonga["translations"] schema.remove_table("glossaries") if Groonga["glossaries"] + schema.remove_table("glossary_sources") if Groonga["glossary_sources"] schema.remove_table("terms") if Groonga["terms"] end end Modified: lib/logaling/repository.rb (+9 -6) =================================================================== --- lib/logaling/repository.rb 2012-01-23 11:21:23 +0900 (e5c63f0) +++ lib/logaling/repository.rb 2012-01-23 17:02:37 +0900 (4a02672) @@ -71,18 +71,21 @@ module Logaling end def index - return if latest_index? projects = Dir[File.join(@path, "projects", "*")] Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db| db.recreate_table projects.each do |project| - get_glossaries_from_project(project).each do |glossary, glossary_name, source_language, target_language| - db.index_glossary(glossary, glossary_name, source_language, target_language) + 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, glossary_name, source_language, target_language| - db.index_glossary(glossary, glossary_name, source_language, target_language) + get_glossaries(cache_path).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 @@ -146,7 +149,7 @@ module Logaling def get_glossaries(path) Dir.glob(get_all_glossary_paths(path)).map do |file| glossary_name, source_language, target_language = File::basename(file, ".*").split(".") - [Glossary.load(file), glossary_name, source_language, target_language] + [file, glossary_name, File::basename(file), source_language, target_language, File.mtime(file)] end end