Skip to content

Commit

Permalink
Use the new get_many method for retrieve multiple keys from this bu…
Browse files Browse the repository at this point in the history
…cket.

This is way too much better than iterate over the keys and do multiple GETs to the database.
  • Loading branch information
emancu committed Oct 7, 2013
1 parent a850906 commit 2e6c261
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/ork/model/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ def delete
protected

# Overwrite attributes with the persisted attributes in Riak.
#
def load!(id)
@id = self.__robject.key = id
@__robject = @__robject.reload(force: true)
self.__robject.key = id
__load_robject! id, @__robject.reload(force: true)
end

# Transform a RObject returned by Riak into a Ork::Document.
#
def __load_robject!(id, robject)
@id = id
@__robject = robject
@attributes = {}
@embedding = {}

Expand Down
10 changes: 8 additions & 2 deletions lib/ork/model/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def exist?(id)
# be used in production applications.
#
def all
bucket.keys.map{|k| load_key k}
load_robjects bucket.get_many(bucket.keys)
end
alias :list :all

Expand Down Expand Up @@ -55,7 +55,7 @@ def find(by_index, value)
raise Ork::IndexNotFound unless indices.has_key? by_index

index = indices[by_index]
bucket.get_index(index.riak_name, value).map{|k| load_key k}
load_robjects bucket.get_many(bucket.get_index(index.riak_name, value))
end

private
Expand All @@ -66,5 +66,11 @@ def load_key(id)
raise e unless e.not_found?
end

def load_robjects(robjects)
robjects.map do |id, robject|
new.send(:__load_robject!, id, robject)
end
end

end
end

0 comments on commit 2e6c261

Please sign in to comment.