Changeset 153

Show
Ignore:
Timestamp:
04/12/08 12:19:09 (7 months ago)
Author:
eric.dumin..@gmail.com
Message:

Still messy, but Models are more balanced now.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/rewrote_indexer/lib/picolena/templates/app/models/document.rb

    r151 r153  
    6767  def cached 
    6868    get_index_id! unless index_id 
    69     Finder.index[index_id][:content] 
     69    IndexReader.new[index_id][:content] 
    7070  end 
    7171   
     
    7474  def date 
    7575    get_index_id! unless index_id 
    76     Finder.index[index_id][:date].sub(/(\d{4})(\d{2})(\d{2})/,'\1-\2-\3') 
     76    IndexReader.new[index_id][:date].sub(/(\d{4})(\d{2})(\d{2})/,'\1-\2-\3') 
    7777  end 
    7878   
    7979  def mtime 
    8080    get_index_id! unless index_id 
    81     Finder.index[index_id][:date].to_i 
     81    IndexReader.new[index_id][:date].to_i 
    8282  end 
    8383   
  • branches/rewrote_indexer/lib/picolena/templates/app/models/finder.rb

    r152 r153  
    44  attr_reader :query 
    55   
    6   def self.index 
     6  def index 
    77    # caching index @@index ||=   
    88    # causes ferret-0.11.6/lib/ferret/index.rb:768: [BUG] Segmentation fault 
    9     Ferret::Index::Index.new(:path => IndexSavePath, :analyzer=>Analyzer)   
     9    IndexReader.new 
    1010  end 
    1111   
     
    1313    @query = Query.extract_from(raw_query) 
    1414    @raw_query= raw_query 
    15     Finder.ensure_that_index_exists_on_disk 
     15    IndexReader.ensure_existence 
    1616    @per_page=results_per_page 
    1717    @offset=(page.to_i-1)*results_per_page 
    18     validate_that_index_has_documents 
     18    index.validate_that_has_documents 
    1919  end 
    2020   
     
    2222    @matching_documents=[] 
    2323    start=Time.now 
    24     top_docs=Finder.index.search(query, :limit => @per_page, :offset=>@offset) 
     24    top_docs=index.search(query, :limit => @per_page, :offset=>@offset) 
    2525    top_docs.hits.each{|hit| 
    2626      index_id,score=hit.doc,hit.score 
    2727      begin 
    28         found_doc=Document.new(Finder.index[index_id][:complete_path]) 
    29         found_doc.matching_content=Finder.index.highlight(query, index_id, 
     28        found_doc=Document.new(index[index_id][:complete_path]) 
     29        found_doc.matching_content=index.highlight(query, index_id, 
    3030                                                          :field => :content, :excerpt_length => 80, 
    3131                                                          :pre_tag => "<<", :post_tag => ">>" 
     
    6060  } 
    6161    
    62    # Returns true if index is existing. 
    63    def self.has_index? 
    64      index_filename and File.exists?(index_filename) 
    65    end 
    66     
    67    # Returns true if there's at least one document indexed. 
    68    def has_documents? 
    69      Finder.index.size>0 
    70    end 
    71  
    7262   # Returns matching document for any given query only if 
    7363   # exactly one document is found. 
     
    8373     end 
    8474   end 
    85     
    86    private 
    87     
    88    def self.index_filename 
    89      Dir.glob(File.join(IndexSavePath,'*.cfs')).first 
    90    end 
    91  
    92    def self.ensure_that_index_exists_on_disk 
    93      force_index_creation unless has_index? or RAILS_ENV=="production" 
    94    end 
    95     
    96    def self.force_index_creation 
    97      #Index every directory, without updating. 
    98      Indexer.index_every_directory(false) 
    99    end 
    100     
    101    def self.delete_index 
    102      FileUtils.rm(Dir.glob(File.join(IndexSavePath,'*.cfs'))) if has_index? 
    103    end 
    104     
    105    def validate_that_index_has_documents 
    106      raise IndexError, "no document found" unless has_documents? 
    107    end  
    10875end 
  • branches/rewrote_indexer/lib/picolena/templates/app/models/index_reader.rb

    r151 r153  
    1313  end 
    1414   
    15   def document(complete_path) 
    16     search_by_complete_path(path) 
    17   end 
    18    
    19   def cached_mtime(complete_path) 
    20      
    21   end 
    22  
    2315  def search_by_complete_path(complete_path) 
    2416    search('complete_path:"'<<complete_path<<'"')   
     
    3022    } 
    3123  end 
     24   
     25   
     26  # Validation methods. 
     27   
     28  def validate_that_has_documents 
     29     raise IndexError, "no document found" unless has_documents? 
     30  end 
     31  
     32  # Returns true if there's at least one document indexed. 
     33  def has_documents? 
     34   size>0 
     35  end 
     36  
     37 class<<self 
     38 
     39   def ensure_existence 
     40     Indexer.index_every_directory(update=false) unless exists? or RAILS_ENV=="production" 
     41   end 
     42  
     43  def exists? 
     44     filename and File.exists?(filename) 
     45  end 
     46  
     47  def filename 
     48    Dir.glob(File.join(IndexSavePath,'*.cfs')).first 
     49  end 
     50  end 
    3251end 
  • branches/rewrote_indexer/lib/picolena/templates/app/models/indexer.rb

    r152 r153  
    1212        :date               => File.mtime(complete_path).strftime("%Y%m%d%H%M%S") 
    1313      }       
    14     end 
    15      
     14    end     
     15         
    1616    def index_every_directory(update=true) 
    1717      log :debug => "Indexing every directory" 
     
    100100    def log(hash) 
    101101      hash.each{|level,message| 
    102         #puts "#{level} -> #{message}" 
     102        puts "#{level} -> #{message}" 
    103103        IndexerLogger.send(level,message) 
    104104      } 
  • branches/rewrote_indexer/lib/picolena/templates/lib/tasks/index.rake

    r152 r153  
    33  desc 'Clear indexes' 
    44  task :clear => :environment do 
    5     Dir.glob(File.join(IndexesSavePath,'/**/*')).each{|f| FileUtils.rm(f) if File.file?(f)} 
     5    IndexWriter.remove 
    66  end 
    77   
  • branches/rewrote_indexer/lib/picolena/templates/spec/models/basic_finder_spec.rb

    r140 r153  
    1111   
    1212  before(:each) do 
    13     Finder.delete_index 
     13    IndexWriter.remove 
    1414  end 
    1515   
    1616  it "should create index" do 
    1717    IndexedDirectories.replace({'spec/test_dirs/indexed/just_one_doc'=>'//justonedoc/'}) 
    18     lambda {@finder_with_new_index=Finder.new("test moi")}.should change(Finder, :has_index?).from(false).to(true) 
     18    lambda {@finder_with_new_index=Finder.new("test moi")}.should change(IndexReader, :exists?).from(false).to(true) 
    1919    File.exists?(File.join(@new_index_path,'_0.cfs')).should be_true 
    20     Finder.index.size.should >0 
     20    IndexReader.new.size.should >0 
    2121  end 
    2222   
     
    4545describe "Basic Finder" do   
    4646  before(:all) do 
    47     Finder.force_index_creation 
     47    Indexer.index_every_directory(update=false) 
    4848  end 
    4949   
     
    8383  fields.each_pair do |description,field_name| 
    8484    it "should index #{description} as :#{field_name}" do 
    85       Finder.index.field_infos[field_name].should be_an_instance_of(Ferret::Index::FieldInfo) 
     85      IndexReader.new.field_infos[field_name].should be_an_instance_of(Ferret::Index::FieldInfo) 
    8686    end 
    8787  end 
  • branches/rewrote_indexer/lib/picolena/templates/spec/models/filters_spec.rb

    r81 r153  
    33describe "Filters" do 
    44  before(:all) do 
    5     Finder.ensure_that_index_exists_on_disk 
     5    IndexReader.ensure_existence 
    66  end   
    77   
  • branches/rewrote_indexer/lib/picolena/templates/spec/models/finder_spec.rb

    r140 r153  
    2222    File.utime(0, a_bit_later, 'spec/test_dirs/indexed/yet_another_dir/office2003-word-template.dot') 
    2323    File.utime(0, nineties, 'spec/test_dirs/indexed/others/placeholder.txt') 
    24     Finder.force_index_creation 
     24    Indexer.index_every_directory(update=false) 
    2525  end 
    2626   
     
    4848   
    4949  it "should give a boost to basename, filename and filetype in index" do 
    50     Finder.index.field_infos[:basename].boost.should > 1.0 
    51     Finder.index.field_infos[:file].boost.should > 1.0 
    52     Finder.index.field_infos[:filetype].boost.should > 1.0 
     50    index=IndexReader.new 
     51    index.field_infos[:basename].boost.should > 1.0 
     52    index.field_infos[:file].boost.should > 1.0 
     53    index.field_infos[:filetype].boost.should > 1.0 
    5354  end 
    5455