-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_zstd_suite_test.go
273 lines (212 loc) · 6.84 KB
/
sqlite_zstd_suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package sqlitezstd_test
import (
"context"
"database/sql"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"github.com/georgysavva/scany/v2/sqlscan"
sqlitezstd "github.com/jtarchie/sqlitezstd"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
func TestSqliteZstd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "SqliteZstd Suite")
}
const maxSize = 100_000
func createDatabase() string {
buildPath, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())
dbPath := filepath.Join(buildPath, "test.sqlite")
client, err := sql.Open("sqlite3", dbPath)
Expect(err).ToNot(HaveOccurred())
_, err = client.Exec(`
CREATE TABLE entries (
id INTEGER PRIMARY KEY
);
`)
Expect(err).ToNot(HaveOccurred())
tx, err := client.Begin()
Expect(err).ToNot(HaveOccurred())
defer func() { _ = tx.Rollback() }()
stmt, err := tx.Prepare("INSERT INTO entries (id) VALUES (?)")
Expect(err).ToNot(HaveOccurred())
defer stmt.Close()
for id := 1; id <= maxSize; id++ {
_, err = stmt.Exec(id)
Expect(err).ToNot(HaveOccurred())
}
err = tx.Commit()
Expect(err).ToNot(HaveOccurred())
zstPath := dbPath + ".zst"
command := exec.Command(
"go", "run", "github.com/SaveTheRbtz/zstd-seekable-format-go/cmd/zstdseek",
"-f", dbPath,
"-o", zstPath,
)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
return zstPath
}
func createComplexDatabase() (string, string) {
buildPath, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())
dbPath := filepath.Join(buildPath, "complex.sqlite")
client, err := sql.Open("sqlite3", dbPath)
Expect(err).ToNot(HaveOccurred())
defer client.Close()
_, err = client.Exec(`
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
user_id INTEGER,
product TEXT,
quantity INTEGER,
FOREIGN KEY (user_id) REFERENCES users(id)
);
`)
Expect(err).ToNot(HaveOccurred())
tx, err := client.Begin()
Expect(err).ToNot(HaveOccurred())
defer func() { _ = tx.Rollback() }()
userStmt, err := tx.Prepare("INSERT INTO users (name, age) VALUES (?, ?)")
Expect(err).ToNot(HaveOccurred())
defer userStmt.Close()
orderStmt, err := tx.Prepare("INSERT INTO orders (user_id, product, quantity) VALUES (?, ?, ?)")
Expect(err).ToNot(HaveOccurred())
defer orderStmt.Close()
for i := 1; i <= maxSize; i++ {
_, err = userStmt.Exec(fmt.Sprintf("User%d", i), 20+(i%60))
Expect(err).ToNot(HaveOccurred())
_, err = orderStmt.Exec(i, fmt.Sprintf("Product%d", i%100), i%10+1)
Expect(err).ToNot(HaveOccurred())
}
err = tx.Commit()
Expect(err).ToNot(HaveOccurred())
err = client.Close()
Expect(err).ToNot(HaveOccurred())
zstPath := dbPath + ".zst"
command := exec.Command(
"go", "run", "github.com/SaveTheRbtz/zstd-seekable-format-go/cmd/zstdseek",
"-f", dbPath,
"-o", zstPath,
"-t",
"-c", "16:32:64",
)
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
return dbPath, zstPath
}
var _ = Describe("SqliteZSTD", func() {
BeforeEach(func() {
err := sqlitezstd.Init()
Expect(err).ToNot(HaveOccurred())
})
It("can read from a compressed sqlite db", func() {
zstPath := createDatabase()
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
Expect(err).ToNot(HaveOccurred())
defer client.Close()
row := client.QueryRow("SELECT COUNT(*) FROM entries;")
Expect(row.Err()).ToNot(HaveOccurred())
var count int64
err = row.Scan(&count)
Expect(err).ToNot(HaveOccurred())
Expect(count).To(BeEquivalentTo(maxSize))
})
It("can handle multiple readers", func() {
zstPath := createDatabase()
waiter := &sync.WaitGroup{}
for range 5 {
waiter.Add(1)
go func() {
defer waiter.Done()
defer GinkgoRecover()
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
Expect(err).ToNot(HaveOccurred())
defer client.Close()
for range 1_000 {
row := client.QueryRow("SELECT * FROM entries ORDER BY RANDOM() LIMIT 1;")
Expect(row.Err()).ToNot(HaveOccurred())
}
}()
}
waiter.Wait()
})
When("file does not exist", func() {
It("returns an error", func() {
client, err := sql.Open("sqlite3", "file:some.db?vfs=zstd")
Expect(err).ToNot(HaveOccurred())
defer client.Close()
row := client.QueryRow("SELECT * FROM entries ORDER BY RANDOM() LIMIT 1;")
Expect(row.Err()).To(HaveOccurred())
})
})
It("allows reading from HTTP server", func() {
zstPath := createDatabase()
zstDir := filepath.Dir(zstPath)
server := httptest.NewServer(http.FileServer(http.Dir(zstDir)))
defer server.Close()
client, err := sql.Open("sqlite3", fmt.Sprintf("%s/%s?vfs=zstd", server.URL, filepath.Base(zstPath)))
Expect(err).ToNot(HaveOccurred())
defer client.Close()
row := client.QueryRow("SELECT COUNT(*) FROM entries;")
Expect(row.Err()).ToNot(HaveOccurred())
var count int64
err = row.Scan(&count)
Expect(err).ToNot(HaveOccurred())
Expect(count).To(BeEquivalentTo(maxSize))
})
It("ensures data integrity between compressed and uncompressed databases", func() {
uncompressedPath, compressedPath := createComplexDatabase()
uncompressedDB, err := sql.Open("sqlite3", uncompressedPath)
Expect(err).ToNot(HaveOccurred())
defer uncompressedDB.Close()
compressedDB, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", compressedPath))
Expect(err).ToNot(HaveOccurred())
defer compressedDB.Close()
row := compressedDB.QueryRow(`SELECT COUNT(*) FROM users;`)
Expect(row.Err()).ToNot(HaveOccurred())
var count int64
Expect(row.Scan(&count)).ToNot(HaveOccurred())
Expect(count).To(BeEquivalentTo(maxSize))
query := `
-- since VFS is read-only, it can not be used for files
-- please use this
PRAGMA temp_store = memory;
SELECT u.age, COUNT(*) as order_count, SUM(o.quantity) as total_quantity
FROM users u
JOIN orders o ON u.id = o.user_id
GROUP BY u.age
ORDER BY u.age
`
type Result struct {
Age int
OrderCount int64
TotalQuantity int64
}
var uncompressedResults, compressedResults []Result
err = sqlscan.Select(context.Background(), uncompressedDB, &uncompressedResults, query)
Expect(err).ToNot(HaveOccurred())
err = sqlscan.Select(context.Background(), compressedDB, &compressedResults, query)
Expect(err).ToNot(HaveOccurred())
Expect(len(compressedResults)).To(BeNumerically(">", 0))
Expect(len(compressedResults)).To(Equal(len(uncompressedResults)), "Compressed and uncompressed databases have different number of rows")
for i := range uncompressedResults {
Expect(compressedResults[i]).To(Equal(uncompressedResults[i]), "Row %d does not match between compressed and uncompressed databases", i)
}
})
})