Skip to content

Commit 0752635

Browse files
committed
Updated indexer
1 parent cabce89 commit 0752635

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

pkg/indexer/indexer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ func (i *Indexer) event(ctx context.Context, evt notify.EventInfo) error {
184184
case notify.Create, notify.Write:
185185
info, err := os.Stat(evt.Path())
186186
if err == nil && info.Mode().IsRegular() && i.ShouldVisit(relpath, info) {
187-
i.queue.Add(i.name, relpath)
187+
i.queue.Add(i.name, relpath, info)
188188
}
189189
case notify.Remove, notify.Rename:
190190
info, err := os.Stat(evt.Path())
191191
if err == nil && info.Mode().IsRegular() && i.ShouldVisit(relpath, info) {
192-
i.queue.Add(i.name, relpath)
192+
i.queue.Add(i.name, relpath, info)
193193
} else {
194194
// Always attempt removal from index
195195
i.queue.Remove(i.name, relpath)
@@ -202,7 +202,7 @@ func (i *Indexer) event(ctx context.Context, evt notify.EventInfo) error {
202202
// visit is used to index a file from the indexer
203203
func (i *Indexer) visit(ctx context.Context, abspath, relpath string, info fs.FileInfo) error {
204204
if info.Mode().IsRegular() {
205-
i.queue.Add(i.name, relpath)
205+
i.queue.Add(i.name, relpath, info)
206206
}
207207
return nil
208208
}

pkg/indexer/queue.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package indexer
22

33
import (
44
"fmt"
5+
"io/fs"
56
"path/filepath"
67
"sync"
78
// Package imports
@@ -22,6 +23,7 @@ type QueueEvent struct {
2223
Event
2324
Name string
2425
Path string
26+
Info fs.FileInfo
2527
}
2628

2729
type Event uint
@@ -80,14 +82,14 @@ func (this *Queue) String() string {
8082

8183
// Add an item to the queue. If the item is already in the queue,
8284
// then it is bumped to the end of the queue
83-
func (q *Queue) Add(name, path string) {
85+
func (q *Queue) Add(name, path string, info fs.FileInfo) {
8486
if elem := q.Get(name, path); elem != nil {
8587
// Remove the element from the existing queue
8688
q.del(name, path)
8789
}
8890

8991
// Add the element to the queue
90-
q.add(EventAdd, name, path)
92+
q.add(EventAdd, name, path, info)
9193
}
9294

9395
// Remove an item to the queue. If the item is already in the queue,
@@ -99,7 +101,7 @@ func (q *Queue) Remove(name, path string) {
99101
}
100102

101103
// Add the element to the queue
102-
q.add(EventRemove, name, path)
104+
q.add(EventRemove, name, path, nil)
103105
}
104106

105107
// Return a queue event from the queue, or nil
@@ -141,7 +143,7 @@ func (q *Queue) Count() int {
141143
///////////////////////////////////////////////////////////////////////////////
142144
// PRIVATE METHODS
143145

144-
func (q *Queue) add(e Event, name, path string) {
146+
func (q *Queue) add(e Event, name, path string, info fs.FileInfo) {
145147
q.RWMutex.Lock()
146148
defer q.RWMutex.Unlock()
147149
// This assumes the key does not exist
@@ -150,7 +152,7 @@ func (q *Queue) add(e Event, name, path string) {
150152
panic("Queue: key already exists")
151153
}
152154
q.q = append(q.q, key)
153-
q.k[key] = &QueueEvent{e, name, path}
155+
q.k[key] = &QueueEvent{e, name, path, info}
154156
}
155157

156158
func (q *Queue) del(name, path string) {

pkg/indexer/schema.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package indexer
33
import (
44
"context"
55
"errors"
6+
"path/filepath"
67

78
// Namespace imports
89
. "github.com/mutablelogic/go-sqlite"
@@ -31,6 +32,12 @@ func CreateSchema(ctx context.Context, pool SQPool, schema string) error {
3132
if _, err := txn.Query(N(filesTableName).WithSchema(schema).CreateTable(
3233
C("name").WithPrimary(),
3334
C("path").WithPrimary(),
35+
C("parent"),
36+
C("filename").NotNull(),
37+
C("isdir").WithType("INTEGER").NotNull(),
38+
C("ext"),
39+
C("modtime"),
40+
C("size").WithType("INTEGER"),
3441
).IfNotExists()); err != nil {
3542
return err
3643
}
@@ -39,8 +46,17 @@ func CreateSchema(ctx context.Context, pool SQPool, schema string) error {
3946
}
4047

4148
func Replace(schema string, evt *QueueEvent) (SQStatement, []interface{}) {
42-
return N(filesTableName).WithSchema(schema).Replace("name", "path"),
43-
[]interface{}{evt.Name, evt.Path}
49+
return N(filesTableName).WithSchema(schema).Replace("name", "path", "parent", "filename", "isdir", "ext", "modtime", "size"),
50+
[]interface{}{
51+
evt.Name,
52+
evt.Path,
53+
filepath.Dir(evt.Path),
54+
evt.Info.Name(),
55+
boolToInt64(evt.Info.IsDir()),
56+
filepath.Ext(evt.Info.Name()),
57+
evt.Info.ModTime(),
58+
evt.Info.Size(),
59+
}
4460
}
4561

4662
func Delete(schema string, evt *QueueEvent) (SQStatement, []interface{}) {

pkg/indexer/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ func stringSliceContains(slice []string, s string) bool {
88
}
99
return false
1010
}
11+
12+
func boolToInt64(v bool) int64 {
13+
if v {
14+
return 1
15+
}
16+
return 0
17+
}

0 commit comments

Comments
 (0)