Class: Silver::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/silver/indexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, time_field, redis_options = {}, &query) ⇒ Index

Create a new index for some given database or web service. Silver doesn’t do any connecting to a database, it requires you to pass a query to it as a block. Use whatever ORM you like best.

key is the name this index will be stored with in Redis. time_field is the name of the field used for determining whether or not there are new entries. query is a block that takes the date of the most recently indexed item as a parameter. Query should always sort entries in descending order.

Example to index all large pictures based on their captions:

index = Silver::Index.new("picturecaptions",
                          "created_time") do |time|

  Pictures.all(:order => :created_time.desc, 
               :created_time.gt => time, 
               :size => "large")
end


37
38
39
40
41
42
43
44
45
# File 'lib/silver/indexer.rb', line 37

def initialize(key,time_field,redis_options={},&query)
    @r = Redis.new(redis_options)
    @r.select 12
    @key = key
    @time_field = time_field
    @query = query
    @new_results = []
    @indexed_items = []
end

Instance Method Details

#find_and_update(&accessor) ⇒ Object

Looks for the most recent date indexed and only fetch entries newer than that date from the database.

Takes a block that takes a results from the database and returns the value by which to index. accessor should be a block that takes an individual results and returns and array containing, first, the item’s id and, second, the value by which to index.

Ex:

index.find_and_update do |result|
  output = result.caption || result.label || "" 
  id = result.id
  [id,output]
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/silver/indexer.rb', line 62

def find_and_update(&accessor)

    last_date = @r.get("#{@key}:last") || "Jan. 1, 1970"
    new_results = @query.call(DateTime.parse(last_date))
    if new_results.empty?
        false
    else
        new_date = new_results[0][@time_field].to_s
        @r.set("#{@key}:last",new_date)
        @new_results = new_results
        parse(accessor)
        true
    end
end