Skip to content

Commit

Permalink
duckdbvfs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Sep 19, 2023
1 parent 69b720c commit 15c2b77
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/include/sqlite_scanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
namespace duckdb {
class SQLiteDB;

ClientContext * getclientcontext();
DatabaseInstance *getdb(void);
void setdb(DatabaseInstance *);

struct SqliteBindData : public TableFunctionData {
string file_name;
Expand Down
93 changes: 43 additions & 50 deletions src/sqlite/duckdbvfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,6 @@

#include "sqlite3.h"

#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/param.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <iostream>

/*
** Size of the write buffer used by journal files in bytes.
*/
Expand Down Expand Up @@ -345,7 +333,6 @@ static int duckdbSync(sqlite3_file *pFile, int flags){
static int duckdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
DemoFile *p = (DemoFile*)pFile;
int rc; /* Return code from fstat() call */
struct stat sStat; /* Output of fstat() call */

/* Flush the contents of the buffer to disk. As with the flush in the
** duckdbRead() method, it would be possible to avoid this and save a write
Expand Down Expand Up @@ -423,7 +410,8 @@ static int duckdbOpen(
duckdbSectorSize, /* xSectorSize */
duckdbDeviceCharacteristics /* xDeviceCharacteristics */
};
auto &fs = duckdb::getclientcontext()->db->GetFileSystem();
D_ASSERT(duckdb::getdb());
auto &fs = duckdb::getdb()->GetFileSystem();

DemoFile *p = (DemoFile*)pFile; /* Populate this structure */
int oflags = 0; /* flags to pass to open() call */
Expand Down Expand Up @@ -466,33 +454,36 @@ static int duckdbOpen(
** file has been synced to disk before returning.
*/
static int duckdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
int rc; /* Return code */
//FIXME: This might need to be properly implemented
return SQLITE_OK;

rc = unlink(zPath);
if( rc!=0 && errno==ENOENT ) return SQLITE_OK;
// int rc; /* Return code */

if( rc==0 && dirSync ){
int dfd; /* File descriptor open on directory */
char *zSlash;
char zDir[MAXPATHNAME+1]; /* Name of directory containing file zPath */
// rc = unlink(zPath);
// if( rc!=0 && errno==ENOENT ) return SQLITE_OK;

// if( rc==0 && dirSync ){
// int dfd; /* File descriptor open on directory */
// char *zSlash;
// char zDir[MAXPATHNAME+1]; /* Name of directory containing file zPath */

/* Figure out the directory name from the path of the file deleted. */
sqlite3_snprintf(MAXPATHNAME, zDir, "%s", zPath);
zDir[MAXPATHNAME] = '\0';
zSlash = strrchr(zDir,'/');
if( zSlash ){
/* Open a file-descriptor on the directory. Sync. Close. */
zSlash[0] = 0;
dfd = open(zDir, O_RDONLY, 0);
if( dfd<0 ){
rc = -1;
}else{
rc = fsync(dfd);
close(dfd);
}
}
}
return (rc==0 ? SQLITE_OK : SQLITE_IOERR_DELETE);
// sqlite3_snprintf(MAXPATHNAME, zDir, "%s", zPath);
// zDir[MAXPATHNAME] = '\0';
// zSlash = strrchr(zDir,'/');
// if( zSlash ){
// /* Open a file-descriptor on the directory. Sync. Close. */
// zSlash[0] = 0;
// dfd = open(zDir, O_RDONLY, 0);
// if( dfd<0 ){
// rc = -1;
// }else{
// rc = fsync(dfd);
// close(dfd);
// }
// }
// }
// return (rc==0 ? SQLITE_OK : SQLITE_IOERR_DELETE);
}

#ifndef F_OK
Expand All @@ -515,20 +506,23 @@ static int duckdbAccess(
int flags,
int *pResOut
){
int rc; /* access() return code */
int eAccess = F_OK; /* Second argument to access() */
//FIXME: This might need to be properly implemented
*pResOut = false;
return SQLITE_OK;
// int rc; /* access() return code */
// int eAccess = F_OK; /* Second argument to access() */

assert( flags==SQLITE_ACCESS_EXISTS /* access(zPath, F_OK) */
|| flags==SQLITE_ACCESS_READ /* access(zPath, R_OK) */
|| flags==SQLITE_ACCESS_READWRITE /* access(zPath, R_OK|W_OK) */
);
// assert( flags==SQLITE_ACCESS_EXISTS /* access(zPath, F_OK) */
// || flags==SQLITE_ACCESS_READ /* access(zPath, R_OK) */
// || flags==SQLITE_ACCESS_READWRITE /* access(zPath, R_OK|W_OK) */
// );

if( flags==SQLITE_ACCESS_READWRITE ) eAccess = R_OK|W_OK;
if( flags==SQLITE_ACCESS_READ ) eAccess = R_OK;
// if( flags==SQLITE_ACCESS_READWRITE ) eAccess = R_OK|W_OK;
// if( flags==SQLITE_ACCESS_READ ) eAccess = R_OK;

rc = access(zPath, eAccess);
*pResOut = (rc==0);
return SQLITE_OK;
// rc = access(zPath, eAccess);
// *pResOut = (rc==0);
// return SQLITE_OK;
}

/*
Expand Down Expand Up @@ -602,8 +596,7 @@ static int duckdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zByte){
** of microseconds slept for.
*/
static int duckdbSleep(sqlite3_vfs *pVfs, int nMicro){
sleep(nMicro / 1000000);
usleep(nMicro % 1000000);
//FIXME: This might need to be properly implemented
return nMicro;
}

Expand Down
4 changes: 2 additions & 2 deletions src/sqlite/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -42768,7 +42768,7 @@ static int proxyClose(sqlite3_file *id) {
******************************************************************************/


sqlite3_vfs *sqlite3_duckdbvfs();
sqlite3_vfs *sqlite3_duckdbvfs(void);

/*
** Initialize the operating system interface.
Expand Down Expand Up @@ -42828,7 +42828,6 @@ SQLITE_API int sqlite3_os_init(void){
unixGetSystemCall, /* xGetSystemCall */ \
unixNextSystemCall, /* xNextSystemCall */ \
}
sqlite3_vfs_register(sqlite3_duckdbvfs(), 0);
/*
** All default VFSes for unix are contained in the following array.
**
Expand Down Expand Up @@ -42873,6 +42872,7 @@ SQLITE_API int sqlite3_os_init(void){
// We DON'T register any other vfs, so duckdbvfs is the only one used
//sqlite3_vfs_register(&aVfs[i], i==0);
}
sqlite3_vfs_register(sqlite3_duckdbvfs(), 0);
unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);

#ifndef SQLITE_OMIT_WAL
Expand Down
1 change: 1 addition & 0 deletions src/sqlite_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace duckdb;
extern "C" {

static void LoadInternal(DatabaseInstance &db) {
setdb(&db);
SqliteScanFunction sqlite_fun;
ExtensionUtil::RegisterFunction(db, sqlite_fun);

Expand Down
13 changes: 6 additions & 7 deletions src/sqlite_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

namespace duckdb {

ClientContext * clientcontext = nullptr;
ClientContext * getclientcontext() {
return clientcontext;
DatabaseInstance *_db;
void setdb(DatabaseInstance *db) {
_db=db;
}
DatabaseInstance * getdb() {
return _db;
}

struct SqliteLocalState : public LocalTableFunctionState {
Expand Down Expand Up @@ -48,7 +51,6 @@ struct SqliteGlobalState : public GlobalTableFunctionState {

static unique_ptr<FunctionData> SqliteBind(ClientContext &context, TableFunctionBindInput &input,
vector<LogicalType> &return_types, vector<string> &names) {

auto result = make_uniq<SqliteBindData>();
result->file_name = input.inputs[0].GetValue<string>();
result->table_name = input.inputs[1].GetValue<string>();
Expand Down Expand Up @@ -88,7 +90,6 @@ static unique_ptr<FunctionData> SqliteBind(ClientContext &context, TableFunction

static void SqliteInitInternal(ClientContext &context, const SqliteBindData &bind_data, SqliteLocalState &local_state,
idx_t rowid_min, idx_t rowid_max) {
clientcontext = &context;
D_ASSERT(rowid_min <= rowid_max);

local_state.done = false;
Expand Down Expand Up @@ -277,7 +278,6 @@ struct AttachFunctionData : public TableFunctionData {

static unique_ptr<FunctionData> AttachBind(ClientContext &context, TableFunctionBindInput &input,
vector<LogicalType> &return_types, vector<string> &names) {
clientcontext = &context;

auto result = make_uniq<AttachFunctionData>();
result->file_name = input.inputs[0].GetValue<string>();
Expand All @@ -294,7 +294,6 @@ static unique_ptr<FunctionData> AttachBind(ClientContext &context, TableFunction
}

static void AttachFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
clientcontext = &context;
auto &data = data_p.bind_data->CastNoConst<AttachFunctionData>();
if (data.finished) {
return;
Expand Down

0 comments on commit 15c2b77

Please sign in to comment.