From 0e962a5fb4a5fafd10657f42380a6420023619e7 Mon Sep 17 00:00:00 2001 From: Aditya <60684641+0x0elliot@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:35:09 +0530 Subject: [PATCH] feat: add ?token= exact-token datastore search via n-gram index --- db-connector.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ shared.go | 6 +++++ 2 files changed, 73 insertions(+) diff --git a/db-connector.go b/db-connector.go index 24dad359..fab0b381 100755 --- a/db-connector.go +++ b/db-connector.go @@ -17403,6 +17403,73 @@ func GetCacheKeysByPrefix(ctx context.Context, orgId string, category string, pr return matched, "", nil } +// GetCacheKeysByToken does an exact-token search using the n-gram index that the +// correlation engine already maintains (crossCorrelateNGrams). It's a single O(1) +// lookup of "orgId_" -> refs ("category|key"), then a Get per ref. No scan, +// no new index, works on both Datastore and OpenSearch. +// +// The n-gram index is over VALUES (string fields 5-70 chars, lowercased, +// timestamps/uuids/JSON mostly skipped), so it finds entries whose value contains +// the token. Matching is exact: the token must equal an indexed term, not a prefix. +// Scoped entirely to orgId (ngram key + entry fetch are both org-prefixed). +func GetCacheKeysByToken(ctx context.Context, orgId string, category string, token string, max int) ([]CacheKeyData, string, error) { + if max > 1000 { + max = 1000 + } + if max <= 0 { + max = 50 + } + + parsedCategory := strings.ReplaceAll(strings.ToLower(category), " ", "_") + parsedToken := strings.ToLower(strings.TrimSpace(token)) + if parsedToken == "" { + return []CacheKeyData{}, "", nil + } + + ngramKey := fmt.Sprintf("%s_%s", orgId, parsedToken) + item, err := GetDatastoreNGramItem(ctx, ngramKey) + if err != nil || item == nil || len(item.Ref) == 0 { + // No token match is a valid empty result, not an error to the caller. + return []CacheKeyData{}, "", nil + } + + results := []CacheKeyData{} + seen := map[string]bool{} + for _, ref := range item.Ref { + parts := strings.SplitN(ref, "|", 2) + if len(parts) != 2 { + continue + } + refCategory, refKey := parts[0], parts[1] + + // Scope to the selected category when one is set. + if parsedCategory != "" && parsedCategory != "default" && refCategory != parsedCategory { + continue + } + if seen[ref] { + continue + } + seen[ref] = true + + cacheId := fmt.Sprintf("%s_%s", orgId, refKey) + if len(refCategory) > 0 && refCategory != "default" { + cacheId = fmt.Sprintf("%s_%s_%s", orgId, refKey, refCategory) + } + + cacheItem, err := GetDatastoreKey(ctx, cacheId, refCategory) + if err != nil || cacheItem == nil || cacheItem.Key == "" { + continue + } + + results = append(results, *cacheItem) + if len(results) >= max { + break + } + } + + return results, "", nil +} + func GetAllDeals(ctx context.Context, orgId string) ([]ResellerDeal, error) { nameKey := "reseller_deal" cacheKey := fmt.Sprintf("%s_%s", nameKey, orgId) diff --git a/shared.go b/shared.go index 18c7e193..1e9a332c 100644 --- a/shared.go +++ b/shared.go @@ -21012,6 +21012,12 @@ func HandleListCacheKeys(resp http.ResponseWriter, request *http.Request) { if err != nil { isSuccess = false } + } else if tokenList, tokenOk := request.URL.Query()["token"]; tokenOk && len(tokenList) > 0 && tokenList[0] != "" { + // Exact-token value search via the n-gram index the correlation engine maintains. + keys, newCursor, err = GetCacheKeysByToken(ctx, org.Id, category, tokenList[0], maxAmount) + if err != nil { + isSuccess = false + } } else { keys, newCursor, err = GetAllCacheKeys(ctx, org.Id, category, maxAmount, cursor) if err != nil {