Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve strings/ return schema when no query is given #80

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions db/count-strings-with-substring.n1ql

This file was deleted.

22 changes: 11 additions & 11 deletions db/count-strings.n1ql
Original file line number Diff line number Diff line change
@@ -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
12 changes: 0 additions & 12 deletions db/file-strings-with-substring.n1ql

This file was deleted.

49 changes: 12 additions & 37 deletions db/file-strings.n1ql
Original file line number Diff line number Diff line change
@@ -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
44 changes: 21 additions & 23 deletions internal/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 3 additions & 12 deletions internal/file/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}
Loading