Difference between revisions of "ResultSet Framework"

From Gcube Wiki
Jump to: navigation, search
(Extending ResultElementBase)
(Usage Example)
Line 38: Line 38:
 
=== Usage Example ===
 
=== Usage Example ===
 
The ResultSets that can be created are content independent but depending on the content inserted different operations can be performed. So different readers and writers have been implemented to offer different levels of versatility depending on the content. Here he will mainly focus on the XML authoring and retrieving which is most commonly used and thouroughly tested up to this point. As more types of readers / writers become popular additional documentation will be made available
 
The ResultSets that can be created are content independent but depending on the content inserted different operations can be performed. So different readers and writers have been implemented to offer different levels of versatility depending on the content. Here he will mainly focus on the XML authoring and retrieving which is most commonly used and thouroughly tested up to this point. As more types of readers / writers become popular additional documentation will be made available
 +
 +
<span style="color:red">The code examples are not exaustive in error checking and should not be considered as coding templates</span>
  
 
==== Creating an RS ====
 
==== Creating an RS ====

Revision as of 13:45, 7 February 2007

ResultSet Framework

Introduction

The ResultSet Framework provides the enabling mechanism to pass by reference data between components and / or services that can be hosted in the same or remote nodes. It utilizes the WSRF Framework for keeping state and offers value adding opeartions for encapsulating remote and local calls, paging of content, data movement, remote processing and much more fun stuf which you can discover your self instead of me having to right them down you lazy bum (J).......

Implementation Overview

The framework consists of 4 sub components :

  1. the ResultSet library
  2. the ResultSet Service
  3. the ResultSet Client
  4. the ResultSet Garbage Collector


  • The ResultSet is the enabling element that creates a linked list of pages holding the records that are part of the specific result set. It can iterate over the created parts and perform various operations on the structure of the list and the contents themselves. Its operations are mosty part specific bu they can also affect the entire result chain. Once a page of content has been created and the next in line has been initialized the part cannot be altered. Once it has been declared that the authoring of a result set has finished, the entire chain of it is immutible.
  • The ResultSet Service is merely a WS front-end to the ResultSet library. It creates and manages WS-Resources holding rferences to instances of the ResultSet components. These instances are responsible to hold state regarding the iteration over the chain of pages and for accessing / modifying / querying the data held.
  • The ResultSet Client is a library giving access to both high and low level operations that can be performed on a specific ResultSet component instance and its underlying data. It enables handling of ResultSets that are both wrapped through a WS front-end and that are locally manipulated though direct java invocations (within JVM).
  • The ResultSet Garbage Collector is a deamon-like program that can be initialized to clear up resources used by the ResultSet Framework (removing filesystem resources and destroying created WS-Resources)

Dependencies

  • ResultSetLibrary
    • jdk 1.5
    • WS-Core
  • ResultSetService
    • jdk 1.5
    • WS-Core
    • ResultSetLibrary
  • ResultSetClient
    • jdk 1.5
    • WS-Core
    • ResultSetLibrary
    • ResultSetService
  • ResultSetGarbageCollector
    • jdk 1.5
    • WS-Core
    • ResultSetLibrary
    • ResultSetService stubs

Usage Example

The ResultSets that can be created are content independent but depending on the content inserted different operations can be performed. So different readers and writers have been implemented to offer different levels of versatility depending on the content. Here he will mainly focus on the XML authoring and retrieving which is most commonly used and thouroughly tested up to this point. As more types of readers / writers become popular additional documentation will be made available

The code examples are not exaustive in error checking and should not be considered as coding templates

Creating an RS

