<?xml version="1.0" encoding="utf-8"?> 
<rss version="2.0">
 <channel>
  <title>SHRIPHANI PALAKODETY: Posts tagged 'ffi'</title>
  <description>SHRIPHANI PALAKODETY: Posts tagged 'ffi'</description>
  <link>http://blog.shriphani.com/tags/ffi.html</link>
  <lastBuildDate>Tue, 27 Aug 2013 05:12:00 UT</lastBuildDate>
  <pubDate>Tue, 27 Aug 2013 05:12:00 UT</pubDate>
  <ttl>1800</ttl>
  <item>
   <title>Racket Whistlepig Bindings</title>
   <link>http://blog.shriphani.com/2013/08/27/racket-whistlepig-bindings/?utm_source=ffi&amp;utm_medium=RSS</link>
   <guid>urn:http-blog-shriphani-com:-2013-08-27-racket-whistlepig-bindings</guid>
   <pubDate>Tue, 27 Aug 2013 05:12:00 UT</pubDate>
   <description>&lt;html&gt;
&lt;p&gt;Whistlepig is a lightweight real-time search engine written in ANSI C. (&lt;a href="http://masanjin.net/whistlepig/"&gt;description&lt;/a&gt; and &lt;a href="https://github.com/wmorgan/whistlepig"&gt;source&lt;/a&gt;) I heard about it when Don Metzler plugged it in an answer he wrote on quora. In this post, with very little code, I was able to build an index, query it and write a servlet that talks to the index using the FFI.&lt;/p&gt;
&lt;!-- more--&gt;

&lt;p&gt;Whistlepig is implemented in less than 3000 lines of ANSI C. It comes with a very decent query language and returns documents (that match a query) sorted by their time-of-insertion (into the index).&lt;/p&gt;

&lt;p&gt;For using the FFI, we need a shared library. I had to modify the Makefile slightly to make it compile on OS X (you can download this from my fork &lt;a href="https://github.com/shriphani/whistlepig/tree/osx_compile"&gt;here&lt;/a&gt;). I was able to build it on Linux with ease though. You can build the &lt;code&gt;.so&lt;/code&gt; (&lt;code&gt;.dylib&lt;/code&gt;) file using the following command:&lt;/p&gt;

&lt;script src="https://gist.github.com/shriphani/6355004.js"&gt;&lt;/script&gt;

&lt;p&gt;On OS X, you will need to replace the &lt;code&gt;.so&lt;/code&gt; with &lt;code&gt;.dylib&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In order for the generated &lt;code&gt;libwhistlepig.so&lt;/code&gt; file to be picked up the Racket FFI, you will need to add the directory where it resides to the environment variable &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we need to use the FFI and write racket functions that call the corresponding Whistlepig routines. This file is sufficient to wrap around all this routines we will need to use.&lt;/p&gt;

&lt;script src="https://gist.github.com/shriphani/6350939.js"&gt;&lt;/script&gt;

&lt;p&gt;Let us now test our implementation. Whistlepig itself ships with two programs: &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;query&lt;/code&gt;. &lt;code&gt;add&lt;/code&gt; adds a bunch of files specified on the command line to a new index. We can replicate the functionality in &lt;code&gt;add.rkt&lt;/code&gt;:&lt;/p&gt;

&lt;script src="https://gist.github.com/shriphani/6350956.js"&gt;&lt;/script&gt;

&lt;p&gt;&lt;code&gt;interactive.rkt&lt;/code&gt; takes and index location and interactively runs queries against it and returns doc-ids. This is &lt;code&gt;interactive.rkt&lt;/code&gt;.&lt;/p&gt;

&lt;script src="https://gist.github.com/shriphani/6350962.js"&gt;&lt;/script&gt;

&lt;p&gt;Now, the next step is obviously very straightforward. I wanted a quick way to get set up and running. We don&amp;rsquo;t have things like stored-fields in Lucene so, we need an external map to doc-ids. I am using a file that contains a list of &lt;code&gt;s-expressions&lt;/code&gt; that look like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(doc-id doc-path doc-title doc-link)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On my laptop (not accessible from the public web), the file looks like:&lt;/p&gt;

&lt;script src="https://gist.github.com/shriphani/6358032.js"&gt;&lt;/script&gt;

&lt;p&gt;So, our search engine will start, load the documents and add them to the index in the order specified.&lt;/p&gt;

&lt;p&gt;When a query comes along, we get the doc-ids from the engine and line them up with the document title (and thus we get the doc-link that can be rendered).&lt;/p&gt;

&lt;p&gt;We accept queries using a URL of the form &lt;code&gt;http://domain/search?q=query&lt;/code&gt;. This tiny servlet accomplishes that:&lt;/p&gt;

&lt;script src="https://gist.github.com/shriphani/6358046.js"&gt;&lt;/script&gt;

&lt;p&gt;You can try it out here : &lt;a href="http://blog.shriphani.com/search?q=hello"&gt;http://blog.shriphani.com/search?q=hello&lt;/a&gt;. It is very clunky and only indexes my blog&amp;rsquo;s post pages. . It won&amp;rsquo;t generate snippets (the postings list doesn&amp;rsquo;t store token positions).&lt;/p&gt;

&lt;p&gt;To make this really work, I will need to integrate it with Frog and flesh the UI out. This was something I threw together quickly so I could build on it later. It ended up being a good excuse to continue using the FFI. The whistlepig bindings are quite sparse too and can use some work.&lt;/p&gt;

&lt;p&gt;The full code is available in &lt;a href="https://github.com/shriphani/racket-whistlepig"&gt;this Github repository&lt;/a&gt;.&lt;/p&gt;&lt;/html&gt;</description></item></channel></rss>