feat(dep): add fsearch for file searching

This commit is contained in:
hexxa 2022-07-12 22:33:20 +08:00 committed by Hexxa
parent a905cef533
commit 23067cad81
5 changed files with 48 additions and 63 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/ihexxa/quickshare/src/idgen"
"github.com/ihexxa/quickshare/src/iolimiter"
"github.com/ihexxa/quickshare/src/kvstore"
"github.com/ihexxa/quickshare/src/search/fileindex"
"github.com/ihexxa/quickshare/src/worker"
)
@ -39,7 +38,6 @@ type Deps struct {
workers worker.IWorkerPool
boltStore *boltstore.BoltStore
cron cron.ICron
fileIndex fileindex.IFileIndex
}
func NewDeps(cfg gocfg.ICfg) *Deps {
@ -141,11 +139,3 @@ func (deps *Deps) Cron() cron.ICron {
func (deps *Deps) SetCron(cronImp cron.ICron) {
deps.cron = cronImp
}
func (deps *Deps) FileIndex() fileindex.IFileIndex {
return deps.fileIndex
}
func (deps *Deps) SetFileIndex(fileIndex fileindex.IFileIndex) {
deps.fileIndex = fileIndex
}

View file

@ -0,0 +1,41 @@
package fileindex
import (
// "strings"
"github.com/ihexxa/fsearch"
"github.com/ihexxa/quickshare/src/fs"
)
type FileTreeIndex struct {
db fs.ISimpleFS
index *fsearch.FSearch
}
func NewFileTreeIndex(db fs.ISimpleFS) *FileTreeIndex {
return &FileTreeIndex{
db: db,
// TODO: support max result size config
index: fsearch.New("/", 1024),
}
}
func (idx *FileTreeIndex) Search(keyword string) ([]string, error) {
return idx.index.Search(keyword)
}
func (idx *FileTreeIndex) AddPath(pathname string) error {
return idx.index.AddPath(pathname)
}
func (idx *FileTreeIndex) DelPath(pathname string) error {
return idx.index.DelPath(pathname)
}
func (idx *FileTreeIndex) RenamePath(pathname, newName string) error {
return idx.index.RenamePath(pathname, newName)
}
func (idx *FileTreeIndex) MovePath(pathname, dstParentPath string) error {
return idx.index.MovePath(pathname, dstParentPath)
}

View file

@ -1,52 +0,0 @@
package fileindex
import (
"strings"
qradix "github.com/ihexxa/q-radix/v3"
"github.com/ihexxa/quickshare/src/fs"
)
// type IFileIndex interface {
// FromString(input chan string) error
// Get(key string) (interface{}, error)
// GetAllPrefixMatches(key string) map[string]interface{}
// GetBestMatch(key string) (string, interface{}, bool)
// Insert(key string, val interface{}) (interface{}, error)
// Remove(key string) bool
// Size() int
// String() chan string
// }
type FileTreeIndex struct {
db fs.ISimpleFS
trie *qradix.RTree
}
func NewFileTreeIndex(db fs.ISimpleFS) *FileTreeIndex {
return &FileTreeIndex{
db: db,
trie: qradix.NewRTree(),
}
}
func (idx *FileTreeIndex) Search(segment string) []string {
results := idx.trie.GetAllPrefixMatches(segment)
paths := []string{}
for _, iPaths := range results {
paths = append(paths, iPaths.([]string)...)
}
return paths
}
func (idx *FileTreeIndex) Add(path string) error {
segments := strings.Split(path, "/")
for _, segment := range segments {
_, err := idx.trie.Insert(segment, path)
if err != nil {
return err
}
}
return nil
}