Tokyo Tyrant HTTP Cheatsheet

Made this today super quick in Markdown while working, hope it helps.

Tokyo Tyrant – HTTP Compatible Protocol – Cheatsheet

A condensed version of http://fallabs.com/tokyotyrant/spex.html for those concerned with HTTP only.

Examples – using curl, not Perl

# Create the initial data
curl -v -X PUT -d "first" 'http://localhost:1978/one'
curl -v -X PUT -d "second" 'http://localhost:1978/two'
curl -v -X PUT -d "third" 'http://localhost:1978/three'

# Check that it's there
curl -v -X GET 'http://localhost:1978/one'

# Iterate over the elements
curl -v -X POST -H "X-TT-MNAME:iterinit" 'http://localhost:1978/'
curl -v -X POST -H "X-TT-MNAME:iternext" 'http://localhost:1978/'
curl -v -X POST -H "X-TT-MNAME:iternext" 'http://localhost:1978/'
curl -v -X POST -H "X-TT-MNAME:iternext" 'http://localhost:1978/'
curl -v -X POST -H "X-TT-MNAME:iternext" 'http://localhost:1978/'

# run a script (assuming you ran ttserver with -ext /usr/share/tokyotyrant/ext/senatus.lua )
curl -v -X POST -H "X-TT-XNAME:echo" -H "X-TT-XOPTS:1" --data-binary "bar" 'http://localhost:1978/foo'

# Delete the elements
curl -v -X DELETE 'http://localhost:1978/one'
curl -v -X DELETE 'http://localhost:1978/two'
curl -v -X DELETE 'http://localhost:1978/three'

# Verify the deletion
curl -v -X GET 'http://localhost:1978/one'

# Sniff your server
curl -v -X OPTIONS 'http://localhost:1978'

Description

As for the HTTP (1.1) compatible protocol, the server implements the following commands; “GET” (relevant to tcrdbget), “HEAD” (relevant to tcrdbvsiz), “PUT” (relevant to tcrdbput), “POST” (relevant to tcrdbext), “DELETE” (relevant to tcrdbout), and “OPTIONS” (relevant to tcrdbstat). The URI of each request is treated as the key encoded by the URL encoding. And the entity body is treated as the value. However, headers except for “Connection” and “Content-Length” are ignored. “PUT” can have the header “X-TT-PDMODE” whose value is either of 1 (relevant to tcrdbputkeep), 2 (relevant to tcrdbputcat), or else (relevant to tcrdbput).

“POST” should have one of the header “X-TT-XNAME” or the header “X-TT-MNAME”. “X-TT-XNAME” is relevant to tcrdbext and specifies the function name. The header “X-TT-XOPTS” stands for bitwise-or options of 1 (record locking) and 2 (global locking). The URI of each request is treated as the key encoded by the URL encoding. And the entity body is treated as the value. The result is expressed as the entity body of the response. “X-TT-MNAME” is relevant to tcrdbmisc and specifies the function name. The header “X-TT-MOPTS” stands for bitwise-or options of 1 (omission of the update log). The request parameters are expressed as the entity body in the “application/x-www-form-urlencoded” format. The names are ignored and the values are treated as a list of the parameters. The result is expressed as the entity body of the response in the “application/x-www-form-urlencoded” format.

tcrdbget – GET

The function tcrdbget is used in order to retrieve a record in a remote database object.

void tcrdbget(TCRDB rdb, const void kbuf, int ksiz, int sp);
rdb specifies the remote database object.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
sp specifies the pointer to the variable into which the size of the region of the return value is assigned.
If successful, the return value is the pointer to the region of the value of the corresponding record. NULL is returned if no record corresponds.
Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the malloc call, it should be released with the free call when it is no longer in use.

tcrdbvsiz – HEAD

The function tcrdbvsiz is used in order to get the size of the value of a record in a remote database object.

int tcrdbvsiz(TCRDB rdb, const void kbuf, int ksiz);
rdb specifies the remote database object.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
If successful, the return value is the size of the value of the corresponding record, else, it is -1.

tcrdbput – PUT

The function tcrdbput is used in order to store a record into a remote database object.

bool tcrdbput(TCRDB rdb, const void kbuf, int ksiz, const void *vbuf, int vsiz);
rdb specifies the remote database object.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
vbuf specifies the pointer to the region of the value.
vsiz specifies the size of the region of the value.
If successful, the return value is true, else, it is false.
If a record with the same key exists in the database, it is overwritten.

tcrdbext – POST X-TT-XNAME function_name X-TT-XOPTS bitmask

The function tcrdbext is used in order to call a function of the script language extension.

void tcrdbext(TCRDB rdb, const char name, int opts, const void kbuf, int ksiz, const void vbuf, int vsiz, int sp);
rdb specifies the remote database object.
name specifies the function name.
opts specifies options by bitwise-or: RDBXOLCKREC for record locking, RDBXOLCKGLB for global locking.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
vbuf specifies the pointer to the region of the value.
vsiz specifies the size of the region of the value.
sp specifies the pointer to the variable into which the size of the region of the return value is assigned.
If successful, the return value is the pointer to the region of the value of the response. NULL is returned on failure.
Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the malloc call, it should be released with the free call when it is no longer in use.

tcrdbout – DELETE

The function tcrdbout is used in order to remove a record of a remote database object.

bool tcrdbout(TCRDB rdb, const void kbuf, int ksiz);
rdb specifies the remote database object.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
If successful, the return value is true, else, it is false.

tcrdbstat – OPTIONS

The function tcrdbstat is used in order to get the status string of the database of a remote database object.

char tcrdbstat(TCRDB rdb);
rdb specifies the remote database object.
The return value is the status message of the database or NULL if the object does not connect to any database server. The message format is TSV. The first field of each line means the parameter name and the second field means the value.
Because the region of the return value is allocated with the malloc call, it should be released with the free call when it is no longer in use.

tcrdbputkeep – PUT X-TT-PDMODE 1

The function tcrdbputkeep is used in order to store a new record into a remote database object.

bool tcrdbputkeep(TCRDB rdb, const void kbuf, int ksiz, const void *vbuf, int vsiz);
rdb specifies the remote database object.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
vbuf specifies the pointer to the region of the value.
vsiz specifies the size of the region of the value.
If successful, the return value is true, else, it is false.
If a record with the same key exists in the database, this function has no effect.

tcrdbputcat – PUT X-TT-PDMODE 2

The function tcrdbputcat is used in order to concatenate a value at the end of the existing record in a remote database object.

bool tcrdbputcat(TCRDB rdb, const void kbuf, int ksiz, const void *vbuf, int vsiz);
rdb specifies the remote database object.
kbuf specifies the pointer to the region of the key.
ksiz specifies the size of the region of the key.
vbuf specifies the pointer to the region of the value.
vsiz specifies the size of the region of the value.
If successful, the return value is true, else, it is false.
If there is no corresponding record, a new record is created.

tcrdbmisc – POST X-TT-MNAME function_name X-TT-MOPTS bitmask application/x-www-form-urlencoded

The function tcrdbmisc is used in order to call a versatile function for miscellaneous operations of a remote database object.

TCLIST tcrdbmisc(TCRDB rdb, const char name, int opts, const TCLIST args);
rdb specifies the remote database object.
name specifies the name of the function. All databases support “put”, “out”, “get”, “putlist”, “outlist”, and “getlist”. “put” is to store a record. It receives a key and a value, and returns an empty list. “out” is to remove a record. It receives a key, and returns an empty list. “get” is to retrieve a record. It receives a key, and returns a list of the values. “putlist” is to store records. It receives keys and values one after the other, and returns an empty list. “outlist” is to remove records. It receives keys, and returns an empty list. “getlist” is to retrieve records. It receives keys, and returns keys and values of corresponding records one after the other.
opts specifies options by bitwise-or: RDBMONOULOG for omission of the update log.
args specifies a list object containing arguments.
If successful, the return value is a list object of the result. NULL is returned on failure.
Because the object of the return value is created with the function tclistnew, it should be deleted with the function `tclistdel’ when it is no longer in use.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s