* bson_object_keys returns the set of keys for the object argument. * * The implementation is heavily based on jsonb_object_keys. * * This SRF operates in value-per-call mode. It processes the * object during the first call, and the keys are simply stashed * in an array, whose size is expanded as necessary. This is probably * safe enough for a list of keys of a single object, since they are
| 346 | * be so huge that it has major memory implications. |
| 347 | */ |
| 348 | Datum |
| 349 | bson_object_keys(PG_FUNCTION_ARGS) |
| 350 | { |
| 351 | FuncCallContext *functionContext; |
| 352 | BsonObjectKeysState *state; |
| 353 | |
| 354 | if (SRF_IS_FIRSTCALL()) |
| 355 | { |
| 356 | MemoryContext oldcontext; |
| 357 | pgbson *sourceBson = PG_GETARG_PGBSON(0); |
| 358 | bson_iter_t sourceBsonIterator; |
| 359 | |
| 360 | PgbsonInitIterator(sourceBson, &sourceBsonIterator); |
| 361 | |
| 362 | functionContext = SRF_FIRSTCALL_INIT(); |
| 363 | oldcontext = MemoryContextSwitchTo(functionContext->multi_call_memory_ctx); |
| 364 | |
| 365 | state = palloc(sizeof(BsonObjectKeysState)); |
| 366 | |
| 367 | state->resultSize = PgbsonCountKeys(sourceBson); |
| 368 | state->resultCount = 0; |
| 369 | state->sentCount = 0; |
| 370 | state->result = palloc(state->resultSize * sizeof(Datum)); |
| 371 | |
| 372 | while (bson_iter_next(&sourceBsonIterator)) |
| 373 | { |
| 374 | const char *key = bson_iter_key(&sourceBsonIterator); |
| 375 | |
| 376 | state->result[state->resultCount++] = CStringGetTextDatum(key); |
| 377 | } |
| 378 | |
| 379 | MemoryContextSwitchTo(oldcontext); |
| 380 | functionContext->user_fctx = (void *) state; |
| 381 | } |
| 382 | |
| 383 | functionContext = SRF_PERCALL_SETUP(); |
| 384 | state = (BsonObjectKeysState *) functionContext->user_fctx; |
| 385 | |
| 386 | if (state->sentCount < state->resultCount) |
| 387 | { |
| 388 | Datum next = state->result[state->sentCount++]; |
| 389 | |
| 390 | SRF_RETURN_NEXT(functionContext, next); |
| 391 | } |
| 392 | |
| 393 | SRF_RETURN_DONE(functionContext); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* |
nothing calls this directly
no test coverage detected