Permalink
Browse files

Added wait-table

NOTE: having problem returning only one row at search
  • Loading branch information...
1 parent d4cd2f0 commit fe7577e51e5378f1e9bf3a6a5408f3acda6064bb Makoto Inoue committed Mar 7, 2010
Showing with 104 additions and 0 deletions.
  1. +1 −0 wait-table/README.rdoc
  2. +51 −0 wait-table/test.rb
  3. +52 −0 wait-table/wait-table.lua
View
@@ -0,0 +1 @@
+d
View
@@ -0,0 +1,51 @@
+# start tyrant like so:
+# ttserver example.tct
+require 'rubygems'
+require 'tokyo_tyrant'
+require 'pp'
+t = TokyoTyrant::Table.new('127.0.0.1', 1978)
+
+
+# bulk operations
+h = {}
+h[0] = { :name => 'A: Joe task one', :region => 'us', :priority => 2, :url => 'http://example.com/0', :status => 'new'}
+h[1] = { :name => 'B: Bob task one', :region => 'us', :priority => 2, :url => 'http://example.com/1', :status => 'new'}
+h[2] = { :name => 'C: Joe task two', :region => 'us', :priority => 2, :url => 'http://example.com/2', :status => 'new'}
+h[3] = { :name => 'D: Bob task two', :region => 'eu', :priority => 1, :url => 'http://example.com/3', :status => 'new'}
+h[4] = { :name => 'E: Bob task three', :region => 'us', :priority => 1, :url => 'http://example.com/3', :status => 'new'}
+
+t.mput(h)
+# t.mget(0..3)
+
+p "Fetch only new tasks"
+# q = t.query
+# q.condition('status', :streq, 'new')
+# q.limit(1)
+# p q.get
+# # => [{"name"=>"A: Joe task one", "region"=>"us", "priority"=>"2", "url"=>"http://example.com/0", "__id"=>"0", "status"=>"new"}]
+p "Changing to pending"
+p t.ext(:update_new_task, 1, 2)
+
+
+p "Fetch only new tasks order by priority"
+# q = t.query
+# q.condition('status', :streq, 'new')
+# q.order_by(:priority, :strasc)
+# q.limit(1)
+# p q.get
+# => [{"name"=>"D: Bob task two", "region"=>"eu", "priority"=>"1", "url"=>"http://example.com/3", "__id"=>"3", "status"=>"new"}]
+
+p "Changing to pending"
+p t.ext(:priority, 1, 2)
+
+p "Fetch only Bob's US new tasks"
+# q = t.query
+# q.condition('status', :streq, 'new')
+# q.condition('region', :streq, 'us')
+# q.condition('name', :strinc, 'Bob')
+# q.limit(1)
+# p q.get
+# => [{"name"=>"B: Bob task one", "region"=>"us", "priority"=>"2", "url"=>"http://example.com/1", "__id"=>"1", "status"=>"new"}]
+
+p "Changing to pending"
+p t.ext(:combo, 1, 2)
View
@@ -0,0 +1,52 @@
+-- Equivalent to this sql
+-- update table set status ="pending" where status = "new" limit 1
+function update_new_task()
+ -- body
+ local args = {};
+ table.insert(args, "addcond\0status\0streq\0new")
+ table.insert(args, "setlimit\01")
+ table.insert(args, "columns")
+ local res = _misc("search", args)
+ if not res then
+ _log("failed", 2)
+ end
+ for key,value in pairs(res) do
+ print(":"..tostring(key).."="..tostring(value)..":")
+ end
+ -- The below is not working because re[1] is empty
+ -- return _strstr(res[1], "new", "pending")
+ return res[1]
+end
+
+function priority()
+ -- body
+ local args = {};
+ table.insert(args, "addcond\0status\0streq\0new")
+ table.insert(args, "setorder\0priority\0strasc")
+ table.insert(args, "setlimit\01")
+ table.insert(args, "columns")
+
+
+ local res = _misc("search", args)
+ if not res then
+ _log("failed", 2)
+ end
+ return res[1]
+end
+
+function combo()
+ -- body
+ local args = {};
+
+ table.insert(args, "addcond\0status\0streq\0new")
+ table.insert(args, "addcond\0region\0streq\0us")
+ table.insert(args, "addcond\0name\0strinc\0Bob")
+ table.insert(args, "setlimit\01")
+ table.insert(args, "columns")
+
+ local res = _misc("search", args)
+ if not res then
+ _log("failed", 2)
+ end
+ return res[1]
+end

0 comments on commit fe7577e

Please sign in to comment.