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

3
go.mod
View file

@ -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

5
go.sum
View file

@ -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=

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
}