MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / ConsumeBytes

Function ConsumeBytes

crates/bindings-cpp/src/internal/Module.cpp:389–449  ·  view source on GitHub ↗

Helper to consume all bytes from a BytesSource

Source from the content-addressed store, hash-verified

387
388// Helper to consume all bytes from a BytesSource
389std::vector<uint8_t> ConsumeBytes(BytesSource source) {
390 if (source.inner == 0) {
391 return {};
392 }
393
394 // Take a buffer from the pool (typically 64 KiB pre-allocated)
395 IterBuf iter_buf = IterBuf::take();
396
397 // Get the remaining length to reserve exact buffer size
398 uint32_t remaining_len = 0;
399 auto ret = FFI::bytes_source_remaining_length(source, &remaining_len);
400 if (ret != 0) {
401 // If we can't get the length, fall back to incremental reading
402 // This shouldn't happen with current host implementation
403 constexpr size_t CHUNK_SIZE = 1024;
404 iter_buf.reserve(CHUNK_SIZE);
405
406 while (true) {
407 size_t chunk_size = CHUNK_SIZE;
408 size_t old_size = iter_buf.size();
409 iter_buf.resize(old_size + chunk_size);
410
411 ret = FFI::bytes_source_read(source, iter_buf.data() + old_size, &chunk_size);
412 iter_buf.resize(old_size + chunk_size); // Resize to actual bytes read
413
414 if (ret == -1) { // EXHAUSTED
415 break;
416 } else if (ret != 0) { // Error
417 fprintf(stderr, "ERROR: Failed to read from BytesSource: %d\n", ret);
418 break;
419 }
420 }
421 return iter_buf.release();
422 }
423
424 // Reserve exact size needed (often no-op since pool buffer is 64 KiB)
425 iter_buf.resize(remaining_len); // Resize to exact size BEFORE reading
426
427 // Read all bytes - should complete in one call since we have capacity
428 size_t bytes_read = 0;
429 while (bytes_read < remaining_len) {
430 size_t chunk_size = remaining_len - bytes_read;
431 ret = FFI::bytes_source_read(source, iter_buf.data() + bytes_read, &chunk_size);
432 bytes_read += chunk_size;
433
434 if (ret == -1) { // EXHAUSTED
435 break;
436 } else if (ret != 0) { // Error
437 fprintf(stderr, "ERROR: Failed to read from BytesSource: %d\n", ret);
438 break;
439 }
440 }
441
442 // Resize to actual bytes read if different (shouldn't normally happen)
443 if (bytes_read != remaining_len) {
444 iter_buf.resize(bytes_read);
445 }
446

Callers 7

__call_http_handler__Method · 0.85
SendImplMethod · 0.85
RegisterReducerMethod · 0.85
RegisterProcedureMethod · 0.85
RegisterReducerCommonMethod · 0.85
RegisterProcedureMethod · 0.85

Calls 6

takeFunction · 0.85
reserveMethod · 0.80
resizeMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…