Facebook Twitter Gplus LinkedIn YouTube E-mail RSS
Home GigaSpaces Hello Worm Document

Hello Worm Document

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
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
                       __
     (\   .-.   .-.   /_")
      \\_//^\\_//^\\_//
       `"`   `"`   `"`

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.

Map leftWorm =  
   [ "name""Ding"
     "numberOfLegs"6
     "asciiArt" : ['     __                          ',                   
                   '    (\"_\\   .-.   .-.   /)      ', 
                   '       \\\\_//^\\\\_//^\\\\_//      ',  
                   '        `"`   `"`   `"`          '], 
      "direction" : "Left"
      "artist" : "Dean" 
   ];   

println "${leftWorm['name']} heads to the ${leftWorm['direction']}"
for (line in leftWorm["asciiArt"]) {
   println line
}

gigaSpace =
   new GigaSpaceConfigurer(new UrlSpaceConfigurer("/./space"))
   .gigaSpace()

gigaspace.write(new SpaceDocument("helloworm.Worm", leftWorm))

 
 
It then looks for a worm that has more than 4 legs and heads to the right
 
def query =  
    new SQLQuery<SpaceDocument>(
       "worm"

       "numberOfLegs > 4 AND direction = 'right'"

        QueryResultType.DOCUMENT)
Map rightWorm = gigaspace.read(query).getProperties()

println "${rightWorm['name']} heads to the ${rightWorm['direction']}" 
for (line in rightWorm.get("asciiArt")) { 
   println line 
}

 
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 
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; 

private String reverseLine(String line) { 
    return  
        new StringBuffer(line) 
            .reverse() 
            .toString() 
            .replace('\\','x'
            .replace('/''\\'
            .replace('x','/'
            .replace(')''x'
            .replace('('')'
            .replace('x''('); 
}

Did you like this? Share it:
 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© GigaSpaces on Application Scalability | Open Source PaaS and More