Minimal Groovy neo4j client

@Grab(‘org.codehaus.groovy.modules.http-builder:http-builder:0.7.2’)
import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper

def authToken = ‘neo4j:password’.bytes.encodeBase64().toString()
def auth = “Basic: $authToken”.toString()
// send Cypher statement to Neo4j Server
def client = new RESTClient( ‘http://localhost:7474’, ‘application/json’ )
client.defaultRequestHeaders.’Authorization’ = auth

def body = “””{
“statements” : [ {
“statement” : “CREATE (wordsworth:Author {firstname:’William’, lastname: ‘Wordsworth’})”
} ]
}”””
def resp = client.post( path : ‘/db/data/transaction/commit’, body : body )

println resp.data.toString()

Handlebars (Java) Templates and Extensions are really powerful

I have recently moved a project from using Thymeleaf to using Handlebars.

Handlebars is a templating library (think mail merge for code if you are a non-developer).

Handlebars uses the {{ }} tags to surround items to be replaced.

{{{   }}} will not escape content provided.

{{ > path/to/file }}  Renders a template. You can supply named blocks that get substituted into that template.

By default the template uses whatever data that the template was bound to. Typically we bind nested Maps.

One of the principles of handlebars is that the templates should be logic free. This means that you are limited to {{#if }}, {{#unless }} and {{#each }} blocks.

#If will check the truthiness of a value. The intent is to include a block of template only if a certain item is present, say an optional middle name.

#unless is the inverse, it will only show the block if the condition is false. This is ideal for providing defaults for missing data.

Any logic above this must be handled by an extension method.

This is not as difficult as it sounds. With a little effort extension methods can be written to:

  • Invoke a template name specified in a variable.
  • Perform custom sorting of data acting as a custom loop over a supplied block.

By keeping the logic isolated it becomes independently testable and simplifies the templates.

Thoughts on Groovy

I have been working in Groovy for about a month now.

It’s definitely a step up from Java in terms of dialing down the ceremony.

However the scripting nature can be nasty on occasions where the compiler just does not protect you from obvious mistakes.

Groovy equivalent of Nuget : Grape

Here is this documentation for the Groovy equivalent to Nuget.

This is a small sample:

@Grab(group=‘org.springframework’, module=‘spring-orm’, version=‘3.2.5.RELEASE’) import    org.springframework.jdbc.core.JdbcTemplate

Grape does not seem to go as far as Nuget’s allowing upgrades, but it does do the configuration in the source code.

Expanding into Groovy

I have spent the last few years working in the .Net ecosystem.

This has generally involved working in C#, PoweShell, Nuget and Chocolatey.

I am now exploring the JVM world.

In order to get the RatPack library installed (which is the Groovy version of Ruby’s Sinatra or the .Net clone Nancy) I have needed to install the JRE, Gradle and Groovy.

Getting Java installed on a corporate dev machine was a little tricky.

The installer was placing files in a folder that I don’t have execute permissions on.

The specific error message that I was getting led to a link that suggested downloading a java based tool to fix. There is a hole in my bucket…

Copying the deployed installer to another location eventually worked.

Several of the Groovy libraries suggest using gvm to install things so I have opted to use posh-gvm:
(new-object Net.WebClient).DownloadString(‘https://raw.githubusercontent.com/flofreud/posh-gvm/master/GetPoshGvm.ps1’) | iex

gvm appears to be the equivalent of chocolatey for the Groovy world.

gradle is the equivalent of msbuild or more specifically Psake.

I now have a list of projects to investigate:

  • asciidoctorj – port of the ruby asciidoctor tool to convert structured text to html
  • crash – looks like a powershell console equivalent for the jvm
  • gaiden – creates html from markdown
  • glide – another generation toolkit, could not get demo to work
  • gradle – build tool
  • grails – web toolkit can’t get to install yet
  • griffon – looks like a wpf equivalent for groovy
  • groovy – Java based programming language without all the ceremony of java
  • groovyserv – speeds up groovy app startup
  • jbake – static site generator
  • lazybones – a scaffolder for ratpack (t4 scaffolder equivalent)
  • springboot – application framework template
  • vertx – nonblocking application platform.

Keeping a top 20 in azure table storage

Azure Table Storage is a cheap, fast and easy to use data storage system.

It does have it’s limitations.

All data has two keys partition key and row key which need to be unique in combination.

The best way to guarantee the return of say the latest 20 entries is to use  DateTime.MaxTicks – DateTime.Now.Utc (in a 19 digit left padded with zeros format) as the row key.

This means that reading the data you can just fetch the items by partition key with a limit on how many rows to return.

The difficulty will come in keeping the data from filling up the storage account if you never clean up.

I would suggest that when the data is read back if you get the maximum number of items that you queried then perform a second query to return 100 rows older than the oldest row that you have just fetched and delete them in a single async batch. This is the optimum delete size (as 100 is the max batch size for azure table storage).