| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef __JSONB_H__ |
| #define __JSONB_H__ |
|
|
| #include "lib/stringinfo.h" |
| #include "utils/array.h" |
| #include "utils/numeric.h" |
|
|
| |
| typedef enum |
| { |
| WJB_DONE, |
| WJB_KEY, |
| WJB_VALUE, |
| WJB_ELEM, |
| WJB_BEGIN_ARRAY, |
| WJB_END_ARRAY, |
| WJB_BEGIN_OBJECT, |
| WJB_END_OBJECT, |
| } JsonbIteratorToken; |
|
|
| |
| #define JsonbContainsStrategyNumber 7 |
| #define JsonbExistsStrategyNumber 9 |
| #define JsonbExistsAnyStrategyNumber 10 |
| #define JsonbExistsAllStrategyNumber 11 |
| #define JsonbJsonpathExistsStrategyNumber 15 |
| #define JsonbJsonpathPredicateStrategyNumber 16 |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #define JGINFLAG_KEY 0x01 |
| #define JGINFLAG_NULL 0x02 |
| #define JGINFLAG_BOOL 0x03 |
| #define JGINFLAG_NUM 0x04 |
| #define JGINFLAG_STR 0x05 |
| #define JGINFLAG_HASHED 0x10 |
| #define JGIN_MAXLENGTH 125 |
|
|
| typedef struct JsonbPair JsonbPair; |
| typedef struct JsonbValue JsonbValue; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef uint32 JEntry; |
|
|
| #define JENTRY_OFFLENMASK 0x0FFFFFFF |
| #define JENTRY_TYPEMASK 0x70000000 |
| #define JENTRY_HAS_OFF 0x80000000 |
|
|
| |
| #define JENTRY_ISSTRING 0x00000000 |
| #define JENTRY_ISNUMERIC 0x10000000 |
| #define JENTRY_ISBOOL_FALSE 0x20000000 |
| #define JENTRY_ISBOOL_TRUE 0x30000000 |
| #define JENTRY_ISNULL 0x40000000 |
| #define JENTRY_ISCONTAINER 0x50000000 |
|
|
| |
| #define JBE_OFFLENFLD(je_) ((je_) & JENTRY_OFFLENMASK) |
| #define JBE_HAS_OFF(je_) (((je_) & JENTRY_HAS_OFF) != 0) |
| #define JBE_ISSTRING(je_) (((je_) & JENTRY_TYPEMASK) == JENTRY_ISSTRING) |
| #define JBE_ISNUMERIC(je_) (((je_) & JENTRY_TYPEMASK) == JENTRY_ISNUMERIC) |
| #define JBE_ISCONTAINER(je_) (((je_) & JENTRY_TYPEMASK) == JENTRY_ISCONTAINER) |
| #define JBE_ISNULL(je_) (((je_) & JENTRY_TYPEMASK) == JENTRY_ISNULL) |
| #define JBE_ISBOOL_TRUE(je_) (((je_) & JENTRY_TYPEMASK) == JENTRY_ISBOOL_TRUE) |
| #define JBE_ISBOOL_FALSE(je_) (((je_) & JENTRY_TYPEMASK) == JENTRY_ISBOOL_FALSE) |
| #define JBE_ISBOOL(je_) (JBE_ISBOOL_TRUE(je_) || JBE_ISBOOL_FALSE(je_)) |
|
|
| |
| #define JBE_ADVANCE_OFFSET(offset, je) \ |
| do { \ |
| JEntry je_ = (je); \ |
| if (JBE_HAS_OFF(je_)) \ |
| (offset) = JBE_OFFLENFLD(je_); \ |
| else \ |
| (offset) += JBE_OFFLENFLD(je_); \ |
| } while(0) |
|
|
| |
| |
| |
| |
| |
| |
| |
| #define JB_OFFSET_STRIDE 32 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct JsonbContainer |
| { |
| uint32 header; |
| |
| JEntry children[FLEXIBLE_ARRAY_MEMBER]; |
|
|
| |
| } JsonbContainer; |
|
|
| |
| #define JB_CMASK 0x0FFFFFFF |
| #define JB_FSCALAR 0x10000000 |
| #define JB_FOBJECT 0x20000000 |
| #define JB_FARRAY 0x40000000 |
|
|
| |
| #define JsonContainerSize(jc) ((jc)->header & JB_CMASK) |
| #define JsonContainerIsScalar(jc) (((jc)->header & JB_FSCALAR) != 0) |
| #define JsonContainerIsObject(jc) (((jc)->header & JB_FOBJECT) != 0) |
| #define JsonContainerIsArray(jc) (((jc)->header & JB_FARRAY) != 0) |
|
|
| |
| typedef struct |
| { |
| int32 vl_len_; |
| JsonbContainer root; |
| } Jsonb; |
|
|
| |
| #define JB_ROOT_COUNT(jbp_) (*(uint32 *) VARDATA(jbp_) & JB_CMASK) |
| #define JB_ROOT_IS_SCALAR(jbp_) ((*(uint32 *) VARDATA(jbp_) & JB_FSCALAR) != 0) |
| #define JB_ROOT_IS_OBJECT(jbp_) ((*(uint32 *) VARDATA(jbp_) & JB_FOBJECT) != 0) |
| #define JB_ROOT_IS_ARRAY(jbp_) ((*(uint32 *) VARDATA(jbp_) & JB_FARRAY) != 0) |
|
|
|
|
| enum jbvType |
| { |
| |
| jbvNull = 0x0, |
| jbvString, |
| jbvNumeric, |
| jbvBool, |
| |
| jbvArray = 0x10, |
| jbvObject, |
| |
| jbvBinary, |
|
|
| |
| |
| |
| |
| |
| |
| jbvDatetime = 0x20, |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| struct JsonbValue |
| { |
| enum jbvType type; |
|
|
| union |
| { |
| Numeric numeric; |
| bool boolean; |
| struct |
| { |
| int len; |
| char *val; |
| } string; |
|
|
| struct |
| { |
| int nElems; |
| JsonbValue *elems; |
| bool rawScalar; |
| } array; |
|
|
| struct |
| { |
| int nPairs; |
| JsonbPair *pairs; |
| } object; |
|
|
| struct |
| { |
| int len; |
| JsonbContainer *data; |
| } binary; |
|
|
| struct |
| { |
| Datum value; |
| Oid typid; |
| int32 typmod; |
| int tz; |
| |
| } datetime; |
| } val; |
| }; |
|
|
| #define IsAJsonbScalar(jsonbval) (((jsonbval)->type >= jbvNull && \ |
| (jsonbval)->type <= jbvBool) || \ |
| (jsonbval)->type == jbvDatetime) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| struct JsonbPair |
| { |
| JsonbValue key; |
| JsonbValue value; |
| uint32 order; |
| }; |
|
|
| |
| typedef struct JsonbParseState |
| { |
| JsonbValue contVal; |
| Size size; |
| struct JsonbParseState *next; |
| bool unique_keys; |
| bool skip_nulls; |
| } JsonbParseState; |
|
|
| |
| |
| |
| |
| typedef enum |
| { |
| JBI_ARRAY_START, |
| JBI_ARRAY_ELEM, |
| JBI_OBJECT_START, |
| JBI_OBJECT_KEY, |
| JBI_OBJECT_VALUE, |
| } JsonbIterState; |
|
|
| typedef struct JsonbIterator |
| { |
| |
| JsonbContainer *container; |
| uint32 nElems; |
| |
| bool isScalar; |
| JEntry *children; |
| |
| char *dataProper; |
|
|
| |
| int curIndex; |
|
|
| |
| uint32 curDataOffset; |
|
|
| |
| |
| |
| |
| |
| uint32 curValueOffset; |
|
|
| |
| JsonbIterState state; |
|
|
| struct JsonbIterator *parent; |
| } JsonbIterator; |
|
|
|
|
| |
| static inline Jsonb * |
| DatumGetJsonbP(Datum d) |
| { |
| return (Jsonb *) PG_DETOAST_DATUM(d); |
| } |
|
|
| static inline Jsonb * |
| DatumGetJsonbPCopy(Datum d) |
| { |
| return (Jsonb *) PG_DETOAST_DATUM_COPY(d); |
| } |
|
|
| static inline Datum |
| JsonbPGetDatum(const Jsonb *p) |
| { |
| return PointerGetDatum(p); |
| } |
|
|
| #define PG_GETARG_JSONB_P(x) DatumGetJsonbP(PG_GETARG_DATUM(x)) |
| #define PG_GETARG_JSONB_P_COPY(x) DatumGetJsonbPCopy(PG_GETARG_DATUM(x)) |
| #define PG_RETURN_JSONB_P(x) PG_RETURN_POINTER(x) |
|
|
| |
| extern uint32 getJsonbOffset(const JsonbContainer *jc, int index); |
| extern uint32 getJsonbLength(const JsonbContainer *jc, int index); |
| extern int compareJsonbContainers(JsonbContainer *a, JsonbContainer *b); |
| extern JsonbValue *findJsonbValueFromContainer(JsonbContainer *container, |
| uint32 flags, |
| JsonbValue *key); |
| extern JsonbValue *getKeyJsonValueFromContainer(JsonbContainer *container, |
| const char *keyVal, int keyLen, |
| JsonbValue *res); |
| extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *container, |
| uint32 i); |
| extern JsonbValue *pushJsonbValue(JsonbParseState **pstate, |
| JsonbIteratorToken seq, JsonbValue *jbval); |
| extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container); |
| extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, |
| bool skipNested); |
| extern void JsonbToJsonbValue(Jsonb *jsonb, JsonbValue *val); |
| extern Jsonb *JsonbValueToJsonb(JsonbValue *val); |
| extern bool JsonbDeepContains(JsonbIterator **val, |
| JsonbIterator **mContained); |
| extern void JsonbHashScalarValue(const JsonbValue *scalarVal, uint32 *hash); |
| extern void JsonbHashScalarValueExtended(const JsonbValue *scalarVal, |
| uint64 *hash, uint64 seed); |
|
|
| |
| extern char *JsonbToCString(StringInfo out, JsonbContainer *in, |
| int estimated_len); |
| extern char *JsonbToCStringIndent(StringInfo out, JsonbContainer *in, |
| int estimated_len); |
| extern char *JsonbUnquote(Jsonb *jb); |
| extern bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res); |
| extern const char *JsonbTypeName(JsonbValue *val); |
|
|
| extern Datum jsonb_set_element(Jsonb *jb, Datum *path, int path_len, |
| JsonbValue *newval); |
| extern Datum jsonb_get_element(Jsonb *jb, Datum *path, int npath, |
| bool *isnull, bool as_text); |
| extern bool to_jsonb_is_immutable(Oid typoid); |
| extern Datum jsonb_build_object_worker(int nargs, const Datum *args, const bool *nulls, |
| const Oid *types, bool absent_on_null, |
| bool unique_keys); |
| extern Datum jsonb_build_array_worker(int nargs, const Datum *args, const bool *nulls, |
| const Oid *types, bool absent_on_null); |
|
|
| #endif |
|
|