diff options
author | Olivier Bertrand <bertrandop@gmail.com> | 2020-12-09 00:55:06 +0100 |
---|---|---|
committer | Olivier Bertrand <bertrandop@gmail.com> | 2020-12-09 00:55:06 +0100 |
commit | 4eeadedc77018da781eb7ea008fd3474f1a354d5 (patch) | |
tree | 66b1146e041862d198f12de82f866ba9c780545b | |
parent | 871532c3b9155fa00f7de61ef02f0c2d0f862d57 (diff) | |
download | mariadb-git-4eeadedc77018da781eb7ea008fd3474f1a354d5.tar.gz |
- Fix json_bjson (s was erase by Json_Subset)
modified: storage/connect/jsonudf.cpp
modified: storage/connect/jsonudf.h
- Fix compile error (Force_Bson was not conditional by BSON_SUPPORT)
modified: storage/connect/ha_connect.cc
- Continue Bjson implementation
modified: storage/connect/block.h
modified: storage/connect/bson.cpp
modified: storage/connect/bson.h
modified: storage/connect/bsonudf.cpp
modified: storage/connect/bsonudf.h
modified: storage/connect/plugutil.cpp
modified: storage/connect/tabbson.cpp
modified: storage/connect/tabjson.cpp
- Typo
deleted: storage/connect/Header.h
-rw-r--r-- | storage/connect/Header.h | 395 | ||||
-rw-r--r-- | storage/connect/block.h | 4 | ||||
-rw-r--r-- | storage/connect/bson.cpp | 58 | ||||
-rw-r--r-- | storage/connect/bson.h | 129 | ||||
-rw-r--r-- | storage/connect/bsonudf.cpp | 124 | ||||
-rw-r--r-- | storage/connect/bsonudf.h | 5 | ||||
-rw-r--r-- | storage/connect/ha_connect.cc | 4 | ||||
-rw-r--r-- | storage/connect/jsonudf.cpp | 9 | ||||
-rw-r--r-- | storage/connect/jsonudf.h | 4 | ||||
-rw-r--r-- | storage/connect/plugutil.cpp | 41 | ||||
-rw-r--r-- | storage/connect/tabbson.cpp | 37 | ||||
-rw-r--r-- | storage/connect/tabjson.cpp | 8 |
12 files changed, 204 insertions, 614 deletions
diff --git a/storage/connect/Header.h b/storage/connect/Header.h deleted file mode 100644 index f9664befaa3..00000000000 --- a/storage/connect/Header.h +++ /dev/null @@ -1,395 +0,0 @@ -#pragma once -/**************** json H Declares Source Code File (.H) ****************/ -/* Name: json.h Version 1.2 */ -/* */ -/* (C) Copyright to the author Olivier BERTRAND 2014 - 2020 */ -/* */ -/* This file contains the JSON classes declares. */ -/***********************************************************************/ -#include <mysql_com.h> -#include "value.h" -#include "xobject.h" - -#if defined(_DEBUG) -#define X assert(false); -#else -#define X -#endif - -enum JTYP { - TYPE_NULL = TYPE_VOID, - TYPE_STRG = TYPE_STRING, - TYPE_DBL = TYPE_DOUBLE, - TYPE_BOOL = TYPE_TINY, - TYPE_BINT = TYPE_BIGINT, - TYPE_DTM = TYPE_DATE, - TYPE_INTG = TYPE_INT, - TYPE_VAL = 12, - TYPE_JSON, - TYPE_JAR, - TYPE_JOB, - TYPE_JVAL -}; - -class JDOC; -class JOUT; -class JSON; -class JVALUE; -class JOBJECT; -class JARRAY; - -typedef class JDOC* PJDOC; -typedef class JSON* PJSON; -typedef class JVALUE* PJVAL; -typedef class JOBJECT* PJOB; -typedef class JARRAY* PJAR; - -// BSON size should be equal on Linux and Windows -#define BMX 255 -typedef struct BSON* PBSON; -typedef struct JPAIR* PJPR; -//typedef struct VAL *PVL; - -/***********************************************************************/ -/* Structure JPAIR. The pairs of a json Object. */ -/***********************************************************************/ -struct JPAIR { - PCSZ Key; // This pair key name - PJVAL Val; // To the value of the pair - PJPR Next; // To the next pair -}; // end of struct JPAIR - -#if 0 -/***********************************************************************/ -/* Structure VAL (string, int, float, bool or null) */ -/***********************************************************************/ -struct VAL { - union { - char* Strp; // Ptr to a string - int N; // An integer value - long long LLn; // A big integer value - double F; // A float value - bool B; // True or false - }; - int Nd; // Decimal number - JTYP Type; // The value type -}; // end of struct VAL -#endif // 0 - -/***********************************************************************/ -/* Structure used to return binary json to Json UDF functions. */ -/***********************************************************************/ -struct BSON { - char Msg[BMX + 1]; - char* Filename; - PGLOBAL G; - int Pretty; - ulong Reslen; - my_bool Changed; - PJSON Top; - PJSON Jsp; - PBSON Bsp; -}; // end of struct BSON - -PBSON JbinAlloc(PGLOBAL g, UDF_ARGS* args, ulong len, PJSON jsp); -//PVL AllocVal(PGLOBAL g, JTYP type); -char* NextChr(PSZ s, char sep); -char* GetJsonNull(void); -const char* GetFmt(int type, bool un); - -PJSON ParseJson(PGLOBAL g, char* s, size_t n, int* prty = NULL, bool* b = NULL); -PSZ Serialize(PGLOBAL g, PJSON jsp, char* fn, int pretty); -DllExport bool IsNum(PSZ s); - -/***********************************************************************/ -/* Class JDOC. The class for parsing and serializing json documents. */ -/***********************************************************************/ -class JDOC : public BLOCK { - friend PJSON ParseJson(PGLOBAL, char*, size_t, int*, bool*); - friend PSZ Serialize(PGLOBAL, PJSON, char*, int); -public: - JDOC(void) : js(NULL), s(NULL), len(0), pty(NULL) {} - - void SetJp(JOUT* jp) { js = jp; } - -protected: - PJAR ParseArray(PGLOBAL g, int& i); - PJOB ParseObject(PGLOBAL g, int& i); - PJVAL ParseValue(PGLOBAL g, int& i); - char* ParseString(PGLOBAL g, int& i); - void ParseNumeric(PGLOBAL g, int& i, PJVAL jvp); - PJAR ParseAsArray(PGLOBAL g, int& i, int pretty, int* ptyp); - bool SerializeArray(PJAR jarp, bool b); - bool SerializeObject(PJOB jobp); - bool SerializeValue(PJVAL jvp); - - // Members used when parsing and serializing -private: - JOUT* js; - char* s; - int len; - bool* pty; -}; // end of class JDOC - -/***********************************************************************/ -/* Class JSON. The base class for all other json classes. */ -/***********************************************************************/ -class JSON : public BLOCK { -public: - // Constructor - JSON(void) { Type = TYPE_JSON; } - JSON(int) {} - - // Implementation - inline JTYP GetType(void) { return Type; } - - // Methods - virtual int size(void) { return 1; } - virtual void Clear(void) { X } - virtual PJOB GetObject(void) { return NULL; } - virtual PJAR GetArray(void) { return NULL; } - virtual PJVAL GetArrayValue(int i) { X return NULL; } - virtual int GetSize(bool b) { X return 0; } - virtual PJSON GetJsp(void) { X return NULL; } - virtual PJPR GetFirst(void) { X return NULL; } - virtual PSZ GetText(PGLOBAL g, PSTRG text) { X return NULL; } - virtual bool Merge(PGLOBAL g, PJSON jsp) { X return true; } - virtual void SetValue(PJSON jsp) { X } - virtual bool DeleteValue(int i) { X return true; } - virtual bool IsNull(void) { X return true; } - - // Members - JTYP Type; -}; // end of class JSON - -/***********************************************************************/ -/* Class JOBJECT: contains a list of value pairs. */ -/***********************************************************************/ -class JOBJECT : public JSON { - friend class JDOC; - friend class JSNX; - friend class SWAP; -public: - JOBJECT(void) : JSON() { Type = TYPE_JOB; First = Last = NULL; } - JOBJECT(int i) : JSON(i) {} - - // Methods - virtual void Clear(void) { First = Last = NULL; } - //virtual JTYP GetValType(void) {return TYPE_JOB;} - virtual PJPR GetFirst(void) { return First; } - virtual int GetSize(bool b); - virtual PJOB GetObject(void) { return this; } - virtual PSZ GetText(PGLOBAL g, PSTRG text); - virtual bool Merge(PGLOBAL g, PJSON jsp); - virtual bool IsNull(void); - - // Specific - PJPR AddPair(PGLOBAL g, PCSZ key); - PJVAL GetKeyValue(const char* key); - PJAR GetKeyList(PGLOBAL g); - PJAR GetValList(PGLOBAL g); - void SetKeyValue(PGLOBAL g, PJVAL jvp, PCSZ key); - void DeleteKey(PCSZ k); - -protected: - PJPR First; - PJPR Last; -}; // end of class JOBJECT - -/***********************************************************************/ -/* Class JARRAY. */ -/***********************************************************************/ -class JARRAY : public JSON { - friend class SWAP; -public: - JARRAY(void); - JARRAY(int i) : JSON(i) {} - - // Methods - virtual void Clear(void) { First = Last = NULL; Size = 0; } - virtual int size(void) { return Size; } - virtual PJAR GetArray(void) { return this; } - virtual int GetSize(bool b); - virtual PJVAL GetArrayValue(int i); - virtual PSZ GetText(PGLOBAL g, PSTRG text); - virtual bool Merge(PGLOBAL g, PJSON jsp); - virtual bool DeleteValue(int n); - virtual bool IsNull(void); - - // Specific - PJVAL AddArrayValue(PGLOBAL g, PJVAL jvp = NULL, int* x = NULL); - bool SetArrayValue(PGLOBAL g, PJVAL jvp, int i); - void InitArray(PGLOBAL g); - -protected: - // Members - int Size; // The number of items in the array - int Alloc; // The Mvals allocated size - PJVAL First; // Used when constructing - PJVAL Last; // Last constructed value - PJVAL* Mvals; // Allocated when finished -}; // end of class JARRAY - -/***********************************************************************/ -/* Class JVALUE. */ -/***********************************************************************/ -class JVALUE : public JSON { - friend class JARRAY; - friend class JSNX; - friend class JSONDISC; - friend class JSONCOL; - friend class JSON; - friend class JDOC; - friend class SWAP; -public: - JVALUE(void) : JSON() { Type = TYPE_JVAL; Clear(); } - JVALUE(PJSON jsp); - //JVALUE(PGLOBAL g, PVL vlp); - JVALUE(PGLOBAL g, PVAL valp); - JVALUE(PGLOBAL g, PCSZ strp); - JVALUE(int i) : JSON(i) {} - - //using JSON::GetVal; - //using JSON::SetVal; - - // Methods - virtual void Clear(void); - //virtual JTYP GetType(void) {return TYPE_JVAL;} - virtual JTYP GetValType(void); - virtual PJOB GetObject(void); - virtual PJAR GetArray(void); - virtual PJSON GetJsp(void) { return (DataType == TYPE_JSON ? Jsp : NULL); } - virtual PSZ GetText(PGLOBAL g, PSTRG text); - virtual bool IsNull(void); - - // Specific - //inline PVL GetVal(void) { return Val; } - //inline void SetVal(PVL vlp) { Val = vlp; } - inline PJSON GetJson(void) { return (DataType == TYPE_JSON ? Jsp : this); } - PSZ GetString(PGLOBAL g, char* buff = NULL); - int GetInteger(void); - long long GetBigint(void); - double GetFloat(void); - PVAL GetValue(PGLOBAL g); - void SetValue(PJSON jsp); - void SetValue(PGLOBAL g, PVAL valp); - void SetString(PGLOBAL g, PSZ s, int ci = 0); - void SetInteger(PGLOBAL g, int n); - void SetBigint(PGLOBAL g, longlong ll); - void SetFloat(PGLOBAL g, double f); - void SetBool(PGLOBAL g, bool b); - -protected: - union { - PJSON Jsp; // To the json value - char* Strp; // Ptr to a string - int N; // An integer value - long long LLn; // A big integer value - double F; // A (double) float value - bool B; // True or false - }; - //PVL Val; // To the string or numeric value - PJVAL Next; // Next value in array - JTYP DataType; // The data value type - int Nd; // Decimal number - bool Del; // True when deleted -}; // end of class JVALUE - - -/***********************************************************************/ -/* Class JOUT. Used by Serialize. */ -/***********************************************************************/ -class JOUT : public BLOCK { -public: - JOUT(PGLOBAL gp) : BLOCK() { g = gp; Pretty = 3; } - - virtual bool WriteStr(const char* s) = 0; - virtual bool WriteChr(const char c) = 0; - virtual bool Escape(const char* s) = 0; - int Prty(void) { return Pretty; } - - // Member - PGLOBAL g; - int Pretty; -}; // end of class JOUT - -/***********************************************************************/ -/* Class JOUTSTR. Used to Serialize to a string. */ -/***********************************************************************/ -class JOUTSTR : public JOUT { -public: - JOUTSTR(PGLOBAL g); - - virtual bool WriteStr(const char* s); - virtual bool WriteChr(const char c); - virtual bool Escape(const char* s); - - // Member - char* Strp; // The serialized string - size_t N; // Position of next char - size_t Max; // String max size -}; // end of class JOUTSTR - -/***********************************************************************/ -/* Class JOUTFILE. Used to Serialize to a file. */ -/***********************************************************************/ -class JOUTFILE : public JOUT { -public: - JOUTFILE(PGLOBAL g, FILE* str, int pty) : JOUT(g) { Stream = str; Pretty = pty; } - - virtual bool WriteStr(const char* s); - virtual bool WriteChr(const char c); - virtual bool Escape(const char* s); - - // Member - FILE* Stream; -}; // end of class JOUTFILE - -/***********************************************************************/ -/* Class JOUTPRT. Used to Serialize to a pretty file. */ -/***********************************************************************/ -class JOUTPRT : public JOUTFILE { -public: - JOUTPRT(PGLOBAL g, FILE* str) : JOUTFILE(g, str, 2) { M = 0; B = false; } - - virtual bool WriteStr(const char* s); - virtual bool WriteChr(const char c); - - // Member - int M; - bool B; -}; // end of class JOUTPRT - - -/***********************************************************************/ -/* Class SWAP. Used to make or unmake a JSON tree movable. */ -/* This is done by making all pointers to offsets. */ -/***********************************************************************/ -class SWAP : public BLOCK { -public: - // Constructor - SWAP(PGLOBAL g, PJSON jsp) { - G = g, Base = (char*)jsp - 8; - } - - // Methods - void SwapJson(PJSON jsp, bool move); - -protected: - size_t MoffJson(PJSON jnp); - size_t MoffArray(PJAR jarp); - size_t MoffObject(PJOB jobp); - size_t MoffJValue(PJVAL jvp); - size_t MoffPair(PJPR jpp); - //size_t MoffVal(PVL vlp); - PJSON MptrJson(PJSON jnp); - PJAR MptrArray(PJAR jarp); - PJOB MptrObject(PJOB jobp); - PJVAL MptrJValue(PJVAL jvp); - PJPR MptrPair(PJPR jpp); - //PVL MptrVal(PVL vlp); - - // Member - PGLOBAL G; - void* Base; -}; // end of class SWAP diff --git a/storage/connect/block.h b/storage/connect/block.h index 6ac3b73bf13..c10fc4761ac 100644 --- a/storage/connect/block.h +++ b/storage/connect/block.h @@ -18,8 +18,8 @@ /* global pointer of the Plug application, and an optional pointer to */ /* the memory pool to use, defaulting to NULL meaning using the Plug */ /* standard default memory pool, example: */ -/* tabp = new(g) XTAB("EMPLOYEE"); */ -/* allocates a XTAB class object in the standard Plug memory pool. */ +/* tabp = new(g) XTAB("EMPLOYEE"); */ +/* allocates a XTAB class object in the standard Plug memory pool. */ /***********************************************************************/ #if !defined(BLOCK_DEFINED) #define BLOCK_DEFINED diff --git a/storage/connect/bson.cpp b/storage/connect/bson.cpp index bf0755d4eae..a291dd69df6 100644 --- a/storage/connect/bson.cpp +++ b/storage/connect/bson.cpp @@ -1,9 +1,9 @@ -/*************** json CPP Declares Source Code File (.H) ***************/ -/* Name: json.cpp Version 1.5 */ +/*************** bson CPP Declares Source Code File (.H) ***************/ +/* Name: bson.cpp Version 1.0 */ /* */ -/* (C) Copyright to the author Olivier BERTRAND 2014 - 2020 */ +/* (C) Copyright to the author Olivier BERTRAND 2020 */ /* */ -/* This file contains the JSON classes functions. */ +/* This file contains the BJSON classes functions. */ /***********************************************************************/ /***********************************************************************/ @@ -15,7 +15,7 @@ /* Include application header files: */ /* global.h is header containing all global declarations. */ /* plgdbsem.h is header containing the DB application declarations. */ -/* xjson.h is header containing the JSON classes declarations. */ +/* bson.h is header containing the BSON classes declarations. */ /***********************************************************************/ #include "global.h" #include "plgdbsem.h" @@ -112,6 +112,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma) { int i, pretty = (ptyp) ? *ptyp : 3; bool b = false; + PBVAL bvp = NULL; s = js; len = lng; @@ -128,26 +129,26 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma) pty[0] = false; try { - Bvp = NewVal(); - Bvp->Type = TYPE_UNKNOWN; + bvp = NewVal(); + bvp->Type = TYPE_UNKNOWN; for (i = 0; i < len; i++) switch (s[i]) { case '[': - if (Bvp->Type != TYPE_UNKNOWN) - Bvp->To_Val = ParseAsArray(i, pretty, ptyp); + if (bvp->Type != TYPE_UNKNOWN) + bvp->To_Val = ParseAsArray(i, pretty, ptyp); else - Bvp->To_Val = ParseArray(++i); + bvp->To_Val = ParseArray(++i); - Bvp->Type = TYPE_JAR; + bvp->Type = TYPE_JAR; break; case '{': - if (Bvp->Type != TYPE_UNKNOWN) { - Bvp->To_Val = ParseAsArray(i, pretty, ptyp); - Bvp->Type = TYPE_JAR; + if (bvp->Type != TYPE_UNKNOWN) { + bvp->To_Val = ParseAsArray(i, pretty, ptyp); + bvp->Type = TYPE_JAR; } else { - Bvp->To_Val = ParseObject(++i); - Bvp->Type = TYPE_JOB; + bvp->To_Val = ParseObject(++i); + bvp->Type = TYPE_JOB; } // endif Type break; @@ -157,7 +158,7 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma) case '\r': break; case ',': - if (Bvp->Type != TYPE_UNKNOWN && (pretty == 1 || pretty == 3)) { + if (bvp->Type != TYPE_UNKNOWN && (pretty == 1 || pretty == 3)) { if (comma) *comma = true; @@ -177,18 +178,18 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma) } // endif b default: - if (Bvp->Type != TYPE_UNKNOWN) { - Bvp->To_Val = ParseAsArray(i, pretty, ptyp); - Bvp->Type = TYPE_JAR; - } else if ((Bvp->To_Val = MOF(ParseValue(i)))) - Bvp->Type = TYPE_JVAL; + if (bvp->Type != TYPE_UNKNOWN) { + bvp->To_Val = ParseAsArray(i, pretty, ptyp); + bvp->Type = TYPE_JAR; + } else if ((bvp->To_Val = MOF(ParseValue(i)))) + bvp->Type = TYPE_JVAL; else throw 4; break; }; // endswitch s[i] - if (Bvp->Type == TYPE_UNKNOWN) + if (bvp->Type == TYPE_UNKNOWN) sprintf(g->Message, "Invalid Json string '%.*s'", MY_MIN((int)len, 50), s); else if (ptyp && pretty == 3) { *ptyp = 3; // Not recognized pretty @@ -205,13 +206,13 @@ PBVAL BDOC::ParseJson(PGLOBAL g, char* js, size_t lng, int* ptyp, bool* comma) if (trace(1)) htrc("Exception %d: %s\n", n, G->Message); GetMsg(g); - Bvp = NULL; + bvp = NULL; } catch (const char* msg) { strcpy(g->Message, msg); - Bvp = NULL; + bvp = NULL; } // end catch - return Bvp; + return bvp; } // end of ParseJson /***********************************************************************/ @@ -391,13 +392,11 @@ suite: bvp->Type = TYPE_JOB; break; case '"': - // jvp->Val = AllocVal(g, TYPE_STRG); bvp->To_Val = ParseString(++i); bvp->Type = TYPE_STRG; break; case 't': if (!strncmp(s + i, "true", 4)) { - // jvp->Val = AllocVal(g, TYPE_BOOL); bvp->B = true; bvp->Type = TYPE_BOOL; i += 3; @@ -407,7 +406,6 @@ suite: break; case 'f': if (!strncmp(s + i, "false", 5)) { - // jvp->Val = AllocVal(g, TYPE_BOOL); bvp->B = false; bvp->Type = TYPE_BOOL; i += 4; @@ -872,7 +870,7 @@ void BJSON::SubSet(bool b) if (b) G->Saved_Size = 0; -} /* end of JsonSubSet */ +} // end of SubSet /* ------------------------ Bobject functions ------------------------ */ diff --git a/storage/connect/bson.h b/storage/connect/bson.h index 03d5b9bd854..a6e160a3f3b 100644 --- a/storage/connect/bson.h +++ b/storage/connect/bson.h @@ -1,4 +1,3 @@ -#pragma once /**************** bson H Declares Source Code File (.H) ****************/ /* Name: bson.h Version 1.0 */ /* */ @@ -6,6 +5,7 @@ /* */ /* This file contains the BSON classe declares. */ /***********************************************************************/ +#pragma once #include <mysql_com.h> #include "json.h" #include "xobject.h" @@ -189,130 +189,3 @@ protected: // Default constructor not to be used BDOC(void) {} }; // end of class BDOC - -#if 0 -/***********************************************************************/ -/* Class JOBJECT: contains a list of value pairs. */ -/***********************************************************************/ -class JOBJECT : public JSON { - friend class JDOC; - friend class JSNX; - friend class SWAP; -public: - JOBJECT(void) : JSON() { Type = TYPE_JOB; First = Last = NULL; } - JOBJECT(int i) : JSON(i) {} - - // Methods - virtual void Clear(void) { First = Last = NULL; } - virtual PJPR GetFirst(void) { return First; } - virtual int GetSize(PBPR prp, bool b); - virtual PJOB GetObject(void) { return this; } - virtual PSZ GetText(PGLOBAL g, PSTRG text); - virtual bool Merge(PGLOBAL g, PJSON jsp); - virtual bool IsNull(void); - - // Specific - PJPR AddPair(PGLOBAL g, PCSZ key); - PJVAL GetKeyValue(const char* key); - PJAR GetKeyList(PGLOBAL g); - PJAR GetValList(PGLOBAL g); - void SetKeyValue(PGLOBAL g, PJVAL jvp, PCSZ key); - void DeleteKey(PCSZ k); - -protected: - PJPR First; - PJPR Last; -}; // end of class JOBJECT - -/***********************************************************************/ -/* Class JARRAY. */ -/***********************************************************************/ -class JARRAY : public JSON { - friend class SWAP; -public: - JARRAY(void); - JARRAY(int i) : JSON(i) {} - - // Methods - virtual void Clear(void) { First = Last = NULL; Size = 0; } - virtual int size(void) { return Size; } - virtual PJAR GetArray(void) { return this; } - virtual int GetSize(bool b); - virtual PJVAL GetArrayValue(int i); - virtual PSZ GetText(PGLOBAL g, PSTRG text); - virtual bool Merge(PGLOBAL g, PJSON jsp); - virtual bool DeleteValue(int n); - virtual bool IsNull(void); - - // Specific - PJVAL AddArrayValue(PGLOBAL g, PJVAL jvp = NULL, int* x = NULL); - bool SetArrayValue(PGLOBAL g, PJVAL jvp, int i); - void InitArray(PGLOBAL g); - -protected: - // Members - int Size; // The number of items in the array - int Alloc; // The Mvals allocated size - PJVAL First; // Used when constructing - PJVAL Last; // Last constructed value - PJVAL* Mvals; // Allocated when finished -}; // end of class JARRAY - -/***********************************************************************/ -/* Class JVALUE. */ -/***********************************************************************/ -class JVALUE : public JSON { - friend class JARRAY; - friend class JSNX; - friend class JSONDISC; - friend class JSONCOL; - friend class JSON; - friend class JDOC; - friend class SWAP; -public: - JVALUE(void) : JSON() { Type = TYPE_JVAL; Clear(); } - JVALUE(PJSON jsp); - JVALUE(PGLOBAL g, PVAL valp); - JVALUE(PGLOBAL g, PCSZ strp); - JVALUE(int i) : JSON(i) {} - - // Methods - virtual void Clear(void); - //virtual JTYP GetType(void) {return TYPE_JVAL;} - virtual JTYP GetValType(void); - virtual PJOB GetObject(void); - virtual PJAR GetArray(void); - virtual PJSON GetJsp(void) { return (DataType == TYPE_JSON ? Jsp : NULL); } - virtual PSZ GetText(PGLOBAL g, PSTRG text); - virtual bool IsNull(void); - - // Specific - inline PJSON GetJson(void) { return (DataType == TYPE_JSON ? Jsp : this); } - PSZ GetString(PGLOBAL g, char* buff = NULL); - int GetInteger(void); - long long GetBigint(void); - double GetFloat(void); - PVAL GetValue(PGLOBAL g); - void SetValue(PJSON jsp); - void SetValue(PGLOBAL g, PVAL valp); - void SetString(PGLOBAL g, PSZ s, int ci = 0); - void SetInteger(PGLOBAL g, int n); - void SetBigint(PGLOBAL g, longlong ll); - void SetFloat(PGLOBAL g, double f); - void SetBool(PGLOBAL g, bool b); - -protected: - union { - PJSON Jsp; // To the json value - char* Strp; // Ptr to a string - int N; // An integer value - long long LLn; // A big integer value - double F; // A (double) float value - bool B; // True or false - }; - PJVAL Next; // Next value in array - JTYP DataType; // The data value type - int Nd; // Decimal number - bool Del; // True when deleted -}; // end of class JVALUE -#endif // 0 diff --git a/storage/connect/bsonudf.cpp b/storage/connect/bsonudf.cpp index 5163f042ad0..b7a778b8387 100644 --- a/storage/connect/bsonudf.cpp +++ b/storage/connect/bsonudf.cpp @@ -1706,3 +1706,127 @@ void bson_locate_all_deinit(UDF_INIT* initid) { JsonFreeMem((PGLOBAL)initid->ptr); } // end of bson_locate_all_deinit +/*********************************************************************************/ +/* Convert a pretty=0 Json file to binary BJSON. */ +/*********************************************************************************/ +my_bool bfile_bjson_init(UDF_INIT* initid, UDF_ARGS* args, char* message) { + unsigned long reslen, memlen; + + if (args->arg_count != 2 && args->arg_count != 3) { + strcpy(message, "This function must have 2 or 3 arguments"); + return true; + } else if (args->arg_count == 3 && args->arg_type[2] != INT_RESULT) { + strcpy(message, "Third Argument must be an integer (LRECL)"); + return true; + } else for (int i = 0; i < 2; i++) + if (args->arg_type[i] != STRING_RESULT) { + sprintf(message, "Arguments %d must be a string (file name)", i + 1); + return true; + } // endif args + + CalcLen(args, false, reslen, memlen); + memlen = memlen * M; + memlen += (args->arg_count == 3) ? (ulong)*(longlong*)args->args[2] : 1024; + return JsonInit(initid, args, message, false, reslen, memlen); +} // end of bfile_bjson_init + +char *bfile_bjson(UDF_INIT *initid, UDF_ARGS *args, char *result, + unsigned long *res_length, char*, char *error) { + char *buf, *str = NULL, fn[_MAX_PATH], ofn[_MAX_PATH]; + bool loop; + ssize_t len, newloc; + size_t lrecl, binszp; + PBVAL jsp; + PBJNX bnxp; + PGLOBAL g = (PGLOBAL)initid->ptr; + BDOC doc(g); + + strcpy(fn, MakePSZ(g, args, 0)); + strcpy(ofn, MakePSZ(g, args, 1)); + + if (args->arg_count == 3) + lrecl = (size_t)*(longlong*)args->args[2]; + else + lrecl = 1024; + + if (!g->Xchk) { + int msgid = MSGID_OPEN_MODE_STRERROR; + FILE *fout; + FILE *fin; + + if (!(fin = global_fopen(g, msgid, fn, "rt"))) + str = strcpy(result, g->Message); + else if (!(fout = global_fopen(g, msgid, ofn, "wb"))) + str = strcpy(result, g->Message); + else if ((buf = (char*)malloc(lrecl))) { + try { + do { + loop = false; + PlugSubSet(g->Sarea, g->Sarea_Size); + + if (!fgets(buf, lrecl, fin)) { + if (!feof(fin)) { + sprintf(g->Message, "Error %d reading %zd bytes from %s", + errno, lrecl, fn); + str = strcpy(result, g->Message); + } else + str = strcpy(result, ofn); + + } else if ((len = strlen(buf))) { + if ((jsp = doc.ParseJson(g, buf, len))) { + newloc = (size_t)PlugSubAlloc(g, NULL, 0); + binszp = newloc - (size_t)jsp; + + if (fwrite(&binszp, sizeof(binszp), 1, fout) != 1) { + sprintf(g->Message, "Error %d writing %zd bytes to %s", + errno, sizeof(binszp), ofn); + str = strcpy(result, g->Message); + } else if (fwrite(jsp, binszp, 1, fout) != 1) { + sprintf(g->Message, "Error %d writing %zd bytes to %s", + errno, binszp, ofn); + str = strcpy(result, g->Message); + } else + loop = true; + + } else { + str = strcpy(result, g->Message); + } // endif jsp + + } else + loop = true; + + } while (loop); + + } catch (int) { + str = strcpy(result, g->Message); + } catch (const char* msg) { + str = strcpy(result, msg); + } // end catch + + free(buf); + } else + str = strcpy(result, "Buffer malloc failed"); + + if (fin) fclose(fin); + if (fout) fclose(fout); + g->Xchk = str; + } else + str = (char*)g->Xchk; + + if (!str) { + if (g->Message) + str = strcpy(result, g->Message); + else + str = strcpy(result, "Unexpected error"); + + } // endif str + + *res_length = strlen(str); + return str; +} // end of bfile_bjson + +void bfile_bjson_deinit(UDF_INIT* initid) { + JsonFreeMem((PGLOBAL)initid->ptr); +} // end of bfile_bjson_deinit + + diff --git a/storage/connect/bsonudf.h b/storage/connect/bsonudf.h index b591b6b89f8..1675d36cee5 100644 --- a/storage/connect/bsonudf.h +++ b/storage/connect/bsonudf.h @@ -106,5 +106,8 @@ extern "C" { DllExport my_bool bson_locate_all_init(UDF_INIT*, UDF_ARGS*, char*); DllExport char* bson_locate_all(UDF_EXEC_ARGS); DllExport void bson_locate_all_deinit(UDF_INIT*); -} // extern "C" + DllExport my_bool bfile_bjson_init(UDF_INIT*, UDF_ARGS*, char*); + DllExport char* bfile_bjson(UDF_EXEC_ARGS); + DllExport void bfile_bjson_deinit(UDF_INIT*); +} // extern "C" diff --git a/storage/connect/ha_connect.cc b/storage/connect/ha_connect.cc index 6728550447c..65c3ea5c5d6 100644 --- a/storage/connect/ha_connect.cc +++ b/storage/connect/ha_connect.cc @@ -170,7 +170,7 @@ #define JSONMAX 10 // JSON Default max grp size extern "C" { - char version[]= "Version 1.07.0002 December 07, 2020"; + char version[]= "Version 1.07.0002 December 12, 2020"; #if defined(__WIN__) char compver[]= "Version 1.07.0002 " __DATE__ " " __TIME__; char slash= '\\'; @@ -516,7 +516,9 @@ char *GetJavaWrapper(void) bool MongoEnabled(void) {return THDVAR(current_thd, enable_mongo);} #endif // JAVA_SUPPORT || CMGO_SUPPORT +#if defined(BSON_SUPPORT) bool Force_Bson(void) {return THDVAR(current_thd, force_bson);} +#endif // BSON_SUPPORT) #if defined(XMSG) || defined(NEWMSG) extern "C" const char *msglang(void) diff --git a/storage/connect/jsonudf.cpp b/storage/connect/jsonudf.cpp index 773828a96dd..0909cb96477 100644 --- a/storage/connect/jsonudf.cpp +++ b/storage/connect/jsonudf.cpp @@ -1174,13 +1174,16 @@ static uint GetJsonGroupSize(void) /*********************************************************************************/ /* Program for SubSet re-initialization of the memory pool. */ /*********************************************************************************/ -my_bool JsonSubSet(PGLOBAL g) +my_bool JsonSubSet(PGLOBAL g, my_bool b) { PPOOLHEADER pph = (PPOOLHEADER)g->Sarea; pph->To_Free = (g->Saved_Size) ? g->Saved_Size : sizeof(POOLHEADER); pph->FreeBlk = g->Sarea_Size - pph->To_Free; - g->Saved_Size = 0; + + if (b) + g->Saved_Size = 0; + return FALSE; } /* end of JsonSubSet */ @@ -1458,7 +1461,7 @@ int IsJson(UDF_ARGS *args, uint i, bool b) char *sap; PGLOBAL g = PlugInit(NULL, (size_t)args->lengths[i] * M + 1024); - JsonSubSet(g); +// JsonSubSet(g); sap = MakePSZ(g, args, i); if (ParseJson(g, sap, strlen(sap))) diff --git a/storage/connect/jsonudf.h b/storage/connect/jsonudf.h index 2a2b2cac20e..d99122aa775 100644 --- a/storage/connect/jsonudf.h +++ b/storage/connect/jsonudf.h @@ -53,13 +53,13 @@ typedef struct _jnode { typedef class JSNX *PJSNX; /*********************************************************************************/ -/* The JSON tree node. Can be an Object or an Array. */ +/* The JSON utility functions. */ /*********************************************************************************/ bool IsNum(PSZ s); char *NextChr(PSZ s, char sep); char *GetJsonNull(void); uint GetJsonGrpSize(void); -my_bool JsonSubSet(PGLOBAL g); +my_bool JsonSubSet(PGLOBAL g, my_bool b = false); my_bool CalcLen(UDF_ARGS* args, my_bool obj, unsigned long& reslen, unsigned long& memlen, my_bool mod = false); my_bool JsonInit(UDF_INIT* initid, UDF_ARGS* args, char* message, my_bool mbn, diff --git a/storage/connect/plugutil.cpp b/storage/connect/plugutil.cpp index ac07f45ccf2..c33639bf744 100644 --- a/storage/connect/plugutil.cpp +++ b/storage/connect/plugutil.cpp @@ -96,7 +96,7 @@ char *msglang(void); typedef struct { ushort Segsize; ushort Size; - } AREASIZE; +} AREASIZE; ACTIVITY defActivity = { /* Describes activity and language */ NULL, /* Points to user work area(s) */ @@ -204,7 +204,7 @@ PGLOBAL PlugExit(PGLOBAL g) /* Note: this routine is not really implemented for Unix. */ /***********************************************************************/ LPSTR PlugRemoveType(LPSTR pBuff, LPCSTR FileName) - { +{ #if defined(__WIN__) char drive[_MAX_DRIVE]; #else @@ -228,8 +228,7 @@ LPSTR PlugRemoveType(LPSTR pBuff, LPCSTR FileName) htrc("buff='%s'\n", pBuff); return pBuff; - } // end of PlugRemoveType - +} // end of PlugRemoveType BOOL PlugIsAbsolutePath(LPCSTR path) { @@ -246,7 +245,7 @@ BOOL PlugIsAbsolutePath(LPCSTR path) /* Note: this routine is not really implemented for Unix. */ /***********************************************************************/ LPCSTR PlugSetPath(LPSTR pBuff, LPCSTR prefix, LPCSTR FileName, LPCSTR defpath) - { +{ char newname[_MAX_PATH]; char direc[_MAX_DIR], defdir[_MAX_DIR], tmpdir[_MAX_DIR]; char fname[_MAX_FNAME]; @@ -347,14 +346,14 @@ LPCSTR PlugSetPath(LPSTR pBuff, LPCSTR prefix, LPCSTR FileName, LPCSTR defpath) } else return FileName; // Error, return unchanged name - } // end of PlugSetPath +} // end of PlugSetPath #if defined(XMSG) /***********************************************************************/ /* PlugGetMessage: get a message from the message file. */ /***********************************************************************/ char *PlugReadMessage(PGLOBAL g, int mid, char *m) - { +{ char msgfile[_MAX_PATH], msgid[32], buff[256]; char *msg; FILE *mfile = NULL; @@ -405,14 +404,14 @@ char *PlugReadMessage(PGLOBAL g, int mid, char *m) msg = stmsg; return msg; - } // end of PlugReadMessage +} // end of PlugReadMessage #elif defined(NEWMSG) /***********************************************************************/ /* PlugGetMessage: get a message from the resource string table. */ /***********************************************************************/ char *PlugGetMessage(PGLOBAL g, int mid) - { +{ char *msg; #if 0 // was !defined(UNIX) && !defined(UNIV_LINUX) @@ -440,7 +439,7 @@ char *PlugGetMessage(PGLOBAL g, int mid) msg = stmsg; return msg; - } // end of PlugGetMessage +} // end of PlugGetMessage #endif // NEWMSG #if defined(__WIN__) @@ -448,13 +447,13 @@ char *PlugGetMessage(PGLOBAL g, int mid) /* Return the line length of the console screen buffer. */ /***********************************************************************/ short GetLineLength(PGLOBAL g) - { +{ CONSOLE_SCREEN_BUFFER_INFO coninfo; HANDLE hcons = GetStdHandle(STD_OUTPUT_HANDLE); BOOL b = GetConsoleScreenBufferInfo(hcons, &coninfo); return (b) ? coninfo.dwSize.X : 0; - } // end of GetLineLength +} // end of GetLineLength #endif // __WIN__ /***********************************************************************/ @@ -527,13 +526,13 @@ void FreeSarea(PGLOBAL g) /* the address and size not larger than memory size. */ /***********************************************************************/ BOOL PlugSubSet(void *memp, size_t size) - { +{ PPOOLHEADER pph = (PPOOLHEADER)memp; pph->To_Free = (size_t)sizeof(POOLHEADER); pph->FreeBlk = size - pph->To_Free; return FALSE; - } /* end of PlugSubSet */ +} /* end of PlugSubSet */ /***********************************************************************/ /* Use it to export a function that do throwing. */ @@ -596,7 +595,7 @@ void *PlugSubAlloc(PGLOBAL g, void *memp, size_t size) /* Program for sub-allocating and copying a string in a storage area. */ /***********************************************************************/ char *PlugDup(PGLOBAL g, const char *str) - { +{ if (str) { char *sm = (char*)PlugSubAlloc(g, NULL, strlen(str) + 1); @@ -605,12 +604,13 @@ char *PlugDup(PGLOBAL g, const char *str) } else return NULL; - } // end of PlugDup +} // end of PlugDup /*************************************************************************/ /* This routine makes a pointer from an offset to a memory pointer. */ /*************************************************************************/ -void* MakePtr(void* memp, size_t offset) { +void* MakePtr(void* memp, size_t offset) +{ // return ((offset == 0) ? NULL : &((char*)memp)[offset]); return (!offset) ? NULL : (char *)memp + offset; } /* end of MakePtr */ @@ -618,11 +618,14 @@ void* MakePtr(void* memp, size_t offset) { /*************************************************************************/ /* This routine makes an offset from a pointer new format. */ /*************************************************************************/ -size_t MakeOff(void* memp, void* ptr) { +size_t MakeOff(void* memp, void* ptr) +{ if (ptr) { #if defined(_DEBUG) || defined(DEVELOPMENT) - if (ptr <= memp) + if (ptr <= memp) { fprintf(stderr, "ptr %p <= memp %p", ptr, memp); + throw 999; + } // endif ptr #endif // _DEBUG || DEVELOPMENT return (size_t)((char*)ptr - (size_t)memp); } else diff --git a/storage/connect/tabbson.cpp b/storage/connect/tabbson.cpp index bac437b2d18..90a49aac1d5 100644 --- a/storage/connect/tabbson.cpp +++ b/storage/connect/tabbson.cpp @@ -259,7 +259,7 @@ int BSONDISC::GetColumns(PGLOBAL g, PCSZ db, PCSZ dsn, PTOS topt) } // endif Lrecl // Allocate the parse work memory - tdp->G = PlugInit(NULL, (size_t)tdp->Lrecl * (tdp->Pretty >= 0 ? 6 : 2)); + tdp->G = PlugInit(NULL, (size_t)tdp->Lrecl * (tdp->Pretty >= 0 ? 4 : 2)); tdp->Ending = GetIntegerTableOption(g, topt, "Ending", CRLF); if (tdp->Zipped) { @@ -1167,7 +1167,7 @@ PTDB BSONDEF::GetTable(PGLOBAL g, MODE m) if (Lrecl) { // Allocate the parse work memory - G = PlugInit(NULL, (size_t)Lrecl * 6); + G = PlugInit(NULL, (size_t)Lrecl * 4); } else { strcpy(g->Message, "LRECL is not defined"); return NULL; @@ -1200,7 +1200,6 @@ PTDB BSONDEF::GetTable(PGLOBAL g, MODE m) } // endif Driver } else if (Zipped) { -// if (Zipped) { #if defined(ZIP_SUPPORT) if (m == MODE_READ || m == MODE_ANY || m == MODE_ALTER) { txfp = new(g) UNZFAM(this); @@ -1226,9 +1225,9 @@ PTDB BSONDEF::GetTable(PGLOBAL g, MODE m) #endif // !GZ_SUPPORT } else if (map) txfp = new(g) MAPFAM(this); - else if (Pretty < 0) // BJsonfile + else if (Pretty < 0) { // BJsonfile txfp = new(g) BINFAM(this); - else + } else txfp = new(g) DOSFAM(this); // Txfp must be set for TDBBSN @@ -1436,7 +1435,6 @@ bool TDBBSN::OpenDB(PGLOBAL g) } // endif Use if (Pretty < 0) { -#if 0 /*******************************************************************/ /* Binary BJSON table. */ /*******************************************************************/ @@ -1450,7 +1448,7 @@ bool TDBBSN::OpenDB(PGLOBAL g) if (!To_Kindex) { Txfp->Rewind(); // see comment in Work.log } else // Table is to be accessed through a sorted index table - To_Kindex->Reset(); + To_Kindex->Reset(); // TODO: NIY return false; } // endif use @@ -1469,14 +1467,12 @@ bool TDBBSN::OpenDB(PGLOBAL g) /*********************************************************************/ size_t linelen = Lrecl; - //To_Line = (char*)PlugSubAlloc(g, NULL, linelen); - //memset(To_Line, 0, linelen); + // Buffer should be the first allocated thing in G->Sarea + Txfp->AllocateBuffer(Bp->G); To_Line = Txfp->GetBuf(); + memset(To_Line, 0, linelen); + Bp->MemSave(); xtrc(1, "OpenJSN: R%hd mode=%d To_Line=%p\n", Tdb_No, Mode, To_Line); - return false; -#endif // 0 - strcpy(g->Message, "TDBBSN: Binary NIY"); - return true; } else if (TDBDOS::OpenDB(g)) return true; @@ -1548,23 +1544,14 @@ int TDBBSN::ReadDB(PGLOBAL g) rc = RC_EF; } else { -#if 0 // Here we get a movable Json binary tree - PJSON jsp; - SWAP* swp; - - jsp = (PJSON)To_Line; - swp = new(g) SWAP(G, jsp); - swp->SwapJson(jsp, false); // Restore pointers from offsets - Row = jsp; - Row = FindRow(g); + Bp->SubSet(); // Perhaps Useful when updating + Row = (PBVAL)To_Line; + Row = Bp->FindRow(g); SameRow = 0; Fpos++; M = 1; rc = RC_OK; -#endif // 0 - strcpy(g->Message, "TDBBSN: Binary NIY"); - rc = RC_FX; } // endif Pretty } // endif ReadDB diff --git a/storage/connect/tabjson.cpp b/storage/connect/tabjson.cpp index f606f4b1a00..429efea56fa 100644 --- a/storage/connect/tabjson.cpp +++ b/storage/connect/tabjson.cpp @@ -298,14 +298,6 @@ int JSONDISC::GetColumns(PGLOBAL g, PCSZ db, PCSZ dsn, PTOS topt) tjnp->SetMode(MODE_READ); // Allocate the parse work memory -#if 0 - PGLOBAL G = (PGLOBAL)PlugSubAlloc(g, NULL, sizeof(GLOBAL)); - memset(G, 0, sizeof(GLOBAL)); - G->Sarea_Size = (size_t)tdp->Lrecl * (tdp->Pretty >= 0 ? 10 : 2); - G->Sarea = PlugSubAlloc(g, NULL, G->Sarea_Size); - PlugSubSet(G->Sarea, G->Sarea_Size); - G->jump_level = 0; -#endif // 0 G = PlugInit(NULL, (size_t)tdp->Lrecl * (tdp->Pretty >= 0 ? 10 : 2)); tjnp->SetG(G); |