From 23067cad81bdd2ac64863c8fadc758c8e66201b7 Mon Sep 17 00:00:00 2001 From: hexxa Date: Tue, 12 Jul 2022 22:33:20 +0800 Subject: [PATCH] feat(dep): add fsearch for file searching --- go.mod | 3 +- go.sum | 5 +++ src/depidx/deps.go | 10 ------ src/search/fileindex/fsearch.go | 41 ++++++++++++++++++++++++ src/search/fileindex/trie_tree.go | 52 ------------------------------- 5 files changed, 48 insertions(+), 63 deletions(-) create mode 100644 src/search/fileindex/fsearch.go delete mode 100644 src/search/fileindex/trie_tree.go diff --git a/go.mod b/go.mod index 83dd225..55e5776 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,10 @@ require ( github.com/gin-gonic/gin v1.7.3 github.com/go-playground/validator/v10 v10.9.0 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/ihexxa/fsearch v0.1.2 // indirect github.com/ihexxa/gocfg v0.0.1 github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d - github.com/ihexxa/q-radix/v3 v3.0.4 // indirect + github.com/ihexxa/q-radix/v3 v3.0.5 github.com/jessevdk/go-flags v1.4.0 github.com/json-iterator/go v1.1.11 // indirect github.com/mattn/go-isatty v0.0.13 // indirect diff --git a/go.sum b/go.sum index 68f3f43..d4e4df4 100644 --- a/go.sum +++ b/go.sum @@ -43,12 +43,17 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/ihexxa/fsearch v0.1.2 h1:xTHVTwpnEF5YLpHbD+XKpEz9cURPE90nOHFl5eCNe3E= +github.com/ihexxa/fsearch v0.1.2/go.mod h1:vikDvlWxDwS1G8Vu5Xt+2A5uvvYo41lS7R6/a6iCDZ8= github.com/ihexxa/gocfg v0.0.1 h1:3zsCHY/SYdqKSoo3pwTBWMgivEB7/ctpPPHL3p43UBg= github.com/ihexxa/gocfg v0.0.1/go.mod h1:oqDTq1ywx4Qi9DdhFwwMHoPCYv6Txrfj2SY5WWcgiJs= github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d h1:+v33khYHVDPEuuWO/JE1IzhoIu5uNvEcSs5GmXc5Sjw= github.com/ihexxa/multipart v0.0.0-20210916083128-8584a3f00d1d/go.mod h1:rhOAe/52S/J1fq1VnXvKX8FHuo65I+IcYUozW4M7+wE= github.com/ihexxa/q-radix/v3 v3.0.4 h1:Hk+Av57Q+fwFtHobd5uQjOFdY/V31nl4p+K4mO3crDg= github.com/ihexxa/q-radix/v3 v3.0.4/go.mod h1:DYXQuLlICQ2pCdPQOzyovzSPq0P8sKwxv5HhVQAwSVQ= +github.com/ihexxa/q-radix/v3 v3.0.5 h1:dF3+NOZXSK3dSL/46BS5JC/gBe4kw7qiKITRPppo+rQ= +github.com/ihexxa/q-radix/v3 v3.0.5/go.mod h1:DYXQuLlICQ2pCdPQOzyovzSPq0P8sKwxv5HhVQAwSVQ= +github.com/ihexxa/randstr v0.3.0/go.mod h1:QUBxemrXFcSziCClYa9/2m5N+87kGNKYaGxDfv05+uY= github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= diff --git a/src/depidx/deps.go b/src/depidx/deps.go index 3353e7d..788cedf 100644 --- a/src/depidx/deps.go +++ b/src/depidx/deps.go @@ -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 -} diff --git a/src/search/fileindex/fsearch.go b/src/search/fileindex/fsearch.go new file mode 100644 index 0000000..0fada8e --- /dev/null +++ b/src/search/fileindex/fsearch.go @@ -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) +} diff --git a/src/search/fileindex/trie_tree.go b/src/search/fileindex/trie_tree.go deleted file mode 100644 index 7375c4f..0000000 --- a/src/search/fileindex/trie_tree.go +++ /dev/null @@ -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 -}