Space Object ID
POJO is available, but in Smart DIH, by default, only Space Document is used.
How is the Space Object ID Generated?
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 asauto-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 asauto-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 ajava.lang.String
type.
The SpaceId Field content
- 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 theread()
andwrite()
methods can be executed. - When a space Class has a property which is declared as
SpaceId
andauto-generate=true
and its value isnull
, theSpaceId
field will store the ID. - When a space Class has a property which is declared as
SpaceId
andauto-generate=true
and its value is not null, theSpaceId
field will store the original value stored within theSpaceId
field. - When a space Class has a property which is declared as
SpaceId
andauto-generate=false
, theSpaceId
field will store the original value stored within theSpaceId
field used to generate the UID.
Compound SpaceId
A compound SpaceId (consisting of more than one field) can be implemented using Java annotations.
@SpaceId(properties = {"firstName", "lastName"})
public class Person {
private String firstName;
private String lastName;
...
// Operations are the same, for example
gigaSpace.write( new Person("John", "Smith"));
Space Routing
When @SpaceRouting
is not specified, the routing will be resolved from the value of the first property in the @SpaceId
properties list. In the above example, the value of the field is "firstName". @SpaceRouting
specified on a different field’s get
method, will return its value as the routing.
IdQuery Matching
Object id = CompoundSpaceId.from("John", "Smith");
gigaSpace.read(new IdQuery<>(Person.class, id));
gigaSpace.readById(Person.class, id);
gigaSpace.readByIds(Person.class, new Object[]{id1, id2});
Template Matching
//exact match
Person template = new Person("John", "Smith");
gigaSpace.read(template);
//partial match
Person template = new Person("John", null);
gigaSpace.read(template);
SQLQuery Matching
//exact matching
SQLQuery<Person> query = new SQLQuery<>(Person.class, "firstName=? AND lastName=?", "John", "Smith");
gigaSpace.read(query);
//partial matching
SQLQuery<Person> query = new SQLQuery<>(Person.class, "firstName=?", "John");
gigaSpace.read(query);
Custom 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 toString
, hashCode
and equals
methods.
The compound ID class must implement a toString
method that return a unique String for each ID.
See below example:
import java.io.Serializable;
public class CompoundId implements Serializable{
public String key1;
public String key2;
public String key3;
public String toString()
{
return key1+ "_" + key2+ "_" + key3;
}
public int hashCode() {
final int 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;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof CompoundId))
return false;
CompoundId other = (CompoundId) obj;
if (key1 == null) {
if (other.key1 != null)
return false;
} else if (!key1.equals(other.key1))
return false;
if (key2 == null) {
if (other.key2 != null)
return false;
} else if (!key2.equals(other.key2))
return false;
if (key3 == null) {
if (other.key3 != null)
return false;
} else if (!key3.equals(other.key3))
return false;
return true;
}
}
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;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String toString() {
return "ID:" + id + " Data:"+ data;
}
}
GigaSpace space = new GigaSpaceConfigurer (new SpaceProxyConfigurer("space")).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);
See the Global ID Generator for a generic ID generator service you may use with your application. To learn how to read space entries by ID see ID Queries.