All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Fields | Private Attributes | Related Functions
as_bytes Struct Reference

Detailed Description

Container for byte arrays.

Initialization

An as_bytes should be initialized via one of the provided function.

The as_bytes_inita(), as_bytes_init() and as_bytes_new() are used to initialize empty internal buffers of a specified size.

To initialize a stack allocated as_string, use as_bytes_init():

as_bytes_init(&b, 20);

The above initialized the variable, and allocated 20 bytes to the buffer using cf_malloc().

To use only stack allocated buffer for as_bytes, ten you should use as_bytes_inita():

You will see the APIs of the two are very similar. The key difference is as_bytes_inita() is a macro, which performs stack allocation inline.

If you need a heap allocated as_bytes instance, then you should use as_bytes_new():

Wrapping Byte Arrays

If you already have a byte array allocated and want to simply wrap it in an as_bytes, then use either:

The as_bytes_init_wrap() function is used to initialize a stack allocated as_bytes, then set the internal buffer to the byte array provided.

The as_bytes_new_wrap() function is used to create an initialize a new heap allocated as_bytes, then it will set the internal buffer to the byte array provided.

Destruction

When the as_bytes instance is no longer required, then you should release the resources associated with it via as_bytes_destroy():

Usage

as_bytes has a number of functions for reading and writing data to its internal buffer.

For reading at specified index:

Function Description
as_bytes_get() Copy the bytes in the buffer to another buffer.
as_bytes_get_byte() Read a byte from the buffer
as_bytes_get_int16() Read a 16-bit integer from the buffer
as_bytes_get_int32() Read a 32-bit integer from the buffer
as_bytes_get_int64() Read a 64-bit integer from the buffer

For writing at specified index:

Function Description
as_bytes_set() Copy a byte array into the buffer.
as_bytes_set_byte() Write a byte from the buffer
as_bytes_set_int16() Write a 16-bit integer from the buffer
as_bytes_set_int32() Write a 32-bit integer from the buffer
as_bytes_set_int64() Write a 64-bit integer from the buffer

For writing at to the end of the buffer:

Function Description
as_bytes_append() Copy a byte array into the buffer.
as_bytes_append_byte() Write a byte from the buffer
as_bytes_append_int16() Write a 16-bit integer from the buffer
as_bytes_append_int32() Write a 32-bit integer from the buffer
as_bytes_append_int64() Write a 64-bit integer from the buffer

Conversions

as_bytes is derived from as_val, so it is generally safe to down cast:

as_val val = (as_val) b;

However, upcasting is more error prone. When doing so, you should use as_bytes_fromval(). If conversion fails, then the return value is NULL.

Definition at line 239 of file modules/common/src/include/aerospike/as_bytes.h.

#include "as_bytes.h"

+ Inheritance diagram for as_bytes:
+ Collaboration diagram for as_bytes:

Data Fields

uint32_t capacity
 
bool free
 
uint32_t size
 
as_bytes_type type
 
uint8_t * value
 
- Data Fields inherited from as_val
cf_atomic32 count
 
bool free
 
enum as_val_t type
 

Private Attributes

as_val _
 

Related Functions

(Note that these are not member functions.)

bool as_bytes_append (as_bytes *bytes, const uint8_t *value, uint32_t size)
 
bool as_bytes_append_byte (as_bytes *bytes, uint8_t value)
 
bool as_bytes_append_int16 (as_bytes *bytes, int16_t value)
 
bool as_bytes_append_int32 (as_bytes *bytes, int32_t value)
 
bool as_bytes_append_int64 (as_bytes *bytes, int64_t value)
 
uint32_t as_bytes_capacity (const as_bytes *bytes)
 
uint32_t as_bytes_copy (const as_bytes *bytes, uint32_t index, uint8_t *value, uint32_t size)
 
void as_bytes_destroy (as_bytes *bytes)
 
bool as_bytes_ensure (as_bytes *bytes, uint32_t n, bool resize)
 
as_bytesas_bytes_fromval (const as_val *v)
 
uint8_t * as_bytes_get (const as_bytes *bytes)
 
uint32_t as_bytes_get_byte (const as_bytes *bytes, uint32_t index, uint8_t *value)
 
uint32_t as_bytes_get_int16 (const as_bytes *bytes, uint32_t index, int16_t *value)
 
uint32_t as_bytes_get_int32 (const as_bytes *bytes, uint32_t index, int32_t *value)
 
uint32_t as_bytes_get_int64 (const as_bytes *bytes, uint32_t index, int64_t *value)
 
