Skip to content

Commit

Permalink
Add transaction support for Tokyo Tyrant Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrosby committed Mar 24, 2009
1 parent 85e8302 commit 80385d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
*.tdb
*.tct
.lock
6 changes: 0 additions & 6 deletions lib/cloudkit/store/memory_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ def query(&block)
q.run(self)
end

# Simulate a transaction. This development-mode transaction merely yields
# to its block.
def transaction
yield
end

protected

def valid?(record)
Expand Down
15 changes: 13 additions & 2 deletions lib/cloudkit/store/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def save
# modify resources that are not current.
def update(json, remote_user=nil)
raise HistoricalIntegrityViolation unless current?
CloudKit.storage_adapter.transaction do
transaction do
record = CloudKit.storage_adapter[@id]
record['uri'] = "#{@uri.string}/versions/#{@etag}"
record['archived'] = escape(true)
Expand All @@ -65,7 +65,7 @@ def update(json, remote_user=nil)
# are not current.
def delete
raise HistoricalIntegrityViolation unless current?
CloudKit.storage_adapter.transaction do
transaction do
original_uri = @uri
record = CloudKit.storage_adapter[@id]
record['uri'] = "#{@uri.string}/versions/#{@etag}"
Expand Down Expand Up @@ -254,5 +254,16 @@ def symbolize_keys(hash)
def escape_values(hash)
hash.inject({}) { |memo, pair| memo.merge({pair[0] => escape(pair[1])}) }
end

def transaction
open('.lock', 'w+') do |f|
f.flock(File::LOCK_EX)
begin
yield
ensure
f.flock(File::LOCK_UN)
end
end
end
end
end

0 comments on commit 80385d4

Please sign in to comment.