Skip to content

Commit

Permalink
Merge pull request #2 from PDOK/add_cachsize_and_curl_logging_options
Browse files Browse the repository at this point in the history
Allow user to configure cache size + cURL debug logging
  • Loading branch information
rkettelerij authored Jan 25, 2024
2 parents 94f751d + a1e7352 commit bec9427
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# go-cloud-sqlite-vfs

# Description
## Description

This project wraps the [Cloud Backed SQLite](https://sqlite.org/cloudsqlite/doc/trunk/www/index.wiki) (CBS)
solution into a golang package. The project uses [The SQLite OS Interface or "VFS"](https://www.sqlite.org/vfs.html)
solution into a Go package. The project uses [The SQLite OS Interface or "VFS"](https://www.sqlite.org/vfs.html)
concept to create a VFS which is backed by either Azure Blob Storage or Google Cloud Storage. The VFS can be used
with every SQLite golang package as long as it supports setting a custom VFS name.
with every SQLite Go package as long as it supports setting a custom VFS name.

Below are some examples about how to use this package, for further information about the workings of this package
please read the [documentation](https://sqlite.org/cloudsqlite/doc/trunk/www/index.wiki) of the CBS project.

# Installation
## Installation

This package can be installed with the `go get` command:

```bash
go get github.com/PDOK/go-cloud-sqlite-vfs
```

**go-cloud-sqlite-vfs is cgo package**. If you want to build your app using go-cloud-sqlite-vfs , you need a C-compiler like gcc.
**go-cloud-sqlite-vfs is cgo package**. If you want to build your app using go-cloud-sqlite-vfs, you need a C-compiler like gcc.

# Usage
## Usage

```go
package main
Expand Down Expand Up @@ -64,9 +64,9 @@ func main() {
}
```

# Example project
## Example project

1. Build `blockcachevfsd` cli. For instuctions see the [CBS website](https://sqlite.org/cloudsqlite/doc/trunk/www/index.wiki)
1. Build `blockcachevfsd` cli. For instructions see the [CBS website](https://sqlite.org/cloudsqlite/doc/trunk/www/index.wiki)
2. Start Azurite
```bash
docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0
Expand All @@ -89,7 +89,7 @@ func main() {
go run ./
```
# Dev
## Dev
Because the C code of the [CBS project](https://sqlite.org/cloudsqlite/dir?ci=tip) needs to be included in
the package it can be updated with the `download-c-code.sh` script located in the root of this project.
60 changes: 34 additions & 26 deletions cloud_sqlite_vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"unsafe"
)

var KEY = ""

// VFS represent a SQLite virtual file systems backed by (Azure/Google) cloud object storage.
type VFS struct {
bcvfs *C.sqlite3_bcvfs
cacheDir string
Expand All @@ -26,8 +29,6 @@ type VFS struct {
Daemonless bool
}

var KEY = ""

//export csAuthCb
func csAuthCb(pCtx *C.void, zStorage *C.char, zAccount *C.char, zContainer *C.char, pzAuthToken **C.char) C.int {
cKey := C.CString(KEY)
Expand All @@ -43,16 +44,18 @@ func removeCacheDir(cacheDir string) error {

func createCacheDir(cacheDir string) error {
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
err := os.Mkdir(cacheDir, 0750)
err = os.Mkdir(cacheDir, 0750)
if err != nil {
return err
}
}

return nil
}

func NewVFS(vfsName string, storage string, account string, key string, containerName string, cacheDir string) (VFS, error) {
// NewVFS build a new VFS. Note: when provided cacheSize is 0 the default CBS cache size of 1Gb will be used
func NewVFS(vfsName string, storage string, account string, key string,
containerName string, cacheDir string, cacheSize int64, curlVerbose bool) (VFS, error) {

KEY = key
vfs := &VFS{}

Expand All @@ -72,39 +75,44 @@ func NewVFS(vfsName string, storage string, account string, key string, containe
defer C.free(unsafe.Pointer(cCacheDir))
defer C.free(unsafe.Pointer(cVFSName))

if rc == C.SQLITE_OK {
if C.sqlite3_bcvfs_isdaemon(pVfs) == 1 {
vfs.Daemonless = false
} else {
vfs.Daemonless = true
}
} else {
if rc != C.SQLITE_OK {
_ = removeCacheDir(cacheDir)
return *vfs, fmt.Errorf("unable to create virtual filesystem with error: %s", C.GoString(zErr))
}

if rc == C.SQLITE_OK {
C.sqlite3_bcvfs_auth_callback(pVfs, nil, (*[0]byte)(unsafe.Pointer(C.csAuthCb)))
if curlVerbose {
C.sqlite3_bcvfs_config(pVfs, C.SQLITE_BCV_CURLVERBOSE, 1)
}
if C.sqlite3_bcvfs_isdaemon(pVfs) == 1 {
vfs.Daemonless = false
} else {
vfs.Daemonless = true

cStorage := C.CString(storage)
cAccount := C.CString(account)
cContainerName := C.CString(containerName)
if cacheSize > 0 {
// cache only works in daemonless mode
C.sqlite3_bcvfs_config(pVfs, C.SQLITE_BCV_CACHESIZE, C.longlong(cacheSize))
}
}

rc = C.sqlite3_bcvfs_attach(pVfs, cStorage, cAccount, cContainerName, nil, C.SQLITE_BCV_ATTACH_IFNOT, &zErr)
C.sqlite3_bcvfs_auth_callback(pVfs, nil, (*[0]byte)(unsafe.Pointer(C.csAuthCb)))

defer C.free(unsafe.Pointer(cStorage))
defer C.free(unsafe.Pointer(cAccount))
defer C.free(unsafe.Pointer(cContainerName))
cStorage := C.CString(storage)
cAccount := C.CString(account)
cContainerName := C.CString(containerName)

if rc != C.SQLITE_OK {
_ = removeCacheDir(cacheDir)
return *vfs, fmt.Errorf("unable to attach virtual filesystem with error: %s", C.GoString(zErr))
}
rc = C.sqlite3_bcvfs_attach(pVfs, cStorage, cAccount, cContainerName, nil, C.SQLITE_BCV_ATTACH_IFNOT, &zErr)

defer C.free(unsafe.Pointer(cStorage))
defer C.free(unsafe.Pointer(cAccount))
defer C.free(unsafe.Pointer(cContainerName))

if rc != C.SQLITE_OK {
_ = removeCacheDir(cacheDir)
return *vfs, fmt.Errorf("unable to attach virtual filesystem with error: %s", C.GoString(zErr))
}

vfs.bcvfs = pVfs
vfs.cacheDir = cacheDir

return *vfs, nil
}

Expand Down
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/PDOK/go-cloud-sqlite-vfs"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
Expand All @@ -23,7 +24,7 @@ type Genre struct {
}

func main() {
vfs, err := cloud_sqlite_vfs.NewVFS(VFS_NAME, STORAGE, ACCOUNT, KEY, CONTAINER_NAME, CACHE_DIR)
vfs, err := cloud_sqlite_vfs.NewVFS(VFS_NAME, STORAGE, ACCOUNT, KEY, CONTAINER_NAME, CACHE_DIR, 0, false)
if err != nil {
fmt.Println(err)
return
Expand Down

0 comments on commit bec9427

Please sign in to comment.