feat(fileindex): add fileindex methods

This commit is contained in:
hexxa 2022-06-29 23:01:05 +08:00 committed by Hexxa
parent 24f1516fd5
commit a905cef533

View file

@ -1,26 +1,52 @@
package fileindex package fileindex
import ( import (
"strings"
qradix "github.com/ihexxa/q-radix/v3" 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 { type FileTreeIndex struct {
*qradix.RTree db fs.ISimpleFS
trie *qradix.RTree
} }
func NewFileTreeIndex() *FileTreeIndex { func NewFileTreeIndex(db fs.ISimpleFS) *FileTreeIndex {
return &FileTreeIndex{ return &FileTreeIndex{
RTree: qradix.NewRTree(), db: db,
trie: qradix.NewRTree(),
} }
} }
type IFileIndex interface { func (idx *FileTreeIndex) Search(segment string) []string {
FromString(input chan string) error results := idx.trie.GetAllPrefixMatches(segment)
Get(key string) (interface{}, error) paths := []string{}
GetAllPrefixMatches(key string) map[string]interface{} for _, iPaths := range results {
GetBestMatch(key string) (string, interface{}, bool) paths = append(paths, iPaths.([]string)...)
Insert(key string, val interface{}) (interface{}, error) }
Remove(key string) bool return paths
Size() int }
String() chan string
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
} }