Summary: Reading entries from the space
OverviewEntries can be retrieved from the space using the Read method. A read operation queries the space for an entry matching the provided template, and returns a copy of that entry (or null, if no match is found).
This page demonstrates basic space operations using a class called Person: public class Person { private String _name; private String _country; public String Name { get { return _name; } set { _name = value; } } public String Country { get { return _country; } set { _country = value; } } public Person() { } }
Blocking ReadCalling Read searches the space for a matching entry. If no match is found, the call returns immediatly with null. Person template = new Person(); // This call will block until a matching Person is available or 10 seconds have elapsed. Person p = proxy.Read(template, 10000); If the space contains a Person when the call is executed, it will return immediately. If the space does not contain a Person, the call will block until someone writes a Person to the space, or until the timeout has elapsed (in which case null is returned). Reading from a Partitioned ClusterIn a partitioned cluster each entry is stored in a specific partition, according to its routing property. When reading from such a cluster, if the routing property is specified in the template, the read request is sent directly to the relevant partition (which may or may not contain a match). If the routing property is not specified, the read request is broadcasted to all the cluster members, looking for a match. Naturally queries which include the routing property are more efficient (no broadcast means less network traffic) and faster (the proxy does not have to wait for a response from multiple members). Setting the routing property is usually done using the [SpaceRouting] attribute. For example, to set the Country property as the routing property of the Person class: public class Person { ... [SpaceRouting] public String Country { get { return _country; } set { _country = value; } } ... }
Improving performanceIf a certain property is commonly used when querying the space, you can instruct the space to index it for faster retrieval. Moreover, if one of the object's properties is marked as a [SpaceID] and is used in the template, the matching mechanism will recognize it and use it to optimize the search.
When a Template Matches More Than One EntryLet's examine the following piece of code: Person p1 = new Person(); p1.Name = "Alice"; proxy.Write(p1); Person p2 = new Person(); p2.Name = "Bob"; proxy.Write(p2); Person result1 = proxy.Read(new Person()); Person result2 = proxy.Read(new Person()); Assuming the space was empty and no one else is accessing the space at the same time: If you're interested in reading multiple entries at once, you can use the ReadMultiple operation. This operation is very similiar to the Read operation but it returns an array of entries instead of a single result. To limit the maximum number of results use the maxItems argument (default is Int32.MaxValue).
See Also
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |