<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>So I decided to take my work back underground &#187; Ocaml</title>
	<atom:link href="http://gaiustech.wordpress.com/category/ocaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://gaiustech.wordpress.com</link>
	<description>To stop it falling into the wrong hands</description>
	<lastBuildDate>Sat, 13 Apr 2013 19:41:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gaiustech.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/0a1f076fa7fc2f9e2944ef7437e6a709?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>So I decided to take my work back underground &#187; Ocaml</title>
		<link>http://gaiustech.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gaiustech.wordpress.com/osd.xml" title="So I decided to take my work back underground" />
	<atom:link rel='hub' href='http://gaiustech.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Quick histograms</title>
		<link>http://gaiustech.wordpress.com/2012/06/15/quick-histograms/</link>
		<comments>http://gaiustech.wordpress.com/2012/06/15/quick-histograms/#comments</comments>
		<pubDate>Fri, 15 Jun 2012 20:35:48 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[OCIML]]></category>
		<category><![CDATA[Operation Foothold]]></category>
		<category><![CDATA[Random thoughts]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=2122</guid>
		<description><![CDATA[Having come back to actively working on OCI*ML recently, it&#8217;s time I cracked on with some more features (I have been promising LOBs for a long time, sorry to anyone who&#8217;s still waiting). Just to get warmed up, inspired by &#8230; <a href="http://gaiustech.wordpress.com/2012/06/15/quick-histograms/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=2122&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Having come back to actively working on <a href="http://gaiustech.github.com/ociml/">OCI*ML</a> recently, it&#8217;s time I cracked on with some more features (I have been promising <a href="http://docs.oracle.com/cd/B28359_01/appdev.111/b28393/adlob_intro.htm#ADLOB001">LOBs</a> for a long time, sorry to anyone who&#8217;s still waiting). Just to get warmed up, inspired by <a href="http://github.com/holman/spark">spark</a> I have added a quick histogram function, similar to <a href="http://gaiustech.wordpress.com/2011/05/14/ocaml-as-a-sqlplus-replacement/">quick query</a> for interactive use. This requires a query of the form of a label and a number, for example a simple view:</p>
<pre class="brush: sql; title: ; wrap-lines: false; notranslate">
SQL&gt; create view v1 as
select object_type, count(1) as howmany from user_objects group by object_type;
</pre>
<p><a href="http://gaiustech.files.wordpress.com/2012/06/qh.png"><img class="aligncenter size-full wp-image-2124" title="qh" src="http://gaiustech.files.wordpress.com/2012/06/qh.png?w=640&#038;h=206" alt="" width="640" height="206" /></a><br />
The histogram automatically <a href="http://pleac.sourceforge.net/pleac_ocaml/userinterfaces.html">scales</a> to the width of the current window.</p>
<p>Also, I have been reading Jordan Mechner&#8217;s book <em><a href="http://www.amazon.co.uk/The-Making-Prince-Persia-ebook/dp/B005WUE6Q2/ref=sr_1_1?s=digital-text&amp;ie=UTF8&amp;qid=1333205986&amp;sr=1-1">The Making Of Prince Of Persia</a></em>&dagger;. It&#8217;s both fascinating and inspiring. Just before that, I read <em><a href="http://www.amazon.co.uk/Future-Here-Platform-Studies-ebook/dp/B007V5BVJG/ref=sr_1_1?s=digital-text&amp;ie=UTF8&amp;qid=1339795151&amp;sr=1-1">The Future Was Here</a></em>, the story of the Commodore Amiga&Dagger;. The book is made even more poignant by my Mac inexplicably showing the beach ball as I scroll through a simple web page, or the mighty RHEL servers at work being unable to keep up with my typing. The future is <em>still</em> back in the 80s.</p>
<p>&dagger; The <a href="http://github.com/jmechner/Prince-of-Persia-Apple-II">original code</a> is also on Github.</p>
<p>&Dagger; I have an <a href="http://en.wikipedia.org/wiki/Amiga_500_Plus">A500+</a> on my desk right now, the best of them IMHO. I might write a post comparing it with the <a href="http://en.wikipedia.org/wiki/Atari_STE#STE_models">Atari STE</a>, and the <a href="http://gaiustech.wordpress.com/2012/01/04/happy-new-year-2/">BBC</a> with the C64, in the cold light of day as an experienced adult. I have a fine collection of classic machines now, often acquired broken with the intention of repairing them myself. Another time-sink from OCaml work&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/2122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/2122/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=2122&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2012/06/15/quick-histograms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2012/06/qh.png" medium="image">
			<media:title type="html">qh</media:title>
		</media:content>
	</item>
		<item>
		<title>OCaml 4 beta</title>
		<link>http://gaiustech.wordpress.com/2012/06/07/ocaml-4-beta/</link>
		<comments>http://gaiustech.wordpress.com/2012/06/07/ocaml-4-beta/#comments</comments>
		<pubDate>Thu, 07 Jun 2012 18:17:03 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[OCIML]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=2118</guid>
		<description><![CDATA[OCaml 4 beta 2 has been released, and so I quickly tested OCI*ML with it. Only a couple of minor tweaks were necessary, due to the following changes: Some .cmi for toplevel internals that used to be installed in`ocamlc -where` &#8230; <a href="http://gaiustech.wordpress.com/2012/06/07/ocaml-4-beta/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=2118&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://caml.inria.fr/pub/distrib/ocaml-4.00/">OCaml 4 beta 2</a> has been released, and so I quickly tested <a href="http://gaiustech.github.com/ociml/">OCI*ML</a> with it. Only a couple of minor tweaks were necessary, due to the following <a href="http://caml.inria.fr/pub/distrib/ocaml-4.00/notes/Changes">changes</a>:</p>
<ul>
<li>Some .cmi for toplevel internals that used to be installed in`<code>ocamlc -where`</code> are now to be found in  <code>`ocamlc -where`/compiler-libs</code>. Add &#8220;<code>-I +compiler-libs</code>&#8221; where needed.</li>
<li>Warning 28 is now enabled by default.</li>
</ul>
<p>The impact of these was that the <a href="http://gaiustech.wordpress.com/2011/05/28/ociml-minor-updates/">toplevel prompt</a> wasn&#8217;t working in the shell, and one non-fatal warning when compiling, so nothing that would have broken any code, but it&#8217;s good to be up-to-date. The necessary changes have been checked in on Github. </p>
<p>Speaking of which, bearing in mind the <a href="http://www.bbc.co.uk/news/technology-18338956">LinkedIn d&eacute;b&agrave;cle</a>, I have a <a href="http://github.com/gaiustech/MkPasswd">password generator</a> too&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/2118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/2118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=2118&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2012/06/07/ocaml-4-beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>
	</item>
		<item>
		<title>Segmentation Faults, TAP and Eclipse</title>
		<link>http://gaiustech.wordpress.com/2011/09/09/segmentation-faults-tap-and-eclipse/</link>
		<comments>http://gaiustech.wordpress.com/2011/09/09/segmentation-faults-tap-and-eclipse/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 11:14:41 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[seg fault]]></category>
		<category><![CDATA[sigsegv]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1717</guid>
		<description><![CDATA[This is quite a neat trick. Consider: Referencing that NULL pointer obviously crashes straight away with a terse segmentation fault: By simply linking it with libSegFault, which seems to be documented only very informally, but comes with glibc: Of course &#8230; <a href="http://gaiustech.wordpress.com/2011/09/09/segmentation-faults-tap-and-eclipse/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1717&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is quite a neat trick. Consider:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;cstdlib&gt;

int main(int argc, char** argv) {
  int* ptr = NULL;
  *ptr = 1;

  exit(EXIT_SUCCESS);
}
</pre>
<p>Referencing that <code>NULL</code> pointer obviously crashes straight away with a terse <a href="http://en.wikipedia.org/wiki/Segmentation_fault">segmentation fault</a>:</p>
<p><a href="http://gaiustech.files.wordpress.com/2011/09/segfault1.png"><img src="http://gaiustech.files.wordpress.com/2011/09/segfault1.png?w=640" alt="" title="segfault1"   class="aligncenter size-full wp-image-1719" /></a><br />
By simply linking it with <code>libSegFault</code>, which seems to be documented only <a href="http://www.cygwin.com/ml/gdb/2007-06/msg00345.html">very informally</a>, but comes with <a href="http://www.gnu.org/s/libc/">glibc</a>:</p>
<p><a href="http://gaiustech.files.wordpress.com/2011/09/segfault2.png"><img src="http://gaiustech.files.wordpress.com/2011/09/segfault2.png?w=640" alt="" title="segfault2"   class="aligncenter size-full wp-image-1720" /></a><br />
Of course you could get all this by starting the program with <a href="http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_42.html">gdb</a> but this looks like a great technique for diagnosing crashes &#8220;in the wild&#8221;. Not that my code segfaults all the time, mind! After writing this, I found some more details on <a href="http://stackoverflow.com/q/77005/447514">Stack Overflow</a>, including a good tip about <code>c++filt</code>. Another useful utility is <code><a href="http://linux.die.net/man/1/addr2line">addr2line</a></code>.</p>
<p>Also I am starting to incorporate <code><a href="http://testanything.org/wiki/index.php/Testing_with_C%2B%2B#Testing_using_libtap.2B.2B">libtap++</a></code>, an implementation of the <a href="http://testanything.org/wiki/index.php/TAP_at_IETF:_Draft_Standard#Introduction">Test Anything Protocol</a>, into my projects. C++ has <a href="http://c2.com/cgi/wiki?StronglyTyped">strong typing</a>, at least stronger than plain C, which helps <a href="http://gaiustech.wordpress.com/2011/04/25/and-while-im-on-the-subject/">trap errors at compile time</a> like in OCaml. But since I always write a test harness anyway, it makes sense to fall into line with the way everyone else does it, especially as I have ambitions for wider adoption of my bindings&dagger;. <s>Presently there is <a href="http://testanything.org/wiki/index.php/TAP_Producers">no TAP producer for OCaml</a></s>&Dagger;, so at some point I will work on that too (or just bind to <a href="https://github.com/zorgnax/libtap">the C libtap</a>).</p>
<p>On the recommendation of a friend of mine who is a very experienced C++ programmer, I have been playing with <a href="http://www.eclipse.org/cdt/">Eclipse CDT</a>, which is pretty nice for C/C++ but doesn&#8217;t deal well at all with mixed C++/OCaml projects, and <a href="http://eclipsefp.github.com">EclipseFP</a> seems to have dropped support for OCaml at some point. I think I&#8217;ll try using it for just the <code>.so</code> portion of my projects and stick with Emacs/<code>make</code> for final integration with OCaml bindings, at least until I see if it really is more productive to do C++ in a full-blown <a href="http://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> versus good old <a href="http://www.gnu.org/software/emacs/tour/">Emacs</a>. Also I&#8217;ve bought a copy of <i><a href="http://www.amazon.co.uk/Boost-C-Libraries-Boris-Schaling/dp/0982219199/ref=sr_1_5?s=books&amp;ie=UTF8&amp;qid=1315564879&amp;sr=1-5">The Boost C++ Libraries</a></i>, again on the recommendation of my friend, that whatever I want, it&#8217;s probably already in <a href="http://www.boost.org/">Boost</a>. </p>
<p><font size="1">&dagger; OPERATION FOOTHOLD<br />
&Dagger; See comments</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1717/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1717/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1717&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/09/09/segmentation-faults-tap-and-eclipse/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/09/segfault1.png" medium="image">
			<media:title type="html">segfault1</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/09/segfault2.png" medium="image">
			<media:title type="html">segfault2</media:title>
		</media:content>
	</item>
		<item>
		<title>One Year On</title>
		<link>http://gaiustech.wordpress.com/2011/09/04/one-year-on/</link>
		<comments>http://gaiustech.wordpress.com/2011/09/04/one-year-on/#comments</comments>
		<pubDate>Sun, 04 Sep 2011 13:06:12 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1702</guid>
		<description><![CDATA[Re-learning C++ is proving to be something of an exercise in frustration thus far; it seems there is so much you need to know before you can get anything useful done. And choices that need to be made, this library &#8230; <a href="http://gaiustech.wordpress.com/2011/09/04/one-year-on/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1702&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://gaiustech.wordpress.com/2011/06/13/jack-of-all-trades-master-of-disaster/">Re-learning C++</a> is proving to be something of an exercise in frustration thus far; it seems there is <i>so much</i> you need to know before you can get anything useful done. And choices that need to be made, this library or that. When I started programming it was 90% about the language and 10% about the libraries, now it is probably the opposite.</p>
<p>Of course, I do actually know that is all irrelevant; no-one springs forth a <a href="http://en.wikipedia.org/wiki/Athena#Birth">fully-formed</a> programmer, you pay your dues slogging through learning and reference materials, gradually working your way up from writing <a href="http://en.wikipedia.org/wiki/Hello_world">Hello, world!</a> via <a href="http://gaiustech.wordpress.com/2011/08/22/putting-it-all-together-pubsub-for-ocaml-with-coherence/">proofs-of-concept</a> to real programs. Rome wasn&#8217;t built in a day! What I need to do is stop reading so much and just start coding <i>something</i> &#8211; this isn&#8217;t as dangerous as it sounds as I do have the foundations already in place for decent OO, and I&#8217;ve kept up with ideas like <a href="http://c2.com/cgi/wiki?ResourceAcquisitionIsInitialization">RAII</a> and <a href="http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap">copy-and-swap</a>, and <a href="http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/smart_ptr.htm">smart pointers</a> and the other goodies in <a href="http://www.boost.org/">Boost</a> etc. </p>
<p>And I have been here before, <a href="http://gaiustech.wordpress.com/2010/09/02/hello-world/">about one year ago</a> I started this blog as I set out to learn and use OCaml! I have been successful in that in as much as I can use it for &#8220;real work&#8221;, particularly since writing <a href="http://gaiustech.github.com/ociml/">OCI*ML</a>&dagger;, which required dusting off my C. This C++ diversion too is mainly in support of <code>OPERATION FOOTHOLD</code>, for working with <a href="http://www.oracle.com/technetwork/middleware/coherence/overview/index.html">Coherence</a>, <a href="http://qt.nokia.com/">Qt</a>, <a href="http://www.antigrain.com/">Agg</a> and others, all of which make use of advanced C++ features.  </p>
<p><font size="1">&dagger; And I haven&#8217;t forgotten there is more work needed on that as well, for example LOBs.</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1702/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1702/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1702&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/09/04/one-year-on/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>
	</item>
		<item>
		<title>Putting it all together: PubSub for OCaml with Coherence</title>
		<link>http://gaiustech.wordpress.com/2011/08/22/putting-it-all-together-pubsub-for-ocaml-with-coherence/</link>
		<comments>http://gaiustech.wordpress.com/2011/08/22/putting-it-all-together-pubsub-for-ocaml-with-coherence/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 19:12:43 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Coherence]]></category>
		<category><![CDATA[COHML]]></category>
		<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1647</guid>
		<description><![CDATA[In this post I will present a simple PubSub system for OCaml using the Continuous Query feature of Oracle Coherence. The code is all on GitHub. For this example, we will have three subscribers interested in different kinds of messages: &#8230; <a href="http://gaiustech.wordpress.com/2011/08/22/putting-it-all-together-pubsub-for-ocaml-with-coherence/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1647&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will present a simple <a href="http://en.wikipedia.org/wiki/PubSub">PubSub</a> system for <a href="http://ocaml.org/">OCaml</a> using the <a href="http://download.oracle.com/docs/cd/E18686_01/coh.37/e18678/cpp_contquery.htm#BGBBADBH">Continuous Query</a> feature of <a href="http://www.oracle.com/us/products/middleware/coherence/coherence-grid-edition/index.html">Oracle Coherence</a>. The code is all on <a href="https://github.com/gaiustech/cohml">GitHub</a>. For this example, we will have three subscribers interested in different kinds of messages:</p>
<ol>
<li>High priority messages</li>
<li>Info messages</li>
<li>Messages which mention <code>XYZ</code> in their body</li>
</ol>
<p>A &#8220;real&#8221; application might have a much more detailed message structure than just priority, subject and body, however the code will be very easy to modify to add new fields, etc. The important thing to understand is that the filters run within the Coherence grid itself; a subscriber only sees messages in which it has registered an interest, the routing and filtering is entirely offloaded from the client. Filters can be combined, for example with <a href="http://download.oracle.com/docs/cd/E18686_01/coh.37/e18684/classcoherence_1_1util_1_1filter_1_1_and_filter.html">AndFilter</a>, tho&#8217; they are not quite as powerful as the <code>WHERE</code> construct in SQL (e.g. no joins). Coherence-based systems are known to scale to thousands of messages/sec with thousands of connected subscribers, and OCaml is an ideal language for <a href="http://en.wikipedia.org/wiki/Complex_event_processing">CEP</a> applications such as <a href="http://en.wikipedia.org/wiki/Algorithmic_trading#Technical_design">trading</a>.</p>
<p>The nature of PubSub is that the publishers and subscribers are fully de-coupled; a publisher does not know if any subscriber is listening, nor does a subscriber know who has sent the message. If reliable delivery or a stronger relationship or  is required, then a <a href="http://en.wikipedia.org/wiki/Message_Bus">message bus</a> such as <a href="http://gaiustech.wordpress.com/2011/06/12/ociml-ref-cursors-and-raw-messages/">Oracle AQ</a> should be used (the tradeoff being lower throughput/higher latency). An analogy is <a href="http://stackoverflow.com/q/47903/447514">UDP vs TCP</a>. In all cases, so long as the data can be interchanged via a common format (database column types for AQ, <a href="http://download.oracle.com/docs/cd/E14526_01/coh.350/e14510/pof.htm#BABJBDGG">POF</a> for Coherence, Apache <a href="http://thrift.apache.org/">Thrift</a>, etc), it does not matter what language any of the individual participants is written in. This is the fundamental principle of <code>OPERATION FOOTHOLD</code>.</p>
<p>The code for the subscriber is as follows. </p>
<pre class="brush: fsharp; highlight: [19]; pad-line-numbers: 3; title: ; notranslate">
open Cohml
open Callback
open Printf
open Log_message
open Unix

(* the message record struct *)
type message = {msg_id : int;  msg_priority : int; msg_subject : string; msg_body : string}

(* the query predicate for the message records we are interested in receiving 
   events for. Fields are 0-3 (see definition of message above), conditions are
   0: less than, 1: equal to, 2: greater than, 3: like, types are 0 for int, 1
   for string. Search term is a string and we'll atoi() it in C if we need to.
   Existing is if we want anything already in the cache before we begin.
*)
type field = | ID | PRIORITY | SUBJECT | BODY
type field_type = | INT | STRING
type condition = | LESS_THAN | EQUAL_TO | GREATER_THAN | LIKE 
type query = {field : field; field_type: field_type; condition : condition; search_term : string; existing: bool}

(* function to register message listener callbacks - insert, update, delete*)
external coh_msg_listen: coh_ptr -&gt; query -&gt; string -&gt; string -&gt; string -&gt; unit = &quot;caml_coh_addmessagelistener&quot;

let string_of_message m = sprintf &quot;id=%d, priority=%d, subject='%s', body='%s'&quot; m.msg_id m.msg_priority m.msg_subject m.msg_body

(* callback functions *)
let _ = register &quot;cbf_msg_insert&quot; (fun k m -&gt; 
  log_message (sprintf &quot;New message: %s&quot; (string_of_message m)))
let _ = register &quot;cbf_msg_update&quot; (fun k om nm -&gt;
  log_message (sprintf &quot;Message changed %s/%s&quot; (string_of_message om) (string_of_message nm)))
let _ = register &quot;cbf_msg_delete&quot; (fun k -&gt;
  log_message (sprintf &quot;Message %d deleted&quot; k))
  
let () =
  let st = (match Array.length Sys.argv with
    |1 -&gt; 1 (* default to subscriber type 1 *)
    |_ -&gt; int_of_string Sys.argv.(1) ) in
  let q = (match st with
    |1 -&gt; { field=PRIORITY; field_type=INT; condition=LESS_THAN; search_term=&quot;2&quot;; existing=false }
    |2 -&gt; { field=SUBJECT; field_type=STRING; condition=EQUAL_TO; search_term=&quot;INFO&quot;; existing=false }
    |_ -&gt; { field=BODY; field_type=STRING; condition=LIKE; search_term=&quot;%XYZ%&quot;; existing=false }) in
  log_message (sprintf &quot;Subscriber starting as type %d&quot; st);
  let c = coh_getcache &quot;message_cache&quot; in
  coh_msg_listen c q &quot;cbf_msg_insert&quot; &quot;cbf_msg_update&quot; &quot;cbf_msg_delete&quot;;
  sleep 1000
</pre>
<p>It builds on the previous example, the new feature being the <code>query</code> struct which describes the type of message the subscriber is interested (all three of the example subscribers are in the same source file, selectable on the command line). This is clearly highly specific to the <code>message</code> record type and it&#8217;s C++ representation the <code>Message</code> class, however at least the subscription can be defined from OCaml code. To test it, I have written a simple load generator that composes random messages:</p>
<pre class="brush: fsharp; pad-line-numbers: 3; title: ; notranslate">
open Cohml
open Log_message

type message = {msg_id : int;  msg_priority : int; msg_subject : string; msg_body : string}
external coh_put_message: coh_ptr -&gt; message -&gt; unit = &quot;caml_put_message&quot;
let print_message m = log_message (Printf.sprintf &quot;id=%d, priority=%d, subject='%s', body='%s'&quot; m.msg_id m.msg_priority m.msg_subject m.msg_body) 

let p () = (Random.int 5) + 1 
let subjects = [|&quot;INFO&quot;; &quot;WARN&quot;; &quot;FAIL&quot;; &quot;RECOVER&quot;|] 
let s () = Random.int (Array.length subjects)
let bodies = [|&quot;ABC&quot;; &quot;DEF&quot;; &quot;GHI&quot;; &quot;JKL&quot;; &quot;SOS&quot;; &quot;XYZ&quot;|]
let b () = Random.int (Array.length bodies)
let pad = String.make 40 'Z' (* for Tibco-style 50-byte messages *) 

let () = 
  Random.self_init ();
  let x = (match Array.length Sys.argv with |1 -&gt; 10000|_ -&gt; int_of_string Sys.argv.(1)) in
  let c = coh_getcache &quot;message_cache&quot; in
  let t1 = Unix.gettimeofday () in
  for i = 1 to x do
    let m = {msg_id = i; msg_priority = p (); msg_subject = subjects.(s ()); msg_body = pad ^ (bodies.(b ()))} in
    coh_put_message c m;
    print_message m;
    Unix.sleep 1
  done;
  log_message (Printf.sprintf &quot;Sent %d messages in %.3fs&quot; x (Unix.gettimeofday () -. t1))
</pre>
<p>The sleep function allows the log messages from all the different programs to be correlated. Running it:<br />
<div id="attachment_1671" class="wp-caption aligncenter" style="width: 650px"><a href="http://gaiustech.files.wordpress.com/2011/08/pubsub_publisher1.png"><img src="http://gaiustech.files.wordpress.com/2011/08/pubsub_publisher1.png?w=640&#038;h=288" alt="" title="pubsub_publisher" width="640" height="288" class="size-full wp-image-1671" /></a><p class="wp-caption-text">Publisher</p></div><br />
And observing all three clients (started already):<br />
<div id="attachment_1673" class="wp-caption aligncenter" style="width: 650px"><a href="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub1.png"><img src="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub1.png?w=640&#038;h=278" alt="" title="pubsub_sub1" width="640" height="278" class="size-full wp-image-1673" /></a><p class="wp-caption-text">Subscriber 1 - high priority messages</p></div><br />
<div id="attachment_1674" class="wp-caption aligncenter" style="width: 650px"><a href="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub2.png"><img src="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub2.png?w=640&#038;h=278" alt="" title="pubsub_sub2" width="640" height="278" class="size-full wp-image-1674" /></a><p class="wp-caption-text">Subscriber 2 - INFO messages</p></div><br />
<div id="attachment_1675" class="wp-caption aligncenter" style="width: 650px"><a href="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub3.png"><img src="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub3.png?w=640&#038;h=278" alt="" title="pubsub_sub3" width="640" height="278" class="size-full wp-image-1675" /></a><p class="wp-caption-text">Subscriber 3 - Messages mentioning XYZ</p></div><br />
A simple test shows that on my test system (a 1G Debian VM in VirtualBox on a MacBook Pro) can  handle up to 1200 msgs/sec&dagger;, making this solution ~12&times; faster than <a href="http://gaiustech.wordpress.com/2011/05/26/ociml-new-feature-aq/">AQ on the same hardware</a> when accessed from OCaml; the difference being of course that AQ is a reliable transport. This may or may not be more important to the application than pure performance.</p>
<p>I think this is probably as far as I am going to take COH*ML for now; I need to get a lot more familiar with its native C++ interface, and re-acquainted C++ in general, before going much further. Nevertheless, this is more than sufficient as a proof-of-concept and will serve as a general framework for further development. I have a much better understanding than when I <a href="http://gaiustech.wordpress.com/2011/08/03/ocaml-bindings-for-coherence-with-swig/">began</a> of what is and is not feasible with this approach, and the trade-offs involved &#8211; it is ideally suited for relatively simple messages on which relatively complex algorithms written in OCaml can be run (then perhaps call back into C/C++ for, say, trade execution). </p>
<p><font size="1">&dagger; when not updating the screen</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1647/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1647/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1647&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/08/22/putting-it-all-together-pubsub-for-ocaml-with-coherence/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/pubsub_publisher1.png" medium="image">
			<media:title type="html">pubsub_publisher</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub1.png" medium="image">
			<media:title type="html">pubsub_sub1</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub2.png" medium="image">
			<media:title type="html">pubsub_sub2</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/pubsub_sub3.png" medium="image">
			<media:title type="html">pubsub_sub3</media:title>
		</media:content>
	</item>
		<item>
		<title>Queries in Coherence with OCaml</title>
		<link>http://gaiustech.wordpress.com/2011/08/16/queries-in-coherence-with-ocaml/</link>
		<comments>http://gaiustech.wordpress.com/2011/08/16/queries-in-coherence-with-ocaml/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 16:34:47 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Coherence]]></category>
		<category><![CDATA[COHML]]></category>
		<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1618</guid>
		<description><![CDATA[Well, it looks like I posted the last entry too soon, as I can get close to what I want to do with the PofExtractor available from Coherence 3.5 onwards, without needing to write any Java (ugh!). It is a &#8230; <a href="http://gaiustech.wordpress.com/2011/08/16/queries-in-coherence-with-ocaml/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1618&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Well, it looks like I posted the last entry too soon, as I <i>can</i> get close to what I want to do with the <a href="http://coherence.oracle.com/display/COH35UG/PofExtractors+and+PofUpdaters">PofExtractor</a> available from Coherence 3.5 onwards, without needing to write any Java (ugh!). It is a 1-line change to the C++, and a couple of OCaml lines to access the new functionality. The <a href="http://github.com/gaiustech/cohml">new example</a> puts two <code>message</code> records in the cache, then retrieves the high priority one:</p>
<pre class="brush: fsharp; highlight: [9,18]; pad-line-numbers: 3; title: ; notranslate">
open Cohml
open Printf
open Log_message

type message = {msg_id : int;  msg_priority : int; msg_subject : string; msg_body : string}

external coh_put_message: coh_ptr -&gt; message -&gt; unit = &quot;caml_put_message&quot;
external coh_get_message: coh_ptr -&gt; int -&gt; message = &quot;caml_get_message&quot;
external coh_pri_message: coh_ptr -&gt; int -&gt; message list = &quot;caml_query_message_pri&quot;

let print_message m = log_message (sprintf &quot;id=%d, priority=%d, subject='%s', body='%s'\n&quot; m.msg_id m.msg_priority m.msg_subject m.msg_body)

let () = 
  let test_msgs = [{msg_id = 1; msg_priority=3; msg_subject=&quot;test&quot;; msg_body=&quot;hello, world!&quot;};
		   {msg_id = 2; msg_priority=1; msg_subject=&quot;urgent!&quot;; msg_body=&quot;High priority message&quot;}] in
  let c = coh_getcache &quot;message_cache&quot; in
  List.iter (coh_put_message c) test_msgs;
  print_message (List.hd (coh_pri_message c 2));
</pre>
<p>The new C++ method, in <code>cohml.c</code>:</p>
<pre class="brush: cpp; first-line: 268; highlight: [271]; pad-line-numbers: 3; title: ; notranslate">
vector&lt;Message*&gt;* Cohml::query_message_pri(int k) {
  vector&lt;Message*&gt;* msgv = new vector&lt;Message*&gt;; 
  // field 1 is the priority of type int
  PofExtractor::Handle hExtractor = PofExtractor::create(typeid(int32_t), 1);
  Filter::View vFilter = LessEqualsFilter::create(hExtractor, Integer32::create(k));

  for (Iterator::Handle hIter = hCache-&gt;entrySet(vFilter)-&gt;iterator(); hIter-&gt;hasNext() ;) {
    Map::Entry::Handle hEntry = cast&lt;Map::Entry::Handle&gt;(hIter-&gt;next());
    Integer32::View vKey = cast&lt;Integer32::View&gt;(hEntry-&gt;getKey());
    Managed&lt;Message&gt;::View vMessage = cast&lt;Managed&lt;Message&gt;::View&gt;(hCache-&gt;get(vKey));
#ifdef DEBUG
    msg &lt;&lt; __func__ &lt;&lt; &quot;: retrieved message: &quot; &lt;&lt; vMessage;
    debug(msg.str().c_str());
#endif
    Message* m = new Message(vMessage-&gt;getId(), vMessage-&gt;getPriority(), vMessage-&gt;getSubject(), vMessage-&gt;getBody());
    msgv-&gt;push_back(m);
  }

  return msgv;
}
</pre>
<p>Replacing:</p>
<pre class="brush: cpp; first-line: 271; title: ; notranslate">
ValueExtractor::Handle hExtractor = ReflectionExtractor::create(&quot;getPriority&quot;);
</pre>
<p>On the way I encountered a bug that causes a segmentation fault in <code>List.iter</code>: </p>
<pre class="brush: bash; light: true; title: ; notranslate">
Program received signal SIGSEGV, Segmentation fault.
0x08085570 in caml_interprete ()
(gdb) bt
#0  0x08085570 in caml_interprete ()
#1  0x08074992 in caml_main ()
#2  0x080846a8 in main ()
(gdb) 
</pre>
<p>Thanks to the time-reversing capabilities of <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual030.html">Ocamldebug</a> I can peer into the interpreter:</p>
<pre class="brush: fsharp; light: true; title: ; notranslate">
Lost connection with process 27541 (active process)
between time 1970 and time 1971
Tue Aug 16 17:13:02 2011: id=2, priority=1, subject='urgent!', body='High priority message'

Time : 1970 - pc : 14392 - module List
69   | a::l -&gt; f a&lt;|a|&gt;; iter f l
(ocd)
</pre>
<p>This is why I am using <code>List.hd</code> there on line 18 instead of:</p>
<pre class="brush: fsharp; first-line: 18; pad-line-numbers: 3; title: ; notranslate">
  List.iter print_message (coh_pri_message c 2);
</pre>
<p><b>UPDATE 17<sup>th</sup> August</b>: This was a bug in my C++ code, I was constructing an <code>Array</code> but treating it as a <code>List</code> in OCaml. D&#8217;oh! Fixed now and pushed to <a href="http://github.com/gaiustech/cohml">GitHub</a>.</p>
<p>Anyway, the moral of the story is, don&#8217;t trust the documentation! Which was even for a later version than this feature was introduced in.</p>
<p>Also, writing C++ <i>really</I> makes you appreciate type inference. Most of the code above is type annotation and coercion. But like I say, it should be possible to just generate much of it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1618/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1618&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/08/16/queries-in-coherence-with-ocaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>
	</item>
		<item>
		<title>Custom classes in Coherence with OCaml</title>
		<link>http://gaiustech.wordpress.com/2011/08/16/custom-classes-in-coherence-with-ocaml/</link>
		<comments>http://gaiustech.wordpress.com/2011/08/16/custom-classes-in-coherence-with-ocaml/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 13:29:04 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>
		<category><![CDATA[COHML]]></category>
		<category><![CDATA[Coherence]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1577</guid>
		<description><![CDATA[I have not updated in a little while as I have been busily (re)learning C++ in my already-scarce free time. Seems there is a lot of work around for C++ types, and it&#8217;s always good to have options. Anyway, on &#8230; <a href="http://gaiustech.wordpress.com/2011/08/16/custom-classes-in-coherence-with-ocaml/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1577&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-size:x-small;">I have not updated in a little while as I have been busily (re)learning C++ in my already-scarce free time. Seems there is a lot of work around for C++ types, and it&#8217;s always good to have options. Anyway, on with the show!</span></p>
<p>To use a more complex data structure than a primitive type such as a string with the <a href="http://www.oracle.com/technetwork/middleware/coherence/overview/index.html">Coherence</a> grid, there are <a href="http://coherence.oracle.com/display/COH34UG/Integrating+user+data+types">three options</a>:</p>
<ol>
<li>A native Coherence class. This would be the option if developing from scratch an application that would only ever run on Coherence (e.g. using Coherence&#8217;s own <code>String</code> type rather than <code>std::string</code> from the C++ standard library).</li>
<li>Retrofitting an existing class to work with the <a href="http://download.oracle.com/docs/cd/E18686_01/coh.37/e18684/classcoherence_1_1lang_1_1_managed.html"><code>Managed</code> template</a>. This is probably the easiest method.</li>
<li>Writing a new (de)serializer to work with an existing class. This is the lowest level but highest performance method.</li>
</ol>
<p>While I <i>am</i> actually developing from scratch here, &#8220;systems integration&#8221; is my middle name†, so for the purposes of this example I am going to go with option 2 as I consider it to be a more representative scenario of &#8220;real work&#8221;. The <a href="https://github.com/gaiustech/cohml">complete code for this example</a> is on GitHub.</p>
<p>The interesting thing about the C++ object definition is that it contains no Coherence-specific code, but it must have:
<ul>
<li>A public or protected zero-parameter constructor</li>
<li>A copy constructor</li>
<li>A <code>==</code> operator, for equality comparison</li>
<li>A <code>&lt;&lt;</code> operator (e.g. for use with <code>cout</code>)</li>
<li>A hash function, to return a unique identifier </li>
</ul>
<p>Some of these a class is likely to have anyway. In addition there are two functions, to (de)serialize the objects. These are Coherence-specific, but are &#8220;free&#8221; so can be implemented independently of the class. I have done some research as to whether it is possible to generate these on-the-fly at runtime as I do with <a href="http://gaiustech.wordpress.com/2011/05/26/ociml-new-feature-aq/">object-types for Oracle AQ</a> and have concluded that it is not; therefore each class corresponding to an OCaml record (or other type) must be coded in C++ at compile time, and this is in addition to the code required to <a href="http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29">marshal</a> the object to and from <a href="http://caml.inria.fr/index.en.html">OCaml</a>. The reason is of course so that <a href="http://coherence.oracle.com/display/COH35UG/The+Portable+Object+Format">objects in Coherence</a> can be created and transparently accessed by clients in C++, Java and .NET. The good news is that the structure is quite regular and both should be quite amenable to code generation, perhaps from parsing the source code to the original existing class, if available. For this trivial example, storing and retrieving a record of type <code>message</code> in the grid, the C++:OCaml <a href="http://en.wikipedia.org/wiki/Source_lines_of_code">SLOC</a> ratio is ~10:1, but the C++ is very simple‡.</p>
<pre class="brush: fsharp; highlight: [5]; pad-line-numbers: 3; title: ; notranslate">
open Cohml
open Printf
open Log_message

type message = {msg_id : int;  msg_priority : int; msg_subject : string; msg_body : string}

external coh_put_message: coh_ptr -&gt; message -&gt; unit = &quot;caml_put_message&quot;
external coh_get_message: coh_ptr -&gt; int -&gt; message = &quot;caml_get_message&quot;

let print_message m = log_message (sprintf &quot;id=%d, priority=%d, subject='%s', body='%s'\n&quot; m.msg_id m.msg_priority m.msg_subject m.msg_body)

let () = 
  let test_msgs = [{msg_id = 1; msg_priority=3; msg_subject=&quot;test&quot;; msg_body=&quot;hello, world!&quot;}] in
  let c = coh_getcache &quot;message_cache&quot; in
  List.iter (coh_put_message c) test_msgs;
  print_message (coh_get_message c 1);
</pre>
<p><div id="attachment_1609" class="wp-caption aligncenter" style="width: 510px"><a href="http://gaiustech.files.wordpress.com/2011/08/message_example1.png"><img src="http://gaiustech.files.wordpress.com/2011/08/message_example1.png?w=500&#038;h=235" alt="" title="message_example1" width="500" height="235" class="size-medium wp-image-1609" /></a><p class="wp-caption-text">Click to enlarge</p></div><br />
I had hoped to present a query in this example too (e.g. messages of a certain priority) but looking at <a href="http://download.oracle.com/docs/cd/E18686_01/coh.37/e18678/cpp_integrationobj.htm#BAJCCABD">the documentation</a>:</p>
<blockquote><p>After completing any of the above approaches, a data object is ready to be stored within the Coherence cluster. You can perform get and put based operations with your objects. However, to make use of more advanced features of Coherence: such as <b>queries</b> or entry processors; or if you use a key that is not a simple type; or when you use a cache loader and cache store, <b>you must write some Java code</b>.</p></blockquote>
<p>Emphasis mine. I have all the C++ written, I just need to do that bit, which should be straightforward enough (and again like the C++ can probably be code-generated for the most part). Still, it adds complexity to the solution, which will make the OCaml a harder sell unless what we are actually doing with the cached objects is much better implemented in OCaml than C++, e.g. the sorts of algorithms that functional programming languages are well-suited for, or the kind of prototyping that a very high level language is well suited for, on already-defined objects (so perhaps the Java code backing the C++ code is already done for us!).</p>
<p>Unfortunately however I must conclude that a pure-OCaml interface to Coherence is impossible. However I am confident that my hybrid approach is going to be useful for &#8220;real work&#8221;, and better than SWIG in this particular case.</p>
<p><b>UPDATE</b>: Perhaps I can get &#8220;close enough&#8221; with a <a href="http://blackbeanbag.net/wp/2009/07/21/coherence-3-5-pof-extractorupdater/">PofExtractor</a>.</p>
<p><span style="font-size:small;">† Actually it&#8217;s Aurelius<br />
‡ Too much to post &#8211; see GitHub</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1577/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1577/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1577&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/08/16/custom-classes-in-coherence-with-ocaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/message_example1.png?w=500" medium="image">
			<media:title type="html">message_example1</media:title>
		</media:content>
	</item>
		<item>
		<title>Updated: MapListener example</title>
		<link>http://gaiustech.wordpress.com/2011/08/08/updated-maplistener-example/</link>
		<comments>http://gaiustech.wordpress.com/2011/08/08/updated-maplistener-example/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 12:14:24 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>
		<category><![CDATA[COHML]]></category>
		<category><![CDATA[Coherence]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1555</guid>
		<description><![CDATA[A slight update to yesterday&#8217;s example listener.ml supporting named callback functions: I am not entirely comfortable with this as a problem in the names can&#8217;t be detected at compile time &#8211; tho&#8217; at runtime there is a check that the &#8230; <a href="http://gaiustech.wordpress.com/2011/08/08/updated-maplistener-example/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1555&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A slight update to yesterday&#8217;s example <code>listener.ml</code> supporting named callback functions:</p>
<pre class="brush: fsharp; highlight: [13]; pad-line-numbers: 2; title: ; notranslate">
open Cohml
open Callback
open Unix

let _ = register &quot;cbf_coh_insert&quot; (fun k v -&gt; print_endline (&quot;Inserted key=&quot; ^ k ^ &quot; value=&quot; ^ v))
let _ = register &quot;cbf_coh_update&quot; 
  (fun k ov nv -&gt; print_endline (&quot;Updated key=&quot; ^ k ^ &quot; old value=&quot; ^ ov ^ &quot; new value=&quot; ^ nv))
let _ = register &quot;cbf_coh_delete&quot; (fun k -&gt; print_endline (&quot;Deleted key=&quot; ^ k))

let () = 
  print_endline &quot;Listening...&quot;;
  let c = coh_getcache &quot;test_cache&quot; in
  coh_listen c &quot;cbf_coh_insert&quot; &quot;cbf_coh_update&quot; &quot;cbf_coh_delete&quot;;
  sleep 1000
</pre>
<p>I am not entirely comfortable with this as a problem in the names can&#8217;t be detected at compile time &#8211; tho&#8217; at runtime there is a check that the callback functions do exist, it does not <a href="http://stackoverflow.com/q/6982849/447514">check their type signatures</a>. I wish it was possible to pass a closure/let binding straight into C&dagger;. It would be possible to accidentally have the arguments out of order:</p>
<pre class="brush: fsharp; first-line: 13; pad-line-numbers: 2; title: ; notranslate">
  coh_listen c &quot;cbf_coh_update&quot; &quot;cbf_coh_insert&quot; &quot;cbf_coh_delete&quot;;
</pre>
<p>And it will compile cleanly and crash as soon as it runs! We use advanced languages like OCaml precisely to avoid that possibility. But it does mean the ability to connect to more than one cache with MapListeners.</p>
<p>Next up: objects. Or maybe <a href="http://download.oracle.com/docs/cd/E13924_01/coh.340/e12192/cppquerycache.htm#COHUG236">queries</a>. The <a href="http://www.amazon.co.uk/Oracle-Coherence-3-5-Aleksandar-Seovic/dp/1847196128/ref=sr_1_1?ie=UTF8&amp;qid=1312828776&amp;sr=8-1">Coherence book</a> (p.335), referring to data object serialization, states </p>
<blockquote><p>Unlike the Java and .NET versions, in C++ this mapping is performed at compilation time.</p></blockquote>
<p>So non-trivial applications will have to include C++ to use custom objects. I&#8217;d hoped to present the users of my bindings with a pure OCaml API, but this should still be straightforward enough.</p>
<p>&dagger; Update: <a href="http://omake.metaprl.org/prerelease/omake-dll-gtk.html#toc112">Apparently you can</a>. Well, sort of. Hmm.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1555/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1555&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/08/08/updated-maplistener-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>
	</item>
		<item>
		<title>OCaml binding for Coherence MapListener</title>
		<link>http://gaiustech.wordpress.com/2011/08/06/ocaml-binding-for-coherence-maplistener/</link>
		<comments>http://gaiustech.wordpress.com/2011/08/06/ocaml-binding-for-coherence-maplistener/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 13:52:07 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>
		<category><![CDATA[COHML]]></category>
		<category><![CDATA[Coherence]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1527</guid>
		<description><![CDATA[In this post I will demonstrate triggering the execution of OCaml code by events within the Coherence grid. The full code is on GitHub, but at the OCaml level it is designed to be very simple&#8224;, using the callback mechanism. &#8230; <a href="http://gaiustech.wordpress.com/2011/08/06/ocaml-binding-for-coherence-maplistener/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1527&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will demonstrate triggering the execution of OCaml code by events within the <a href="http://www.oracle.com/us/products/middleware/coherence/index.html">Coherence</a> grid. The full code is on <a href="https://github.com/gaiustech/cohml">GitHub</a>, but at the OCaml level it is designed to be very simple&dagger;, using the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html#toc142">callback mechanism</a>. This is <code>listener.ml</code>:</p>
<pre class="brush: fsharp; highlight: [6,9,12]; pad-line-numbers: 3; title: ; notranslate">
open Cohml
open Callback
open Unix

let cbf_coh_insert k v = print_endline (&quot;Inserted key=&quot; ^ k ^ &quot; value=&quot; ^ v)
let _ = register &quot;cbf_coh_insert&quot; cbf_coh_insert

let cbf_coh_update k ov nv = print_endline (&quot;Updated key=&quot; ^ k ^ &quot; old value=&quot; ^ ov ^ &quot; new value=&quot; ^ nv)
let _ = register &quot;cbf_coh_update&quot; cbf_coh_update

let cbf_coh_delete k = print_endline (&quot;Deleted key=&quot; ^ k)
let _ = register &quot;cbf_coh_delete&quot; cbf_coh_delete

let () = 
  print_endline &quot;Listening...&quot;;
  let c = coh_getcache &quot;test_cache&quot; in
  coh_listen c ;
  sleep 1000
</pre>
<p>The new library function is <code>coh_listen</code> on line 17. It sleeps after registering for callbacks, but could do some additional processing if thread-safe. The registered names correspond to those in the lower-level C++, an excerpt from <code>cohml.c</code>:</p>
<pre class="brush: cpp; first-line: 186; highlight: [188,195,203]; pad-line-numbers: 3; title: ; notranslate">
void CohmlMapListener::entryInserted(MapEvent::View vEvent) {
  // TODO: stash these function pointers
  caml_callback2(*caml_named_value(&quot;cbf_coh_insert&quot;), 
		 caml_copy_string(cast&lt;String::View&gt;(vEvent-&gt;getKey())-&gt;getCString()), 
		 caml_copy_string(cast&lt;String::View&gt;(vEvent-&gt;getNewValue())-&gt;getCString()));
}

// call the OCaml function cbf_coh_update with the key, the previous value and the new value
void CohmlMapListener::entryUpdated(MapEvent::View vEvent) {
  caml_callback3(*caml_named_value(&quot;cbf_coh_update&quot;), 
		 caml_copy_string(cast&lt;String::View&gt;(vEvent-&gt;getKey())-&gt;getCString()),  
		 caml_copy_string(cast&lt;String::View&gt;(vEvent-&gt;getOldValue())-&gt;getCString()), 
		 caml_copy_string(cast&lt;String::View&gt;(vEvent-&gt;getNewValue())-&gt;getCString()));
}

// call the OCaml function cb_coh_delete with the key just removed from the cache
void CohmlMapListener::entryDeleted(MapEvent::View vEvent) {
  caml_callback(*caml_named_value(&quot;cbf_coh_delete&quot;), 
		caml_copy_string(cast&lt;String::View&gt;(vEvent-&gt;getKey())-&gt;getCString()));
}
</pre>
<p>Now if we run up the <code>listener</code> process and manipulate the cache from <code>cohmlsh</code> in another window:<br />
<div id="attachment_1528" class="wp-caption aligncenter" style="width: 510px"><a href="http://gaiustech.files.wordpress.com/2011/08/maplistener_cohmlsh.png"><img src="http://gaiustech.files.wordpress.com/2011/08/maplistener_cohmlsh.png?w=500&#038;h=235" alt="" title="maplistener_cohmlsh" width="500" height="235" class="size-medium wp-image-1528" /></a><p class="wp-caption-text">Click to enlarge</p></div><br />
We can clearly see our callback functions being executed:<br />
<div id="attachment_1529" class="wp-caption aligncenter" style="width: 510px"><a href="http://gaiustech.files.wordpress.com/2011/08/maplistener_listener.png"><img src="http://gaiustech.files.wordpress.com/2011/08/maplistener_listener.png?w=500&#038;h=235" alt="" title="maplistener_listener" width="500" height="235" class="size-medium wp-image-1529" /></a><p class="wp-caption-text">Click to enlarge</p></div><br />
This is still trivial but it is a definite step in the direction of doing &#8220;real work&#8221; with this&Dagger;. The next challenge will be to do all this with a more sophisticated data structure than just a string. I would like to keep everything within OCaml (as I have <a href="http://gaiustech.wordpress.com/2011/05/26/ociml-new-feature-aq/">managed to do</a> in <a href="http://gaiustech.github.com/ociml/">OCI*ML</a>) but it is looking increasingly like each one will require some <a href="http://download.oracle.com/docs/cd/E14526_01/coh.350/e14513/cppobjectmod.htm#CIHGCAJB">custom C++</a> as reverse-engineering <a href="http://coherence.oracle.com/display/COH35UG/The+Portable+Object+Format">POF</a> is well beyond me! I will also need a way to pass the callback names into Coherence &#8211; a <code>MapListener</code> is created not with <code>new</code> but with a <code>::create()</code> factory method, so I never actually &#8220;have&#8221; it to set properties on. This is so I can listen to more than one cache at a time.</p>
<p>Towards that end I have have bought a copy of <a href="https://www.amazon.co.uk/Thinking-Introduction-Standard-v-1-Vol/dp/0139798099/ref=sr_1_3?ie=UTF8&amp;qid=1312638475&amp;sr=8-3">Bruce Eckel&#8217;s <i>Thinking in C++</i></a>. Reading it is giving me a strange sense of <a href="http://gaiustech.wordpress.com/2011/06/13/jack-of-all-trades-master-of-disaster/">d&eacute;j&agrave; vu</a>&#8230;</p>
<p><font size="1">&dagger; This is possibly because we are still only using strings!<br />
&Dagger;Get something off the grid, process it in OCaml and store it in the database, for example, or vice-versa</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1527/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1527/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1527&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/08/06/ocaml-binding-for-coherence-maplistener/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/maplistener_cohmlsh.png?w=500" medium="image">
			<media:title type="html">maplistener_cohmlsh</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/maplistener_listener.png?w=500" medium="image">
			<media:title type="html">maplistener_listener</media:title>
		</media:content>
	</item>
		<item>
		<title>OCaml bindings for Coherence (without SWIG)</title>
		<link>http://gaiustech.wordpress.com/2011/08/05/ocaml-bindings-for-coherence-2/</link>
		<comments>http://gaiustech.wordpress.com/2011/08/05/ocaml-bindings-for-coherence-2/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 12:34:41 +0000</pubDate>
		<dc:creator>Gaius</dc:creator>
				<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Operation Foothold]]></category>
		<category><![CDATA[COHML]]></category>
		<category><![CDATA[Coherence]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://gaiustech.wordpress.com/?p=1508</guid>
		<description><![CDATA[I am much happier with my second attempt at an OCaml binding for Oracle Coherence. This way is easier to build, easier to use, and I could reuse all of the C++ code from my first attempt. See the code &#8230; <a href="http://gaiustech.wordpress.com/2011/08/05/ocaml-bindings-for-coherence-2/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1508&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I am much happier with my second attempt at an <a href="http://caml.inria.fr/ocaml/index.en.html">OCaml</a> binding for <a href="http://www.oracle.com/technetwork/middleware/coherence/overview/index.html">Oracle Coherence</a>. This way is easier to build, easier to use, and I could reuse all of the C++ code from my <a href="http://gaiustech.wordpress.com/2011/08/03/ocaml-bindings-for-coherence-with-swig/">first attempt</a>. See the <a href="http://github.com/gaiustech/cohml">code on GitHub</a>.<br />
<div id="attachment_1509" class="wp-caption aligncenter" style="width: 510px"><a href="http://gaiustech.files.wordpress.com/2011/08/cohml_take2.png"><img src="http://gaiustech.files.wordpress.com/2011/08/cohml_take2.png?w=500&#038;h=235" alt="" title="cohml_take2" width="500" height="235" class="size-medium wp-image-1509" /></a><p class="wp-caption-text">Click to enlarge</p></div><br />
My general strategy for this is going to be:</p>
<ol>
<li>Use a class (or classes) written in pure C++ to interact with Coherence</li>
<li>Use <code>extern "C"</code> functions first to instantiate an object from (1) and return an object pointer to OCaml, then to invoke methods via that pointer.</li>
<li>From OCaml, call the function to create the object, then pass the pointer from (2) into the other C functions using the <a href="http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php#ref_cplusplus">normal OCaml/C interface</a></li>
<li>Pre- and post- grid operations implemented in OCaml</li>
</ol>
<p>The next challenge will be to use <a href="http://download.oracle.com/docs/cd/E18686_01/coh.37/e18678/cpp_events.htm">MapListener</a>/Continuous Query to execute code in OCaml in response to events within the grid &#8211; if possible, configuring it from the OCaml side. And then to do it with more complex data structures than simple strings.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gaiustech.wordpress.com/1508/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gaiustech.wordpress.com/1508/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gaiustech.wordpress.com&#038;blog=15595609&#038;post=1508&#038;subd=gaiustech&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gaiustech.wordpress.com/2011/08/05/ocaml-bindings-for-coherence-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ced82e97b7a13f79da0aaaca2302cd45?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">gaiush</media:title>
		</media:content>

		<media:content url="http://gaiustech.files.wordpress.com/2011/08/cohml_take2.png?w=500" medium="image">
			<media:title type="html">cohml_take2</media:title>
		</media:content>
	</item>
	</channel>
</rss>
