Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add methods for adding/removing node id in radix #4

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
168 changes: 75 additions & 93 deletions fsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,11 @@ func (fs *FSearch) AddPath(pathname string) error {
return err
}

var keyword string
var nodeIdsVal interface{}
for _, node := range nodes {
fs.nodes[node.id] = node
runes := []rune(node.name)

for i := 0; i < len(runes); i++ {
keyword = string(runes[i:])
nodeIdsVal, err = fs.radix.Get(keyword)
if err != nil {
if errors.Is(err, qradix.ErrNotExist) {
nodeIdsVal = []int64{}
} else {
return err
}
}

nodeIds := nodeIdsVal.([]int64)
_, err = fs.radix.Insert(keyword, append(nodeIds, node.id))
if err != nil {
return err
}
err = fs.insertNodeIdToRadix(node.name, node.id)
if err != nil {
return err
}
}

Expand All @@ -149,30 +132,9 @@ func (fs *FSearch) DelPath(pathname string) error {

parts := strings.Split(pathname, fs.tree.PathSeparator)
for _, part := range parts {
runes := []rune(part)
for i := 0; i < len(runes); i++ {
suffix := string(runes[i:])
idsVal, err := fs.radix.Get(suffix)
if err != nil {
if errors.Is(err, qradix.ErrNotExist) {
continue
}
}
ids := idsVal.([]int64)
for i, id := range ids {
if id == deletedNode.id {
if len(ids) == 1 {
fs.radix.Remove(suffix)
} else {
ids[i], ids[len(ids)-1] = ids[len(ids)-1], ids[i]
_, err := fs.radix.Insert(suffix, ids[:len(ids)-1])
if err != nil {
return err
}
}
break
}
}
err = fs.deleteNodeIdFromRadix(part, deletedNode.id)
if err != nil {
return err
}
}

Expand All @@ -191,81 +153,101 @@ func (fs *FSearch) MovePath(pathname, dstParentPath string) error {
return fs.tree.MovePath(pathname, dstParentPath)
}

// Rename renames the file/folder name
func (fs *FSearch) RenamePath(pathname, newName string) error {
if !fs.on {
return ErrStopped
}
fs.lock.Lock()
defer fs.lock.Unlock()

if strings.Contains(newName, fs.tree.PathSeparator) {
return ErrInvalidPath
}

originalName := filepath.Base(pathname)
if len(originalName) == 0 {
return ErrInvalidPath
}

renamedNode, err := fs.tree.Rename(pathname, newName)
if err != nil {
return err
}

var keyword string
func (fs *FSearch) insertNodeIdToRadix(keyword string, nodeId int64) error {
var err error
var suffix string
var nodeIdsVal interface{}
runes := []rune(originalName)
for i := 0; i < len(runes); i++ {
keyword = string(runes[i:])
nodeIdsVal, err = fs.radix.Get(keyword)
nodeIds := nodeIdsVal.([]int64)

runes := []rune(keyword)
for i := 0; i < len(runes); i++ {
suffix = string(runes[i:])
nodeIdsVal, err = fs.radix.Get(suffix)
if err != nil {
if errors.Is(err, qradix.ErrNotExist) {
continue
nodeIdsVal = []int64{}
} else {
return err
}
}

for i, nodeId := range nodeIds {
if nodeId == renamedNode.id {
nodeIdsVal, err = fs.radix.Insert(keyword, append(nodeIds[:i], nodeIds[i+1:]...))
if err != nil {
// TODO: although it is impossible reach here
// better to add a checking in searching side since not all keys are removed
return err
}
break
}
nodeIds := nodeIdsVal.([]int64)
_, err = fs.radix.Insert(suffix, append(nodeIds, nodeId))
if err != nil {
// TODO: although it is impossible reach here
// better to add a checking in searching side since not all keys are removed
return err
}
}

runes = []rune(newName)
return nil
}

func (fs *FSearch) deleteNodeIdFromRadix(keyword string, nodeId int64) error {
var err error
var suffix string
var nodeIdsVal interface{}

runes := []rune(keyword)
for i := 0; i < len(runes); i++ {
keyword = string(runes[i:])
nodeIdsVal, err = fs.radix.Get(keyword)
suffix = string(runes[i:])
nodeIdsVal, err = fs.radix.Get(suffix)
if err != nil {
if errors.Is(err, qradix.ErrNotExist) {
nodeIdsVal = []int64{}
continue
} else {
return err
}
}

nodeIds := nodeIdsVal.([]int64)
_, err = fs.radix.Insert(keyword, append(nodeIds, renamedNode.id))
if err != nil {
// TODO: although it is impossible reach here
// better to add a checking in searching side since not all keys are removed
return err
ids := nodeIdsVal.([]int64)
for i, id := range ids {
if id == nodeId {
if len(ids) == 1 {
fs.radix.Remove(suffix)
} else {
ids[i], ids[len(ids)-1] = ids[len(ids)-1], ids[i]
_, err := fs.radix.Insert(suffix, ids[:len(ids)-1])
if err != nil {
return err
}
}
break
}
}
}

return nil
}

// Rename renames the file/folder name
func (fs *FSearch) RenamePath(pathname, newName string) error {
if !fs.on {
return ErrStopped
}
fs.lock.Lock()
defer fs.lock.Unlock()

if strings.Contains(newName, fs.tree.PathSeparator) {
return ErrInvalidPath
}

originalName := filepath.Base(pathname)
if len(originalName) == 0 {
return ErrInvalidPath
}

renamedNode, err := fs.tree.Rename(pathname, newName)
if err != nil {
return err
}

err = fs.deleteNodeIdFromRadix(originalName, renamedNode.id)
if err != nil {
return err
}
return fs.insertNodeIdToRadix(newName, renamedNode.id)
}

// Search searches keyword in the FSearch
// It returns pahtnames which contains keyword, the result size is limited by the resultLimit
func (fs *FSearch) Search(keyword string) ([]string, error) {
Expand Down