as_bytes_type as_bytes_get_type (const as_bytes *bytes)
 
uint8_t * as_bytes_getorelse (const as_bytes *bytes, uint8_t *fallback)
 
as_bytesas_bytes_init (as_bytes *bytes, uint32_t capacity)
 
as_bytesas_bytes_init_wrap (as_bytes *bytes, uint8_t *value, uint32_t size, bool free)
 
as_bytesas_bytes_new (uint32_t capacity)
 
as_bytesas_bytes_new_wrap (uint8_t *value, uint32_t size, bool free)
 
bool as_bytes_set (as_bytes *bytes, uint32_t index, const uint8_t *value, uint32_t size)
 
bool as_bytes_set_byte (as_bytes *bytes, uint32_t index, uint8_t value)
 
bool as_bytes_set_int16 (as_bytes *bytes, uint32_t index, int16_t value)
 
bool as_bytes_set_int32 (as_bytes *bytes, uint32_t index, int32_t value)
 
bool as_bytes_set_int64 (as_bytes *bytes, uint32_t index, int64_t value)
 
void as_bytes_set_type (as_bytes *bytes, as_bytes_type type)
 
uint32_t as_bytes_size (const as_bytes *bytes)
 
uint8_t * as_bytes_tobytes (const as_bytes *bytes, uint32_t *size)
 
as_valas_bytes_toval (const as_bytes *b)
 
bool as_bytes_truncate (as_bytes *bytes, uint32_t n)
 

Friends And Related Function Documentation

bool as_bytes_append ( as_bytes bytes,
const uint8_t *  value,
uint32_t  size 
)
related

Append raw bytes of given size.

uint8_t value[3] = {'a','b','c'};
as_bytes_append(&bytes, value, 3);
Parameters
bytesThe bytes to append to.
valueThe buffer to read from.
sizeThe number of bytes to read from the value.
Returns
On success, true. Otherwise an error occurred.
bool as_bytes_append_byte ( as_bytes bytes,
uint8_t  value 
)
related

Append a uint8_t (byte).

as_bytes_append_byte(&bytes, 'a');
Returns
On success, true. Otherwise an error occurred.

Definition at line 734 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_append_int16 ( as_bytes bytes,
int16_t  value 
)
related

Append an int16_t value.

as_bytes_append_int16(&bytes, 123);
Returns
On success, true. Otherwise an error occurred.

Definition at line 750 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_append_int32 ( as_bytes bytes,
int32_t  value 
)
related

Append an int32_t value.

as_bytes_append_int32(&bytes, 123);
Returns
On success, true. Otherwise an error occurred.

Definition at line 766 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_append_int64 ( as_bytes bytes,
int64_t  value 
)
related

Append an int64_t value.

Returns
On success, true. Otherwise an error occurred.

Definition at line 782 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_capacity ( const as_bytes bytes)
related

Get the number of bytes allocated.

Parameters
bytesThe bytes to get the capacity of.
Returns
The number of bytes allocated.

Definition at line 423 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_copy ( const as_bytes bytes,
uint32_t  index,
uint8_t *  value,
uint32_t  size 
)
related

Copy into value up to size bytes from the given as_bytes, returning the number of bytes copied.

uint8_t value[3] = {0};
uint32_t sz = as_bytes_copy(&bytes, 0, value, 3);
if ( sz == 0 ) {
// sz == 0, means that an error occurred
}
Parameters
bytesThe bytes to read from.
indexThe positing in bytes to read from.
valueThe byte buffer to copy into.
sizeThe number of bytes to copy into the buffer.
Returns
The number of bytes read and stored into value. 0 (zero) indicates an error has occurred.
void as_bytes_destroy ( as_bytes bytes)
related

Destroy the as_bytes and release associated resources.

Parameters
bytesThe bytes to destroy.

Definition at line 390 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_ensure ( as_bytes bytes,
uint32_t  n,
bool  resize 
)
related

Ensure the bytes buffer can handle n additional bytes.

Using the current size, we see if size + n is within the capacity of the bytes' buffer. If so, then return true.

If resize is true and size + n exceeds the capacity of the bytes's buffer, then resize the capacity of the buffer by n bytes. If the buffer was heap allocated, then cf_realloc() will be used to resize. If the buffer was stack allocated, it will be converted to a heap allocated buffer using cf_malloc() and then its contents will be copied into the new heap allocated buffer.

If resize is false, and if the capacity is not sufficient, then return false.

as_bytes_ensure(&bytes, 100, true);
Parameters
bytesThe bytes to ensure the capacity of.
nThe number of additional bytes to ensure bytes can handle.
resizeIf true and capacity is not sufficient, then resize the buffer.
Returns
On success, true. Otherwise an error occurred.
as_bytes * as_bytes_fromval ( const as_val v)
related

Convert from an as_val.

Definition at line 881 of file modules/common/src/include/aerospike/as_bytes.h.

uint8_t * as_bytes_get ( const as_bytes bytes)
related

Get the raw value of this instance.

uint8_t * raw = as_bytes_get(&bytes);
Parameters
bytesThe bytes to get the raw value from.
Returns
The pointer to the raw value.

Definition at line 492 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_get_byte ( const as_bytes bytes,
uint32_t  index,
uint8_t *  value 
)
related

Read a single byte from the given bytes.

uint8_t value = 0;
uint32_t sz = as_bytes_get_byte(&bytes, 0, &value);
if ( sz == 0 ) {
// sz == 0, means that an error occurred
}
Returns
The number of bytes read and stored into value. 0 (zero) indicates an error has occurred.

Definition at line 544 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_get_int16 ( const as_bytes bytes,
uint32_t  index,
int16_t *  value 
)
related

Read an int16_t from the given bytes.

int16_t value = 0;
uint32_t sz = as_bytes_get_int16(&bytes, 0, &value);
if ( sz == 0 ) {
// sz == 0, means that an error occurred
}
Returns
The number of bytes read and stored into value. 0 (zero) indicates an error has occurred.

Definition at line 565 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_get_int32 ( const as_bytes bytes,
uint32_t  index,
int32_t *  value 
)
related

Read an int32_t from the given bytes.

int32_t value = 0;
uint32_t sz = as_bytes_get_int32(&bytes, 0, &value);
if ( sz == 0 ) {
// sz == 0, means that an error occurred
}
Returns
The number of bytes read and stored into value. 0 (zero) indicates an error has occurred.

Definition at line 586 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_get_int64 ( const as_bytes bytes,
uint32_t  index,
int64_t *  value 
)
related

Read an int64_t from the given bytes.

int64_t value = 0;
uint32_t sz = as_bytes_get_int64(&bytes, 0, &value);
if ( sz == 0 ) {
// sz == 0, means that an error occurred
}
Returns
The number of bytes read and stored into value. 0 (zero) indicates an error has occurred.

Definition at line 607 of file modules/common/src/include/aerospike/as_bytes.h.

as_bytes_type as_bytes_get_type ( const as_bytes bytes)
related

Get the type of bytes.

Parameters
bytesThe bytes to get the type of.
Returns
The type of bytes.

Definition at line 438 of file modules/common/src/include/aerospike/as_bytes.h.

uint8_t * as_bytes_getorelse ( const as_bytes bytes,
uint8_t *  fallback 
)
related

Get the raw value of this instance. If the instance is NULL, then return the fallback value.

uint8_t * raw = as_bytes_getorelse(&bytes, NULL);
Parameters
bytesThe bytes to get the raw value from.
fallbackThe value to return if bytes is NULL.
Returns
The pointer to the raw value if bytes is not NULL. Otherwise return the fallback.

Definition at line 474 of file modules/common/src/include/aerospike/as_bytes.h.

as_bytes * as_bytes_init ( as_bytes bytes,
uint32_t  capacity 
)
related

Initializes a stack allocated as_bytes. Allocates an internal buffer on the heap of specified capacity using cf_malloc().

as_bytes bytes;
as_bytes_init_empty(&bytes, 10);
Parameters
bytesThe bytes to initialize.
capacityThe number of bytes to allocate on the heap.
Returns
On success, the initializes bytes. Otherwise NULL.
as_bytes * as_bytes_init_wrap ( as_bytes bytes,
uint8_t *  value,
uint32_t  size,
bool  free 
)
related

Initializes a stack allocated as_bytes, wrapping the given buffer.

uint8_t raw[10] = {0};
as_bytes bytes;
as_bytes_init_wrap(&bytes, raw, 10, false);
Parameters
bytesThe bytes to initialize.
valueThe initial value.
sizeThe number of bytes of the initial value.
freeIf true, then as_bytes_destroy() will free the value.
Returns
On success, the initializes bytes. Otherwise NULL.
as_bytes * as_bytes_new ( uint32_t  capacity)
related

Create and initialize a new heap allocated as_bytes. Allocates an internal buffer on the heap of specified capacity using cf_malloc().

