Now that you understand how Slice
objects are used, let's go back to the simple Put
and Get
operations. Binary values can be stored just the same as string values, remembering a std::string
value can be regarded, such as Slice
as containing just data bytes and a length. The difference is that a std::string
retains ownership of its bytes and so is a safe container for binary data. In the following testBin()
sample we create a Slice
object with a pointer and length of an arbitrary binary struct
, and do the opposite to get the binary struct
back out of the std::string
value returned from Get
:
struct binValues { int intVal; double realVal; }; binValues b = {-99, 3.14}; Slice binSlice((const char*)&b, sizeof(binValues) ); assert( db->Put(WriteOptions(), "BinSample", binSlice).ok() ); std::string binRead; assert( db->Get(ReadOptions(), "BinSample", &binRead).ok() ); // treat the std::string as a container for arbitary...