-
Notifications
You must be signed in to change notification settings - Fork 43
/
sqlite.go
142 lines (114 loc) · 3.28 KB
/
sqlite.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
package sqlite
import (
"context"
"os/user"
"path/filepath"
"strconv"
"strings"
"gorm.io/gorm/logger"
"github.com/FleekHQ/space-daemon/core/search"
"github.com/pkg/errors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
const DbFileName = "filesIndex.db"
type sqliteSearchOption struct {
dbPath string
logLevel logger.LogLevel
}
type Option func(o *sqliteSearchOption)
// sqliteFilesSearchEngine is a files search engine that is backed by sqlite
type sqliteFilesSearchEngine struct {
db *gorm.DB
opts sqliteSearchOption
}
// Creates a new SQLite backed search engine for files and folders
func NewSearchEngine(opts ...Option) *sqliteFilesSearchEngine {
usr, _ := user.Current()
searchOptions := sqliteSearchOption{
dbPath: filepath.Join(usr.HomeDir, ".fleek-space"),
}
for _, opt := range opts {
opt(&searchOptions)
}
return &sqliteFilesSearchEngine{
db: nil,
opts: searchOptions,
}
}
func (s *sqliteFilesSearchEngine) Start() error {
dsn := filepath.Join(s.opts.dbPath, DbFileName)
if db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(s.opts.logLevel),
}); err != nil {
return errors.Wrap(err, "failed to open database")
} else {
s.db = db
}
return s.db.AutoMigrate(&SearchIndexRecord{})
}
func (s *sqliteFilesSearchEngine) InsertFileData(ctx context.Context, data *search.InsertIndexRecord) (*search.IndexRecord, error) {
record := SearchIndexRecord{
ItemName: data.ItemName,
ItemExtension: data.ItemExtension,
ItemPath: data.ItemPath,
ItemType: data.ItemPath,
BucketSlug: data.BucketSlug,
DbId: data.DbId,
}
result := s.db.Create(&record)
if result.Error != nil {
if strings.Contains(result.Error.Error(), "UNIQUE constraint failed") {
return nil, errors.New("a similar file has already been inserted")
}
return nil, result.Error
}
return modelToIndexRecord(&record), nil
}
func (s *sqliteFilesSearchEngine) DeleteFileData(ctx context.Context, data *search.DeleteIndexRecord) error {
stmt := s.db.Where(
"item_name = ? AND item_path = ? AND bucket_slug = ?",
data.ItemName,
data.ItemPath,
data.BucketSlug,
)
if data.DbId != "" {
stmt = stmt.Where("dbId = ?", data.DbId)
}
result := stmt.Delete(&SearchIndexRecord{})
return result.Error
}
func (s *sqliteFilesSearchEngine) QueryFileData(ctx context.Context, query string, limit int) ([]*search.IndexRecord, error) {
var records []*SearchIndexRecord
result := s.db.Where(
"LOWER(item_name) LIKE ? OR LOWER(item_extension) = ?",
"%"+strings.ToLower(query)+"%",
strings.ToLower(query),
).Limit(limit).Find(&records)
if result.Error != nil {
return nil, result.Error
}
searchResults := make([]*search.IndexRecord, len(records))
for i, record := range records {
searchResults[i] = modelToIndexRecord(record)
}
return searchResults, nil
}
func (s *sqliteFilesSearchEngine) Shutdown() error {
db, err := s.db.DB()
if err != nil {
return err
}
return db.Close()
}
func modelToIndexRecord(model *SearchIndexRecord) *search.IndexRecord {
return &search.IndexRecord{
Id: strconv.Itoa(int(model.ID)),
ItemName: model.ItemName,
ItemExtension: model.ItemExtension,
ItemPath: model.ItemPath,
ItemType: model.ItemType,
BucketSlug: model.BucketSlug,
DbId: model.DbId,
}
}