Summary: GigaSpaces.NET - Batch Operations - Space operations with multiple objects in one call.

Overview

There are scenarios where the conventional space operation that work on a single entry object does not fit and there is a need to work on multiple entries from the space. Batch operations boost performance, since they interact with the server in one call, and retrieve the result from the space with one space operation. This allows the client and server to utilize the network bandwidth in an efficient manner - in some cases, up to 10 times faster than single based operations.

Operations Supported

WriteMultiple, UpdateMultiple, ReadMultiple, TakeMultiple

  • ReadMultiple should be handled with care since it can return a large data set (potentially all the space data). This might cause an out of memory error in the space and client process. You should use the Space Iterator to return the result in batches in such cases.
  • Destructive batch operations (take , write , update) should be performed with transactions – this allows the client to roll back the space to its initial state before the operation was started, in case of failure.
  • When calling WriteMultiple or UpdateMultiple, make sure null values are not part of the passed array.
  • When using WriteMultiple, you should verify that duplicated entries (with the same ID) do not appear as part of the passed array, since the identity of the object is determined based on its ID and not based on its reference. This is extremely important with an embedded space, since WriteMultiple injects the ID value into the object after the write operation.
  • ReadMultiple and TakeMultiple do not support timeout operations. The simple way to achieve this is by calling the Read operation with the proper timeout, and if non-null values are returned, perform the batch operation.

Code Example

The following example demonstrates the usage of the different batch operations:

public void BatchOperationsDemo()
{
	_logger.LogMainHeader("Batch Operations Demo");

	// Clear any old objects from the space:
	_proxy.Clear();

	_logger.LogHeader("WriteMultiple<T>(T[] objects) writes multiple objects at once.");
	Person[] persons = new Person[]
	{
		new Person("pid-001", "Alice", 25, null, null),
		new Person("pid-002", "Bob", 35, null, null),
		new Person("pid-137", "Charlie", 30, null, null)
	};
	_logger.LogMessage("Created array of {0} Persons:", persons.Length);
	for (int i = 0; i < persons.Length; i++)
		_logger.LogMessage("persons[{0}] - {1}", i, persons[i]);
	_logger.LogMessage("Writing persons array to space...");
	_proxy.WriteMultiple(persons);
	int count = _proxy.Count();
	_logger.LogMessage("Count() = {0}.", count);

	_logger.LogHeader("UpdateMultiple<T>(T[] objects) updates multiple objects at once.");
	_logger.LogMessage("Changing everyone's weight to 100.");
	for (int i = 0; i < persons.Length; i++)
		persons[i].Weight = 100;
	_logger.LogMessage("Updating persons in space...");
	_proxy.UpdateMultiple(persons);
	Person template = new Person();
	template.Weight = 100;
	count = _proxy.Count(template);
	_logger.LogMessage("Count Person with Weight=100: {0}.", count);

	_logger.LogHeader("ReadMultiple<T>(T template, int maxItems) reads up to maxItems objects at once.");
	_logger.LogMessage("Reading up to 3 persons...");
	Person[] results = _proxy.ReadMultiple(new Person(), persons.Length);
	_logger.LogMessage("Persons received from space: {0}", results.Length);
	for (int i = 0; i < results.Length; i++)
		_logger.LogMessage("results[{0}] - {1}", i, results[i]);
	_logger.LogMessage("Count() = {0}.", count);

	_logger.LogHeader("TakeMultiple<T>(T template, int maxItems) reads up to maxItems objects at once.");
	_logger.LogMessage("Taking up to 2 persons...");
	results = _proxy.TakeMultiple(new Person(), 2);
	_logger.LogMessage("Persons received from space: {0}", results.Length);
	for (int i = 0; i < results.Length; i++)
		_logger.LogMessage("results[{0}] - {1}", i, results[i]);
	count = _proxy.Count(new Person());
	_logger.LogMessage("Count() = {0}.", count);
	_logger.LogMessage("Again, Taking up to 2 persons...");
	results = _proxy.TakeMultiple(new Person(), 2);
	_logger.LogMessage("Persons received from space: {0}", results.Length);
	for (int i = 0; i < results.Length; i++)
		_logger.LogMessage("results[{0}] - {1}", i, results[i]);
	count = _proxy.Count(new Person());
	_logger.LogMessage("Count() = {0}.", count);
}
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence