Class: DataMapper::Adapters::LuceneAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/dm_lucene_adapter/dm_lucene_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ LuceneAdapter

Returns a new instance of LuceneAdapter.



16
17
18
19
# File 'lib/dm_lucene_adapter/dm_lucene_adapter.rb', line 16

def initialize(name, options = {})
  super
  (@path = Pathname(@options[:path]).freeze).mkpath
end

Instance Method Details

#create(resources) ⇒ Integer

Returns The number of records that were actually saved into the data-store.

Parameters:

  • resources (Enumerable<Resource>)

    The list of resources (model instances) to create

Returns:

  • (Integer)

    The number of records that were actually saved into the data-store



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dm_lucene_adapter/dm_lucene_adapter.rb', line 28

def create(resources)
  count = 0
  reader = lucene(resources.first.model).create_reader
  indexer = lucene(resources.first.model).create_indexer
  resources.each do |resource|
    resource.id = reader.next_id
    map = {}
    resource.attributes.each { |k,v| map[k.to_s] = v.to_s}
    indexer.index(map)
    count += 1
  end
  count
ensure
  indexer.close if indexer
  reader.close if reader
end

#delete(collection) ⇒ Integer

Returns the number of records deleted.

Parameters:

  • collection (Collection)

    collection of records to be deleted

Returns:

  • (Integer)

    the number of records deleted



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dm_lucene_adapter/dm_lucene_adapter.rb', line 187

def delete(collection)
  count = 0
  indexer = lucene(collection.model).create_deleter
  collection.each do |resource|
    indexer.delete(resource.id)
    count += 1
  end
  count
ensure
  indexer.close if indexer
end

#read(query) ⇒ Enumerable<Hash>

Returns an array of hashes to become resources.

Parameters:

  • query (Query)

    the query to match resources in the datastore

Returns:

  • (Enumerable<Hash>)

    an array of hashes to become resources



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dm_lucene_adapter/dm_lucene_adapter.rb', line 52

def read(query)
  is_sorting = query.order.size > 1 || query.order[0].target.name != :id || query.order[0].operator != :asc
  offset, limit = is_sorting ? [0, -1] : [query.offset, query.limit || -1]

  resources = do_read(offset, limit, query)
  resources.each do |resource|
    query.fields.each do |field| 
      attribute = field.name.to_s
      resource[attribute] = field.typecast(resource[attribute])
    end
  end
  
  if is_sorting
    #records = records.uniq if unique?
    resources = query.sort_records(resources)
    resources = query.limit_records(resources)
  end
  resources
end

#update(attributes, collection) ⇒ Integer

Returns the number of records updated.

Parameters:

  • attributes (Hash(Property => Object))

    hash of attribute values to set, keyed by Property

  • collection (Collection)

    collection of records to be updated

Returns:

  • (Integer)

    the number of records updated



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/dm_lucene_adapter/dm_lucene_adapter.rb', line 157

def update(attributes, collection)
  service = lucene(collection.model)
  deleter = service.create_deleter
  resources = read(collection.query)
  resources.each do |resource|
    deleter.delete(resource["id"])
  end
  deleter.close
  deleter = nil
  indexer = service.create_indexer
  attributes = attributes_as_fields(attributes)
  resources.each do |resource|
    resource.update(attributes)
    map = {}
    resource.each { |k,v| map[k.to_s] = v.to_s}
    indexer.index(map)
  end
  resources.size
ensure
  indexer.close if indexer
  deleter.close if deleter
end