Class: DataMapper::Adapters::RindaAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/rinda_adapter.rb

Overview

This is probably the simplest functional adapter possible. It simply stores and queries from a hash containing the model classes as keys, and an array of hashes. It is not persistent whatsoever; when the Ruby process finishes, everything that was stored it lost. However, it doesn’t require any other external libraries, such as data_objects, so it is ideal for writing specs against. It also serves as an excellent example for budding adapter developers, so it is critical that it remains well documented and up to date.

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#create(resources) ⇒ Object

Used by DataMapper to put records into a data-store: “INSERT” in SQL-speak. It takes an array of the resources (model instances) to be saved. Resources each have a key that can be used to quickly look them up later without searching, if the adapter supports it.

Parameters:

  • resources (Enumerable(Resource))

    The set of resources (model instances)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rinda_adapter.rb', line 65

def create(resources)
  name = self.name
  #    DataMapper.logger <<  "create #{resources.first.model}"

  resources.each do |resource|
    model      = resource.model
    serial     = model.serial(name)

    #  DataMapper.logger <<  "res #{resource.inspect}"
    #initialize_serial(resource, rand(2**32))
    #DataMapper.logger <<  "att #{resource.attributes(:field).inspect}"

    saveblock = { }

    resource.attributes.each do |key, value|
      #     DataMapper.logger <<  "before convert #{resource.model.properties[key].type}"
      saveblock[key.to_s]=convert_to_ts(resource.model.properties[key].type, value)
    end
#          model      = resource.model
    #         attributes = resource.dirty_attributes

    #       model.properties_with_subclasses(name).each do |property|
    #       next unless attributes.key?(property)

    #     value = attributes[property]
    #    saveblock[property.field.to_s]=convert_to_ts(property.type, value)
    #end
    # add model name to be included into tuple
    saveblock["_model_"]=resources.first.model.storage_name(name).to_s

    DataMapper.logger <<  "write #{saveblock.inspect}"
    @monitor.synchronize do
      if serial
        id = @ts.writeID saveblock
        serial.set!(resource, id)
      else
        @ts.write saveblock
      end

      #  @ts.write saveblock
      #initialize_serial(resource,id)
    end

  end
end

#delete(collection) ⇒ Integer

Destroys all the records matching the given query. “DELETE” in SQL.

Parameters:

  • resources (DataMapper::Collection)

    The collection of resources to delete.

Returns:

  • (Integer)

    The number of records that were deleted.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rinda_adapter.rb', line 205

def delete(collection)
  #DataMapper.logger <<  "delete #{collection.model.to_s}"
  query = generate_query(collection.model)
  # @mutex.synchronize do

  result=@ts.read_all(query)

  records_to_delete = collection.query.filter_records(result)
  #DataMapper.logger <<  "entries to delete #{records_to_delete.inspect}"

  records_to_delete.each do |record|
    result=@ts.take(record)
  end
  records_to_delete.size
  #end # class mutex synchronize
end

#notify(action, query, callback, model, dm_query, time = nil) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rinda_adapter.rb', line 244

def notify(action,query,callback,model,dm_query,time = nil)
  x = Thread.start do
    observer = notifyInternal(model, action, query,time)
    DataMapper.logger <<  "waiting on #{model.to_s} model new #{action} changes with a state change to #{query.inspect}"

    observer.each do |e,t|
      @monitor.synchronize {
        DataMapper.logger <<  "TRIGGERED on #{model.to_s} model new #{action} changes with a state change to #{query.inspect}"

        if check_descendents(model,t) # quick patch that belongs into tuplespace
          DataMapper.logger <<   "#{e} change detected for #{t.inspect}"
          resource = nil

          repository = dm_query.repository
          model      = dm_query.model
          identity_fields = model.key(repository.name).map &:name

          DataMapper.logger <<   "rep: #{repository.name}  model:#{model} identifier key: #{identity_fields.inspect}"

          retrieve = identity_fields.map do |x| t[x.to_s] end

          resource = model.get(*retrieve)
          DataMapper.logger <<   "found resource  #{resource.inspect}"

          callback.call resource
        end
      }
    end
  end
  return x
end

#read(query) ⇒ Array

Looks up one record or a collection of records from the data-store: “SELECT” in SQL.

Parameters:

  • query (Query)

    The query to be used to seach for the resources

Returns:

  • (Array)

    An Array of Hashes containing the key-value pairs for each record



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rinda_adapter.rb', line 122

def read(query)


#        DataMapper.logger <<  "query #{query.model.to_s}"
#       DataMapper.logger <<  "query #{query.fields.inspect}"
queryblock = generate_query_with_conditions(query)
DataMapper.logger <<  "ts query #{queryblock.inspect}"
result=@ts.read_all(queryblock)

DataMapper.logger <<  "result  #{result.inspect}"
#Kernel.const_get(s)


query.fields.each do |property|
  if (property.type == DataMapper::Types::Discriminator)

    key = property.name.to_s
    result.each do |entry|
      begin
      entry[key]=eval(entry[key].to_s)
      rescue NameError => e
        DataMapper.logger <<  "unknown symbol #{e.to_s}"
        end

    end
  end
end
#         DataMapper.logger <<  "result after  transformation of discriminators  #{result.inspect}"
query.filter_records(result)

end

#update(attributes, collection) ⇒ Object

Used by DataMapper to update the attributes on existing records in a data-store: “UPDATE” in SQL-speak. It takes a hash of the attributes to update with, as well as a collection object that specifies which resources should be updated.

Parameters:

  • attributes (Hash)

    A set of key-value pairs of the attributes to update the resources with.

  • resources (DataMapper::Collection)

    The collection of resources to update.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rinda_adapter.rb', line 165

def update(attributes, collection)
  DataMapper.logger <<  "update attributes: #{attributes.inspect} collection: #{collection.inspect}"
  query = collection.query

  query = generate_query_with_conditions(query)
  # generate_query(collection.model)

  records_to_delete=[]
  @monitor.synchronize do
    result=@ts.read_all(query)

    records_to_delete = collection.query.filter_records(result)

    records_to_delete.each do |record|
      result=@ts.take(record)
      saveblock ={ }
      attributes.each do |key, value|
        #   DataMapper.logger <<  "key: #{key.name} value: #{value}"
        saveblock[key.name.to_s]=convert_to_ts(key.name, value)
      end
      new = result.merge  saveblock
      @ts.write(new)

      DataMapper.logger <<  "replaced: #{result.inspect} with: #{new.inspect}"
    end
  end # class synchronize

  return records_to_delete.size
  #end # class mutex synchronize
end

#wait(action, query, callback, model, dm_query, time = 10000) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rinda_adapter.rb', line 223

def wait(action,query,callback,model,dm_query,time = 10000)

  query = generate_query(model).merge create_conditions(dm_query)

  x = Thread.start do
    begin
      t = @ts.read query,(time/1000)
    end until t and check_descendents(model,t) # quick patch that belongs into tuplespace

    repository = dm_query.repository
    model      = dm_query.model
    identity_fields = model.key(repository.name).map &:name

    retrieve = identity_fields.map do |x| t[x.to_s] end

    resource = model.get(*retrieve)
    callback.call resource
  end
  x
end