Skip to content

Commit

Permalink
simple session trail logger (~2000 req/s)
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jul 7, 2009
1 parent b81df73 commit f3db0dc
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion high-low-game/README.rdoc
@@ -1,4 +1,4 @@
= Echo command
= Simple High-Low Betting Game

Implements a simple high-low betting game via Lua and TC as a database to persist bet / player information

Expand Down
27 changes: 27 additions & 0 deletions session-trail/README.rdoc
@@ -0,0 +1,27 @@
= Simple Session trail database

Tracks user ids and visited user resources by timestamping each interaction. The list of all visits can then be retrieved by querying for a user id.

== Starting server with incr extension
> ttserver -ext session-trail.lua test.tch

== Executing from command line
> tcrmgr ext localhost add 1 123
> tcrmgr ext localhost add 1 256
> tcrmgr ext localhost add 1 987
> tcrmgr ext localhost add 2 987
> tcrmgr ext localhost list 1
987 1247008220
256 1247008216
123 1247008123

== Executing via Ruby

> ruby session-trail.rb
1
2
3
1
343 1247008682
253 1247008681
123 1247008680
67 changes: 67 additions & 0 deletions session-trail/session-trail.lua
@@ -0,0 +1,67 @@
MAXPRINT = 60

function add(key, value)
key = tonumber(key)
value = tonumber(value)
if not key or not value or key == value then
return nil
end
local ksel = _pack("i", key)
local time = os.time()
local date = os.date("*t", time)
date.hour = 0
date.min = 0
date.sec = 0
local mintime = os.time(date)
local vsel
local ary = _unpack("i*", _get(ksel))
local anum = 1
if ary and #ary > 0 then
local nary = {}
local nidx = 1
local nidxmax = MAXPRINT * 2 - 1
for i = 1, #ary, 2 do
if ary[i] ~= value or ary[i+1] < mintime then
nary[nidx] = ary[i]
nary[nidx+1] = ary[i+1]
nidx = nidx + 2
end
end
vsel = _pack("i*", nary, value, time)
anum = (#nary / 2) + 1
if anum > MAXPRINT then
vsel = string.sub(vsel, MAXPRINT * -8)
anum = MAXPRINT
end
else
vsel = _pack("ii", value, time)
end
if not _put(ksel, vsel) then
return nil
end
return anum
end

function list(key, value)
key = tonumber(key)
value = tonumber(value)
if not key then
return nil
end
if not value or value < 1 then
value = MAXPRINT
end
local result = ""
local ksel = _pack("i", key)
local ary = _unpack("i*", _get(ksel))
if ary and #ary > 0 then
for i = #ary - 1, 0, -2 do
if value < 1 then
break
end
result = result .. ary[i] .. "\t" .. ary[i+1] .. "\n"
value = value - 1
end
end
return result
end
13 changes: 13 additions & 0 deletions session-trail/session-trail.rb
@@ -0,0 +1,13 @@
require 'rubygems'
require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo

t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)

puts t.ext(:add, 1, 123); sleep 1
puts t.ext(:add, 1, 253); sleep 1
puts t.ext(:add, 1, 343); sleep 1
puts t.ext(:add, 2, 123); sleep 1

puts t.ext(:list, 1, '')

t.close

0 comments on commit f3db0dc

Please sign in to comment.