Páginas

SyntaxHighlighter

sexta-feira, 29 de outubro de 2010

Using Text instead of String to store large texts in GAE's datastore

I just started playing with Gaelyk and GAE. The goal is to learn the GAE APIs and services and improve my groovy foo!
So, in order to practice it I've chosen to develop a simple pastebin like application using Gaelyk.

I started it by modeling the main entity: Snippet, which is a very simple POGO with only 3 properties:

class Snippet implements Serializable {
 
 String name
 String text
 String language
 
}

The groovylet below ilustrates how we can easily instantiate a new entity from the posted request parameters and then persist it into the data store.

def snippet = new Snippet(params);

// coerce a POJO into an entity
def snippetEntity = snippet as Entity
snippetEntity.save()

forward "/snippet.gptl"

So far so good. After some tests I got the following error:

Error: GroovyServlet Error: script: '/WEB-INF/groovy/snippet/create.groovy': Script processing failed.name: String properties must be 500 characters or less. Instead, use com.google.appengine.api.datastore.Text, which can store strings of any length.com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:192)

The message is very self explanatory. I changed the Snippet and the groovylet respectively to:

import com.google.appengine.api.datastore.Text

class Snippet implements Serializable {
 
 String name
 Text text
 String language
 
}

// create.groovy
def snippet = new Snippet();
snippet.name = params.name;
snippet.language = params.language;
snippet.text = params.text as Text;

def snippetEntity = snippet as Entity
snippetEntity.save()


Note that I changed property type from String to Text and had to set each property separately in the groovylet because there is no autocasting support for Text types.

You can check the last deployed version in AppEngine here: Snippetr
And the source code here: http://code.google.com/p/snippetr/