Skip to content

Commit

Permalink
Refactored DAV::Children (fixed).
Browse files Browse the repository at this point in the history
* use data string internally
* store paths, not URLs
* drop Set dependency
* store changes in order
* reduced amount of objects created
* litmus passes again
  • Loading branch information
boof committed Apr 3, 2011
1 parent dfb1c0e commit 0bbc059
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
4 changes: 1 addition & 3 deletions lib/dav/resource/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def delete(responder)
status.ok! if response.precondition.ok?
end

response.on(:finish) do |status|
delete_all if status.ok?
end
response.on(:finish) { |status| delete_all if status.ok? }
end
end

Expand Down
54 changes: 26 additions & 28 deletions lib/dav/resource/children.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
require 'set'

module DAV
class Children < Struct.new(:parent)
include DAV

SEPARATOR = "\n"
FINDER = "^%s#{ SEPARATOR }"

def initialize(parent)
super parent
@adds, @removes = [], []
@changes = []
end

def write
storage = relation_storage.memory

# TODO support optimistic locking with other storage engines, too
return write_optimistic(storage) if storage.class.name == 'Redis'

update get_data
return true
# TODO Implement a sync class to support locking with other storages
# TODO make this work on resource level
if storage.class.name == 'Redis'
# RADAR This is just a hotfix!
return write_optimistic(storage)
else
update get_data
return true
end
end

def store
Expand All @@ -32,16 +31,16 @@ def store
end

def add(child)
@adds << child.decoded_uri.path
@changes << [child.decoded_uri.path, :add]
self
end
def remove(child)
@removes << child.decoded_uri.path
@changes << [child.decoded_uri.path, :remove]
self
end

def include?(resource)
paths.include? resource.decoded_uri.path
get_data =~ /^#{ Regexp.escape resource.decoded_uri.path }\n/
end

def each
Expand All @@ -63,19 +62,19 @@ def get_data
relation_storage.get(parent.id) || ''
end
def paths
collection = get_data.split SEPARATOR
collection.pop

collection
# Ruby does not create an empty element if the last character is the
# separator!
get_data.split "\n"
end
def update(data)
unless @removes.empty?
esc_paths = @removes.map { |path| Regexp.escape path }
data.gsub!(/#{ FINDER % "(?:#{ esc_paths.join '|' })" }/, '')
end
@adds.each do |path|
next if data =~ /#{ FINDER % Regexp.escape(path) }/
data << "#{ path }\n"
@changes.each do |(path, change)|
path_rx = /^#{ Regexp.escape path }\n/
case change
when :add
data << "#{ path }\n" unless data =~ path_rx
when :remove
data.sub! path_rx, ''
end
end

unless data.empty?
Expand All @@ -91,11 +90,10 @@ def write_optimistic(storage)
end

def changed?
not @adds.empty? && @removes.empty?
not @changes.empty?
end
def reset!
@adds.clear
@removes.clear
@changes.clear
end

end
Expand Down

0 comments on commit 0bbc059

Please sign in to comment.