Skip to content

Commit

Permalink
use ncruces recommendations for ensursing this a read only vfs
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Mar 16, 2024
1 parent 49ace73 commit 2f959ef
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 33 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,28 @@ if initErr != nil {
panic(fmt.Sprintf("Failed to initialize SQLiteZSTD: %s", initErr))
}

db, err := sql.Open("sqlite3", "<path-to-your-file>?vfs=zstd&mode=ro&immutable=true&synchronous=off")
db, err := sql.Open("sqlite3", "<path-to-your-file>?vfs=zstd")
if err != nil {
panic(fmt.Sprintf("Failed to open database: %s", err))
}
```

In this Go code example:

- The SQLiteZSTD library is initialized first with sqlitezstd.Init().
- The SQLiteZSTD library is initialized first with `sqlitezstd.Init()`.
- An SQL connection to a compressed SQLite database is established with
sql.Open().
`sql.Open()`.

The sql.Open() function takes as a parameter the path to the compressed SQLite
The `sql.Open()` function takes as a parameter the path to the compressed SQLite
database, appended with a query string. Key query string parameters include:

- `vfs=zstd`: Ensures the ZSTD VFS is used.
- `mode=ro`: Opens the database in read-only mode.
- `immutable=true`: Ensures the database is protected from any accidental write
operations.
- `synchronous=off`: Disables SQLite's disk synchronization for improved
performance on read-heavy operations.

## Performance

Here's a simple benchmark comparing performance between reading from an
uncompressed vs. a compressed SQLite database, involving the insertion of 10k
records and retrieval of the MAX value, without an index.
records and retrieval of the `MAX` value, without an index.

```
BenchmarkReadUncompressedSQLite-8 5301 214922 ns/op
Expand Down
2 changes: 1 addition & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func BenchmarkReadCompressedSQLite(b *testing.B) {
_, zstPath, cleanup := setupDB(b)
defer cleanup()

client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd&mode=ro&immutable=true&synchronous=off", zstPath))
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
Expand Down
22 changes: 3 additions & 19 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sqlitezstd

import (
"os"
"sync/atomic"

seekable "github.com/SaveTheRbtz/zstd-seekable-format-go"
"github.com/klauspost/compress/zstd"
Expand All @@ -14,16 +13,13 @@ type ZstdFile struct {
file *os.File
seekable seekable.Reader

count int64
size int64
size int64
}

var _ sqlite3vfs.File = &ZstdFile{}

func (z *ZstdFile) CheckReservedLock() (bool, error) {
count := atomic.LoadInt64(&z.count)

return count > 0, nil
return false, nil
}

func (z *ZstdFile) Close() error {
Expand All @@ -34,20 +30,14 @@ func (z *ZstdFile) Close() error {
}

func (z *ZstdFile) DeviceCharacteristics() sqlite3vfs.DeviceCharacteristic {
return 0
return sqlite3vfs.IocapImmutable
}

func (z *ZstdFile) FileSize() (int64, error) {
return z.size, nil
}

func (z *ZstdFile) Lock(elock sqlite3vfs.LockType) error {
if elock == sqlite3vfs.LockNone {
return nil
}

atomic.AddInt64(&z.count, 1)

return nil
}

Expand All @@ -68,12 +58,6 @@ func (z *ZstdFile) Truncate(size int64) error {
}

func (z *ZstdFile) Unlock(elock sqlite3vfs.LockType) error {
if elock == sqlite3vfs.LockNone {
return nil
}

atomic.AddInt64(&z.count, -1)

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions sqlite_zstd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var _ = Describe("SqliteZSTD", func() {
It("can read from a compressed sqlite db", func() {
zstPath := createDatabase()

client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd&mode=ro&immutable=true&synchronous=off", zstPath))
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
Expect(err).ToNot(HaveOccurred())
defer client.Close()

Expand All @@ -90,7 +90,7 @@ var _ = Describe("SqliteZSTD", func() {
defer waiter.Done()
defer GinkgoRecover()

client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd&mode=ro&immutable=true&synchronous=off", zstPath))
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
Expect(err).ToNot(HaveOccurred())
defer client.Close()

Expand Down
2 changes: 1 addition & 1 deletion vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (z *ZstdVFS) Open(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File,
file: file,
seekable: seekable,
size: size,
}, 0, nil
}, flags | sqlite3vfs.OpenReadOnly, nil
}

func Init() error {
Expand Down

0 comments on commit 2f959ef

Please sign in to comment.