Skip to content
Browse files

Set operations with Lua + TC

  • Loading branch information...
1 parent 00d3f11 commit 2a0792d3fb45795ea571ed3b06ea03bf8137a5c6 @igrigorik committed Jul 8, 2009
Showing with 114 additions and 0 deletions.
  1. +31 −0 sets/README.rdoc
  2. +72 −0 sets/sets.lua
  3. +11 −0 sets/sets.rb
View
31 sets/README.rdoc
@@ -0,0 +1,31 @@
+= Set operations for TC
+
+Lua extension with set operations for Tokyo Tyrant edit
+
+== Starting server
+ > ttserver -ext set.lua test.tch
+
+== Executing from command line
+ > tcrmgr ext localhost set_append keyname 1
+ 1
+ > tcrmgr ext localhost set_append keyname 3
+ 3
+ > tcrmgr ext localhost set_append keyname 5
+ 5
+ > tcrmgr ext localhost set_append keyname2 5
+ 5
+ > tcrmgr ext localhost set_length keyname
+ 3
+ > tcrmgr ext localhost set_get keyname
+ 1
+ 3
+ 5
+
+== Executing via Ruby
+
+ > ruby sets.rb
+ > bar
+ > nil
+ > bar
+
+Source: http://github.com/uggedal/tokyo-tyrant-sets/tree/master
View
72 sets/sets.lua
@@ -0,0 +1,72 @@
+-----------------------------------------------------------------------------
+-- Tokyo Tyrant Set Operations (by Eivind Uggedal)
+-- http://github.com/uggedal/tokyo-tyrant-sets/blob/f28b61e7c9e5aaa7282b9ef1c1bc81c48e806e6d/set.lua
+-----------------------------------------------------------------------------
+
+local SEP = '\n'
+
+function _set_len(stream)
+ local count = 0
+ if stream then
+ count = table.getn(_split(stream, SEP))
+ end
+ return count
+end
+
+function set_length(key, value)
+ return _set_len(_get(key))
+end
+
+function set_get(key)
+ return _get(key)
+end
+
+function set_append(key, value)
+ local stream = _get(key)
+
+ if not stream then
+ _put(key, value)
+ else
+ local set_len = _set_len(stream)
+
+ if set_len == 1 then
+ if stream == value then return nil end
+ elseif set_len > 1 then
+ for _, element in ipairs(_split(stream, SEP)) do
+ if element == value then return nil end
+ end
+ end
+ if not _putcat(key, SEP .. value) then
+ return nil
+ end
+ end
+ return value
+end
+
+function set_delete(key, value)
+ local stream = _get(key)
+
+ if stream then
+ local set_len = _set_len(stream)
+
+ if set_len == 1 and stream == value then
+ if _out(key) then return value end
+ elseif set_len > 1 then
+ local found = -1
+ local set_list = _split(stream, SEP)
+
+ for i, element in ipairs(set_list) do
+ if element == value then
+ found = i
+ break
+ end
+ end
+
+ if found > -1 then
+ table.remove(set_list, found)
+ if _put(key, table.concat(set_list, SEP)) then return value end
+ end
+ end
+ end
+ return nil
+end
View
11 sets/sets.rb
@@ -0,0 +1,11 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)
+
+puts t.ext(:set_append, 'foo', 'bar')
+puts t.ext(:set_append, 'foo', 'bar')
+
+puts t.ext(:set_get, 'foo', '')
+
+t.close

0 comments on commit 2a0792d

Please sign in to comment.
Something went wrong with that request. Please try again.