From 1b35892acd8a8262e934a80f22f2bab68533a8e3 Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 19 Jul 2023 11:35:16 -0400 Subject: [PATCH] fix: too slow when initializing cache --- search.go | 33 ++++++++++++++++++---------- tempstorage.go | 14 ++++++++++-- testing/fulltext/insert_key_merge.go | 6 ++--- testing/fulltext/main.go | 10 ++++----- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/search.go b/search.go index c0dd9ee..bf4be26 100644 --- a/search.go +++ b/search.go @@ -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]]]) } } } diff --git a/tempstorage.go b/tempstorage.go index 11880ab..948d6d0 100644 --- a/tempstorage.go +++ b/tempstorage.go @@ -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 { @@ -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 } } } @@ -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 } diff --git a/testing/fulltext/insert_key_merge.go b/testing/fulltext/insert_key_merge.go index c7196aa..26da472 100644 --- a/testing/fulltext/insert_key_merge.go +++ b/testing/fulltext/insert_key_merge.go @@ -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, }) diff --git a/testing/fulltext/main.go b/testing/fulltext/main.go index e234e08..0feb936 100644 --- a/testing/fulltext/main.go +++ b/testing/fulltext/main.go @@ -2,9 +2,9 @@ package main func main() { insert_key_merge() - delete() - clean() - ft() - search() - set() + //delete() + //clean() + //ft() + //search() + //set() }