Skip to content

Commit 555fcfb

Browse files
committed
Updated indexer
1 parent 2a1a554 commit 555fcfb

File tree

6 files changed

+50
-18
lines changed

6 files changed

+50
-18
lines changed

etc/server.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ sqlite3:
4040

4141
# Set trace to true to enable the ability to profile queries. Profiling information
4242
# can be displayed through the API.
43-
trace: true
43+
trace: false
4444

4545
# Set max number of connections that can be simultaneously opened
4646
max: 100

pkg/indexer/queue.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ type Queue struct {
1717
}
1818

1919
type QueueEvent struct {
20-
Event
20+
EventType
2121
Name string
2222
Path string
2323
Info fs.FileInfo
2424
}
2525

26-
type Event uint
26+
type EventType uint
2727

2828
///////////////////////////////////////////////////////////////////////////////
2929
// GLOBALS
@@ -33,7 +33,7 @@ const (
3333
)
3434

3535
const (
36-
EventNone Event = iota
36+
EventNone EventType = iota
3737
EventAdd
3838
EventRemove
3939
EventReindexStarted
@@ -151,7 +151,7 @@ func (q *Queue) Count() int {
151151
///////////////////////////////////////////////////////////////////////////////
152152
// PRIVATE METHODS
153153

154-
func (q *Queue) add(e Event, name, path string, info fs.FileInfo) {
154+
func (q *Queue) add(e EventType, name, path string, info fs.FileInfo) {
155155
q.RWMutex.Lock()
156156
defer q.RWMutex.Unlock()
157157
// This assumes the key does not exist

pkg/indexer/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ func CreateSchema(ctx context.Context, conn SQConnection, schema string, tokeniz
9393
return err
9494
}
9595
if _, err := txn.Query(N(searchTriggerDeleteName).WithSchema(schema).CreateTrigger(filesTableName,
96-
Q("INSERT INTO ", searchTableName, " (", searchTableName, ",rowid, name, parent, filename) VALUES ('delete', old.rowid, old.name, old.parent, old.filename)"),
96+
Q("INSERT INTO ", searchTableName, " (", searchTableName, ", rowid, name, parent, filename) VALUES ('delete', old.rowid, old.name, old.parent, old.filename)"),
9797
).After().Delete().IfNotExists()); err != nil {
9898
return err
9999
}
100100
if _, err := txn.Query(N(searchTriggerUpdateName).WithSchema(schema).CreateTrigger(filesTableName,
101-
Q("INSERT INTO ", searchTableName, " (", searchTableName, ",rowid, name, parent, filename) VALUES ('delete', old.rowid, old.name, old.parent, old.filename)"),
101+
Q("INSERT INTO ", searchTableName, " (", searchTableName, ", rowid, name, parent, filename) VALUES ('delete', old.rowid, old.name, old.parent, old.filename)"),
102102
Q("INSERT INTO ", searchTableName, " (rowid, name, parent, filename) VALUES (new.rowid, new.name, new.parent, new.filename)"),
103103
).After().Update().IfNotExists()); err != nil {
104104
return err

pkg/indexer/store.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ import (
1212

1313
// Import namepaces
1414
. "github.com/djthorpe/go-errors"
15+
. "github.com/mutablelogic/go-server"
1516
. "github.com/mutablelogic/go-sqlite"
1617
)
1718

1819
///////////////////////////////////////////////////////////////////////////////
1920
// TYPES
2021

2122
type Store struct {
22-
pool SQPool
23-
queue *Queue
24-
workers uint
25-
schema string
23+
pool SQPool
24+
queue *Queue
25+
renderer Renderer
26+
workers uint
27+
schema string
2628
}
2729

2830
type operation struct {
@@ -42,12 +44,13 @@ var (
4244
// LIFECYCLE
4345

4446
// Create a new store object
45-
func NewStore(pool SQPool, schema string, queue *Queue, workers uint) *Store {
47+
func NewStore(pool SQPool, schema string, queue *Queue, r Renderer, workers uint) *Store {
4648
s := new(Store)
4749
s.pool = pool
4850
s.queue = queue
51+
s.renderer = r
4952

50-
// Create workers - use cpus
53+
// Create workers - use double number of cores by default
5154
if workers == 0 {
5255
s.workers = defaultWorkers
5356
} else {
@@ -59,6 +62,11 @@ func NewStore(pool SQPool, schema string, queue *Queue, workers uint) *Store {
5962
pool.SetMax(int(s.workers))
6063
}
6164

65+
// Reduce number of workers if the pool max does not match
66+
if pool.Max() > 0 {
67+
s.workers = uintMin(s.workers, uint(pool.Max()))
68+
}
69+
6270
// Get a database connection
6371
conn := pool.Get()
6472
if conn == nil {
@@ -105,6 +113,18 @@ func (s *Store) Run(ctx context.Context, errs chan<- error) error {
105113
return result
106114
}
107115

116+
///////////////////////////////////////////////////////////////////////////////
117+
// STRINGIFY
118+
119+
func (s *Store) String() string {
120+
str := "<store"
121+
str += fmt.Sprint(" workers=", s.workers)
122+
if s.schema != "" {
123+
str += fmt.Sprintf(" schema=%q", s.schema)
124+
}
125+
return str + ">"
126+
}
127+
108128
///////////////////////////////////////////////////////////////////////////////
109129
// PUBLIC METHODS
110130

@@ -174,7 +194,7 @@ func (s *Store) worker(ctx context.Context, id uint, errs chan<- error) error {
174194
}
175195

176196
func (s *Store) process(evt *QueueEvent) operation {
177-
switch evt.Event {
197+
switch evt.EventType {
178198
case EventAdd:
179199
if replace, args := Replace(s.schema, evt); replace != nil {
180200
return operation{replace, args}

pkg/indexer/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ func boolToInt64(v bool) int64 {
3333
}
3434
return 0
3535
}
36+
37+
func uintMin(a, b uint) uint {
38+
if a < b {
39+
return a
40+
}
41+
return b
42+
}

plugin/indexer/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ func New(ctx context.Context, provider Provider) Plugin {
8383
}
8484

8585
// Create a queue, indexers and stores
86+
// TODO: Add a renderer interface for the store
8687
q := indexer.NewQueueWithCapacity(defaultCapacity)
8788
if q == nil {
8889
provider.Print(ctx, "unable to create queue")
8990
return nil
90-
} else if store := indexer.NewStore(p.pool, schema, q, 0); store == nil {
91+
} else if store := indexer.NewStore(p.pool, schema, q, nil, 0); store == nil {
9192
provider.Print(ctx, "unable to create store")
9293
return nil
9394
} else {
@@ -102,8 +103,6 @@ func New(ctx context.Context, provider Provider) Plugin {
102103
}
103104
}
104105

105-
fmt.Println("got", p)
106-
107106
// Return success
108107
return p
109108
}
@@ -113,8 +112,14 @@ func New(ctx context.Context, provider Provider) Plugin {
113112

114113
func (p *plugin) String() string {
115114
str := "<indexer"
115+
if p.store != nil {
116+
str += fmt.Sprint(" store=", p.store)
117+
}
118+
for name, index := range p.index {
119+
str += fmt.Sprintf(" %q=%v", name, index)
120+
}
116121
if p.pool != nil {
117-
str += fmt.Sprint(" ", p.pool)
122+
str += fmt.Sprint(" pool=", p.pool)
118123
}
119124
return str + ">"
120125
}

0 commit comments

Comments
 (0)