* Creates a bsonsequence from a single pgbson. */
| 188 | * Creates a bsonsequence from a single pgbson. |
| 189 | */ |
| 190 | Datum |
| 191 | bson_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(¤tDoc, 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, ¤tDoc); |
| 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 | /* |
nothing calls this directly
no test coverage detected