Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09


Published on

FutureRuby presentation on extending Tokyo Cabinet with Lua extensions.

GitHub repo with sample code & extensions:

Published in: Technology, Self Improvement
1 Comment
  • Tokyo Cabinet + Lua examples & recipes: http://github.com/igrigorik/tokyo-recipes/tree/master
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • So what is Tokyo Cabinet? Not surprisingly, as the name implies Tokyo Cabinet is a software project that was started in Japan, and as any Rubyist knows, Japanese developers have made some amazing contributions. Case in point: Ruby and Matz. Started as a research project back in ’93, it made its way to North America in around 2001 and 2002, and the rest is history.For that reason, when I stumbled across Tokyo Cabinet, I decided to do some digging. Turns out, the project is a brainchild of one developer.. and facebook to the rescue to help us put a face to the project. And I don’t know about you, but when I saw that photo.. I had one association pop up..
  • Yeah? What do you think? In fact, I think the analogy may be well suited because Tokyo Cabinet has all the potential to make it big in the database world.
  • When we talk about TC, we are actually referring to three distinct products under the umbrella: - cabinet – a set of database management routines - tyrant – a standalone server implementation around cabinet - dystopia – full text search engine built on top of tokyo cabinetAll of the projects are written in very clean, easy to read, and well documented C, and released under LGPL. For the purposes of this talk, we’ll first focus on tokyo cabinet and work through some of the basics and then move on to tyrant, which is where we’ll spend most of the time. I’m not going to talk about Dystopia, but I’d encourage you to check it out and play with it.
  • QDBM is/was a library of routines for managing a database. - 2000 – 2007Tokyocabinet is a successor to QDBM.
  • - Thread-safeRow level lockingSupports many different data layouts as we will see shortlyFull ACID supportHas bindings to all the most popular languages, which is pretty important considering this is an embedded database
  • TC has many different engines, which if you’re ever used MySQL is exactly the same as deciding between MyISAM or InnoDB, or any other engine. Each one has it’s advantages and the right choice really depends on your data and performance requirements. The bread and butter is the hashtable, which is just a key value store like BDB and many other database. It’s fast, really fast.B-Tree table is similar to the hash table, but because of the supported data layout it actually allows storage of duplicate keys, and iteration over that data.Fixed-length is a no frills array. It’s probably the fastest engine there is, just because of it’s simplicity, but you certainly give up a lot of functionality as well.Last but not least, the Table engine is one of the most interesting features of TC. It is essentially a schemaless document store with support for arbitrary indexing and query capabilities. Let’s take a look at a few examples…
  • We’re using tokyo cabinet via rufus/tokyo gem, which means we’re building an embedded database. There are two good alternatives for interfacing with TC from Ruby. There is a native gem provided by the author, and then there is the rufus/tokyo gem which I’m using here which is built via FFI which means you can this library from Jruby, MRI, Rubinius and so forth. It is definitely a little bit slower, but I like the syntax it provides a lot more than the native gem.
  • It walks & talks just like a Ruby Hash. Very easy to use.
  • Create a query, order by ‘age’. In this case we didn’t declare any explicit indexes, but the dataset is small so the query is still very fast. If we were working with a larger dataset we would explicitly declare an index either via ruby or when we created the database. Check the docs for details on how to do this.
  • Also, as you would expect, there is full transaction support. For this reason alone, it’s not completely unreasonable to think about using TC in place of Ruby hashes in some of your code. It’s dead simple, and fast.
  • http://www.slideshare.net/rawwell/tokyotalk
  • - High concurrency (multi-thread uses epoll/kqueue) - 3 Protocol Options: Binary, memcached, and HTTP - Hot backup - Update logging - Replication (master/slave, master/master) - Lua extensions
  • Interacting via rest_client.. No rufus/tokyo here!
  • Unfortunately, even though the project is very mature at this point, and is being used in production at mixi, finding discussions or support around the project is a bit of a challenge. Mikio regularly writes on his developer blog at mixi, but even that is in Japanese.. And I’m a big fan of statistical machine translation techniques, but there is definitely a lot of room for improvement… What you see on the slide is a start of one his blog posts of August of last year, in which he goes on to announce… By the way, this is where you should gasp and all jump in joy, because this is huge!
  • Lua" (pronounced LOO-ah) means "Moon" in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun. Lua runs on all flavors of Unix and Windows, and also on mobile devices (such as handheld computers and cell phones that use BREW, Symbian, Pocket PC, etc.) and embedded microprocessors (such as ARM and Rabbit) for applications like Lego MindStorms. Lua is a fast language engine with small footprint that you can embed easily into your application. Lua has a simple and well documented API that allows strong integration with code written in other languages.Lua allows many applications to easily add scripting within a sandbox…
  • So why is Lua + TokyoCabinet so interesting after all? Who’s used MySQL UDFs’? Anyone written one? They’re a pain on both accounts.User-defined functions are compiled as object files and then added to and removed from the server dynamically using the CREATE FUNCTION and DROP FUNCTION statements. You can add functions as native (built-in) MySQL functions. Native functions are compiled into the mysqld server and become available on a permanent basis. Couple of problems: C/C++ plus a very messy internal API. If you get it wrong, you crash the database, so you better get it right. Why use it? Faster then triggers, allows linking against other libraries. For example, there is a very popular memcached library which allows you to interface with memcache right from MySQL. There are number of reasons why you would want to use this, but one great use case is for replicating memcached. That is, use mysql protocol to replay queries and then update your memcached instances in different data centers, etc. This is exactly how Facebook keeps their clusters in sync.
  • TT is a hassle to extend the protocol and implementation. Lua on the server is able to register any function.The "passing as an argument and then returns the results" so that the interface that is common to all database operations, "the method name string, string key, string value to send, running and passing the key value of the name of the method, the return string will be returned "only if a protocol that eliminates the need to define for each protocol.
  • - Lua Extension * defines arbitrary database operations * atomic operation by record locking - define DB operations as Lua functions * clients call each giving functionname and record data * the server returns the return value of the function - options about atomicity * no locking / record locking / global lockinghttp://www.scribd.com/doc/12016121/Tokyo-Cabinet-and-Tokyo-Tyrant-Presentation
  • Enough handwaving, let’s look at the code… A no-op extension which will return the key and value pair without storing anything in the database.The “..” syntax is Lua’sconcat operator for strings.
  • Next, we start the tokyo tyrant server via a command line utility ‘ttserver’ and pass in the –ext parameter with the name of the extension. That’s all you need to start the server with our new extension.After that, we can use the manager utility and invoke some commands. First we specify ext as the command name, which indicates that we’re going to be calling an extension, then the address of the server, then we specify the command, which is “echo” and finally we pass in our key and value. In return, we get the string from TC!
  • Alternatively, we could do the same thing from ruby. Except this time, instead of creating a local database, we’re going to specify an IP address and port. From there, we call the ext method again, pass in the name of the command as a symbol and the parameters. Voila.
  • Ok, so echo is a cute example, but let’s look at something slightly more interesting. We’re going to build an increment command in Lua. Now, TC already has this in it’s API, but we’re going to do this for the same of an example.
  • Redis is a key-value database. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists and sets with atomic operations to push/pop elements.In order to be very fast but at the same time persistent the whole dataset is taken in memory and from time to time and/or when a number of changes to the dataset are performed it is written asynchronously on disk. You may lost the last few queries that is acceptable in many applications but it is as fast as an in memory DB (Redis supports non-blocking master-slave replication in order to solve this problem by redundancy).
  • Do some iteration over keys, and finally append the new value if it’s actually new.
  • In similar fashion, implement delete get and length functions and we have a minimal set of SET operations in TC. Of course, this is not going to be as fast as a native implementation in Redis, but that’s not the point either. The point is the potential extensibility of the database.
  • The extpc argument allows us to specify a function and an interval, in this case 5 seconds which will be executed by the tokyo server. Which means that our cleanup script will be executed every 5 seconds, effectively removing the expired records from the database! Additionally, we’re going to add an index on x, in decreasing order to help speed up this operation.Once again, TC+Lua = memcached? Not quiet. Memcached will perform better because it doesn’t care to sweep it’s memory for expired records, it just lets them hang around and evicts them when it runs of out free memory. But, it’s a great example of scripting your database.
  • Finally, here’s a real use case deployed at mixi and documented by Mikio. They use tyrant as a proxy cache server in front of a tier of their database. They use lua to interface with their cache and fallback to the actual MySQL and Hbase tables on a cache miss or write. This is possible because Lua has bindings for MySQL, which means that the entire cache server layer is built in Lua and runs as Tokyo Tyrant. No need for any extra frills and the different TC engines allow them to customize the cache layout to fit their use case.
  • Another example documented by Mikio is a session trail tracker. Instead of analyzing the logs in offline fashion, they built a system which recorded user’s visit based on their session cookie. In this case, there are two sessions one with ID 1, and second with ID 2. First user visits resources 123, 256, and 987, which are then easily retrieved via the list operation.The source for this is available in my github repo.
  • Back in November of ’08Mikio added another native interface for invoking mapreduce jobs on the database. There is no particular reason for this, as this implementation does not really have the distributed features that make Map-Reduce what it is, but it is a great example of a programming paradigm making the rounds…To get started with this functionality, you will once again need Lua and we’ll have to build just two functions to make it all work, a mapper and a reduce function. Let’s take a look at an example…
  • We’re going to build a wordcount example. That is, we’re going to assume that the values in our database are strings, and we’ll try to get a word frequency count across all the keys. To do that, we’re going to iterate over all the keys, which will be passed to the mapper function repeatedly, and emit a temporary tuple, where each word will have a count of 1.
  • Next, the reducer function takes over. In the background TC aggregates all the results with the same key and passes them into reduce. At this point, we simple count the number of values for each word, and.. we’re done!
  • Start the server, add a few strings, and then execute our MR job. Easy as that.
  • On that note.. Hopefully these examples gave you a taste for how easy it is to extend Tokyo Cabinet, and I would really encourage you to give it a try. All of the examples we went through in the slides are available in my github repo which I created for this talk, as well as a number of other examples.Take a look at the high-low-game, and the inverted-index extensions. Both great examples of what you can do with less than a hundred lines of Lua and TC.
  • Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09

    1. Lean & Mean Tokyo Cabinet Recipes<br />Ilya Grigorik<br />@igrigorik<br />
    2. postrank.com/topic/ruby<br />The slides…<br />Twitter<br />My blog<br />
    3. MikioHirabayashi<br />Yukihiro Matsumoto<br />
    4. MikioHirabayashi<br />Yukihiro Matsumoto<br />???<br />
    5. Hashtable<br />Berkeley DB, DBM, QDB, TDB…<br />B-Tree Table<br />Key-Value with duplicates & ordering<br />3. Fixed-length <br />An in memory array.. No hashing.<br />4. Table Engine<br />Schemaless, indexes & queries<br />Choose your engine<br />
    6. gem install rufus-tokyo<br />require &apos;rubygems&apos;require &apos;rufus/tokyo&apos;db = Rufus::Tokyo::Cabinet.new(&apos;data.tch&apos;)db[&apos;nada&apos;] = &apos;surf&apos;p db[&apos;nada&apos;] # =&gt; &apos;surf&apos;p db[&apos;lost&apos;] # =&gt; nildb.close<br />TC: Hashtable<br />
    7. require &apos;rubygems&apos;require &apos;rufus/tokyo&apos;db = Rufus::Tokyo::Cabinet.new(&apos;data.tch&apos;)db[&apos;nada&apos;] = &apos;surf&apos;p db[&apos;nada&apos;] # =&gt; &apos;surf&apos;p db[&apos;lost&apos;] # =&gt; nildb.close<br />~ Ruby Hash<br />TC: Hashtable<br />
    8. require &apos;rubygems&apos;require &apos;rufus/tokyo&apos;t = Rufus::Tokyo::Table.new(&apos;table.tct&apos;)t[&apos;pk0&apos;] = { &apos;name&apos; =&gt; &apos;alfred&apos;, &apos;age&apos; =&gt; &apos;22&apos; }t[&apos;pk1&apos;] = { &apos;name&apos; =&gt; &apos;bob&apos;, &apos;age&apos; =&gt; &apos;18&apos;, &apos;sex&apos; =&gt; &apos;male&apos; }t[&apos;pk2&apos;] = { &apos;name&apos; =&gt; &apos;charly&apos;, &apos;age&apos; =&gt; &apos;45&apos; }t[&apos;pk4&apos;] = { &apos;name&apos; =&gt; &apos;ephrem&apos;, &apos;age&apos; =&gt; &apos;32&apos; }p t.query { |q|q.add_condition &apos;age&apos;, :numge, &apos;32&apos;q.order_by &apos;age&apos;}# =&gt; [ {&quot;name&quot;=&gt;&quot;ephrem&quot;, :pk=&gt;&quot;pk4&quot;, age&quot;=&gt;&quot;32&quot;},# {&quot;name&quot;=&gt;&quot;charly&quot;, :pk=&gt;&quot;pk2&quot;, &quot;age&quot;=&gt;&quot;45&quot;} ]t.close<br />Table Engine<br />TC: Table Engine<br />
    9. require &apos;rubygems&apos;require &apos;rufus/tokyo&apos;t = Rufus::Tokyo::Table.new(&apos;table.tct&apos;)t[&apos;pk0&apos;] = { &apos;name&apos; =&gt; &apos;alfred&apos;, &apos;age&apos; =&gt; &apos;22&apos; }t[&apos;pk1&apos;] = { &apos;name&apos; =&gt; &apos;bob&apos;, &apos;age&apos; =&gt; &apos;18&apos;, &apos;sex&apos; =&gt; &apos;male&apos; }t[&apos;pk2&apos;] = { &apos;name&apos; =&gt; &apos;charly&apos;, &apos;age&apos; =&gt; &apos;45&apos; }t[&apos;pk4&apos;] = { &apos;name&apos; =&gt; &apos;ephrem&apos;, &apos;age&apos; =&gt; &apos;32&apos; }p t.query { |q|q.add_condition&apos;age&apos;, :numge, &apos;32&apos;q.order_by&apos;age&apos;}# =&gt; [ {&quot;name&quot;=&gt;&quot;ephrem&quot;, :pk=&gt;&quot;pk4&quot;, age&quot;=&gt;&quot;32&quot;},# {&quot;name&quot;=&gt;&quot;charly&quot;, :pk=&gt;&quot;pk2&quot;, &quot;age&quot;=&gt;&quot;45&quot;} ]t.close<br />age &gt; 32 order by age<br />TC: Table Engine<br />
    10. p t.size# =&gt; 0t.transactiondo t[&apos;pk0&apos;] = { &apos;name&apos; =&gt; &apos;alfred&apos;, &apos;age&apos; =&gt; &apos;22&apos; } t[&apos;pk1&apos;] = { &apos;name&apos; =&gt; &apos;bob&apos;, &apos;age&apos; =&gt; &apos;18&apos; }t.abortendp t.size# =&gt; 0<br />Uh oh…<br />TC: Table Engine Transactions<br />
    11. Network<br />Embedded<br />
    12. require &quot;rubygems&quot;require &quot;rest_client&quot;# Interacting with TokyoTyrant via RESTful HTTP!db = RestClient::Resource.new(&quot;http://localhost:1978&quot;)db[&quot;key&quot;].put &quot;value 1&quot; # insert via HTTPdb[&quot;key&quot;].put &quot;value 2&quot; # update via HTTPputs db[&quot;key&quot;].get # get via HTTP# =&gt; &quot;value 2&quot;db[&quot;key&quot;].delete # delete via HTTPputs db[&quot;key&quot;].get rescue RestClient::ResourceNotFound<br />RESTful Tokyo Tyrant<br />
    13. require &quot;rubygems&quot;require &quot;rest_client&quot;# Interacting with TokyoTyrant via HTTP!db = RestClient::Resource.new(&quot;http://localhost:1978&quot;)db[&quot;key&quot;].put &quot;value 1&quot;# insert via HTTPdb[&quot;key&quot;].put &quot;value 2&quot;# update via HTTPputs db[&quot;key&quot;].get # get via HTTP# =&gt; &quot;value 2&quot;db[&quot;key&quot;].delete # delete via HTTPputs db[&quot;key&quot;].get rescueRestClient::ResourceNotFound<br />RESTful Tokyo Tyrant<br />Awesome.<br />
    14. “Recently, I sophisticated Hanami and the Sumida River in a houseboat, I was sad that day and not even a minute yet mikio bloom …”<br />… so I added Lua scripting to Tyrant.<br />http://alpha.mixi.co.jp/blog/?p=236<br />
    15. “Powerful, fast, lightweight, embeddable scripting language”<br /><ul><li> Procedural syntax
    16. Everything is an associatiave array
    17. Dynamically typed
    18. Interpreted bytecode
    19. Garbage collection</li></ul>GZIP(Source + Docs + Examples) = 212 Kb<br />What is Lua?<br />It’s like Ruby.. except it’s not.<br />Fast + Lightweight = Great for embedded apps<br />
    20. +<br />CREATE FUNCTION json_members RETURNS STRING SONAME &apos;lib_mysqludf_json.so&apos;; <br />SELECT json_object(customer_id, first_name) FROM customer;<br />+---------------------------------------------------+<br />| customer |<br />+---------------------------------------------------+<br />| {customer_id:1,first_name:&quot;MARY&quot;} |<br />+---------------------------------------------------+<br />Extending the Database?<br />MySQL User Defined Functions<br />JSON Response<br />http://www.mysqludf.org/lib_mysqludf_json/index.php<br />
    21. = C/C++ <br />+<br />+<br />= Lua<br />TC+Lua? Why?<br />To make our lives easier, and more fun!<br />Easy to learn & easy to extend!<br />
    22. Lua extension within Tokyo Cabinet<br />_put(key, value)<br />_putkeep(key, value)<br />_putcat(key, value)<br />_rnum()_vanish()<br />_mapreduce(mapper, reducer, keys)<br />_out(key)<br />_get(key)<br />_vsiz(key)<br />_addint(key, value)<br />TC + Lua Extensions <br />Request / Response data-flow<br />http://tokyocabinet.sourceforge.net/tyrantdoc/#luaext<br />
    23. -- -- echo.lua-- function echo(key, value)return key .. &quot;:&quot; .. valueend<br />[ilya@igvita] &gt;ttserver-ext echo.lua test.tch <br />[ilya@igvita] &gt;tcrmgrextlocalhostechofoo bar <br />foo:bar<br />require &apos;rubygems&apos;require &apos;rufus/tokyo/tyrant&apos; # sudo gem install rufus-tokyot = Rufus::Tokyo::Tyrant.new(&apos;;, 1978)puts t.ext(:echo, &apos;hello&apos;, &apos;world&apos;)t.close<br />Lua + TC Echo Server<br />
    24. -- -- echo.lua-- function echo(key, value)return key .. &quot;:&quot; .. valueend<br />[ilya@igvita] &gt;ttserver-ext echo.lua test.tch <br />[ilya@igvita] &gt;tcrmgrextlocalhostechofoo bar <br />foo:bar<br />require &apos;rubygems&apos;require &apos;rufus/tokyo/tyrant&apos; # sudo gem install rufus-tokyot = Rufus::Tokyo::Tyrant.new(&apos;;, 1978)puts t.ext(:echo, &apos;hello&apos;, &apos;world&apos;)t.close<br />Lua + TC Echo Server<br />
    25. -- -- echo.lua-- function echo(key, value)return key .. &quot;:&quot; .. valueend<br />[ilya@igvita] &gt;ttserver-ext echo.lua test.tch <br />[ilya@igvita] &gt;tcrmgrextlocalhostechofoo bar <br />foo:bar<br />require &apos;rubygems&apos;require &apos;rufus/tokyo/tyrant&apos;# sudo gem install rufus-tokyot = Rufus::Tokyo::Tyrant.new(&apos;;, 1978)puts t.ext(:echo, &apos;hello&apos;, &apos;world&apos;)t.close<br />Lua + TC Echo Server<br />
    26. -- -- incr.lua-- function incr (key, i)i = tonumber(i)ifnotithenreturnnilend local old = tonumber(_get(key)) if old theni = old + i end if not _put(key, i) then return nil end return iend<br />Verify input<br />Implementing INCR in Lua+TC<br />
    27. -- -- incr.lua-- function incr (key, i)i = tonumber(i)ifnotithenreturnnilend local old = tonumber(_get(key))if old theni = old + iend if not _put(key, i) then return nil end return iend<br />Get old value & increment it<br />Implementing INCR in Lua+TC<br />
    28. -- -- incr.lua-- function incr (key, i)i = tonumber(i)ifnotithenreturnnilend local old = tonumber(_get(key))if old theni = old + iendifnot _put(key, i) thenreturnnilendreturniend<br />Save new value<br />Implementing INCR in Lua+TC<br />
    29. [ilya@igvita] &gt;ttserver-ext incr.lua test.tch <br />[ilya@igvita] &gt;tcrmgrextlocalhostincrkeyname 1<br /> 1<br />[ilya@igvita] &gt;tcrmgrextlocalhostincrkeyname 5<br /> 6<br />require &apos;rubygems&apos;require &apos;rufus/tokyo/tyrant&apos; # sudo gem install rufus-tokyot = Rufus::Tokyo::Tyrant.new(&apos;;, 1978)5.times do puts t.ext(:incr, &apos;my-counter&apos;, 2).to_iendt.close<br />Implementing INCR in Lua+TC<br />
    30. [ilya@igvita] &gt;ttserver-ext incr.lua test.tch <br />[ilya@igvita] &gt;tcrmgrextlocalhostincrkeyname 1<br /> 1<br />[ilya@igvita] &gt;tcrmgrextlocalhostincrkeyname 5<br /> 6<br />require &apos;rubygems&apos;require &apos;rufus/tokyo/tyrant&apos;# sudo gem install rufus-tokyot = Rufus::Tokyo::Tyrant.new(&apos;;, 1978)5.times do puts t.ext(:incr, &apos;my-counter&apos;, 2).to_iendt.close<br />Implementing INCR in Lua+TC<br />
    31. Lua + TC = Database Kung-fu<br />TTL, Sets & Caching<br />
    32. “Redis as a data structures server, it is not just another key-value DB”<br />
    33. functionset_append(key, value) local stream = _get(key)ifnot stream then _put(key, value)else local set_len = _set_len(stream) if set_len == 1 then if stream == value then return nil endelseifset_len &gt; 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 endendreturn valueend<br />Empty Set<br />Implementing Set operations in TC<br />
    34. functionset_append(key, value) local stream = _get(key)ifnot stream then _put(key, value)else local set_len = _set_len(stream)ifset_len == 1 thenif stream == value thenreturnnilendelseifset_len &gt; 1 thenfor _, element inipairs(_split(stream, SEP)) doif element == value thenreturnnilendendendifnot _putcat(key, SEP .. value) thenreturnnilendendreturn valueend<br />Append key if unique<br />Implementing Set operations in TC<br />
    35. = ?<br />+<br />[ilya@igvita] &gt;ttserver-ext set.lua test.tch <br />[ilya@igvita] &gt;tcrmgrextlocalhostset_append key 1<br />[ilya@igvita] &gt;tcrmgrextlocalhostset_appendkey 2<br />[ilya@igvita] &gt;tcrmgrextlocalhostset_append key 1<br />[ilya@igvita] &gt;tcrmgrextlocalhostset_getkey<br /> 1<br /> 2<br />set_length<br />set_get<br />set_delete<br />set_append<br />Implementing Set operations in TC<br />
    36. “memcachedis a general-purpose distributed memory caching system that is used by many top sites on the internet”<br /> Key Value Time <br />key1<br />value1<br />10<br />key2<br />value2<br />20<br />Time = 15<br />key2<br />value2<br />30<br />Implementing TTL’s in TC<br />
    37. DELETE where x &gt; Time.now<br />function expire() local args = {} local cdate = string.format(&quot;%d&quot;, _time())table.insert(args, &quot;addcondxNUMLE&quot; .. cdate)table.insert(args, &quot;out&quot;) local res = _misc(&quot;search&quot;, args)ifnot res then _log(&quot;expiration was failed&quot;)end print(&quot;rnum=&quot; .. _rnum() .. &quot; size=&quot; .. _size())end<br />Expiring Records with Lua<br />
    38. = ?<br />+<br />[ilya@igvita] &gt;ttserver-ext expire.lua -extpc expire 5 &quot;casket.tct#idx=x:dec&quot; <br />Invoke “expire” command every 5 seconds<br />Table database, with index on expiry column (x)<br />Implementing Set operations in TC<br />
    39. [ilya@igvita] &gt;ttserver -ext session-trail.lua test.tch<br />[ilya@igvita] &gt;tcrmgrextlocalhostadd 1 123<br />[ilya@igvita] &gt;tcrmgrextlocalhostadd 1 256<br />[ilya@igvita] &gt;tcrmgrextlocalhostadd 1 987<br />[ilya@igvita] &gt;tcrmgrextlocalhostadd 2 987<br />[ilya@igvita] &gt;tcrmgrextlocalhostlist 1<br />987 1247008220<br /> 256 1247008216<br /> 123 1247008123 <br />Session-trail with Lua<br />Timestamped session trail<br />
    40. Lua + TC = Map Reduce!<br />Just for kicks.<br />
    41. _out(key)<br />_get(key)<br />_vsiz(key)<br />_addint(key, value)<br />_mapreduce(mapper, reducer, keys)<br />Executing MR jobs within Tokyo Cabinet<br />
    42. functionwordcount()functionmapper(key, value, mapemit)for word instring.gmatch(string.lower(value), &quot;%w+&quot;) domapemit(word, 1)endreturntrueendlocal res = &quot;&quot;functionreducer(key, values) res = res .. key .. &quot; &quot; .. #values .. &quot; &quot; return true end if not _mapreduce(mapper, reducer) then res = nil end return resend<br />Emit: {word: 1} <br />Map-Reduce within Tokyo Cabinet<br />
    43. functionwordcount()functionmapper(key, value, mapemit)for word instring.gmatch(string.lower(value), &quot;%w+&quot;) domapemit(word, 1)endreturntrueend local res = &quot;&quot;functionreducer(key, values) res = res .. key .. &quot; &quot; .. #values .. &quot; &quot;returntrueendifnot _mapreduce(mapper, reducer) then res = nilendreturn resend<br />Emit: {word: 1} <br />sizeof(values)<br />Map-Reduce within Tokyo Cabinet<br />
    44. [ilya@igvita] &gt;ttserver-ext wordcount.lua test.tch<br />[ilya@igvita] &gt;tcrmgrputlocalhost1 “This is a pen.“<br />[ilya@igvita] &gt;tcrmgrputlocalhost1 “Hello World“<br />[ilya@igvita] &gt;tcrmgrputlocalhost1 “Life is good“ <br />[ilya@igvita] &gt;tcrmgrextlocalhostwordcount<br /> a 1<br /> good 1<br /> is 2<br /> life 1<br /> pen 1<br /> this 1<br />Execute Map-Reduce Job<br />Map-Reduce within Tokyo Cabinet<br />
    45. github.com/igrigorik/tokyo-recipes<br />The slides…<br />Twitter<br />My blog<br />