Skip to content

Commit

Permalink
fix: too slow when initializing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
realTristan committed Jul 19, 2023
1 parent ee6a477 commit 1b35892
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
33 changes: 21 additions & 12 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,56 @@ func (c *Cache) search(sp SearchParams) []map[string]any {
var result []map[string]any = []map[string]any{}

// Variables for storing the smallest words array
var (
smallest int = 0 //nolint:ineffassign
smallestIndex int = 0
)
var smallestData []int = []int{}

// Check if the query is in the cache
if indices, ok := c.ft.storage[words[0]]; !ok {
return []map[string]any{}
} else {
for {
if v, ok := indices.(string); ok {
indices = c.ft.storage[v]
} else {
break
}
}
if temp, ok := indices.(int); ok {
return []map[string]any{
c.data[c.ft.indices[temp]],
}
}
smallest = len(indices.([]int))
smallestData = indices.([]int)
}

// Find the smallest words array
// Don't include the first or last words from the query
for i := 1; i < len(words)-1; i++ {
if indices, ok := c.ft.storage[words[i]]; ok {
for {
if v, ok := indices.(string); ok {
indices = c.ft.storage[v]
} else {
break
}
}
if index, ok := indices.(int); ok {
return []map[string]any{
c.data[c.ft.indices[index]],
}
}
if l := len(indices.([]int)); l < smallest {
smallest = l
smallestIndex = i
if l := len(indices.([]int)); l < len(smallestData) {
smallestData = indices.([]int)
}
}
}

// Loop through the indices
var keys []int = c.ft.storage[words[smallestIndex]].([]int)
for i := 0; i < len(keys); i++ {
for _, value := range c.data[c.ft.indices[keys[i]]] {
for i := 0; i < len(smallestData); i++ {
for _, value := range c.data[c.ft.indices[smallestData[i]]] {
// Check if the value contains the query
if v, ok := value.(string); ok {
if strings.Contains(strings.ToLower(v), sp.Query) {
result = append(result, c.data[c.ft.indices[keys[i]]])
result = append(result, c.data[c.ft.indices[smallestData[i]]])
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions tempstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (ts *TempStorage) update(ft *FullText, words []string, cacheKey string) {
}
if temp, ok := ts.data[word]; !ok {
ts.data[word] = []int{ts.index}
} else if _, ok := temp.(string); ok {
continue
} else if v, ok := temp.([]int); !ok {
ts.data[word] = []int{temp.(int), ts.keys[cacheKey]}
} else {
Expand Down Expand Up @@ -156,9 +158,15 @@ func (ts *TempStorage) mergeKeys() {
continue
}
if strings.Contains(k1, k2) {
if _, ok := v1.(string); ok {
continue
}
if _, ok := v2.(string); ok {
continue
}
var v1, v2 = intToSlice(v1), intToSlice(v2)
ts.data[k1] = append(v1, v2...)
delete(ts.data, k2)
ts.data[k2] = k1
}
}
}
Expand Down Expand Up @@ -197,9 +205,11 @@ func (ts *TempStorage) insert(ft *FullText, cacheKey string, ftv string) error {

// Update the temp storage
ts.update(ft, words, cacheKey)
ts.mergeKeys()
}

// Finally, merge the keys
ts.mergeKeys()

// Return no error
return nil
}
6 changes: 3 additions & 3 deletions testing/fulltext/insert_key_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ func insert_key_merge() {
"age": 17,
})
cache.Set("user_id2", map[string]any{
"name": cache.WithFT("tris"),
"name": cache.WithFT("tris is cool"),
"age": 17,
})

// Search for tris
var result, _ = cache.SearchOneWord(hermes.SearchParams{
Query: "tris",
var result, _ = cache.Search(hermes.SearchParams{
Query: "tris is",
Limit: 100,
Strict: false,
})
Expand Down
10 changes: 5 additions & 5 deletions testing/fulltext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

func main() {
insert_key_merge()
delete()
clean()
ft()
search()
set()
//delete()
//clean()
//ft()
//search()
//set()
}

0 comments on commit 1b35892

Please sign in to comment.