Process data in a SPUPad
After you define the code to create and save data into a SPUPad, the next step is to process the SPUPad data in some way.
Continuing the stringpad example, the following sample code is
a new UDF called string_pad_get that takes an input
position. The UDF uses the position value to return the character
in that position of the array that is saved in the SPUPad. If the
stringpad does not exist, the function exits with an error.
Note: This
sample SPUPad uses shared memory because it calls the getPad() function.
If you want the SPUPad to use file-backed memory, use the getFilePad()
function instead. If you accidentally use both getPad (or getMemPad)
and getFilePad, the UDF will fail with an error that the Existing
CPad type not same as this get request.
#include "udxinc.h"
#include <string.h>
using namespace nz::udx;
struct Root
{
char* data;
int size;
};
class StringPadGet: public Udf
{
private:
public:
static Udf* instantiate();
virtual ReturnValue evaluate()
{
if(isArgNull(0))
{
throwUdxException("cannot accept null arguments.");
}
if(argType(0)!=UDX_INT32)
{
throwUdxException("First argument must be int4.");
}
CPad* pad = getPad("stringpad");
Root *ro = (Root*) pad->getRootObject(sizeof(Root));
if(!ro)
{
throwUdxException("Pad does not exist");
}
else
{
int index = int32Arg(0);
if(index<0||index>=ro->size)
{
throwUdxException("Index out of bounds");
}
StringReturn *ret = stringReturnInfo();
ret->size=1;
ret->data[0]=ro->data[index];
NZ_UDX_RETURN_STRING(ret);
}
}
};
Udf* StringPadGet::instantiate()
{
return new StringPadGet;
}