Class: Vines::Services::Indexer

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/vines/services/indexer.rb

Overview

Save system ohai documents in a sqlite database for fast searching. This index powers the live search feature of the service builder user interface. The indexing happens on a separate thread for two reasons:

- writes to sqlite block the EventMachine thread
- writes to sqlite lock the database file, so we need one writer process

Constant Summary collapse

@@indexers =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Indexer

There must be only one Indexer managing a sqlite file at one time. Use Indexer to create or retrieve the Indexer for a given file rather than calling the constructor directly.



25
26
27
28
29
30
31
# File 'lib/vines/services/indexer.rb', line 25

def initialize(file)
  @db = database(file)
  @tasks = PriorityQueue.new do |a, b|
    a[:priority] <=> b[:priority]
  end
  process_tasks
end

Class Method Details

.[](file) ⇒ Object

Return the Indexer instance managing this file, creating a new Indexer instance if needed.



17
18
19
20
# File 'lib/vines/services/indexer.rb', line 17

def self.[](file)
  file = File.expand_path(file)
  @@indexers[file] ||= self.new(file)
end

Instance Method Details

#<<(doc) ⇒ Object

Queue the document for indexing at some point in the future. Because adding documents to the index is less time-sensitive than querying the index, these tasks may be delayed by query tasks.



36
37
38
39
40
41
42
# File 'lib/vines/services/indexer.rb', line 36

def <<(doc)
  @tasks.push({
    priority: Time.now.to_f,
    type: :index,
    doc: doc
  })
end

#find(query, *args, &callback) ⇒ Object

Run the SQL query with optional replacement parameters (e.g. ?) and yield the results array to the callback block. Queries are prioritized ahead of document indexing tasks so they will return quickly even when many documents are waiting to be indexed.



48
49
50
51
52
53
54
55
56
# File 'lib/vines/services/indexer.rb', line 48

def find(query, *args, &callback)
  @tasks.push({
    priority: 0,
    type: :query,
    query: query,
    args: args.flatten,
    callback: callback
  })
end