You can insert an object into the space using the write() and writeMultiple() methods. When a new object is inserted into the space, it embeds a unique ID - called the UID. The UID can be generated explicitly by the client using a unique value generated by the application business logic or using a sequencer running within the space.
The space UID for space object can be created in three different ways:
When a space object has no SpaceId property declared, the space generates a UID for the object.
When a space object has a property which is declared as SpaceId and marked as auto-generate=false, the UID is generated based on the value of the ID field the user is setting.
When a space object has a property which is declared as SpaceId and marked as auto-generate=true, the UID is generated by the space and placed back into the field using the relevant setter method. In this case, the field must be a java.lang.String type.
See the Global ID Generator for a generic ID generator service you may use with your application.
Fetching an object from the Space
Space objects can be fetched from the space using the GigaSpace interface methods read(), readMultiple(), readIfExists(), take(), takeMultiple(), takeIfExists() , readByID() and takeByID().
The readByID() and takeByID() allows you to read or remove objects using their IDs (and their routing value) without constructing a template or SQLQuery.
XAP 7.1.1 support the readByIds and takeByIds methods. These allows fetching a collection of objects using their IDs.
Reading an Object using its ID
With the following code example, the EmployeeID field is decorated using @SpaceId (autogenerate=false) or via:
<id name="employeeID" auto-generate="false" />
Here is how you can fetch the object back from the space:
The following shows how to read multiple objects using their IDs:
GigaSpace gigaSpace;
// Initialize an ids array
Integer[] ids = newInteger[] { ... };
// Set a routing key value (not mandatory but more efficient)
Integer routingKey = ...;
// Read objects from space
ReadByIdsResult<Employee> result = gigaSpace.readByIds(Employee.class, ids, routingKey);
// Loop through results
for (Employee employee : result) {
// ...
}
When a space Class has no SpaceId property declared, it will be returned without any ID. This means that update operations cannot be executed on the space Class. Only the read() and write() methods can be executed.
When a space Class has a property which is declared as SpaceId and auto-generate=true and its value is null, the SpaceId field will store the ID.
When a space Class has a property which is declared as SpaceId and auto-generate=true and its value is not null, the SpaceId field will store the original value stored within the SpaceId field.
When a space Class has a property which is declared as SpaceId and auto-generate=false, the SpaceId field will store the original value stored within the SpaceId field used to generate the UID.
Compound SpaceId
You might need to construct an ID that will be comprised from a user defined Object rather than using a Numeric or String type field. In such a case your user defined class used as the SpaceId data type must implement the hashCode and equals methods. See below example:
The CompoundId
import java.io.Serializable;
public class CompoundId implements Serializable{
publicString key1;
publicString key2;
publicString key3;
publicString toString()
{
return key1+ "_" + key2+ "_" + key3;
}
publicint hashCode() {
finalint prime = 31;
int result = 1;
result = prime * result + ((key1 == null) ? 0 : key1.hashCode());
result = prime * result + ((key2 == null) ? 0 : key2.hashCode());
result = prime * result + ((key3 == null) ? 0 : key3.hashCode());
return result;
}
publicboolean equals(Object obj) {
if (this == obj)
returntrue;
if (obj == null)
returnfalse;
if (getClass() != obj.getClass())
returnfalse;
CompoundId other = (CompoundId) obj;
if (key1 == null) {
if (other.key1 != null)
returnfalse;
} elseif (!key1.equals(other.key1))
returnfalse;
if (key2 == null) {
if (other.key2 != null)
returnfalse;
} elseif (!key2.equals(other.key2))
returnfalse;
if (key3 == null) {
if (other.key3 != null)
returnfalse;
} elseif (!key3.equals(other.key3))
returnfalse;
returntrue;
}
}
The Space Class
import com.gigaspaces.annotation.pojo.SpaceClass;
import com.gigaspaces.annotation.pojo.SpaceId;
@SpaceClass
public class MySpaceClass {
CompoundId id;
String data;
@SpaceId(autoGenerate = false)
public CompoundId getId() {
return id;
}
public void setId(CompoundId id) {
this.id = id;
}
publicString getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
publicString toString() {
return"ID:" + id + " Data:"+ data;
}
}
The Application Code
GigaSpace space = new GigaSpaceConfigurer (new UrlSpaceConfigurer ("jini://*/*/mySpace")).gigaSpace();
CompoundId id = new CompoundId();
id.key1="1";
id.key2="2";
id.key3="3";
MySpaceClass obj = new MySpaceClass();
obj.setData("AAAAAAAAA");
obj.setId(id);
space.write(obj);
MySpaceClass ret = space.readById(MySpaceClass.class, id);