Skip to content

Commit

Permalink
Add setting reliableHeadRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Jun 5, 2024
1 parent 17065c3 commit a1865ea
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/include/duckdb/web/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct DuckDBConfigOptions {
struct FileSystemConfig {
/// Allow falling back to full HTTP reads if the server does not support range requests
std::optional<bool> allow_full_http_reads = std::nullopt;
std::optional<bool> reliableHeadRequests = std::nullopt;
};

struct WebDBConfig {
Expand All @@ -87,7 +88,7 @@ struct WebDBConfig {
.cast_decimal_to_double = std::nullopt,
};
/// The filesystem
FileSystemConfig filesystem = {.allow_full_http_reads = std::nullopt};
FileSystemConfig filesystem = {.allow_full_http_reads = std::nullopt, .reliableHeadRequests = std::nullopt};

/// These options are fetched from DuckDB
DuckDBConfigOptions duckdb_config_options = {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ WebDBConfig WebDBConfig::ReadFrom(std::string_view args_json) {
.filesystem =
FileSystemConfig{
.allow_full_http_reads = std::nullopt,
.reliableHeadRequests = std::nullopt,
},
.duckdb_config_options =
DuckDBConfigOptions{
Expand Down Expand Up @@ -97,6 +98,9 @@ WebDBConfig WebDBConfig::ReadFrom(std::string_view args_json) {
if (fs.HasMember("allowFullHTTPReads") && fs["allowFullHTTPReads"].IsBool()) {
config.filesystem.allow_full_http_reads = fs["allowFullHTTPReads"].GetBool();
}
if (fs.HasMember("reliableHeadRequests") && fs["reliableHeadRequests"].IsBool()) {
config.filesystem.reliableHeadRequests = fs["reliableHeadRequests"].GetBool();
}
}
}
if (!config.query.cast_bigint_to_double.has_value()) {
Expand Down
7 changes: 7 additions & 0 deletions lib/src/io/web_filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ rapidjson::Value WebFileSystem::WebFile::WriteInfo(rapidjson::Document &doc) con
filesystem_.config_->filesystem.allow_full_http_reads.value_or(true)) {
value.AddMember("allowFullHttpReads", true, allocator);
}
if ((data_protocol_ == DataProtocol::HTTP || data_protocol_ == DataProtocol::S3) &&
filesystem_.config_->filesystem.reliableHeadRequests.value_or(true)) {
value.AddMember("reliableHeadRequests", true, allocator);
}
value.AddMember("collectStatistics", filesystem_.file_statistics_->TracksFile(file_name_), doc.GetAllocator());

if (data_protocol_ == DataProtocol::S3) {
Expand Down Expand Up @@ -493,6 +497,9 @@ rapidjson::Value WebFileSystem::WriteGlobalFileInfo(rapidjson::Document &doc, ui
if (config_->filesystem.allow_full_http_reads.value_or(true)) {
value.AddMember("allowFullHttpReads", true, allocator);
}
if (config_->filesystem.reliableHeadRequests.value_or(true)) {
value.AddMember("reliableHeadRequests", true, allocator);
}

value.AddMember("s3Config", writeS3Config(config_->duckdb_config_options, allocator), allocator);

Expand Down
2 changes: 2 additions & 0 deletions packages/duckdb-wasm-shell/crate/src/duckdb/web_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct WebFile {
pub data_url: Option<String>,
#[serde(rename = "dataNativeFd")]
pub data_native_fd: Option<u64>,
#[serde(rename = "reliableHeadRequests")]
pub allow_full_http_reads: Option<bool>,
#[serde(rename = "allowFullHttpReads")]
pub allow_full_http_reads: Option<bool>,
#[serde(rename = "collectStatistics")]
Expand Down
1 change: 1 addition & 0 deletions packages/duckdb-wasm/src/bindings/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface DuckDBFilesystemConfig {
/**
* Allow falling back to full HTTP reads if the server does not support range requests.
*/
reliableHeadRequests?: boolean;
allowFullHTTPReads?: boolean;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/duckdb-wasm/src/bindings/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ export interface DuckDBFileInfo {
fileName: string;
dataProtocol: DuckDBDataProtocol;
dataUrl: string | null;
reliableHeadRequests?: boolean;
allowFullHttpReads?: boolean;
s3Config?: S3Config;
}

/** Global info for all files registered with DuckDB */
export interface DuckDBGlobalFileInfo {
cacheEpoch: number;
reliableHeadRequests?: boolean;
allowFullHttpReads?: boolean;
s3Config?: S3Config;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/duckdb-wasm/src/bindings/runtime_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
// Supports ranges?
let contentLength = null;
let error: any | null = null;
if (!file.allowFullHttpReads) {
if (file.reliableHeadRequests || !file.allowFullHttpReads) {
try {
// Send a dummy HEAD request with range protocol
// -> good IFF status is 206 and contentLenght is present
Expand Down Expand Up @@ -211,7 +211,7 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
let presumedLength = null;
if (contentRange !== undefined) {
presumedLength = contentRange;
} else {
} else if (!file.reliableHeadRequests) {
// Send a dummy HEAD request with range protocol
// -> good IFF status is 206 and contentLenght is present
const head = new XMLHttpRequest();
Expand Down
1 change: 1 addition & 0 deletions packages/duckdb-wasm/src/bindings/web_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export interface WebFile {
dataUrl?: string;
dataNativeFd?: number;
collectStatistics?: boolean;
reliableHeadRequests?: boolean;
allowFullHttpReads?: boolean;
}

0 comments on commit a1865ea

Please sign in to comment.