28 #include "deelx/deelx.h" 37 void pow_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
38 void lg_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
39 void ln_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
40 void exp_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
41 void regex_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
42 void BDC_ratio_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
45 void MD5_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv );
49 int blob_pack (
const mxArray* pcItem,
bool bStreamable,
50 void** ppBlob,
size_t* pBlob_size,
51 double *pdProcess_time,
double* pdRatio,
52 const char* compressor = g_compression_type,
53 int level = g_compression_level );
54 int blob_unpack (
const void* pBlob,
size_t blob_size,
55 bool bStreamable, mxArray** ppItem,
56 double* pProcess_time,
double* pdRatio );
74 void pow_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv ){
76 double base, exponent, result;
79 switch( sqlite3_value_type( argv[0] ) )
82 sqlite3_result_null( ctx );
85 base = sqlite3_value_double( argv[0] );
89 switch( sqlite3_value_type( argv[1] ) )
92 sqlite3_result_null( ctx );
95 exponent = sqlite3_value_double( argv[1] );
100 result = pow( base, exponent );
104 sqlite3_result_error( ctx,
"pow(): evaluation error", -1 );
108 sqlite3_result_double( ctx, result );
124 void lg_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv ){
126 double value, result;
129 switch( sqlite3_value_type( argv[0] ) )
132 sqlite3_result_null( ctx );
135 value = sqlite3_value_double( argv[0] );
140 result = log10( value );
144 sqlite3_result_error( ctx,
"lg(): evaluation error", -1 );
148 sqlite3_result_double( ctx, result );
162 void ln_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv ){
164 double value, result;
167 switch( sqlite3_value_type( argv[0] ) )
170 sqlite3_result_null( ctx );
173 value = sqlite3_value_double( argv[0] );
178 result = log( value );
182 sqlite3_result_error( ctx,
"ln(): evaluation error", -1 );
186 sqlite3_result_double( ctx, result );
200 void exp_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv ){
202 double value, result;
205 switch( sqlite3_value_type( argv[0] ) )
208 sqlite3_result_null( ctx );
211 value = sqlite3_value_double( argv[0] );
216 result = exp( value );
220 sqlite3_result_error( ctx,
"exp(): evaluation error", -1 );
224 sqlite3_result_double( ctx, result );
242 void regex_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv ){
244 char *str = NULL, *pattern = NULL, *replace = NULL;
246 sqlite3_result_null( ctx );
262 CRegexpT <char> regexp( pattern );
265 MatchResult result = regexp.Match( str );
268 if( result.IsMatched() )
270 char *str_value = NULL;
275 int start = result.GetStart();
276 int end = result.GetEnd();
277 int len = end - start;
279 str_value = (
char*)
MEM_ALLOC( len + 1,
sizeof(
char) );
282 if( str_value && len > 0 )
284 memset( str_value, 0, len + 1 );
285 strncpy( str_value, &str[start], len );
291 char* result = regexp.Replace( str, replace );
296 int len = (int)strlen( result );
297 str_value = (
char*)
MEM_ALLOC( len + 1,
sizeof(
char) );
299 if( str_value && len )
301 strcpy( str_value, result );
304 CRegexpT<char>::ReleaseString( result );
314 char *temp = (
char*)
MEM_ALLOC( len,
sizeof(
char) );
326 sqlite3_result_text( ctx, str_value, -1, SQLITE_TRANSIENT );
357 void MD5_func( sqlite3_context *ctx,
int argc, sqlite3_value **argv ){
364 tbhv1_t* tbh1 = NULL;
365 tbhv2_t* tbh2 = NULL;
368 unsigned char digest[16];
369 char* str_result = NULL;
370 const char hex_chars[] =
"0123456789ABCDEF";
372 sqlite3_result_null( ctx );
376 sqlite3_result_error( ctx,
"MD5(): implementation for litte endian only!", -1 );
381 switch( sqlite3_value_type( argv[0] ) )
385 int bytes = sqlite3_value_bytes( argv[0] );
386 sqlite3_int64 value = (long)sqlite3_value_int64( argv[0] );
388 MD5_Init( &md5_ctx );
389 MD5_Update( &md5_ctx, &value, bytes );
390 MD5_Final( digest, &md5_ctx );
396 int bytes = sqlite3_value_bytes( argv[0] );
397 double value = (double)sqlite3_value_double( argv[0] );
399 MD5_Init( &md5_ctx );
400 MD5_Update( &md5_ctx, &value,
sizeof(
double ) );
401 MD5_Final( digest, &md5_ctx );
407 int bytes = sqlite3_value_bytes( argv[0] );
409 assert( NULL != value );
413 MD5_Init( &md5_ctx );
414 MD5_Update( &md5_ctx, value, (
int)strlen( value ) );
415 MD5_Final( digest, &md5_ctx );
422 int bytes = sqlite3_value_bytes( argv[0] );
423 tbhv1_t* tbh1 = (tbhv1_t*)sqlite3_value_blob( argv[0] );
424 tbhv2_t* tbh2 = (tbhv2_t*)sqlite3_value_blob( argv[0] );
427 if( !tbh1->validMagic() )
429 MD5_Init( &md5_ctx );
430 MD5_Update( &md5_ctx, (
void*)tbh1, bytes );
431 MD5_Final( digest, &md5_ctx );
436 if( tbh1->validVer() )
438 MD5_Init( &md5_ctx );
439 MD5_Update( &md5_ctx, tbh1->getData(), (int)(bytes - tbh1->dataOffset()) );
440 MD5_Final( digest, &md5_ctx );
445 if( tbh2->validVer() && tbh2->validCompression() )
447 mxArray* pItem = NULL;
448 double process_time = 0.0, ratio = 0.0;
454 MD5_Init( &md5_ctx );
455 MD5_Update( &md5_ctx, mxGetData( pItem ), (
int)data_size );
456 MD5_Final( digest, &md5_ctx );
469 str_result = (
char*)
MEM_ALLOC( 16*2+1, 1 );
473 memset( str_result, 0, 16*2+1 );
475 for(
int i = 0; i < 16; i++ )
477 str_result[2*i+0] = hex_chars[digest[i] >> 4];
478 str_result[2*i+1] = hex_chars[digest[i] & 0x0f];
481 sqlite3_result_text( ctx, str_result, -1, SQLITE_TRANSIENT );
504 sqlite3_result_null( ctx );
507 if( SQLITE_BLOB == sqlite3_value_type( argv[0] ) )
509 tbhv1_t* tbh1 = (tbhv1_t*)sqlite3_value_blob( argv[0] );
510 tbhv2_t* tbh2 = (tbhv2_t*)sqlite3_value_blob( argv[0] );
511 size_t blob_size = (size_t)sqlite3_value_bytes( argv[0] );
515 if( tbh1 && tbh1->validMagic() && tbh1->validVer() )
517 sqlite3_result_double( ctx, 1.0 );
519 else if( tbh2 && tbh2->validMagic() && tbh2->validVer() && tbh2->validCompression() )
521 mxArray* pItem = NULL;
522 double process_time = 0.0;
526 sqlite3_result_error( ctx,
"BDCRatio(): an error while unpacking occured!", -1 );
530 sqlite3_result_double( ctx, ratio );
538 sqlite3_result_error( ctx,
"BDCRatio(): only BLOB type supported!", -1 );
560 sqlite3_result_null( ctx );
563 if( SQLITE_BLOB == sqlite3_value_type( argv[0] ) )
565 tbhv1_t* tbh1 = (tbhv1_t*)sqlite3_value_blob( argv[0] );
566 tbhv2_t* tbh2 = (tbhv2_t*)sqlite3_value_blob( argv[0] );
567 size_t blob_size = (size_t)sqlite3_value_bytes( argv[0] );
568 double process_time = 0.0;
571 sqlite3_result_null( ctx );
574 if( tbh1 && tbh1->validMagic() && tbh1->validVer() )
576 sqlite3_result_double( ctx, 0.0 );
578 else if( tbh2 && tbh2->validMagic() && tbh2->validVer() && tbh2->validCompression() )
580 mxArray* pItem = NULL;
581 void* dummy_blob = NULL;
582 size_t dummy_blob_size = 0;
583 double process_time = 0.0;
587 sqlite3_result_error( ctx,
"BDCRatio(): an error while unpacking occured!", -1 );
591 sqlite3_result_error( ctx,
"BDCRatio(): an error while packing occured!", -1 );
595 sqlite3_result_double( ctx, process_time );
598 sqlite3_free( dummy_blob );
604 sqlite3_result_error( ctx,
"BDCPackTime(): only BLOB type supported!", -1 );
626 sqlite3_result_null( ctx );
629 if( SQLITE_BLOB == sqlite3_value_type( argv[0] ) )
631 tbhv1_t* tbh1 = (tbhv1_t*)sqlite3_value_blob( argv[0] );
632 tbhv2_t* tbh2 = (tbhv2_t*)sqlite3_value_blob( argv[0] );
633 size_t blob_size = (size_t)sqlite3_value_bytes( argv[0] );
634 double process_time = 0.0;
638 if( tbh1 && tbh1->validMagic() && tbh1->validVer() )
640 sqlite3_result_double( ctx, 0.0 );
642 else if( tbh2 && tbh2->validMagic() && tbh2->validVer() && tbh2->validCompression() )
644 mxArray *pItem = NULL;;
648 sqlite3_result_error( ctx,
"BDCUnpackTime(): an error while unpacking occured!", -1 );
652 sqlite3_result_double( ctx, process_time );
660 sqlite3_result_error( ctx,
"BDCUnpackTime(): only BLOB type supported!", -1 );
679 sqlite3_free( *ppBlob );
700 void** ppBlob,
size_t* pBlob_size,
701 double *pdProcess_time,
double* pdRatio,
702 const char* compressor,
int level )
706 assert( pcItem && ppBlob && pBlob_size && pdProcess_time && pdRatio );
709 mxArray* byteStream = NULL;
719 if( !bStreamable || !
serialize( pcItem, byteStream ) )
721 err.
set( MSG_ERRMEMORY );
732 *pdProcess_time = 0.0;
743 if( g_compression_level )
755 size_t blob_size_uncompressed;
761 blob_size_uncompressed =
765 assert( blob_size_uncompressed != 0 );
768 *pdRatio = (double)*pBlob_size / blob_size_uncompressed;
770 if( *pBlob_size >= blob_size_uncompressed )
785 err.
set( MSG_BLOBTOOBIG );
793 err.
set( MSG_ERRMEMORY );
807 if( g_compression_check && !numericSequence.
isLossy() )
809 mxArray* unpacked = NULL;
810 bool is_equal =
false;
814 if( !
blob_unpack( (
void*)tbh2, (
int)*pBlob_size, bStreamable, &unpacked, &dummy, &dummy ) )
816 sqlite3_free( tbh2 );
818 err.
set( MSG_ERRCOMPRESSION );
828 sqlite3_free( tbh2 );
830 err.
set( MSG_ERRCOMPRESSION );
836 *ppBlob = (
void*)tbh2;
851 err.
set( MSG_BLOBTOOBIG );
858 err.
set( MSG_ERRMEMORY );
870 *ppBlob = (
void*)tbh1;
899 int blob_unpack(
const void* pBlob,
size_t blob_size,
bool bStreamable,
901 double* pdProcess_time,
double* pdRatio )
904 bool bIsByteStream =
false;
909 mxArray* pItem = NULL;
912 assert( NULL != ppItem && NULL != pdProcess_time && NULL != pdRatio );
915 *pdProcess_time = 0.0;
918 tbhv1_t* tbh1 = (tbhv1_t*)pBlob;
919 tbhv2_t* tbh2 = (tbhv2_t*)pBlob;
922 if( !tbh1->validPlatform() )
924 mexWarnMsgIdAndTxt(
"MATLAB:MKSQLITE:BlobDiffArch", ::
getLocaleMsg( MSG_WARNDIFFARCH ) );
935 if( !tbh1->validMagic() )
937 err.
set( MSG_UNSUPPTBH );
942 if( tbh1->m_clsid == mxUNKNOWN_CLASS )
944 bIsByteStream =
true;
945 tbh1->m_clsid = mxUINT8_CLASS;
948 switch( tbh1->m_ver )
951 case sizeof( tbhv1_t ):
954 pItem = tbh1->createNumericArray(
true );
959 case sizeof( tbhv2_t ):
961 if( !tbh2->validCompression() )
963 err.
set( MSG_UNKCOMPRESSOR );
968 pItem = tbh2->createNumericArray(
false );
976 void* cdata = tbh2->getData();
977 size_t cdata_size = blob_size - tbh2->dataOffset();
982 err.
set( MSG_ERRCOMPRESSION );
991 *pdRatio = (double)cdata_size / numericSequence.
m_result_size;
1004 err.
set( MSG_UNSUPPTBH );
1012 mxArray* pDeStreamed = NULL;
1016 err.
set( MSG_ERRMEMORY );
1021 pItem = pDeStreamed;
Encapsulating a MATLAB mxArray.
void * m_result
compressor output
bool deserialize(const mxArray *pByteStream, mxArray *&pItem)
Converts byte stream back into originally MATLAB variable.
void ln_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
Natural logarithm function implementation.
type_complexity_e Complexity(bool bCanSerialize=false) const
Get complexity information. Which storage level is necessary (scalar, vector, matrix, text, blob)
void BDC_pack_time_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
BDCPackTime function implementation.
const char * getLocaleMsg(int iMsgNr)
Returns the translation for a defined message.
size_t ByElement() const
Returns size in bytes of one element.
int blob_unpack(const void *pBlob, size_t blob_size, bool bStreamable, mxArray **ppItem, double *pProcess_time, double *pdRatio)
uncompress a typed blob and return as MATLAB array
size_t ByData() const
Returns data size in bytes.
const char * getCompressorName()
Get compressor name.
void regex_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
Regular expression function implementation.
void init(mxClassID clsid, mwSize nDims, const mwSize *pSize)
Initialization (hides base class init() function)
Compression of numeric (real number) arrays.
double utils_get_wall_time()
Returns current counter time in seconds.
void blob_free(void **pBlob)
Free memory allocated for a BLOB.
char TBH_endian[]
endian (little or big)
MATLAB hidden (officially undocumented) feature of serializing data.
bool pack(void *rdata, size_t rdata_size, size_t rdata_element_size, bool isDoubleClass)
Calls the qualified compressor (deflate) which always allocates sufficient memory (m_cdata) ...
size_t dataOffset()
Get header offset to begin of array data (initialized)
structs, cells, complex data (SQLite typed ByteStream BLOB)
bool isLossy()
Returns true, if current compressor modifies value data.
void utils_destroy_array(mxArray *&pmxarr)
Freeing memory allocated by mxCreateNumericMatrix() or mxCreateNumericArray().
int NumDims() const
Returns number of dimensions.
void MD5_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
MD5 hashing implementation.
bool can_serialize()
Returns true, if streaming is switched on (user setting) and serialization is accessible.
Helperclass for error message transport.
Template class extending base class uniquely.
#define HC_NOTES(ptr, notes)
int g_convertUTF8
Flag: String representation (utf8 or ansi)
int blob_pack(const mxArray *pcItem, bool bStreamable, void **ppBlob, size_t *pBlob_size, double *pdProcess_time, double *pdRatio, const char *compressor=g_compression_type, int level=g_compression_level)
create a compressed typed blob from a Matlab item (deep copy)
bool serialize(const mxArray *pItem, mxArray *&pByteStream)
Converts MATLAB variable of any complexity into byte stream.
#define CONFIG_MKSQLITE_MAX_BLOB_SIZE
SQLite itself limits BLOBs to 1MB, mksqlite limits to INT32_MAX.
void set(const char *strMsg, const char *strId=NULL)
Set error message to a constant string (without translation)
void free_result()
Clear self created results with memory deallocation.
void utils_free_ptr(T *&pmxarr)
Freeing memory allocated by mxAlloc() or mxRealloc()
void * Data() const
Returns pointer to raw data.
char * utils_strnewdup(const char *s, int flagConvertUTF8)
duplicate a string and recode from UTF8 to char due to flag flagConvertUTF8
int utils_latin2utf(const unsigned char *s, unsigned char *buffer)
Convert char string to UTF-8 string.
void pow_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
Power function implementation.
bool unpack(void *cdata, size_t cdata_size, void *rdata, size_t rdata_size, size_t rdata_element_size)
Calls the qualified compressor (inflate)
void BDC_unpack_time_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
BDCUnpackTime function implementation.
int getMsgId()
Get the current message identifier.
void * getData(mwSize nDims)
Get pointer to array data (while initializing)
Packing MATLAB data in a memory block with type information for storing as SQL BLOB.
const mxArray * Item() const
Returns hosted MATLAB array.
void lg_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
Logarithm function to base 10 implementation.
void exp_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
Exponential function implementation.
void BDC_ratio_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
BDCRatio function implementation.
bool IsDoubleClass() const
Returns true if m_pcItem is of type mxDOUBLE_CLASS.
size_t m_result_size
size of compressor output in bytes
bool setCompressor(const char *strCompressorType, int iCompressionLevel=-1)
Converts compressor ID string to category enum.
#define MEM_ALLOC(count, bytes)
standard memory allocator