Skip to content

Commit

Permalink
Filestuff / DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Jan 22, 2025
1 parent 20f7562 commit 8445df9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/include/duckdb/web/io/web_filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ class WebFileSystem : public duckdb::FileSystem {
file_(file),
readahead_(nullptr),
position_(0) {
std::cout<<"constructor\n";
++file_->handle_count_;
}
/// Delete copy constructor
WebFileHandle(const WebFileHandle &) = delete;
/// Destructor
virtual ~WebFileHandle() { Close(); }
virtual ~WebFileHandle() {std::cout<<"destructor\t"<<"\n"; try{Close();}catch(...) {std::cout << "ooops\n";} }
/// Get the file name
auto &GetName() const { return file_->file_name_; }
/// Resolve readahead
Expand Down
11 changes: 5 additions & 6 deletions lib/src/io/web_filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ void WebFileSystem::DataBuffer::Resize(size_t n) {
data_ = std::move(next);
capacity_ = cap;
} else if (n < (capacity_ / 2)) {
auto next = std::unique_ptr<char[]>(new char[n]);
::memcpy(next.get(), data_.get(), n);
data_ = std::move(next);
capacity_ = n;
// auto next = std::unique_ptr<char[]>(new char[n]);
// ::memcpy(next.get(), data_.get(), n);
// data_ = std::move(next);
// capacity_ = n;
}
size_ = n;
}
Expand Down Expand Up @@ -840,6 +840,7 @@ int64_t WebFileSystem::Write(duckdb::FileHandle &handle, void *buffer, int64_t n
}
case DataProtocol::NODE_FS:
case DataProtocol::BROWSER_FSACCESS: {
case DataProtocol::BROWSER_FILEREADER:
auto end = file_hdl.position_ + nr_bytes;
size_t n;

Expand Down Expand Up @@ -876,8 +877,6 @@ int64_t WebFileSystem::Write(duckdb::FileHandle &handle, void *buffer, int64_t n
break;
}

case DataProtocol::BROWSER_FILEREADER:
throw std::runtime_error("HTML FileReaders do not support writing");
case DataProtocol::HTTP:
throw std::runtime_error("HTTP files do not support writing");
case DataProtocol::S3:
Expand Down
1 change: 1 addition & 0 deletions lib/src/webdb_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ void duckdb_web_tokenize(WASMResponse* packed, const char* query) {
}
/// Tokenize a query
void duckdb_web_tokenize_buffer(WASMResponse* packed, const uint8_t* buffer, size_t buffer_length) {
std::cout << buffer_length << " is the size in input\n";
GET_WEBDB(*packed);
std::string_view query(reinterpret_cast<const char*>(buffer), buffer_length);
auto tokens = webdb.Tokenize(query);
Expand Down
16 changes: 12 additions & 4 deletions packages/duckdb-wasm/src/bindings/bindings_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings {
/** Tokenize a script */
public tokenize(text: string): ScriptTokens {
const BUF = TEXT_ENCODER.encode(text);
const bufferPtr = this.mod._malloc(BUF.length );
const bufferOfs = this.mod.HEAPU8.subarray(bufferPtr, bufferPtr + BUF.length );
console.log(text, BUF, BUF.length);
const bufferPtr = this.mod._malloc(BUF.length +10);
const bufferOfs = this.mod.HEAPU8.subarray(bufferPtr, bufferPtr + BUF.length+10 );
bufferOfs.set(new Uint8Array(BUF.length + 5));
bufferOfs.set(BUF);
const [s, d, n] = callSRet(this.mod, 'duckdb_web_tokenize_buffer', ['number', 'number'], [bufferPtr, BUF.length]);
console.log("YO");
this.mod._free(bufferPtr);
if (s !== StatusCode.SUCCESS) {
throw new Error(readString(this.mod, d, n));
Expand Down Expand Up @@ -169,10 +172,15 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings {
/** Send a query and return the full result */
public runQuery(conn: number, text: string): Uint8Array {
const BUF = TEXT_ENCODER.encode(text);
const bufferPtr = this.mod._malloc(BUF.length);
const bufferOfs = this.mod.HEAPU8.subarray(bufferPtr, bufferPtr + BUF.length);
const bufferPtr = this.mod._malloc(BUF.length + 10);
const bufferOfs = this.mod.HEAPU8.subarray(bufferPtr, bufferPtr + BUF.length + 10);
bufferOfs.set(new Uint8Array(BUF.length + 5));
bufferOfs[BUF.length] = 0;
bufferOfs[BUF.length+1] = 0;
bufferOfs[BUF.length+2] = 0;
bufferOfs.set(BUF);
const [s, d, n] = callSRet(this.mod, 'duckdb_web_query_run_buffer', ['number', 'number', 'number'], [conn, bufferPtr, BUF.length]);
console.log("YOP");
this.mod._free(bufferPtr);
if (s !== StatusCode.SUCCESS) {
throw new Error(readString(this.mod, d, n));
Expand Down
20 changes: 15 additions & 5 deletions packages/duckdb-wasm/src/bindings/runtime_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,9 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
return;
case DuckDBDataProtocol.BUFFER:
case DuckDBDataProtocol.NODE_FS:
case DuckDBDataProtocol.BROWSER_FILEREADER:
failWith(mod, `truncateFile not implemented`);
return;
case DuckDBDataProtocol.BROWSER_FILEREADER:
case DuckDBDataProtocol.BROWSER_FSACCESS: {
const handle = BROWSER_RUNTIME._files?.get(file.fileName);
if (!handle) {
Expand Down Expand Up @@ -484,8 +484,10 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
if (!handle) {
throw new Error(`No HTML5 file registered with name: ${file.fileName}`);
}
const sliced = handle!.slice(location, location + bytes);
const data = new Uint8Array(new FileReaderSync().readAsArrayBuffer(sliced));

const data = new Uint8Array(bytes);
for (var i = location; i< location + bytes; i++) data[i-location] = handle[i];

mod.HEAPU8.set(data, buf);
return data.byteLength;
}
Expand Down Expand Up @@ -524,8 +526,16 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
return bytes;
}
case DuckDBDataProtocol.BROWSER_FILEREADER:
failWith(mod, 'cannot write using the html5 file reader api');
return 0;
let handle = BROWSER_RUNTIME._files?.get(file.fileName);
if (!handle) {
handle = BROWSER_RUNTIME._files?.set(file.fileName, new Uint8Array(1000000));
//throw new Error(`No OPFS access handle registered with name: ${file.fileName}`);
}
const input = mod.HEAPU8.subarray(buf, buf + bytes);
for (var i = buf; i< buf + bytes; i++) handle[i] = input[i-buf];
//return handle.write(input, { at: location });
console.log("write bytes", bytes, location);
return bytes;
case DuckDBDataProtocol.BROWSER_FSACCESS: {
const handle = BROWSER_RUNTIME._files?.get(file.fileName);
if (!handle) {
Expand Down

0 comments on commit 8445df9

Please sign in to comment.