indexer

package
v1.0.49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

README

sqlite3 file indexer

This package provides a file indexer, which indexes files in one or more folders (and their child folders) in an sqlite3 database, and can update when files and folders are added, changed and deleted.

This package is part of a wider project, github.com/mutablelogic/go-sqlite. Please see the module documentation for more information.

Introduction

An indexing process consists of:

  1. In Indexer object, which runs the indexing process;
  2. A Queue object, which receives change events from the indexer and passes them onto the storage;
  3. A Store object, which consumes change events and maintains the database.

To create an indexer, use the NewIndexer function. Pass the unique name for the index, the path to the folder to index, and a Queue object. For example,

package main

import (
	// Packages
	"github.com/mutablelogic/go-sqlite/pkg/indexer"
)

func main() {
    name, path := // ....

    // Create a queue and indexer
    queue := indexer.NewQueue()
	indexer, err := indexer.NewIndexer(name, path, queue)
	if err != nil {
        panic(err)
	}

    // Perform indexing in background process...
    var wg sync.WaitGroup
    go Index(&wg, indexer)
    go Walk(&wg, indexer)
    go Process(&wg, queue)
   
	// Wait for all goroutines to finish
	wg.Wait()

    // Do any cleanup here
}

The three background goroutines are:

  1. Index: Watches for changes to the folder and indexes the files;
  2. Walk: Performs a recursive walk of the folder and indexes the files;
  3. Process: Consumes change events from the queue and updates the database.

Consuming change events

TODO

File and path inclusions and exclusions

TODO

Example Applications

There is an example application here which will index a folder into an sqlite database, and a plugin for go-server here which provides a REST API to the indexing process. You may want to build your own application to index your files, in which case read on.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSchema added in v1.0.49

func CreateSchema(ctx context.Context, conn SQConnection, schema string, tokenizer string) error

func Delete added in v1.0.49

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

func ListIndexWithCount added in v1.0.49

func ListIndexWithCount(ctx context.Context, conn SQConnection, schema string) (map[string]int64, error)

Get indexes and count of documents for each index

func Replace added in v1.0.49

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

Types

type EventType added in v1.0.49

type EventType uint
const (
	EventNone EventType = iota
	EventAdd
	EventRemove
	EventReindexStarted
	EventReindexCompleted
)

type Indexer

type Indexer struct {
	*walkfs.WalkFS
	// contains filtered or unexported fields
}

func NewIndexer

func NewIndexer(name, path string, queue *Queue) (*Indexer, error)

Create a new indexer with an identifier, path to the root of the indexer and a queue

func (*Indexer) IsIndexing added in v1.0.49

func (i *Indexer) IsIndexing() bool

Return true if indexing

func (*Indexer) Name

func (i *Indexer) Name() string

Return name of the index

func (*Indexer) Path

func (i *Indexer) Path() string

Return the absolute path of the index

func (*Indexer) Queue added in v1.0.49

func (i *Indexer) Queue() *Queue

Return the queue

func (*Indexer) Run

func (i *Indexer) Run(ctx context.Context, errs chan<- error) error

run indexer, provider channel to receive errors

func (*Indexer) String

func (i *Indexer) String() string

func (*Indexer) Walk

func (i *Indexer) Walk(ctx context.Context, fn WalkFunc) error

Walk will initiate a walk of the index, and block until context is cancelled or walk is started

type Queue

type Queue struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewQueue added in v1.0.49

func NewQueue() *Queue

Create a new queue with default capacity

func NewQueueWithCapacity

func NewQueueWithCapacity(cap int) *Queue

Create a new queue which acts as a buffer between the file indexing and the processng/rendering which can be slower than the file indexing

func (*Queue) Add

func (q *Queue) Add(name, path string, info fs.FileInfo)

Add an item to the queue. If the item is already in the queue, then it is bumped to the end of the queue

func (*Queue) Count

func (q *Queue) Count() int

Return a queue event from the head

func (*Queue) Get

func (q *Queue) Get(name, path string) *QueueEvent

Return a queue event from the queue, or nil

func (*Queue) Mark added in v1.0.49

func (q *Queue) Mark(name, path string, flag bool)

Indicate reindexing in progress or completed

func (*Queue) Next

func (q *Queue) Next() *QueueEvent

Return a queue event from the head

func (*Queue) Remove

func (q *Queue) Remove(name, path string)

Remove an item to the queue. If the item is already in the queue, it is removed

func (*Queue) String

func (this *Queue) String() string

type QueueEvent

type QueueEvent struct {
	EventType
	Name string
	Path string
	Info fs.FileInfo
}

type Store added in v1.0.49

type Store struct {
	// contains filtered or unexported fields
}

func NewStore added in v1.0.49

func NewStore(pool SQPool, schema string, queue *Queue, r Renderer, workers uint) *Store

Create a new store object

func (*Store) Run added in v1.0.49

func (s *Store) Run(ctx context.Context, errs chan<- error) error

func (*Store) Schema added in v1.0.49

func (s *Store) Schema() string

func (*Store) String added in v1.0.49

func (s *Store) String() string

type WalkFunc

type WalkFunc func(err error)

WalkFunc is called after a reindexing with any walk errors

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL