{"id":561,"date":"2010-08-11T23:12:04","date_gmt":"2010-08-11T23:12:04","guid":{"rendered":"http:\/\/brej.org\/blog\/?p=561"},"modified":"2010-08-12T09:16:55","modified_gmt":"2010-08-12T09:16:55","slug":"tidbit-more-technical","status":"publish","type":"post","link":"https:\/\/brej.org\/blog\/?p=561","title":{"rendered":"Tidbit: More technical"},"content":{"rendered":"<p>In the <a href=\"http:\/\/brej.org\/blog\/?p=540\">previous post<\/a> I covered the ideas behind tidbit. In this post I will try and cover the technical aspects of the tidbit system. Currently the work is very exploratory, so everything may change.<\/p>\n<h2>Tidbit record structure<\/h2>\n<p>This is a typical tidbit which was generated using the Rhythmbox plugin:<\/p>\n<pre><span style=\"color: #0000ff;\">TIDBIT\/0.1; libtidbit\/0.1; Rhythmbox Tidbit Plugin v0.1<\/span>\r\n<span style=\"color: #800000;\">tidbit_userkey==usePzEg4Cl4g1ASdzpssVHtQ1hJJilS+ryiBWjF...\r\ntidbit_table==audio\/track\r\ntidbit_created:=1281479640\r\ntidbit_expires:=1313037240<\/span>\r\nartist==Arcade Fire\r\ntitle==Keep The Car Running\r\nalbum:=Indie\/Rock Playlist: May (2007)\r\ngenre:=Indie\r\nyear:=2007\r\nplay_count:=34\r\nrating:=0.8\r\n<span style=\"color: #800000;\">tidbit_signed:=JyJ1fIwhRL5t3y9CACmshm\/UibYVhvInxh7XVx4...<\/span><\/pre>\n<p>The first line is the header. It states the version of the tidbit followed by the user agent. The rest of the record is composed of key-value pairs. The key has a strict format of lower-case letters and underscore. The value can contain any character above 0x1F, and is terminated by a new line. Other characters must be escaped. The first\u00c2\u00a0 four pairs are compulsory and they all contain &#8220;tidbit_&#8221; at the start to distinguish them from normal data. The userkey is a unique(ish) 1024 bit RSA key the user uses to identify themselves and also serves as the public portion of their signing key. It is base64 encoded and in the text above it is clipped but in reality it is over 100 characters long. Table is compulsory field which designates the subject matter. The created and expires values state when the record was created (must be in the past) and when it will expire. Expired records are no longer valid. These are currently using Unix time, but a more general format will be used in the future. This is followed by a number of values specific to the record type. Finally, the record is completed by a signature which signs the body up to that point (also base64 encoded). The signature is generated using the user key which signs an SHA512 hash of the record (up to that line). There is a hard limit of 2KB per record to prevent abuse.<\/p>\n<p>The separation between the key and the value is either &#8216;==&#8217; or &#8216;:=&#8217;. These signify if to search for that value, or overwrite the value. When inserting a new record, a search is performed for any records which match all the key\/value pairs with the &#8216;==&#8217; separator. These records are discarded as they are overwritten by the new record. To ensure the correct sequence in cases where an old record is re inserted into the database, the created date is checked. This allows a record to be updated by destroying an older version.<\/p>\n<h2>Library<\/h2>\n<p>A library (libtidbit) handles most of the complexity of creating tidbits, key handling, communicating with databases and performing queries. Keys are stored in a gnome-keyring. There are also python bindings which make creating plugins simple. Here is partial mock-up of an example use in Rhythmbox:<\/p>\n<p><a href=\"http:\/\/brej.org\/blog\/wp-content\/uploads\/2010\/08\/tidbit_rhythmbox.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-562\" title=\"tidbit_rhythmbox\" src=\"http:\/\/brej.org\/blog\/wp-content\/uploads\/2010\/08\/tidbit_rhythmbox-300x136.png\" alt=\"\" width=\"300\" height=\"136\" srcset=\"https:\/\/brej.org\/blog\/wp-content\/uploads\/2010\/08\/tidbit_rhythmbox-300x136.png 300w, https:\/\/brej.org\/blog\/wp-content\/uploads\/2010\/08\/tidbit_rhythmbox-1024x466.png 1024w, https:\/\/brej.org\/blog\/wp-content\/uploads\/2010\/08\/tidbit_rhythmbox.png 1630w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>In this plugin, forming tidbits and passing the out is very easy. Presenting the data is the hard part.<\/p>\n<h2>Databases<\/h2>\n<p>There are several database backends used in tidbit:<\/p>\n<ul>\n<li><strong>Memory database<\/strong> is used to cache recently accessed records.<\/li>\n<li><strong>Fork database<\/strong> is not a real database but rather a connection to two, which fetches records from the local database to minimise long distance transactions.<\/li>\n<li><strong>D-Bus database<\/strong> is a service which allows several applications to share a single cache, and minimise external accesses.<\/li>\n<li><strong>HTTP database<\/strong> is the method used for long distance transactions with the global servers.<\/li>\n<li><strong>Sqlite database<\/strong> allows cached records to be saved between sessions.<\/li>\n<\/ul>\n<p>The default database supplied for libtidbit access is a caching fork of a memory database and a D-Bus connection. The D-Bus service wakes up automatically to connect the applications to the global servers.<\/p>\n<p>There are just three database commands at the moment:<\/p>\n<ul>\n<li><strong>Insert<\/strong> to push new tidbits into the system<\/li>\n<li><strong>Query<\/strong> to ask for tidbit GUIDs which match a query<\/li>\n<li><strong>Fetch<\/strong> to get the full record from a GUID<\/li>\n<\/ul>\n<p>The GUID is actually the signature and is unique(ish) to each record.<\/p>\n<h2>Example<\/h2>\n<p>Lets do a 2 minute into of how to create and post a tidbit for a fictional TV application. The following should be the same in both C and Python (although C requires types).<\/p>\n<h3>Step 1: Get a key<\/h3>\n<pre>key = tidbit_key_get (\"mytv\", \"MyTV v1.2\");<\/pre>\n<p>Here we supply the name of out application twice. The first should never change so we pick up the same key each time, and the second is used for the user agent.<\/p>\n<h3>Step 2: Get a database<\/h3>\n<pre>database = tidbit_database_default_new ();<\/pre>\n<p>This gets the default database on the system.<\/p>\n<h3>Step 3: Create the record<\/h3>\n<pre>record = tidbit_record_new (\"television\/episode\");<\/pre>\n<p>This creates a new record we can put data into. The table name is compulsory so we supply it here.<\/p>\n<h3>Step 4: Add the data<\/h3>\n<pre>tidbit_record_add_element (record, \"series_name\", \"Ugly Betty\", TIDBIT_RECORD_ELEMENT_TYPE_KEY);\r\ntidbit_record_add_element (record, \"episode_name\", \"The Butterfly Effect (Part 1)\", TIDBIT_RECORD_ELEMENT_TYPE_KEY);\r\ntidbit_record_add_element (record, \"rating\", \"0.6\", TIDBIT_RECORD_ELEMENT_TYPE_VALUE);\r\n<\/pre>\n<p>Note the difference between the key and value entries (as the &#8216;==&#8217; and &#8216;:=&#8217; before). We may change our rating later, so that is a value, and so overwrite the records which match on the keys.<\/p>\n<h3>Step 5: Sign the record<\/h3>\n<pre>tidbit_record_sign (record, key);\r\n<\/pre>\n<p>Once a record is signed, it cannot be altered.<\/p>\n<h3>Step 6: Insert it into the database<\/h3>\n<pre>tidbit_database_insert (database, record);\r\n<\/pre>\n<h3>Step 7: Tidy up<\/h3>\n<pre>tidbit_record_unref (record);<\/pre>\n<p>Now we are finished with this record, we free it. By now, the record is happily on its way around the world.<\/p>\n<h2>Development<\/h2>\n<p>If you have interests in the semantic web\/distributed hashtables, you have an idea for an awesome application, you found a fundamental error or you just want to have a bit of a play, then the <a href=\"http:\/\/cgit.freedesktop.org\/~cbrej\/tidbit\">source is available<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post I covered the ideas behind tidbit. In this post I will try and cover the technical aspects of the tidbit system. Currently the work is very exploratory, so everything may change. Tidbit record structure This is a typical tidbit which was generated using the Rhythmbox plugin: TIDBIT\/0.1; libtidbit\/0.1; Rhythmbox Tidbit Plugin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2,21],"tags":[],"_links":{"self":[{"href":"https:\/\/brej.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561"}],"collection":[{"href":"https:\/\/brej.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brej.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brej.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brej.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=561"}],"version-history":[{"count":5,"href":"https:\/\/brej.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/brej.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/brej.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brej.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brej.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}