/*A method that creates a new RSXMLWriter and populates it with some results*/
public static RSXMLWriter createRSWriter() throws Exception{

/*Create a new writer with the default parametrizable values*/
RSXMLWriter writer=RSXMLWriter.getRSXMLWriter();
/*Create a new writer which should have as a condition for paging inserted results either having
30 records per page or having a total size surpasing 1024 bytes */
//writer=RSXMLWriter.getRSXMLWriter(30,1024);
/*Create a new writer and set a lifetime property of 1000 millisecs. Also set that the paging
condition will be having 10 records per page or a total size surpasing teh default value*/
//writer=RSXMLWriter.getRSXMLWriter(new PropertyElementBase[]{new PropertyElementLifeSpanGC(1000)});
//writer.setRecsPerPart(10);
/*Add a new result with the given id, collection, rank and payload*/
writer.addResults(new ResultElementGeneric("id1","collection1","rank1","payload"));
/*Add a new result with the given id, collection, rank and payload*/
writer.addResults(new ResultElementGeneric("id2","collection1","rank2","payload"));
/*Add an array of results with the given id, collection, rank and payload*/
writer.addResults(new ResultElementBase[]{new ResultElementGeneric("id3","collection1","rank3","payload"),
new ResultElementGeneric("id4","collection1","rank4","payload")});
/*Give back the writer*/
return writer;

}

Retrieving a locator

From a writer

/*This method retrieves an instance of an RSLocator capable of identifying a ResultSet authored by the provided RSXMLWriter. Depending on the type of resource requested, this identifier can either be an identifier pointing to the local node (filesystem) or an idenitfier capable of pinpointing the RS through a web service using the WS-Resource pattern. One can create many locators (of the same or different) type for the same authored RS*/
public static RSLocator getLocator(RSXMLWriter writer) throws Exception{

RSLocator locator=null;
/*Retrieves a locator identifying the RS in the local node*/
//locator=writer.getRSLocator(new RSResourceLocalType());
/*Retrieves a locator identifying the RS through WS-Resource pattern accesible from any node. The
The service pointed to must be in the localhost. This method can be used when manipulating the RSXMLWriter
outside container context. Otherwise the following method can be used*/
//locator=writer.getRSLocator(new RSResourceWSRFType("http://localhost:8080/wsrf/services/diligentproject/searchservice/ResultSetService"));
/*Retrieves a locator identifying the RS through WS-Resource pattern accesible from any node. The
default constructor utilizes the container context (which must be available) to compose the localy hosted
ResultSetService that must be deployed*/
locator=writer.getRSLocator(new RSResourceWSRFType());
return locator;

}

From a reader

/*This method retrieves an instance of an RSLocator capable of identifying the ResultSet read by the provided RSXMLReader. Everythime the method is called the same resource is identified. Both types of locators can be serialized to string and utilized to initialize a new RSLocator*/
public static RSLocator getLocator(RSXMLReader reader) throws Exception{

RSLocator locator=null;
/*Retrieves a locator identifying the RS. The type is the same as the one used to initialize the RSXMLReader*/
locator=reader.getRSLocator();
return new RSLocator(locator.getLocator());

}

Reading an RS

Page by Page

/*This method instantiates a new RSXMLReader to point to the RS identified by the provided RSLocator and iterates over each page retrieving the contents*/
public static void readRS(RSLocator locator) throws Exception {

RSXMLReader reader=RSXMLReader.getRSXMLReader(locator);
while(!reader.isLast()){
reader.getNumberOfResults();
/*Retrieve the records in the current page as instances of the provided class that extends the ResultElementBase*/
ResultElementBase []res=reader.getResults(ResultElementGeneric.class);
reader.getNextPart();
}
reader.getFirstPart();
do{
reader.getFullPayload();
}while(reader.getNextPart());

}

Iterating

/*This method instantiates a new RSXMLReader to point to the RS identified by the provided RSLocator and iterates over each record retrieving the contents*/
public static void iterateRS(RSLocator locator) throws Exception {

RSXMLIterator iter=RSXMLReader.getRSXMLReader(locator).getRSIterator();
while(iter.hasNext()){
ResultElementGeneric elem=(ResultElementGeneric)iter.next(ResultElementGeneric.class);
/*Retrieve the DocID of the retrieved record*/
if(elem!=null) elem.getRecordAttributes(ResultElementGeneric.RECORD_ID_NAME);
}

}

Extending Base elements

Two types of elements are currently extendable in the context of the Framework.

  • PropertyElementBase - Which is used to add / retrieve property elements describing the authored RSs
  • ResultElementBase - Which is used to add / retrieve records from the RS

These elements must be available to both the producer and the consumer as library elements in order to be used. of course this is not mandatory and a consumer can ustilize a differnt element to retrieve the records that a consumer inserted using some other element. It is in the repsonsibility of the producer / consumer to synchronize the elements they use. These elements serve mainly as containers for the actual payload that should be inserted / retrieved possibly adding some common handling functionality on top of that payload.

