Load the field data of an entity
The following examples demonstrate how you can load (read from the state fabric) the field data of an entity. These examples use the following function:
Result<std::size_t> LoadEntityField( Transaction& txn, const Entity& entity, TypeId keyTypeId, FieldIndex index, std::int8_t** dest) noexcept;
The Api::TypeId keyTypeId
parameter should receive the corresponding
Api::TypeId
from Api::BuiltinTypeId
. If there is no appropriate conversion, you
can use Api::BuiltinTypeId::Dynamic
.
Note
The value of FieldIndex
index must be greater than 0. The value 0 is
reserved for the index key (see StoreEntityIndexKey()
).
Example using primitive data types
namespace { constexpr Api::FieldIndex k_isTrueFieldId { /* value */ 1 }; } Result<void> LoadEntityFields( Api::Entity& entity, Transaction& transaction) { std::int8_t* dest = nullptr; WEAVERRUNTIME_TRY(Api::LoadEntityField( transaction, entity, Api::BuiltinTypeIdToTypeId( Aws::WeaverRuntime::Api::BuiltinTypeId::Bool), k_isTrueFieldId, &dest)); bool isTrueValue = *reinterpret_cast<bool*>(dest); }
Example using a struct to hold the data
namespace { constexpr Api::FieldIndex k_dataFieldId { /* value */ 1 }; } struct Data { bool boolData; float floatData; }; Result<void> LoadEntityFields( Api::Entity& entity, Transaction& transaction) { std::int8_t* dest = nullptr; WEAVERRUNTIME_TRY(Api::LoadEntityField( transaction, entity, Api::BuiltinTypeIdToTypeId( Aws::WeaverRuntime::Api::BuiltinTypeId::Dynamic), k_dataFieldId, &dest)); Data dataValue = *reinterpret_cast<Data*>(dest); }