Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Refactored how ranges of chunks are selected, to be more efficient.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Mar 3, 2011
1 parent 729c5bc commit 62332d4
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions lib/dm-chunked_query/chunks.rb
Expand Up @@ -36,25 +36,12 @@ def initialize(query,per_chunk)
def [](key)
case key
when Range
if (key.min >= 0 && key.min < length)
if (key.max >= 0 && key.max < length)
chunks = nil
index = key.first
span = key.to_a.size

key.each do |index|
if chunks
chunks |= chunk_at(index)
else
chunks = chunk_at(index)
end
end

chunks
end
end
chunk_at(index,span)
when Integer
if (key >= 0 && key < length)
chunk_at(key)
end
chunk_at(key)
end
end

Expand All @@ -76,7 +63,7 @@ def each
return enum_for(:each) unless block_given?

(0...length).each do |index|
yield chunk(index)
yield chunk_at(index)
end

return self
Expand Down Expand Up @@ -112,14 +99,16 @@ def length
# @param [Integer] index
# The index of the chunk.
#
# @param [Integer] span
# The number of chunks the chunk should span.
#
# @return [DataMapper::Collection]
# The collection of resources that makes up the chunk.
#
def chunk_at(index)
@query.all(
:limit => @per_chunk,
:offset => (index * @per_chunk)
)
def chunk_at(index,span=1)
if (index >= 0 && index < length)
@query[(index * @per_chunk), (span * @per_chunk)]
end
end

end
Expand Down

0 comments on commit 62332d4

Please sign in to comment.