Extending PropertyElementBase

/*This is ane example of extending the PropertyElementBase to derive Property Element that is used to add WSRF EndpointReferenceType serializations to the RS head page*/
public class PropertyElementWSEPR extends PropertyElementBase{

/* The Type of the Property this Property element produces*/
public static String propertyType="WS-EPR";
private String epr=null;
/* Default contructor required by the PropertyElementBase in order to instantiate the class using reflection*/
public PropertyElementWSEPR(){}
/* Initializes a new PropertyElementWSEPR with the given EndpointReferenceType serialization */
public PropertyElementWSEPR(String epr) throws Exception{
this.epr=epr;
setType(PropertyElementWSEPR.propertyType);
}
/* The usefull property payload from the PropertyElementWSEPR point of view as valid xml */
public String toXML() throws Exception{
return this.epr;
}
/* The usefull property payload from the PropertyElementWSEPR point of view as returned by
PropertyElementWSEPR.fromXML */
public void fromXML(String xml) throws Exception{
this.epr=xml;
}

}

Extending ResultElementBase

/* A Result element that extends the ResultElementBase and can be used to insert and retrieve xml records from an RS */ public class ResultElementGeneric extends ResultElementBase{

/** * The name of the id attribute holding the identifier value */ public static final String RECORD_ID_NAME="DocID"; /** * The name of the collection attribute holding the collection value */ public static final String RECORD_COLLECTION_NAME="CollID"; /** * The name of the rank attribute holding the record ranking */ public static final String RECORD_RANK_NAME="RankID";

private String payload;

/** * Default contructor nessecary for the framework */ public ResultElementGeneric(){}

/** * Creates a new {@link ResultElementGeneric} * * @param id The value of the id. This cannot be null or an empty string * @param collection The collection this id belongs to. This cannot be null or an empty string * @param rank The rank this id got. This cannot be null or an empty string * @param payload The payload of the record * @throws Exception The {@link ResultElementGeneric} could not be created */ public ResultElementGeneric(String id,String collection, String rank,String payload) throws Exception{ Vector<RecordAttribute> at=new Vector<RecordAttribute>(); at.add(new RecordAttribute(ResultElementGeneric.RECORD_ID_NAME,id)); at.add(new RecordAttribute(ResultElementGeneric.RECORD_COLLECTION_NAME,collection)); at.add(new RecordAttribute(ResultElementGeneric.RECORD_RANK_NAME,rank)); setRecordAttributes(at.toArray(new RecordAttribute[0])); if(payload==null || payload.trim().length()==0) this.payload=null; else this.payload=payload; }

/** * Creates a new {@link ResultElementGeneric}. The rank attribute is set to 1.0 * * @param id The value of the id. This cannot be null or an empty string * @param collection The collection this id belongs to. This cannot be null or an empty string * @param payload The payload of the record * @throws Exception The {@link ResultElementGeneric} could not be created */ public ResultElementGeneric(String id,String collection,String payload) throws Exception{ Vector<RecordAttribute> at=new Vector<RecordAttribute>(); at.add(new RecordAttribute(ResultElementGeneric.RECORD_ID_NAME,id)); at.add(new RecordAttribute(ResultElementGeneric.RECORD_COLLECTION_NAME,collection)); at.add(new RecordAttribute(ResultElementGeneric.RECORD_RANK_NAME,"1.0")); setRecordAttributes(at.toArray(new RecordAttribute[0])); if(payload==null || payload.trim().length()==0) this.payload=null; else this.payload=payload; }

private void setPayload(String payload){ this.payload=payload; }

/** * Retrieves the payload * * @return The payload */ public String getPayload(){ return this.payload; }

/** * @see org.diligentproject.searchservice.searchlibrary.resultset.elements.ResultElementBase#toXML() */ public String toXML() throws Exception{ return payload; }

/** * @see org.diligentproject.searchservice.searchlibrary.resultset.elements.ResultElementBase#fromXML(java.lang.String) */ public void fromXML(String xml) throws Exception{ try{ setPayload(xml); }catch(Exception e){ throw new Exception("provided xml string is not valid ResultSelement serialization",e); } }

}

Complex Operations