as_bytes * bytes = as_bytes_new(10);
Parameters
capacityThe number of bytes to allocate.
Returns
On success, the initializes bytes. Otherwise NULL.
as_bytes * as_bytes_new_wrap ( uint8_t *  value,
uint32_t  size,
bool  free 
)
related

Creates a new heap allocated as_bytes, wrapping the given buffer.

uint8_t raw[10] = {0};
as_bytes * bytes = as_bytes_new_wrap(raw, 10, false);
Parameters
valueThe initial value.
sizeThe number of bytes of the initial value.
freeIf true, then as_bytes_destroy() will free the value.
Returns
On success, the initializes bytes. Otherwise NULL.
bool as_bytes_set ( as_bytes bytes,
uint32_t  index,
const uint8_t *  value,
uint32_t  size 
)
related

Copy raw bytes of given size into the given as_bytes starting at specified index.

as_bytes_set(&bytes, 0, (uint8_t[]){'a','b','c'}, 3);
Parameters
bytesThe bytes to write to.
indexThe position to write to.
valueThe buffer to read from.
sizeThe number of bytes to read from the value.
Returns
On success, true. Otherwise an error occurred.
bool as_bytes_set_byte ( as_bytes bytes,
uint32_t  index,
uint8_t  value 
)
related

Set a byte at given index.

as_bytes_append_byte(&bytes, 'a');
Returns
On success, true. Otherwise an error occurred.

Definition at line 646 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_set_int16 ( as_bytes bytes,
uint32_t  index,
int16_t  value 
)
related

Set a byte at given index.

as_bytes_append_byte(&bytes, 'a');
Returns
On success, true. Otherwise an error occurred.

Definition at line 662 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_set_int32 ( as_bytes bytes,
uint32_t  index,
int32_t  value 
)
related

Set a byte at given index.

as_bytes_append_byte(&bytes, 'a');
Returns
On success, true. Otherwise an error occurred.

Definition at line 678 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_set_int64 ( as_bytes bytes,
uint32_t  index,
int64_t  value 
)
related

Set a byte at given index.

as_bytes_append_byte(&bytes, 'a');
Returns
On success, true. Otherwise an error occurred.

Definition at line 694 of file modules/common/src/include/aerospike/as_bytes.h.

void as_bytes_set_type ( as_bytes bytes,
as_bytes_type  type 
)
related

Set the type of bytes.

Parameters
bytesThe bytes to set the type of.
typeThe type for the bytes.

Definition at line 452 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes_size ( const as_bytes bytes)
related

Get the number of bytes used.

Parameters
bytesThe bytes to get the size of.
Returns
The number of bytes used.

Definition at line 408 of file modules/common/src/include/aerospike/as_bytes.h.

uint8_t * as_bytes_tobytes ( const as_bytes bytes,
uint32_t *  size 
)
related

Get the bytes value.

Get the bytes value.

Get the bytes value.

Definition at line 851 of file modules/common/src/include/aerospike/as_bytes.h.

as_val * as_bytes_toval ( const as_bytes b)
related

Convert to an as_val.

Definition at line 871 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes_truncate ( as_bytes bytes,
uint32_t  n 
)
related

Truncate the bytes' buffer. The size specifies the number of bytes to remove from the end of the buffer.

This means, if the buffer has size of 100, and we truncate 10, then the remaining size is 90.

Truncation does not modify the capacity of the buffer.

as_bytes_truncate(&bytes, 10);
Parameters
bytesThe bytes to truncate.
nThe number of bytes to remove from the end.
Returns
On success, true. Otherwise an error occurred.

Field Documentation

as_val as_bytes::_
private

as_boolean is a subtype of as_val. You can cast as_boolean to as_val.

Definition at line 246 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes::capacity

The number of bytes allocated to as_bytes.value.

Definition at line 251 of file modules/common/src/include/aerospike/as_bytes.h.

bool as_bytes::free

If true, then as_bytes.value will be freed when as_bytes_destroy() is called.

Definition at line 267 of file modules/common/src/include/aerospike/as_bytes.h.

uint32_t as_bytes::size

The number of bytes used by as_bytes.value.

Definition at line 256 of file modules/common/src/include/aerospike/as_bytes.h.

as_bytes_type as_bytes::type

The type of bytes.

Definition at line 272 of file modules/common/src/include/aerospike/as_bytes.h.

uint8_t * as_bytes::value

A sequence of bytes.

Definition at line 261 of file modules/common/src/include/aerospike/as_bytes.h.


The documentation for this struct was generated from the following file: