Code Snippets
Write and Read
Write and ReadIn order to write or update objects in the Space, you should use the write method of the GigaSpace interface. The write method is used to write objects if these are introduced for the first time, or update them if these already exist in the space. In order to override these default semantics, you can use the overloaded write methods which accept update modifiers such as UpdateModifiers.UPDATE_ONLY. The read methods are used to retrieve objects from the Space. The read method returns a copy of the matching object to the client. To read more than one object, you should use the readMultiple methods of the GigaSpace interface. To define the criteria for the operation, all of these methods accept either a template object, or an SQLQuery instance. A template object is an example object of the class you would like to read. For an object in the space to match the template, each of the non-null properties in the template must match its values for these properties. To read an object by its ID (key), you should use one of the readById/readByIds methods. Getting Space proxy: UrlSpaceConfigurer urlSpaceConfigurer = new UrlSpaceConfigurer("jini://*/*/mySpace"); GigaSpace space = new GigaSpaceConfigurer(urlSpaceConfigurer.space()) .defaultTakeTimeout(1000) .defaultReadTimeout(1000) .gigaSpace(); The following writes an Employee object and reads it back using a simple template: GigaSpace space; Employee employee = new Employee("Last Name", new Integer(32)); employee.setFirstName("first name"); LeaseContext<Employee> lc = space.write(employee); Employee template = new Employee(); Employee result = space.read(template); Notification Registration Notification RegistrationThe following registers for notifications: GigaSpace space; SimpleNotifyEventListenerContainer notifyEventListenerContainer = new SimpleNotifyContainerConfigurer(space) .template(new Employee()) .eventListenerAnnotation(new Object() { @SpaceDataEvent public void eventHappened(Object event) { System.out.println("onEvent called Got" + event); } }) .fifo(true) .notifyWrite(true) .notifyUpdate(true) .notifyContainer(); Batch Write Batch WriteWhen writing a batch of objects into the space, these should be placed into an array to be used by the GigaSpace.writeMultiple operation. The returned array will include the corresponding LeaseContext object. GigaSpace space; Employee emps[] = new Employee[2]; emps[0] = new Employee("Last Name A", new Integer(10)); emps[1] = new Employee("Last Name B", new Integer(20)); try { LeaseContext[] leaseContexts = space.writeMultiple(emps); for (int i = 0;i<leaseContexts.length ; i++) { System.out.println ("Object UID " + leaseContexts[i].getUID() + " inserted into the space"); } } catch (WriteMultipleException e) { IWriteResult[] writeResult = e.getResults(); for (int i = 0;i< writeResult.length ; i++) { System.out.println ("Problem with Object UID " + writeResult "); } } Batch Read Batch ReadThe following queries the space using SQL: GigaSpace space; String querystr = "age>40"; SQLQuery query = new SQLQuery(Employee.class, querystr); Employee results[] = space.readMultiple(query , Integer.MAX_VALUE);
Clear Clear ObjectsYou can use the SQLQuery with the GigaSpace.clear to remove objects from the space: GigaSpace space; String querystr = "age>30"; SQLQuery query = new SQLQuery(Employee.class, querystr); space.clear(query);
Updating an Object Updating an ObjectThe GigaSpace.write with the WriteModifiers.UPDATE_ONLY modifier should be used to explicitly perform an update operation. The WriteModifiers.UPDATE_OR_WRITE is the default mode with write operations. This means that subsequent calls to the write operation with an object with identical SpaceId will result in an update operation - i.e. a new object will not be inserted into the space.
The GigaSpace.write has a few activity modes - With each mode the return object options are different.:
When updating an object, you can specify 0 (ZERO) as the lease time. This will instruct the space to use the original lease time used when the object has been written into the space. UPDATE_OR_WRITE Example: try { LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , WriteModifiers.UPDATE_OR_WRITE); if ( ret.getObject() == null) { // successful write } if (ret.getObject() instanceof Employee) { // successful update } } catch (UpdateOperationTimeoutException uote) { // Object is locked - unsuccessful update } WRITE_ONLY Example: try { LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , WriteModifiers.WRITE_ONLY); if ( ret.getObject() == null) { // successful write } } catch (EntryAlreadyInSpaceException eainse) { // Object already exists - unsuccessful write } UPDATE_ONLY Example: try { LeaseContext ret = space.write(employee ,/*lease*/ 0 ,/*timeout*/ 1000 , WriteModifiers.UPDATE_ONLY); if ( ret == null) { // Object is locked - unsuccessful update } else if (ret.getObject() instanceof Employee) { // successful update } } catch (EntryNotInSpaceException enise) { // Object not in space - unsuccessful update } catch (SpaceOptimisticLockingFailureException solfe) { // Client holds wrong version of the object - unsuccessful update. We need to read it again and issue the update call again. } PARTIAL_UPDATE Example: GigaSpace space = new GigaSpaceConfigurer (new UrlSpaceConfigurer("jini://*/*/mySpace").noWriteLease(true)).gigaSpace(); // initial insert MyClass obj = new MyClass(); obj.setId("1"); obj.setField1("A"); obj.setField2("B"); obj.setField3("C"); space.write(obj); // reading object back from the space MyClass obj2 = space.readById(MyClass.class , "1"); // updating only field2 obj2.setField1(null); obj2.setField2("BBBB"); obj2.setField3(null); try { space.write(obj2,0,0,WriteModifiers.PARTIAL_UPDATE); } catch (EntryNotInSpaceException enise) { // Object not in space - unsuccessful update } Batch Update Batch Update
The GigaSpace.writeMultiple returns an array of objects which correspond to the input object array. The returned object element can be one of the following:
Changing an Object Changing an ObjectThe GigaSpace.change operation allows you to change a specific content of an existing object(s) in the space. Unlike the write operation that may update an existing object, the change operation does not require reading the object and later sending its updated copy with the operation back to the space. Unlike the write operation PARTIAL_UPDATE modifier, you may use the change operation to update a specific field value without retrieving its prior value. This is very helpful when incrementing or decrementing a numeric field, updating value of nested field or adding an item to a collection field without having to send the entire updated collection to the space. The change operation is designed to boost the application performance since only the required “delta” is sent to the space. When having a replica space deployed, only the change operation is replicated. For example, when having a collection field, using the change operation reduces the need to send the entire updated collection as only the added member is replicated. The following example incrementing an integer field within an existing space object by 1 using the change operation. String id = "KEY_123456789"; IdQuery<WordCount> idQuery = new IdQuery<WordCount>(WordCount.class, id); space.change(idQuery, new ChangeSet().increment("count", 1)); Upon any failure, the change operation will throw ChangeException which will contain full details regarding the failure.
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |