Skip to content

Commit

Permalink
api: improve list page search performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta committed Dec 30, 2024
1 parent fc360a6 commit df069cc
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions internal/persistence/local/dag_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,11 @@ func (d *dagStoreImpl) ensureDirExist() error {
return nil
}

func (d *dagStoreImpl) searchName(fileName string, searchText *string) bool {
if searchText == nil {
func (d *dagStoreImpl) containsSearchText(text string, search *string) bool {
if search == nil {
return true
}
fileName = strings.TrimSuffix(fileName, path.Ext(fileName))
ret := strings.Contains(strings.ToLower(fileName), strings.ToLower(*searchText))
ret := strings.Contains(strings.ToLower(text), strings.ToLower(*search))
return ret
}

Expand Down Expand Up @@ -191,20 +190,31 @@ func (d *dagStoreImpl) ListPagination(ctx context.Context, params persistence.DA
currentDag *digraph.DAG
)

if err := filepath.WalkDir(d.dir, func(_ string, dir fs.DirEntry, err error) error {
if err := filepath.WalkDir(d.dir, func(_ string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}

if dir.IsDir() || !checkExtension(dir.Name()) {
if entry.IsDir() || !checkExtension(entry.Name()) {
return nil
}

if currentDag, err = d.GetMetadata(ctx, dir.Name()); err != nil {
errList = append(errList, fmt.Sprintf("reading %s failed: %s", dir.Name(), err))
baseName := path.Base(entry.Name())
dagName := strings.TrimSuffix(baseName, path.Ext(baseName))
if params.Tag == nil || *params.Tag == "" {
// if tag is not provided, check before reading the file
if !d.containsSearchText(dagName, params.Name) {
// skip the file if the name
return nil
}
}

// if tag is provided, read the file and check the tag
if currentDag, err = d.GetMetadata(ctx, dagName); err != nil {
errList = append(errList, fmt.Sprintf("reading %s failed: %s", dagName, err))
}

if !d.searchName(dir.Name(), params.Name) || currentDag == nil || !d.searchTags(currentDag.Tags, params.Tag) {
if !d.containsSearchText(dagName, params.Name) || currentDag == nil || !d.searchTags(currentDag.Tags, params.Tag) {
return nil
}

Expand Down

0 comments on commit df069cc

Please sign in to comment.