GigaSpaces 8 now supports documents composed of key,value pairs. Unlike POJOs, which force users to design a fixed data schema (in the form of a class definition) a document allows adding and removing properties at runtime. With GigaSpaces the two data models are interoperable. You can write a POJO and read a document and vice-versa. Indexing and transactions are of-course also supported. Such an exciting feature warrants a "Hello Worm" app that demonstrates how weakly typed Documents are interoperable with strongly typed POJOs. Writing a new Worm to the Space ASCII Art credit belongs to Dean The groovy code below writes a worm to the space using the (weekly typed) document API. Notice how the worm heads to the left. println "${leftWorm['name']} heads to the ${leftWorm['direction']}" gigaSpace = gigaspace.write(new SpaceDocument("helloworm.Worm", leftWorm)) println "${rightWorm['name']} heads to the ${rightWorm['direction']}" private String reverseLine(String line) {
Ding heads to the left
__
("_\ .-. .-. /)
\\_//^\\_//^\\_//
`"` `"` `"`
Looking for a worm that has more than 4 legs and heads to the right
gniD heads to the right
__
(\ .-. .-. /_")
\\_//^\\_//^\\_//
`"` `"` `"`
[ "name": "Ding",
"numberOfLegs": 6,
"asciiArt" : [' __ ',
' (\"_\\ .-. .-. /) ',
' \\\\_//^\\\\_//^\\\\_// ',
' `"` `"` `"` '],
"direction" : "Left",
"artist" : "Dean"
];
for (line in leftWorm["asciiArt"]) {
println line
}
new GigaSpaceConfigurer(new UrlSpaceConfigurer("/./space"))
.gigaSpace()
new SQLQuery<SpaceDocument>(
"worm",
"numberOfLegs > 4 AND direction = 'right'",
QueryResultType.DOCUMENT)
Map rightWorm = gigaspace.read(query).getProperties()
for (line in rightWorm.get("asciiArt")) {
println line
}
SQLQuery<Worm> unprocessedWorms() {
SQLQuery<Worm> query = new SQLQuery<Worm>(Worm.class, "direction = 'left'");
return query;
}
@SpaceDataEvent
public Worm processWorm(Worm worm) {
worm.setDirection("right");
String name = worm.getName();
worm.setName(reverseLine(name));
List<String> asciiArt = worm.getAsciiArt();
for (int i = 0 ; i < asciiArt.size() ; i++) {
String line = asciiArt.get(i);
asciiArt.set(i,reverseLine(line));
}
return worm;
}
return
new StringBuffer(line)
.reverse()
.toString()
.replace('\\','x')
.replace('/', '\\')
.replace('x','/')
.replace(')', 'x')
.replace('(', ')')
.replace('x', '(');
}
Hello Worm Document
Map leftWorm =
It then looks for a worm that has more than 4 legs and heads to the right
def query =
The following code is deployed on the Space itself. It listens for worms heading left and turns them right. This code, written in Java, uses the (strongly typed) POJO API. It is interoperable with the (weakly typed) document API.
@EventTemplate





