Summary: Reading objects from the space

Overview

Reading an object from the space can be done using the ISpaceProxy.Read method. A read operation fetches an object from the space, which matches a certain criteria. This criteria can be template-based or query-based.

Reading a Single Object

Reading from the space can be done either using an object template or an IQuery. The operation searches the space for entries that match the given template or query, and returns an object that matches it, or null if none exists. The object that is returned, is a copy of the actual object that is stored in the space. Hence, any changes on the object that is returned by a read operation do not affect the object that is stored in the space.

A template is an instance of the object type that is being read. The template should include field values that you would like to use for matching, and NullValue values for all the rest of the fields.

The Person object:

class Person
{
  private string _userId;
  private string _name;    
  [SpaceID]
  public string UserId
  {
    get { return _userId; }
    set { _userId = value; }
  }

  public string Name
  {
    get { return _name; }
    set { _name = value; }
  }

  public Person()
  {
  }
}
//Create a template of person with user id "011-1111111"
Person personTemplate = new Person();
personTemplate.UserId = "011-1111111";

//Finds a remote space named mySpace
ISpaceProxy proxy = SpaceProxyProviderFactory.Instance.FindSpace("jini://*/*/mySpace");
//Reads a person from the space with user id "011-1111111"
Person resultRead = proxy.Read(personTemplate);

See how to connect to a space

See what SpaceID is in Object Metadata

Alternatively,an IQuery can used as the template, for instance an SqlQuery:

//Create a template of person with user id "011-1111111"
Person personTemplate = new Person();
personTemplate.UserId = "011-1111111";
SqlQuery<Person> query = new SqlQuery<Person>(personTemplate, "UserId = ?");

//Reads a person from the space with user id "011-1111111"
Person resultRead = proxy.Read(personTemplate);

SqlQuery gives more power than a simple template. It can include ranges and more – for full details, refer to SqlQuery.

Null Values
If the template object contains fields or properties that are not nullable (Primitives, Structures), they should include a NullValue indication that should be ignored when matching is performed. See Object Metadata to find out how to specify null values.

Reading Multiple Objects

A template that is used for matching can be used to read multiple objects. This is done using the ISpaceProxy.ReadMultiple operation. This operation is very similiar to the ISpaceProxy.Read operation, but instead of returning one object, it returns an array of objects that matches the supplied template or query.

Person personTemplate = new Person();
personTemplate.Name = "John";

//Reads all the persons that their name is "John"
Person[] resultReadMultiple = proxy.ReadMultiple(personTemplate);
Considerations

ReadMultiple might need too much memory if it returns more objects than the available memory can contain, or too much network traffic if the size of the entire objects is large. See Working with Large Object Sets to find out how to address this matter.

GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence