Skip to content

Commit

Permalink
fix redis issues (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezbaul-h committed Aug 22, 2023
1 parent 94a9fbd commit 63df83b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions storage/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (r *Redis) DeleteDocuments(ctx context.Context, collections []string, condi
builder.WriteString(fmt.Sprintf(" @id:{ %s }", escape(*condition.Id)))
}
if condition.Before != nil {
builder.WriteString(fmt.Sprintf(" @timestamp:[-inf,%d]", condition.Before.UnixMicro()))
builder.WriteString(fmt.Sprintf(" @timestamp:[-inf (%d]", condition.Before.UnixMicro()))
}
for {
// search documents
Expand Down Expand Up @@ -448,7 +448,7 @@ func (r *Redis) AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoin

func (r *Redis) GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time) ([]TimeSeriesPoint, error) {
result, err := r.client.Do(ctx, "FT.SEARCH", r.PointsTable(),
fmt.Sprintf("@name:{ %s } @timestamp:[%d (%d]", name, begin.UnixMicro(), end.UnixMicro()),
fmt.Sprintf("@name:{ %s } @timestamp:[%d (%d]", escape(name), begin.UnixMicro(), end.UnixMicro()),
"SORTBY", "timestamp").Result()
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -457,6 +457,14 @@ func (r *Redis) GetTimeSeriesPoints(ctx context.Context, name string, begin, end
if err != nil {
return nil, errors.Trace(err)
}

// Results in the JSON serializer producing an empty array instead of a
// null value. More info at:
// https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices
if points == nil {
return []TimeSeriesPoint{}, nil
}

return points, nil
}

Expand Down Expand Up @@ -547,6 +555,11 @@ func decodeCategories(s string) ([]string, error) {

// escape -:.
func escape(s string) string {
r := strings.NewReplacer("-", "\\-", ":", "\\:", ".", "\\.")
r := strings.NewReplacer(
"-", "\\-",
":", "\\:",
".", "\\.",
"/", "\\/",
)
return r.Replace(s)
}
2 changes: 1 addition & 1 deletion storage/cache/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (suite *RedisTestSuite) SetupSuite() {
func (suite *RedisTestSuite) TestEscapeCharacters() {
ts := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
ctx := context.Background()
for _, c := range []string{"-", ":", "."} {
for _, c := range []string{"-", ":", ".", "/"} {
suite.Run(c, func() {
collection := fmt.Sprintf("a%s1", c)
subset := fmt.Sprintf("b%s2", c)
Expand Down

0 comments on commit 63df83b

Please sign in to comment.