|
| 1 | +package indexer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "strconv" |
| 11 | + "sync" |
| 12 | + |
| 13 | + // Package imports |
| 14 | + walkfs "github.com/mutablelogic/go-sqlite/pkg/walkfs" |
| 15 | + notify "github.com/rjeczalik/notify" |
| 16 | + |
| 17 | + // Import namepaces |
| 18 | + . "github.com/djthorpe/go-errors" |
| 19 | +) |
| 20 | + |
| 21 | +/////////////////////////////////////////////////////////////////////////////// |
| 22 | +// TYPES |
| 23 | + |
| 24 | +type Indexer struct { |
| 25 | + *walkfs.WalkFS |
| 26 | + name string |
| 27 | + path string |
| 28 | + walk chan struct{} |
| 29 | +} |
| 30 | + |
| 31 | +/////////////////////////////////////////////////////////////////////////////// |
| 32 | +// GLOBALS |
| 33 | + |
| 34 | +const ( |
| 35 | + defaultCapacity = 1024 |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + // Name for an index must be alphanumeric |
| 40 | + reIndexName = regexp.MustCompile(`^([A-Za-z0-9\_\-]+)$`) |
| 41 | +) |
| 42 | + |
| 43 | +/////////////////////////////////////////////////////////////////////////////// |
| 44 | +// LIFECYCLE |
| 45 | + |
| 46 | +// Create a new indexer with an identifier, path to the root of the indexer |
| 47 | +// and a channel to receive any errors |
| 48 | +func NewIndexer(name, path string) (*Indexer, error) { |
| 49 | + this := new(Indexer) |
| 50 | + |
| 51 | + // Check path argument |
| 52 | + if stat, err := os.Stat(path); err != nil { |
| 53 | + return nil, err |
| 54 | + } else if !stat.IsDir() { |
| 55 | + return nil, ErrBadParameter.With("invalid path: ", strconv.Quote(path)) |
| 56 | + } else if abspath, err := filepath.Abs(path); err != nil { |
| 57 | + return nil, err |
| 58 | + } else if !reIndexName.MatchString(name) { |
| 59 | + return nil, ErrBadParameter.With("invalid index name: ", strconv.Quote(name)) |
| 60 | + } else { |
| 61 | + this.name = name |
| 62 | + this.path = abspath |
| 63 | + this.WalkFS = walkfs.New(this.visit) |
| 64 | + this.walk = make(chan struct{}) |
| 65 | + } |
| 66 | + |
| 67 | + // Return success |
| 68 | + return this, nil |
| 69 | +} |
| 70 | + |
| 71 | +// run indexer |
| 72 | +func (i *Indexer) Run(ctx context.Context, errs chan<- error) error { |
| 73 | + var walking sync.Mutex |
| 74 | + |
| 75 | + in := make(chan notify.EventInfo, defaultCapacity) |
| 76 | + if err := notify.Watch(filepath.Join(i.path, "..."), in, notify.Create, notify.Remove, notify.Write, notify.Rename); err != nil { |
| 77 | + fmt.Println("err", err) |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | +FOR_LOOP: |
| 82 | + for { |
| 83 | + // Dispatch events to index files and folders until context is cancelled |
| 84 | + select { |
| 85 | + case <-ctx.Done(): |
| 86 | + break FOR_LOOP |
| 87 | + case evt := <-in: |
| 88 | + if err := i.event(ctx, evt); err != nil { |
| 89 | + senderr(errs, err) |
| 90 | + } |
| 91 | + case <-i.walk: |
| 92 | + walking.Lock() |
| 93 | + go func() { |
| 94 | + defer walking.Unlock() |
| 95 | + if err := i.WalkFS.Walk(ctx, i.path); err != nil { |
| 96 | + senderr(errs, err) |
| 97 | + } |
| 98 | + }() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Stop notify and close channels |
| 103 | + notify.Stop(in) |
| 104 | + close(in) |
| 105 | + close(i.walk) |
| 106 | + |
| 107 | + // Return success |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +/////////////////////////////////////////////////////////////////////////////// |
| 112 | +// STRINGIFY |
| 113 | + |
| 114 | +func (i *Indexer) String() string { |
| 115 | + str := "<indexer" |
| 116 | + if i.name != "" { |
| 117 | + str += fmt.Sprintf(" name=%q", i.name) |
| 118 | + } |
| 119 | + if i.path != "" { |
| 120 | + str += fmt.Sprintf(" path=%q", i.path) |
| 121 | + } |
| 122 | + return str + ">" |
| 123 | +} |
| 124 | + |
| 125 | +/////////////////////////////////////////////////////////////////////////////// |
| 126 | +// PROPERTIES |
| 127 | + |
| 128 | +// Return name of the index |
| 129 | +func (i *Indexer) Name() string { |
| 130 | + return i.name |
| 131 | +} |
| 132 | + |
| 133 | +// Return the absolute path of the index |
| 134 | +func (i *Indexer) Path() string { |
| 135 | + return i.path |
| 136 | +} |
| 137 | + |
| 138 | +/////////////////////////////////////////////////////////////////////////////// |
| 139 | +// PUBLIC METHODS |
| 140 | + |
| 141 | +// Walk will initiate a walk of the index, and block until context is |
| 142 | +// cancelled or walk is started |
| 143 | +func (i *Indexer) Walk(ctx context.Context) error { |
| 144 | + select { |
| 145 | + case <-ctx.Done(): |
| 146 | + return ctx.Err() |
| 147 | + case i.walk <- struct{}{}: |
| 148 | + break |
| 149 | + } |
| 150 | + // Return success |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +/////////////////////////////////////////////////////////////////////////////// |
| 155 | +// PRIVATE METHODS |
| 156 | + |
| 157 | +// event is used to process an event from the notify |
| 158 | +func (i *Indexer) event(ctx context.Context, evt notify.EventInfo) error { |
| 159 | + fmt.Println("event", evt) |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +// visit is used to index a file from the indexer |
| 164 | +func (i *Indexer) visit(ctx context.Context, abspath, relpath string, info fs.FileInfo) error { |
| 165 | + fmt.Println("visited", abspath, relpath, info.IsDir()) |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +// senderr is used to send an error without blocking |
| 170 | +func senderr(ch chan<- error, err error) { |
| 171 | + if ch != nil { |
| 172 | + select { |
| 173 | + case ch <- err: |
| 174 | + return |
| 175 | + default: |
| 176 | + // Channel blocked, ignore error |
| 177 | + return |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +/* |
| 183 | +// Return an indexer event or nil if no event should be sent |
| 184 | +func (this *Indexer) process(e EventType, path string, info fs.FileInfo, out chan<- IndexerEvent, block bool) error { |
| 185 | + // Normalize the path |
| 186 | + relpath, err := filepath.Rel(this.path, path) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } else { |
| 190 | + relpath = pathSeparator + relpath |
| 191 | + } |
| 192 | +
|
| 193 | + // Deal with exclusions |
| 194 | + if e&EVENT_TYPE_ADDED > 0 { |
| 195 | + // Check for path exclusions |
| 196 | + for exclusion := range this.expath { |
| 197 | + if strings.HasPrefix(relpath, exclusion) { |
| 198 | + return nil |
| 199 | + } |
| 200 | + } |
| 201 | + // Check for extension exclusions |
| 202 | + if info != nil && info.Mode().IsRegular() { |
| 203 | + ext := strings.ToUpper(filepath.Ext(info.Name())) |
| 204 | + if _, exists := this.exext[ext]; exists { |
| 205 | + return nil |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +
|
| 210 | + // Send event |
| 211 | + if block { |
| 212 | + out <- NewEvent(e, this.name, relpath, info) |
| 213 | + } else { |
| 214 | + select { |
| 215 | + case out <- NewEvent(e, this.name, relpath, info): |
| 216 | + // No-op |
| 217 | + default: |
| 218 | + return ErrChannelBlocked.With(this.name) |
| 219 | + } |
| 220 | + } |
| 221 | +
|
| 222 | + // Return success |
| 223 | + return nil |
| 224 | +} |
| 225 | +
|
| 226 | +// Translate notify types to internal types |
| 227 | +func toEventType(e notify.Event, info fs.FileInfo) EventType { |
| 228 | + switch e { |
| 229 | + case notify.Create: |
| 230 | + if info != nil { |
| 231 | + return EVENT_TYPE_ADDED |
| 232 | + } |
| 233 | + case notify.Remove: |
| 234 | + return EVENT_TYPE_REMOVED |
| 235 | + case notify.Rename: |
| 236 | + if info != nil { |
| 237 | + return EVENT_TYPE_ADDED | EVENT_TYPE_RENAMED |
| 238 | + } else { |
| 239 | + return EVENT_TYPE_REMOVED | EVENT_TYPE_RENAMED |
| 240 | + } |
| 241 | + case notify.Write: |
| 242 | + if info != nil { |
| 243 | + return EVENT_TYPE_ADDED | EVENT_TYPE_CHANGED |
| 244 | + } |
| 245 | + } |
| 246 | +
|
| 247 | + // Ignore unhandled cases |
| 248 | + return EVENT_TYPE_NONE |
| 249 | +} |
| 250 | +*/ |
0 commit comments