-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistenceDB.go
680 lines (574 loc) · 18.2 KB
/
persistenceDB.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
Copyright © 2024 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
*/
package nele
// #cgo CFLAGS: -I/usr/include/
// #cgo LDFLAGS: -lsqlite3 -lm
import "C"
import (
"context"
"database/sql"
"errors"
"fmt"
"math"
"path/filepath"
"sync"
"time"
"unsafe"
_ "github.com/mattn/go-sqlite3"
se "github.com/mwat56/sourceerror"
)
//lint:file-ignore ST1017 - I prefer Yoda conditions
/* Defined in `persistence.go`:
type (
TPosting struct {
id uint64 // integer representation of date/time
lastModified time.Time // file modification time
markdown []byte // article contents in Markdown markup
}
TPostList []TPosting
TWalkFunc func(aID uint64) error
IPersistence interface {
Create(aPost *TPosting) (int, error)
Read(aID uint64) (*TPosting, error)
Update(aPost *TPosting) (int, error)
Delete(aID uint64) error
Count() int
Exists(aID uint64) bool
PathFileName(aID uint64) string
Rename(aOldID, aNewID uint64) error
Search(aText string, aOffset, aLimit uint) (*TPostList, error)
Walk(aWalkFunc TWalkFunc) error
}
)
*/
const (
maxSqliteInt int64 = math.MaxInt64
u2iOffset uint64 = 1 << 63
)
type (
// `TDBpersistence` is a database-based `IPersistence` implementation.
TDBpersistence struct {
_ struct{}
db *sql.DB // the database to use
mtx *sync.RWMutex // pointer to avoid copying warnings
fts5 bool // whether SQLite supports full-text search
}
)
// --------------------------------------------------------------------------
// constructor function
// `NewDBpersistence()` creates a new instance of `TDBpersistence`.
//
// In case of errors initialising the database connection, the function
// returns a `nil` value.
//
// Parameters:
// - `aName`: The name of the database file to use.
//
// Returns:
// - `*TDBpersistence`: A persistence instance instance.
func NewDBpersistence(aName string) *TDBpersistence {
fName := filepath.Join(poPostingBaseDirectory, aName)
dbInstance, hasFTS, err := initDatabase(fName)
if nil != err {
return nil
}
return &TDBpersistence{
db: dbInstance,
mtx: new(sync.RWMutex),
fts5: hasFTS,
}
} // NewDBpersistence()
// --------------------------------------------------------------------------
// private helper functions:
// `dbInt2id()` converts a 64-bit integer to a uint64, handling
// potential overflow.
//
// Parameters:
// - `aInt`: The 64-bit integer to convert.
//
// Returns:
// - `uint64`: The input integer, potentially with an offset applied.
func dbInt2id(aInt int64) uint64 {
if 0 > aInt {
offset := u2iOffset
return uint64(aInt + int64(offset))
}
return uint64(aInt)
} //dbInt2id ()
// `dbInt2time()` converts a signed 64-bit integer from SQLite to
// a Go `time.Time` value.
//
// Parameters:
// - `aInt`: A 64-bit integer ID to be converted.
//
// Returns:
// - `time.Time`: The UnixNano value of the provided time.Time.
func dbInt2time(aInt int64) time.Time {
return time.Unix(0, aInt)
} //dbInt2time ()
// `id2dbInt()` converts a 64-bit integer to a database-compatible integer.
//
// This function is used to convert the unsigned 64-bit integer IDs used
// in the application to the 64-bit integer IDs used in the SQLite database.
// If the provided `aID` is larger than the maximum SQLite integer
// (9223372036854775807), the function subtracts the offset `u2iOffset`
// (1 << 63) to ensure that the resulting integer is within the valid
// range for SQLite integers.
//
// Parameters:
// - `aID`: The unsigned 64-bit integer ID to be converted.
//
// Returns:
// - `int64`: The converted integer.
func id2dbInt(aID uint64) int64 {
if aID > uint64(maxSqliteInt) {
return int64(aID - u2iOffset)
}
return int64(aID)
} // id2dbInt()
// `time2dbInt()` converts a `time.Time` value to a signed 64-bit integer
// value suitable for SQLite's INTEGER field.
//
// Parameters:
// - `aTime`: A time.Time value to be converted to a 64-bit integer.
//
// Returns:
// - `int64`: The converted integer.
func time2dbInt(aTime time.Time) int64 {
return aTime.UnixNano()
} // time2dbInt()
// --------------------------------------------------------------------------
// `init()` ensures proper interface implementation.
func init() {
var (
_ IPersistence = TDBpersistence{}
_ IPersistence = (*TDBpersistence)(nil)
)
} // init()
// --------------------------------------------------------------------------
// The SQL statement to create the database table
const dbInitTable = `
CREATE TABLE IF NOT EXISTS "postings" (
"id" INTEGER PRIMARY KEY,
"lastModified" INTEGER NOT NULL,
"markdown" TEXT NOT NULL
);
`
// `initDatabase()` initialises a new SQLite database connection and
// checks whether it supports full-text search (FTS5).
//
// The function takes a path to the SQLite database file as a parameter
// and returns a pointer to a new SQLite database connection, a boolean
// indicating whether the database supports FTS5, and an error if any
// occurs during the initialisation process.
// The database connection is opened using the provided path and the
// "sqlite3" driver. If the database creation SQL statement fails to
// execute, the function returns `nil`, `false`, and an `error`.
// After the database connection is established, the function checks
// whether the SQLite database supports FTS5. If it does, the function
// creates an FTS5 virtual table referencing the regular table and adds
// triggers to keep the FTS table in sync with the regular table.
// The function returns the SQLite database connection, a boolean
// indicating whether the database supports FTS5, and nil if no errors occur.
//
// Parameters:
// - `aPathFile`: The path to the SQLite database file.
// - `*sql.DB`: A pointer to a new SQLite database connection.
//
// Returns:
// - `bool`: An indicator for whether the database supports FTS5.
// - `error`: An error if any occurs during the initialisation process.
func initDatabase(aPathFile string) (*sql.DB, bool, error) {
// `cache=shared` is essential to avoid running out of file
// handles since each query seems to hold its own file handle.
// `loc=auto` gets `time.Time` with current locale.
dsn := `file:` + aPathFile + `?cache=shared&loc=auto`
db, err := sql.Open("sqlite3", dsn)
if err != nil {
// failed to open database
return nil, false, se.Wrap(err, 3)
}
// Create the table
if _, err = db.Exec(dbInitTable); err != nil {
db.Close()
return nil, false, se.Wrap(err, 2)
}
// Check and add FTS5 database
hasFTS, err := initFTS5(db)
if err != nil {
// db.Close()
return db, false, nil // err
}
return db, hasFTS, nil
} // initDatabase()
// For the full-text search to work, we need to use the following build tag:
//
// go build -tags "sqlite_fts5"
// `initFTS5()` checks the SQLite database for the FTS5 full-text
// search engine.
//
// It returns a boolean value indicating whether the SQLite database
// supports FTS5, and a possible error.
//
// Parameters:
// - `aDB`: The SQLite database connection.
//
// Returns:
// - `bool`: `true` if the SQLite database supports FTS5, `false` otherwise.
// - `error`: A possible error, or `nil` on success.
func initFTS5(aDB *sql.DB) (bool, error) {
const check4FTS5 = `SELECT sqlite_compileoption_used('ENABLE_FTS5')`
var fts string
if err := aDB.QueryRow(check4FTS5).Scan(&fts); nil != err {
return false, se.Wrap(err, 1)
}
if "1" != fts {
return false, nil
}
const dbAddFTS5 = `
-- FTS virtual table referencing the regular table
CREATE VIRTUAL TABLE IF NOT EXISTS postings_FTS USING FTS5(
markdown,
content='postings',
content_rowid='id'
);
-- Trigger to keep FTS table in sync
CREATE TRIGGER postings_ai AFTER INSERT ON postings BEGIN
INSERT INTO postings_FTS(rowid, markdown) VALUES (new.id, new.markdown);
END;
CREATE TRIGGER postings_ad AFTER DELETE ON postings BEGIN
INSERT INTO postings_FTS(postings_FTS, rowid, markdown) VALUES('delete', old.id, old.markdown);
END;
CREATE TRIGGER postings_au AFTER UPDATE ON postings BEGIN
INSERT INTO postings_FTS(postings_FTS, rowid, markdown) VALUES('delete', old.id, old.markdown);
INSERT INTO postings_FTS(rowid, markdown) VALUES (new.id, new.markdown);
END;
`
if _, err := aDB.Exec(dbAddFTS5); nil != err {
return false, se.Wrap(err, 1)
}
return true, nil
} // initFTS5()
// --------------------------------------------------------------------------
// TDBpersistence methods
const dbGetCount = `SELECT COUNT(*) FROM postings`
// `Count()` returns the number of postings currently available.
//
// NOTE: This method is very resource intensive as it has to count all the
// posts stored in the filesystem.
//
// Returns:
// - `int`: The number of available postings, or `0` in case of errors.
func (dbp TDBpersistence) Count() int {
var result int
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<1)
defer cancel()
if err := dbp.db.QueryRowContext(ctx, dbGetCount).Scan(&result); err != nil {
return 0 //, fmt.Errorf("error counting rows: %v", err)
}
return result
} // Count()
const dbCreateRow = `INSERT INTO postings(id, lastModifies, markdown) VALUES(?, ?, ?)`
// `Create()` creates a new posting in the filesystem.
//
// If the provided `aPost` is `nil`, an `ErrEmptyPosting` error
// is returned.
//
// Parameters:
// - `aPost`: The `TPosting` instance containing the article's data.
//
// Returns:
// - `int`: The number of bytes stored.
// - 'error`:` A possible error, or `nil` on success.
func (dbp TDBpersistence) Create(aPost *TPosting) (int, error) {
if nil == aPost {
return 0, se.Wrap(ErrEmptyPosting, 1)
}
dbp.mtx.Lock()
defer dbp.mtx.Unlock()
dbID := id2dbInt(aPost.id)
dbLM := time2dbInt(aPost.lastModified)
dbText := string(aPost.markdown)
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<2)
defer cancel()
result, err := dbp.db.ExecContext(ctx, dbCreateRow, dbID, dbLM, dbText)
if err != nil {
return 0, se.Wrap(err, 3)
}
if _, err = result.LastInsertId(); err != nil {
return 0, se.Wrap(err, 1)
}
return int(unsafe.Sizeof(aPost.id)) +
int(unsafe.Sizeof(aPost.lastModified)) +
aPost.Len(), nil
} // Create()
const dbDeleteRow = `DELETE FROM postings WHERE id = ?`
// `Delete()` removes the posting/article from the filesystem
// and returns a possible I/O error.
//
// Parameters:
// - `aID`: The unique identifier of the posting to delete.
//
// Returns:
// - 'error`: A possible I/O error, or `nil` on success.
//
// Side Effects:
// - Invalidates the internal count cache.
func (dbp TDBpersistence) Delete(aID uint64) error {
dbp.mtx.Lock()
defer dbp.mtx.Unlock()
dbID := id2dbInt(aID)
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<2)
defer cancel()
res, err := dbp.db.ExecContext(ctx, dbDeleteRow, dbID)
if err != nil {
return se.Wrap(err, 2)
}
// Check the number of rows affected
rowsAffected, err := res.RowsAffected()
if err != nil {
return se.Wrap(err, 2)
}
if 0 == rowsAffected {
return fmt.Errorf("no rows deleted")
}
return nil
} // Delete()
const dbExistRow = `SELECT EXISTS(SELECT 1 FROM postings WHERE id = ?)`
// `Exists()` checks if a file with the given ID exists in the filesystem.
//
// It returns a boolean value indicating whether the file exists.
//
// Parameters:
// - `aID`: The unique identifier of the posting to check.
//
// Returns:
// - `bool`: `true` if the file exists, `false` otherwise.
func (dbp TDBpersistence) Exists(aID uint64) bool {
dbp.mtx.RLock()
defer dbp.mtx.RUnlock()
var result bool
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<1)
defer cancel()
if err := dbp.db.QueryRowContext(ctx, dbExistRow, aID).Scan(&result); err != nil {
return false
}
return result
} // Exists()
// `PathFileName()` returns the posting's complete path-/filename.
//
// The returned path-/filename is in the format:
//
// <base_directory>/<posting_id>.md
//
// Parameters:
// - `aID`: The unique identifier of the posting to handle.
//
// Returns:
// - `*string`: The path-/filename associated with `aID`.
func (dbp TDBpersistence) PathFileName(aID uint64) string {
return poPostingBaseDirectory
} // PathFileName()
const dbReadRow = `SELECT id, lastModified, markdown FROM postings WHERE id = ?`
// `Read()` reads the posting from disk, returning a possible I/O error.
//
// Parameters:
// - `aID`: The unique identifier of the posting to be read.
//
// Returns:
// - `*TPosting`: The `TPosting` instance containing the article's data, or `nil` if the record doesn't exist.
// - 'error`: A possible I/O error, or `nil` on success.
func (dbp TDBpersistence) Read(aID uint64) (*TPosting, error) {
dbp.mtx.RLock()
defer dbp.mtx.RUnlock()
var (
dbID, dbLM int64
dbText string
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<1)
defer cancel()
err := dbp.db.QueryRowContext(ctx, dbReadRow, id2dbInt(aID)).
Scan(&dbID, &dbLM, &dbText)
if err != nil {
return nil, se.Wrap(err, 3)
}
post := &TPosting{
id: dbInt2id(dbID),
lastModified: dbInt2time(dbLM),
markdown: []byte(dbText),
}
return post, nil
} // Read()
const dbRenameRow = `UPDATE postings SET id = ? WHERE id = ?"`
// `Rename()` renames a posting from its old ID to a new ID.
//
// Parameters:
// - aOldID: The unique identifier of the posting to be renamed.
// - aNewID: The new unique identifier for the new posting.
//
// Returns:
// - `error`: An error if the operation fails, or `nil` on success.
func (dbp TDBpersistence) Rename(aOldID, aNewID uint64) error {
dbp.mtx.Lock()
defer dbp.mtx.Unlock()
dbOldID, dbNewID := id2dbInt(aOldID), id2dbInt(aNewID)
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<1)
defer cancel()
result, err := dbp.db.ExecContext(ctx, dbRenameRow, dbNewID, dbOldID)
if err != nil {
return se.Wrap(err, 2)
}
// Get the number of affected rows
if _, err = result.RowsAffected(); err != nil {
return se.Wrap(err, 1)
}
return nil
} // Rename()
const (
dbSearchLIKE = `SELECT id, lastModified, markup FROM postings WHERE markup LIKE ? LIMIT ? OFFSET ? ORDER BY id DESC`
dbSearchMATCH = `SELECT id, lastModified, markup FROM postings WHERE markup MATCH ? LIMIT ? OFFSET ? ORDER BY id DESC`
)
// `Search()` retrieves a list of postings based on a search term.
//
// The method uses SQLite's FTS5 (Full-Text Search) feature to perform
// the search. If the underlying database does not support FTS5, the
// method falls back to a LIKE-based search.
//
// A zero value of `aLimit` means: no limit alt all.
//
// The returned `TPostList` type is a slice of `TPosting` instances, where
// `TPosting` is a struct representing a single posting. If the returned
// slice is an empty list then no matching postings were found; if it is
// `nil` it means there was an error retrieving the matches.
//
// Parameters:
// - `aText`: The search query string.
// - `aOffset`: An offset in the database result set of the search results.
// - `aLimit`: The maximum number of search results to return.
//
// Returns:
// - `*TPostList`: The list of search results, or `nil` in case of errors.
// - `error`: If the search operation fails, or `nil` on success.
func (dbp TDBpersistence) Search(aText string, aOffset, aLimit uint) (*TPostList, error) {
dbp.mtx.RLock()
defer dbp.mtx.RUnlock()
if 0 == aLimit {
aLimit = 1 << 15 // 64K
}
var (
err error
rows *sql.Rows
search string
)
if dbp.fts5 {
search = dbSearchMATCH
} else {
aText = fmt.Sprintf("%%%s%%", aText)
search = dbSearchLIKE
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<2)
defer cancel()
if rows, err = dbp.db.QueryContext(ctx, search, aText, aLimit, aOffset); err != nil {
return nil, se.Wrap(err, 1)
}
defer rows.Close()
postlist := NewPostList()
for rows.Next() {
var (
dbID, dbLM int64
dbText string
)
if err = rows.Scan(&dbID, &dbLM, &dbText); err != nil {
return nil, se.Wrap(err, 1)
}
post := &TPosting{
id: dbInt2id(dbID),
lastModified: dbInt2time(dbLM),
markdown: []byte(dbText),
}
postlist.insert(post)
}
if err = rows.Err(); err != nil {
return nil, se.Wrap(err, 1)
}
return postlist, nil
} // Search()
const dbUpdateRow = `UPDATE postings SET lastModified = ?, markdown = ? WHERE id = ?"`
// `Update()` updates the article's Markdown in the database.
//
// It returns the number of bytes stored and a possible I/O error.
//
// If the provided `aPost` is `nil`, an `ErrEmptyPosting` error
// is returned.
//
// Parameters:
// - `aPost`: A `TPosting` instance containing the article's data.
//
// Returns:
// - `int`: The number of bytes written to the file.
// - 'error`:` A possible I/O error, or `nil` on success.
//
// Side Effects:
// - Invalidates the internal count cache.
func (dbp TDBpersistence) Update(aPost *TPosting) (int, error) {
dbp.mtx.Lock()
defer dbp.mtx.Unlock()
dbID := id2dbInt(aPost.id)
dbLM := time2dbInt(aPost.lastModified)
dbText := string(aPost.markdown)
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<2)
defer cancel()
result, err := dbp.db.ExecContext(ctx, dbUpdateRow, dbLM, dbText, dbID)
if err != nil {
return 0, se.Wrap(err, 2)
}
// Get the number of affected rows
if _, err = result.RowsAffected(); err != nil {
return 0, se.Wrap(err, 1)
}
return int(unsafe.Sizeof(aPost.id)) +
int(unsafe.Sizeof(aPost.lastModified)) +
aPost.Len(), nil
} // Update()
const dbWalkRows = `SELECT id FROM postings ORDER BY id DESC;`
// `Walk()` visits all existing postings, calling `aWalkFunc`
// for each posting.
//
// Parameters:
// - `aWalkFunc`: The function to call for each posting.
//
// Returns:
// - `error`: a possible error occurring the traversal process.
func (dbp TDBpersistence) Walk(aWalkFunc TWalkFunc) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second<<3)
defer cancel()
rows, err := dbp.db.QueryContext(ctx, dbWalkRows)
if err != nil {
return se.Wrap(err, 2)
}
defer rows.Close()
dirLoop: // Iterate over rows
for rows.Next() {
// Call the callback function with the row data
var dbID int64
if err := rows.Scan(&dbID); err != nil {
continue
}
id := dbInt2id(dbID)
if err := aWalkFunc(id); nil != err {
if errors.Is(err, ErrSkipAll) {
break dirLoop
}
return se.Wrap(err, 4)
}
}
// Check for any errors encountered during iteration
if err := rows.Err(); err != nil {
return se.Wrap(err, 1)
}
return nil
} // Walk()
/* _EoF_ */