return v;
}
-static bool
-j_atoi(char *c, int l, int *acc)
-{
- bool negative = false;
- char *p = c;
-
- *acc = 0;
-
- while(isspace(*p) && p - c < l)
- p++;
-
- if (p - c >= l)
- return false;
-
- if (*p == '-')
- {
- negative = true;
- p++;
- }
- else if (*p == '+')
- {
- p++;
- }
-
- if (p - c >= l)
- return false;
-
-
- while(p - c < l)
- {
- if (!isdigit(*p))
- return false;
-
- *acc *= 10;
- *acc += (*p - '0');
- p++;
- }
-
- if (negative)
- *acc = - *acc;
-
- return true;
-}
-
-static JsonbValue*
-JsonbDeepFetch(Jsonb *in, ArrayType *path)
-{
- JsonbValue *v = NULL;
- static JsonbValue init /* could be returned */;
- Datum *path_elems;
- bool *path_nulls;
- int path_len, i;
-
- Assert(ARR_ELEMTYPE(path) == TEXTOID);
-
- if (ARR_NDIM(path) > 1)
- ereport(ERROR,
- (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
- errmsg("wrong number of array subscripts")));
-
- if (JB_ROOT_COUNT(in) == 0)
- return NULL;
-
- deconstruct_array(path, TEXTOID, -1, false, 'i',
- &path_elems, &path_nulls, &path_len);
-
- init.type = jbvBinary;
- init.size = VARSIZE(in);
- init.binary.data = VARDATA(in);
- init.binary.len = VARSIZE_ANY_EXHDR(in);
-
- v = &init;
-
- if (path_len == 0)
- return v;
-
- for(i=0; v != NULL && i<path_len; i++)
- {
- uint32 header;
-
- if (v->type != jbvBinary || path_nulls[i])
- return NULL;
-
- header = *(uint32*)v->binary.data;
-
- if (header & JB_FLAG_OBJECT)
- {
- v = findUncompressedJsonbValue(v->binary.data, JB_FLAG_OBJECT,
- NULL,
- VARDATA_ANY(path_elems[i]),
- VARSIZE_ANY_EXHDR(path_elems[i]));
- }
- else if (header & JB_FLAG_ARRAY)
- {
- int ith;
-
- if (j_atoi(VARDATA_ANY(path_elems[i]),
- VARSIZE_ANY_EXHDR(path_elems[i]), &ith) == false)
- return NULL;
-
- if (ith < 0)
- {
- if (-ith > (int)(header & JB_COUNT_MASK))
- return NULL;
- else
- ith = ((int)(header & JB_COUNT_MASK)) + ith;
- }
- else
- {
- if (ith >= (int)(header & JB_COUNT_MASK))
- return NULL;
- }
-
- v = getJsonbValue(v->binary.data, JB_FLAG_ARRAY, ith);
- }
- else
- {
- elog(ERROR,"wrong jsonb container type");
- }
- }
-
- return v;
-}
-
Datum
jsonb_exists(PG_FUNCTION_ARGS)
{
}
/*
- * B-Tree operator class functions.
+ * B-Tree operator class procedures
*/
Datum
jsonb_cmp(PG_FUNCTION_ARGS)
#include "utils/array.h"
#include "utils/numeric.h"
-/*
- * JEntry: there is one of these for each key _and_ value in an jsonb
- *
- * the position offset points to the _end_ so that we can get the length
- * by subtraction from the previous entry. the ISFIRST flag lets us tell
- * whether there is a previous entry.
- */
-typedef struct
-{
- uint32 entry;
-} JEntry;
-
-#define JENTRY_ISFIRST 0x80000000
-#define JENTRY_ISSTRING (0x00000000) /* keep binary compatibility */
+#define JENTRY_ISFIRST (0x80000000)
+#define JENTRY_ISSTRING (0x00000000)
#define JENTRY_ISNUMERIC (0x10000000)
#define JENTRY_ISNEST (0x20000000)
-#define JENTRY_ISNULL (0x40000000) /* keep binary compatibility */
+#define JENTRY_ISNULL (0x40000000)
#define JENTRY_ISBOOL (0x10000000 | 0x20000000)
#define JENTRY_ISFALSE JENTRY_ISBOOL
#define JENTRY_ISTRUE (0x10000000 | 0x20000000 | 0x40000000)
*/
#define JSONB_MAX_STRING_LEN JENTRY_POSMASK
-typedef struct
-{
- int32 vl_len_; /* varlena header (do not touch directly!) */
- /* header of hash or array jsonb type */
- /* array of JEntry follows */
-} Jsonb;
-
/*
* it's not possible to get more than 2^28 items into an jsonb.
*/
typedef struct JsonbPair JsonbPair;
typedef struct JsonbValue JsonbValue;
+/*
+ * JEntry: there is one of these for each key _and_ value in a jsonb
+ *
+ * The position offset points to the _end_ so that we can get the length
+ * by subtraction from the previous entry. the ISFIRST flag lets us tell
+ * whether there is a previous entry.
+ */
+typedef struct
+{
+ uint32 entry;
+} JEntry;
+
+typedef struct
+{
+ int32 vl_len_; /* varlena header (do not touch directly!) */
+ /* header of hash or array jsonb type */
+ /* array of JEntry follows */
+} Jsonb;
+
struct JsonbValue
{
enum
jbvBool,
jbvArray,
jbvHash,
- jbvBinary /* binary form of jbvArray/jbvHash */
+ jbvBinary /* Binary form of jbvArray/jbvHash */
} type;
- uint32 size; /* estimation size of node (including
+ uint32 size; /* Estimation size of node (including
* subnodes) */
union
struct
{
uint32 len;
- char *val; /* could be not null-terminated */
+ char *val; /* Not necessarily null-terminated */
} string;
struct
{
int nelems;
JsonbValue *elems;
- bool scalar; /* scalar actually shares representation with
+ bool scalar; /* Scalar actually shares representation with
* array */
} array;
{
JsonbValue key;
JsonbValue value;
- uint32 order; /* to keep order of pairs with equal key */
+ uint32 order; /* To preserve order of pairs with equal keys */
};
+typedef struct ToJsonbState
+{
+ JsonbValue v;
+ uint32 size;
+ struct ToJsonbState *next;
+} ToJsonbState;
+
/*
- * jsonb support functios
+ * jsonb support functions
*/
-
#define WJB_KEY (0x001)
#define WJB_VALUE (0x002)
#define WJB_ELEM (0x004)
typedef void (*walk_jsonb_cb) (void * /* arg */ , JsonbValue * /* value */ ,
uint32 /* flags */ , uint32 /* level */ );
extern void walkUncompressedJsonb(JsonbValue *v, walk_jsonb_cb cb, void *cb_arg);
-
extern int compareJsonbStringValue(const void *a, const void *b, void *arg);
extern int compareJsonbPair(const void *a, const void *b, void *arg);
-
extern int compareJsonbBinaryValue(char *a, char *b);
extern int compareJsonbValue(JsonbValue *a, JsonbValue *b);
-
extern JsonbValue *findUncompressedJsonbValueByValue(char *buffer, uint32 flags,
uint32 *lowbound, JsonbValue *key);
extern JsonbValue *findUncompressedJsonbValue(char *buffer, uint32 flags,
uint32 *lowbound, char *key, uint32 keylen);
-
extern JsonbValue *getJsonbValue(char *buffer, uint32 flags, int32 i);
-
-typedef struct ToJsonbState
-{
- JsonbValue v;
- uint32 size;
- struct ToJsonbState *next;
-} ToJsonbState;
-
extern JsonbValue *pushJsonbValue(ToJsonbState ** state, int r /* WJB_* */ , JsonbValue *v);
-
extern void uniqueJsonbValue(JsonbValue *v);
-
extern uint32 compressJsonb(JsonbValue *v, char *buffer);
typedef struct JsonbIterator
} JsonbIterator;
extern JsonbIterator *JsonbIteratorInit(char *buffer);
-extern int /* WJB_* */ JsonbIteratorGet(JsonbIterator **it, JsonbValue *v, bool skipNested);
+extern int JsonbIteratorGet(JsonbIterator **it, JsonbValue *v, bool skipNested);
+extern char *JsonbToCString(StringInfo out, char *in, int estimated_len);
+extern Jsonb *JsonbValueToJsonb(JsonbValue *v);
+extern void JsonbPutEscapedValue(StringInfo out, JsonbValue *v);
+/* I/O routines */
extern Datum jsonb_in(PG_FUNCTION_ARGS);
extern Datum jsonb_out(PG_FUNCTION_ARGS);
extern Datum jsonb_recv(PG_FUNCTION_ARGS);
extern Datum jsonb_send(PG_FUNCTION_ARGS);
-
extern Datum jsonb_typeof(PG_FUNCTION_ARGS);
-extern char *JsonbToCString(StringInfo out, char *in, int estimated_len);
-extern Jsonb *JsonbValueToJsonb(JsonbValue *v);
-extern void JsonbPutEscapedValue(StringInfo out, JsonbValue *v);
/* Indexing-related ops */
extern Datum jsonb_exists(PG_FUNCTION_ARGS);