MCPcopy Create free account
hub / github.com/documentdb/documentdb / bson_to_bsonsequence

Function bson_to_bsonsequence

pg_documentdb_core/src/io/pgbsonsequence.c:190–225  ·  view source on GitHub ↗

* Creates a bsonsequence from a single pgbson. */

Source from the content-addressed store, hash-verified

188 * Creates a bsonsequence from a single pgbson.
189 */
190Datum
191bson_to_bsonsequence(PG_FUNCTION_ARGS)
192{
193 pgbson *bson = PG_GETARG_PGBSON(0);
194 bson_writer_t *writer;
195 uint8_t *buf = NULL;
196 size_t buflen = 0;
197
198 writer = bson_writer_new(&buf, &buflen, 0, bson_realloc_ctx, NULL);
199
200 bson_value_t currentValue = ConvertPgbsonToBsonValue(bson);
201 bson_t *doc;
202 bson_t currentDoc;
203
204 bson_writer_begin(writer, &doc);
205
206 if (!bson_init_static(&currentDoc, currentValue.value.v_doc.data,
207 currentValue.value.v_doc.data_len))
208 {
209 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_FAILEDTOPARSE),
210 errmsg("Bson value initialization failed")));
211 }
212
213 bson_concat(doc, &currentDoc);
214 bson_writer_end(writer);
215 buflen = bson_writer_get_length(writer);
216 bson_writer_destroy(writer);
217
218 uint32_t sequenceSize = VARHDRSZ + buflen;
219 pgbsonsequence *sequence = palloc(sequenceSize);
220 SET_VARSIZE(sequence, sequenceSize);
221 memcpy(VARDATA(sequence), buf, buflen);
222
223 bson_free(buf);
224 PG_RETURN_POINTER(sequence);
225}
226
227
228/*

Callers

nothing calls this directly

Calls 1

ConvertPgbsonToBsonValueFunction · 0.85

Tested by

no test coverage detected