From 4fe1a638478d2cd2d7411b1cd8636ec9c6ed9137 Mon Sep 17 00:00:00 2001 From: LordNoteworthy Date: Sat, 14 Sep 2024 06:56:08 +1000 Subject: [PATCH] feat: improve strings/ return schema when no query is given --- db/count-strings-with-substring.n1ql | 12 ------- db/count-strings.n1ql | 22 ++++++------- db/file-strings-with-substring.n1ql | 12 ------- db/file-strings.n1ql | 49 +++++++--------------------- internal/db/query.go | 44 ++++++++++++------------- internal/file/repository.go | 15 ++------- 6 files changed, 47 insertions(+), 107 deletions(-) delete mode 100644 db/count-strings-with-substring.n1ql delete mode 100644 db/file-strings-with-substring.n1ql diff --git a/db/count-strings-with-substring.n1ql b/db/count-strings-with-substring.n1ql deleted file mode 100644 index 76370f3..0000000 --- a/db/count-strings-with-substring.n1ql +++ /dev/null @@ -1,12 +0,0 @@ -SELECT RAW - ARRAY_LENGTH ( - ARRAY_FLATTEN ( - [ - ARRAY {"value": s, "encoding": "ascii"} FOR s IN f.strings.ascii WHEN s LIKE $term END, - ARRAY {"value": s, "encoding": "wide"} FOR s IN f.strings.wide WHEN s LIKE $term END, - ARRAY {"value": s, "encoding": "asm"} FOR s IN f.strings.asm WHEN s LIKE $term END - ], 1 ) - ) - -FROM `bucket_name` f -USE KEYS $sha256 diff --git a/db/count-strings.n1ql b/db/count-strings.n1ql index f90ee09..76370f3 100644 --- a/db/count-strings.n1ql +++ b/db/count-strings.n1ql @@ -1,12 +1,12 @@ SELECT RAW - ARRAY_MAX( - [ - ARRAY_LENGTH(f.strings.ascii), - ARRAY_LENGTH(f.strings.wide), - ARRAY_LENGTH(f.strings.asm) - ] - ) -FROM - `bucket_name` f -USE KEYS - $sha256 + ARRAY_LENGTH ( + ARRAY_FLATTEN ( + [ + ARRAY {"value": s, "encoding": "ascii"} FOR s IN f.strings.ascii WHEN s LIKE $term END, + ARRAY {"value": s, "encoding": "wide"} FOR s IN f.strings.wide WHEN s LIKE $term END, + ARRAY {"value": s, "encoding": "asm"} FOR s IN f.strings.asm WHEN s LIKE $term END + ], 1 ) + ) + +FROM `bucket_name` f +USE KEYS $sha256 diff --git a/db/file-strings-with-substring.n1ql b/db/file-strings-with-substring.n1ql deleted file mode 100644 index 88c1086..0000000 --- a/db/file-strings-with-substring.n1ql +++ /dev/null @@ -1,12 +0,0 @@ -SELECT RAW - ARRAY_FLATTEN ( - [ - ARRAY {"value": s, "encoding": "ascii"} FOR s IN f.strings.ascii WHEN s LIKE $term END, - ARRAY {"value": s, "encoding": "wide"} FOR s IN f.strings.wide WHEN s LIKE $term END, - ARRAY {"value": s, "encoding": "asm"} FOR s IN f.strings.asm WHEN s LIKE $term END - ], 1 ) - -FROM `bucket_name` f -USE KEYS $sha256 -OFFSET $offset -LIMIT $limit diff --git a/db/file-strings.n1ql b/db/file-strings.n1ql index 989bef5..88c1086 100644 --- a/db/file-strings.n1ql +++ b/db/file-strings.n1ql @@ -1,37 +1,12 @@ -SELECT - { - "ascii": ( - SELECT - RAW ascii_str - FROM - `bucket_name` f - USE KEYS - $sha256 - UNNEST - f.strings.ascii ascii_str OFFSET $offset - LIMIT - $limit - ), "wide": ( - SELECT - RAW wide_str - FROM - `bucket_name` f - USE KEYS - $sha256 - UNNEST - f.strings.wide wide_str OFFSET $offset - LIMIT - $limit - ), "asm": ( - SELECT - RAW asm_str - FROM - `bucket_name` f - USE KEYS - $sha256 - UNNEST - f.strings.asm asm_str OFFSET $offset - LIMIT - $limit - ) - }.* +SELECT RAW + ARRAY_FLATTEN ( + [ + ARRAY {"value": s, "encoding": "ascii"} FOR s IN f.strings.ascii WHEN s LIKE $term END, + ARRAY {"value": s, "encoding": "wide"} FOR s IN f.strings.wide WHEN s LIKE $term END, + ARRAY {"value": s, "encoding": "asm"} FOR s IN f.strings.asm WHEN s LIKE $term END + ], 1 ) + +FROM `bucket_name` f +USE KEYS $sha256 +OFFSET $offset +LIMIT $limit diff --git a/internal/db/query.go b/internal/db/query.go index 037d174..bc18ef7 100644 --- a/internal/db/query.go +++ b/internal/db/query.go @@ -39,29 +39,27 @@ const ( ) var fileQueryMap = map[string]n1qlQuery{ - "ano-user-activities.n1ql": AnoUserActivities, - "ano-user-comments.n1ql": AnoUserComments, - "ano-user-followers.n1ql": AnoUserFollowers, - "ano-user-following.n1ql": AnoUserFollowing, - "ano-user-likes.n1ql": AnoUserLikes, - "ano-user-submissions.n1ql": AnoUserSubmissions, - "behavior-report.n1ql": BehaviorReport, - "count-ano-user-activities.n1ql": CountAnoUserActivities, - "count-strings.n1ql": CountStrings, - "count-strings-with-substring.n1ql": CountStringsWithSubstring, - "count-user-activities.n1ql": CountUserActivities, - "delete-activity.n1ql": DeleteActivity, - "file-comments.n1ql": FileComments, - "file-strings.n1ql": FileStrings, - "file-strings-with-substring.n1ql": FileStringsWithSubstring, - "file-summary.n1ql": FileSummary, - "get-all-doc-type.n1ql": GetAllDocType, - "user-activities.n1ql": UserActivities, - "user-comments.n1ql": UserComments, - "user-followers.n1ql": UserFollowers, - "user-following.n1ql": UserFollowing, - "user-likes.n1ql": UserLikes, - "user-submissions.n1ql": UserSubmissions, + "ano-user-activities.n1ql": AnoUserActivities, + "ano-user-comments.n1ql": AnoUserComments, + "ano-user-followers.n1ql": AnoUserFollowers, + "ano-user-following.n1ql": AnoUserFollowing, + "ano-user-likes.n1ql": AnoUserLikes, + "ano-user-submissions.n1ql": AnoUserSubmissions, + "behavior-report.n1ql": BehaviorReport, + "count-ano-user-activities.n1ql": CountAnoUserActivities, + "count-strings.n1ql": CountStrings, + "count-user-activities.n1ql": CountUserActivities, + "delete-activity.n1ql": DeleteActivity, + "file-comments.n1ql": FileComments, + "file-strings.n1ql": FileStrings, + "file-summary.n1ql": FileSummary, + "get-all-doc-type.n1ql": GetAllDocType, + "user-activities.n1ql": UserActivities, + "user-comments.n1ql": UserComments, + "user-followers.n1ql": UserFollowers, + "user-following.n1ql": UserFollowing, + "user-likes.n1ql": UserLikes, + "user-submissions.n1ql": UserSubmissions, } // walk returns list of files in directory. diff --git a/internal/file/repository.go b/internal/file/repository.go index af74924..c3d1b78 100644 --- a/internal/file/repository.go +++ b/internal/file/repository.go @@ -176,9 +176,6 @@ func (r repository) Summary(ctx context.Context, id string) ( return nil, err } - if len(results.([]interface{})) == 0 { - return results, nil - } return results.([]interface{})[0], nil } @@ -205,9 +202,6 @@ func (r repository) Comments(ctx context.Context, id string, offset, return nil, err } - if len(results.([]interface{})) == 0 { - return []interface{}{}, nil - } return results.([]interface{}), nil } @@ -220,8 +214,8 @@ func (r repository) CountStrings(ctx context.Context, id string, params["sha256"] = id query := r.db.N1QLQuery[dbcontext.CountStrings] - if queryString == "" { - query = r.db.N1QLQuery[dbcontext.CountStringsWithSubstring] + params["term"] = "%" + if queryString != "" { params["term"] = "%" + queryString + "%" } err := r.db.Count(ctx, query, params, &count) @@ -237,18 +231,15 @@ func (r repository) Strings(ctx context.Context, id, queryString string, offset, params["offset"] = offset params["limit"] = limit params["sha256"] = id + params["term"] = "%" query := r.db.N1QLQuery[dbcontext.FileStrings] if queryString != "" { - query = r.db.N1QLQuery[dbcontext.FileStringsWithSubstring] params["term"] = "%" + queryString + "%" } err := r.db.Query(ctx, query, params, &results) if err != nil { return nil, err } - if len(results.([]interface{})) == 0 { - return results, nil - } return results.([]interface{})[0], nil }