Class: DataMapperCassandra::Adapter
- Inherits:
-
DataMapper::Adapters::AbstractAdapter
- Object
- DataMapper::Adapters::AbstractAdapter
- DataMapperCassandra::Adapter
- Defined in:
- lib/dm-cassandra-adapter/adapter.rb
Overview
TODO: Do not store IDs in the object hash ????
Instance Method Summary collapse
- #column_family(model) ⇒ Object
- #convert_value(property, value) ⇒ Object
- #create(resources) ⇒ Object
- #delete(collection) ⇒ Object
- #read(query) ⇒ Object
- #update(dirty_attributes, collection) ⇒ Object
Instance Method Details
#column_family(model) ⇒ Object
53 54 55 |
# File 'lib/dm-cassandra-adapter/adapter.rb', line 53 def column_family(model) model.storage_name(self.name) end |
#convert_value(property, value) ⇒ Object
57 58 59 |
# File 'lib/dm-cassandra-adapter/adapter.rb', line 57 def convert_value(property, value) property.dump(value) end |
#create(resources) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dm-cassandra-adapter/adapter.rb', line 5 def create(resources) client.batch do resources.each do |resource| repository = resource.repository model = resource.model attributes = resource.attributes properties = model.properties(repository.name) ## Figure out or generate the key kind = self.column_family(model) keys = properties.key raise "Multiple keys in #{resource.inspect}" if keys.size > 1 if keys.size == 1 name = keys.first.name property = properties[name] key = convert_value(property, attributes[name]) end if keys.first.serial? && (key.nil? || key == 0 || key == '') name = keys.first.name property = properties[name] key = if property.primitive == Integer # BAD: for Serial Time.stamp & 0x7FFFFFFF else # GOOD: for UUID/:key => true SimpleUUID::UUID.new.to_guid end end initialize_serial(resource, key) attributes = resource.attributes #puts "#{key} => #{attributes.inspect}" ## Convert to serialized data ## data = {} attributes.each do |name, value| property = properties[name] data[property.field] = convert_value(property, value) end # Insert this resource into Cassandra client.insert(kind, key.to_s, data); end end resources end |
#delete(collection) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dm-cassandra-adapter/adapter.rb', line 100 def delete(collection) client.batch do count = collection.select do |resource| model = resource.model kind = self.column_family(model) key = model.key id = key.get(resource).join client.remove(kind, id) end end.size end |
#read(query) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dm-cassandra-adapter/adapter.rb', line 61 def read(query) model = query.model kind = self.column_family(model) records = if id = extract_id_from_query(query) data = client.get(kind, id.to_s) [ load_resource(data, model) ] else # raise NotImplementedError.new("SimpleDB supports only a single order clause") # FIXME - This is terrible, we should not get all keys all_keys = client.get_range(kind) data_hash = client.multi_get(kind, all_keys) data_hash.map do |id, data| load_resource(data, model) end end query.filter_records(records) end |
#update(dirty_attributes, collection) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dm-cassandra-adapter/adapter.rb', line 81 def update(dirty_attributes, collection) client.batch do count = collection.select do |resource| model = resource.model kind = self.column_family(model) key = model.key id = key.get(resource).join data = {} dirty_attributes.each do |property, value| property.set!(resource, value) data[property.field] = convert_value(property, value) end client.insert(kind, id, data); end end.size end |