Summary: Writing objects to the space

Overview

Objects are written to the space using the ISpaceProxy.Write method. A write operation inserts a copy of the given object to the space, hence, any changes made to the object after the write operation do not affect the object in the space.

Writing a Single Object

When writing your objects to the space, construct the object as usual, and use the ISpaceProxy.Write operation through a proxy to the space:

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()
  {
  }
}
Person kermit = new Person();
kermit.UserId = "011-1111111";
kermit.Name = "Kermit the frog";
//Starts an embedded space and creates a proxy to the space
ISpaceProxy proxy = GigaSpacesFactory.FindSpace("/./mySpace");
//Write the object to the space
proxy.Write(kermit);

Writing Multiple Objects

Sometimes it is required to write multiple objects at once. For instance, when the proxy is to a remote space on a different machine or a different process. Instead of sending each object separately on the network, and suffering from high latency, a batch of objects can be sent together in a single operation using the ISpaceProxy.WriteMultiple operation. When writing multiple objects to the space, construct an array of your objects, and use the ISpaceProxy.WriteMultiple operation through a proxy to the space.

Person kermit = new Person();
kermit.UserId = "011-1111111";
kermit.Name = "Kermit the frog";

Person missPiggy = new Person();
missPiggy.UserId = "022-2222222";
missPiggy.Name = "Miss Piggy";

Person[] persons = new Person[]{kermit, missPiggy};
//Write the objects to the space
proxy.WriteMultiple(persons);

Updating Existing Objects in the Space

When objects that exist in the space need to be updated, the ISpaceProxy.Write or ISpaceProxy.WriteMultiple operation should be used with the proper WriteModifier:

Person kermit = new Person();
kermit.UserId = "011-1111111";
kermit.Name = "Kermit the frog";
proxy.Write(kermit);

...
//Updating the name of the already existing kermit object in space
kermit.Name = "Kermit the green frog";
proxy.Write(kermit, WriteModifiers.WriteOrUpdate);

The modifier WriteModifiers.UpdateOnly can be used if the operation is only allowed to update an existing object and throw exception
if no matching object exists in space.

Performance optimization

No Return Value

By default the write operation returns a LeaseContext object with Lease object or the previous version of the object (via the LeaseContext.Object). To avoid this overhead you should use the WriteModifiers.NoReturnValue modifier with the write operation. Once used, the write operation will have a null as a return value. This avoids the usual network traffic generated by sending the previous version of the object (update operation) or the Lease object (write operation) back into the client side. Moreover, is it reduces the need to instansiate a return value on embedded spaces as well which is reduced garbage collection and more significant for update operation.
Use this option to improve application write operation performance both with remote and embedded space when the return value is not needed. Here is how you can use the WriteModifiers.NoReturnValue modifier, in this case together with WriteModifiers.WriteOrUpdate modifier:

proxy.Write(kermit, WriteModifiers.WriteOrUpdate | WriteModifiers.NoReturnValue);

Delta Update

You may update selected space object fields (delta) using the WriteModifiers.PartialUpdate modifier. This option is useful when having objects with large number of fields where you would like to update only few of the space object fields. This optimizes the network utilization and avoids serializing/de-serializing the entire object fields when interacting with a remote space as well with embedded space.

When using this modifier, fields that you do not want be update should have the value null. This means that only fields which are set will be sent from the client into the space to replace the existing field's value. In case of a backup (replica) space, the primary space will replicate only the updated fields (delta) to the replica space. Make sure the updated object include its ID when using this option.

To use Delta updates you don't have to implement any special interface or have special serialization code. You can use regular object as usual.

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.

Here is a partial update example:

MyClass obj = new MyClass(){Id="1", Field1="A", Field2="B", Field3="C"};
proxy.Write(obj);

...
//Reading the object from the space
MyClass obj2 = proxy.ReadById<MyClass>("1");
//Updating only relevant property, in this case Field2
obj2.Field2 = "BBBB";
//Marking properties that are not updated with null;
obj2.Field1 = null;
obj3.Field3 = null;
//Partially updating the object in space, only value of Field2 will be updated.
proxy.Write(obj2, WriteModifiers.PartialUpdate);
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence