The Blob serializes binary data more efficiently than using types like byte[]
or char[]
.
A field of type Blob is initialized by providing a void
pointer to a binary data buffer and the length of the buffer.
The Blob supports two modes:
Unmanaged
- The Blob points to the user's data and it is the user's responsibiliby to release the memory. Managed
- The Blob holds a copy of the user's data and is responsible for allocating and releasing its own memory.blob
.blobBuffer
and blobBufferBinary
(the latter is using binary
storage type):
<class name="com.gigaspaces.tests.test_blob" cpp-name="EntryWithBlob" dotnet-name="GigaSpaces.Tests.Interop.test_blob" persist="true" replicate="true" fifo="false" >
<property name="idField" type="string" null-value="" />
<id name="idField" auto-generate="true" />
<property name="intIndex" type="int" null-value="0" index="true"/>
<routing name="intIndex">
<property name="blobBuffer" type="blob">
<property name="blobBufferBinary" type="blob" storage-type="binary" />
</class>
Here is an example for using the Blob:
void writeData(void* buffer, int length)
{
// Writing the entry to the space
EntryWithBlob entry;
entry.intIndex = 1;
entry.blobBuffer.reset(buffer, length, Blob::Unmanaged);
globalProxy->write(&entry);
}
void readData()
{
// Reading the entry from the space
EntryWithBlobPtr spaceEntry;
EntryWithBlob entryWithBlobTemplate;
entryWithBlobTemplate.intIndex = 1;
spaceEntry.reset(globalProxy->read(&entryWithBlobTemplate));
// Accessing the data
void* buffer = spaceEntry->blobBuffer.getBuffer();
// ...
// Performing operations using buffer provided that spaceEntry is not destroyed
// ...
// Getting ownership of the data.
buffer = spaceEntry->blobBuffer.detachBuffer();
// The variable 'buffer' now points to the data memory while the 'blobBuffer' field
// is empty.
spaceEntry.reset(); // Destroying the content of spaceEntry. We can continue using 'buffer'.
// ...
// Performing operations using buffer
// ...
// Memory cleanup (since we got ownership of the data it's our responsibility to release it)
delete buffer;
}
Public Types | |
enum | Mode { Unmanaged, Managed } |
Mode of memory management in the Blob. More... | |
Public Member Functions | |
Blob (const Blob &blob) | |
Copy constructor. | |
Blob (void *p, int length, Mode mode) | |
Constructs a Blob that encapsulates a given binary data. | |
Blob () | |
Default constructor - constructs an empty Blob. | |
void * | detachBuffer () |
Enables the user to get ownership of the data. | |
void * | getBuffer () |
Returns a pointer to the data. | |
int | getLength () |
Returns the length of the data. | |
Mode | getMode () |
Returns the mode that specifies how the data is kept (either Unmanaged or Managed ). | |
bool | isEmpty () |
Returns whether the Blob holds any data. | |
Blob & | operator= (const Blob &blob) |
Assigns new content to the Blob based on another blob. | |
void | reset (void *p, int length, Mode mode) |
Initializes the Blob with new content. | |
void | reset () |
Releases the Blob's content and leaves it empty. | |
virtual | ~Blob (void) |
Performs any required memory cleanup. |
enum Blob::Mode |
Blob::Blob | ( | ) |
Default constructor - constructs an empty Blob.
Blob::Blob | ( | void * | p, | |
int | length, | |||
Mode | mode | |||
) |
Blob::Blob | ( | const Blob & | blob | ) |
Copy constructor.
blob | - Other blob to copy from |
Managed
then the data is not copied only a reference is added to it. Blob::~Blob | ( | void | ) | [virtual] |
Performs any required memory cleanup.
void * Blob::detachBuffer | ( | ) |
Enables the user to get ownership of the data.
A pointer to the data is returned and the content of the Blob is released so it becomes empty.
void
pointer to the dataManaged
then it is now the responsibility of the user to release the memory of the data. void * Blob::getBuffer | ( | ) |
Returns a pointer to the data.
void
pointer to the dataint Blob::getLength | ( | ) | [inline] |
Returns the length of the data.
Mode Blob::getMode | ( | ) | [inline] |
bool Blob::isEmpty | ( | ) | [inline] |
void Blob::reset | ( | void * | p, | |
int | length, | |||
Mode | mode | |||
) |
void Blob::reset | ( | ) |
Releases the Blob's content and leaves it empty.