Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding initial documentation, and some initial spikes to speed up rec…

…ords_for
  • Loading branch information...
commit 0361ca945ffc00cacb7f91b3115d11641c4d9855 1 parent 487e337
Dan Herrera whoahbot authored

Showing 2 changed files with 95 additions and 3 deletions. Show diff stats Hide diff stats

  1. +5 1 README.textile
  2. +90 2 lib/redis_adapter.rb
6 README.textile
Source Rendered
@@ -2,7 +2,11 @@ h1. DM-Redis
2 2
3 3 This is an experimental datamapper adapter for the <a href="http://code.google.com/p/redis/wiki/README">Redis</a> key-value database.
4 4
5   -Please be aware that this is very alpha quality software! It is not recommended for production use. Oh, and it will be _slow_ for large sets of data until I'm finished refactoring, which sort of defeats the purpose, wouldn't you say?
  5 +Please be aware that this is very alpha quality software! It is not recommended for production use.
  6 +
  7 +h1. TODO
  8 +
  9 +Currently, the records_for(query) method returns a set of hashes of all of the
6 10
7 11 h1. Install
8 12
92 lib/redis_adapter.rb
@@ -5,6 +5,16 @@ module Adapters
5 5 Extlib::Inflection.word 'redis'
6 6
7 7 class RedisAdapter < AbstractAdapter
  8 + ##
  9 + # Used by DataMapper to put records into the redis data-store: "INSERT" in SQL-speak.
  10 + # It takes an array of the resources (model instances) to be saved. Resources
  11 + # each have a key that can be used to quickly look them up later without
  12 + # searching.
  13 + #
  14 + # @param [Enumerable(Resource)] resources
  15 + # The set of resources (model instances)
  16 + #
  17 + # @api semipublic
8 18 def create(resources)
9 19 resources.each do |resource|
10 20 initialize_identity_field(resource, @redis.incr("#{resource.model}:#{redis_key_for(resource.model)}:serial"))
@@ -14,6 +24,18 @@ def create(resources)
14 24 update_attributes(resources)
15 25 end
16 26
  27 + ##
  28 + # Looks up one record or a collection of records from the data-store:
  29 + # "SELECT" in SQL.
  30 + #
  31 + # @param [Query] query
  32 + # The query to be used to seach for the resources
  33 + #
  34 + # @return [Array]
  35 + # An Array of Hashes containing the key-value pairs for
  36 + # each record
  37 + #
  38 + # @api semipublic
17 39 def read(query)
18 40 records = records_for(query).each do |record|
19 41 query.fields.each do |property|
@@ -24,11 +46,34 @@ def read(query)
24 46 query.filter_records(records)
25 47 end
26 48
  49 + ##
  50 + # Used by DataMapper to update the attributes on existing records in the redis
  51 + # data-store: "UPDATE" in SQL-speak. It takes a hash of the attributes
  52 + # to update with, as well as a collection object that specifies which resources
  53 + # should be updated.
  54 + #
  55 + # @param [Hash] attributes
  56 + # A set of key-value pairs of the attributes to update the resources with.
  57 + # @param [DataMapper::Collection] collection
  58 + # The query that should be used to find the resource(s) to update.
  59 + #
  60 + # @api semipublic
27 61 def update(attributes, collection)
28 62 attributes = attributes_as_fields(attributes)
29 63 read(collection.query).each { |r| r.update(attributes) }
30 64 end
31 65
  66 + ##
  67 + # Destroys all the records matching the given query. "DELETE" in SQL.
  68 + #
  69 + # @param [DataMapper::Collection] collection
  70 + # The query used to locate the resources to be deleted.
  71 + #
  72 + # @return [Array]
  73 + # An Array of Hashes containing the key-value pairs for
  74 + # each record
  75 + #
  76 + # @api semipublic
32 77 def delete(collection)
33 78 collection.query.filter_records(records_for(collection.query)).each do |record|
34 79 collection.query.model.properties.each do |p|
@@ -40,10 +85,28 @@ def delete(collection)
40 85
41 86 private
42 87
  88 + ##
  89 + # Creates a string representation for the keys in a given model
  90 + #
  91 + # @param [DataMapper::Model] model
  92 + # The query used to locate the resources to be deleted.
  93 + #
  94 + # @return [Array]
  95 + # An Array of Hashes containing the key-value pairs for
  96 + # each record
  97 + #
  98 + # @api private
43 99 def redis_key_for(model)
44 100 model.key.collect {|k| k.name}.join(":")
45 101 end
46 102
  103 + ##
  104 + # Saves each key value pair to the redis data store
  105 + #
  106 + # @param [Array] resources
  107 + # An array of resources to save
  108 + #
  109 + # @api private
47 110 def update_attributes(resources)
48 111 resources.each do |resource|
49 112 resource.attributes.each do |property, value|
@@ -52,12 +115,37 @@ def update_attributes(resources)
52 115 end
53 116 end
54 117
  118 + ##
  119 + # Retrieves all of the records for a particular model
  120 + #
  121 + # @param [DataMapper::Query] query
  122 + # The query used to locate the resources
  123 + #
  124 + # @return [Array]
  125 + # An array of hashes of all of the records for a particular model
  126 + #
  127 + # @api private
55 128 def records_for(query)
56   - @redis.set_members("#{query.model}:#{redis_key_for(query.model)}:all").inject(Set.new) do |s, val|
57   - s << {"#{redis_key_for(query.model)}" => val.to_i}
  129 + set = @redis.set_members("#{query.model}:#{redis_key_for(query.model)}:all")
  130 + arr = Array.new(set.size)
  131 + set.each_with_index do |val, i|
  132 + arr[i] = {"#{redis_key_for(query.model)}" => val.to_i}
58 133 end
  134 +
  135 + arr
59 136 end
60 137
  138 + ##
  139 + # Make a new instance of the adapter. The @redis ivar is the 'data-store'
  140 + # for this adapter.
  141 + #
  142 + # @param [String, Symbol] name
  143 + # The name of the Repository using this adapter.
  144 + # @param [String, Hash] uri_or_options
  145 + # The connection uri string, or a hash of options to set up
  146 + # the adapter
  147 + #
  148 + # @api semipublic
61 149 def initialize(name, uri_or_options)
62 150 super
63 151 @redis = Redis.new(@options)

0 comments on commit 0361ca9

Please sign in to comment.
Something went wrong with that request. Please try again.