Skip to content

Commit 16b3a13

Browse files
JoshVanLkgal-akl
authored andcommitted
State: KeysLike (dapr#4087)
Signed-off-by: joshvanl <me@joshvanl.dev> Signed-off-by: Kobbi Gal <kobbi.g@akeyless.io>
1 parent 09c3044 commit 16b3a13

File tree

4 files changed

+99
-16
lines changed

4 files changed

+99
-16
lines changed

state/oracledatabase/oracledatabaseaccess.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,80 @@ func tableExists(db *sql.DB, tableName string) (bool, error) {
527527
}
528528
return true, nil
529529
}
530+
531+
func (o *oracleDatabaseAccess) KeysLike(ctx context.Context, req state.KeysLikeRequest) (*state.KeysLikeResponse, error) {
532+
if o.db == nil {
533+
return nil, errors.New("oracle db not initialized")
534+
}
535+
536+
table := o.metadata.TableName
537+
538+
baseWhere := " WHERE key LIKE :pat ESCAPE '\\' AND (expiration_time IS NULL OR expiration_time > SYSTIMESTAMP) "
539+
540+
args := []any{req.Pattern}
541+
542+
seek := ""
543+
if req.ContinuationToken != nil && *req.ContinuationToken != "" {
544+
seek = " AND key > :token "
545+
args = append(args, *req.ContinuationToken)
546+
}
547+
548+
orderBy := " ORDER BY key ASC "
549+
550+
var query string
551+
var pageSize uint32
552+
553+
if req.PageSize != nil && *req.PageSize > 0 {
554+
pageSize = *req.PageSize
555+
take := int64(pageSize + 1)
556+
557+
query = fmt.Sprintf(`
558+
SELECT key FROM (
559+
SELECT key
560+
FROM %s
561+
%s%s%s
562+
)
563+
WHERE ROWNUM <= :take
564+
`, table, baseWhere, seek, orderBy)
565+
566+
args = append(args, take)
567+
} else {
568+
query = fmt.Sprintf(`
569+
SELECT key
570+
FROM %s
571+
%s%s%s
572+
`, table, baseWhere, seek, orderBy)
573+
}
574+
575+
rows, err := o.db.QueryContext(ctx, query, args...)
576+
if err != nil {
577+
return nil, err
578+
}
579+
defer rows.Close()
580+
581+
keys := make([]string, 0, 256)
582+
for rows.Next() {
583+
var k string
584+
if err := rows.Scan(&k); err != nil {
585+
return nil, err
586+
}
587+
keys = append(keys, k)
588+
}
589+
if err := rows.Err(); err != nil {
590+
return nil, err
591+
}
592+
593+
resp := &state.KeysLikeResponse{
594+
Keys: make([]string, 0, len(keys)),
595+
}
596+
597+
//nolint:gosec
598+
if pageSize > 0 && uint32(len(keys)) > pageSize {
599+
next := keys[pageSize]
600+
resp.ContinuationToken = &next
601+
keys = keys[:pageSize]
602+
}
603+
604+
resp.Keys = append(resp.Keys, keys...)
605+
return resp, nil
606+
}

state/postgresql/v2/postgresql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ CREATE INDEX ON %[1]s (expires_at);
251251
}
252252
}
253253

254-
// 2) Backfill NULLs deterministically (created_at, key)
254+
// 2) Backfill NULLs deterministically (insertdate, key)
255255
var nulls int64
256256
if err := p.db.QueryRow(ctx, `SELECT COUNT(*) FROM `+fqtnQI+` WHERE row_id IS NULL`).Scan(&nulls); err != nil {
257257
return fmt.Errorf("count NULL row_id: %w", err)
258258
}
259259
if nulls > 0 {
260260
if _, err := p.db.Exec(ctx, `
261261
WITH ranked AS (
262-
SELECT key, ROW_NUMBER() OVER (ORDER BY created_at ASC, key ASC) AS rn
262+
SELECT key, ROW_NUMBER() OVER (ORDER BY insertdate ASC, key ASC) AS rn
263263
FROM `+fqtnQI+`
264264
WHERE row_id IS NULL
265265
)

state/redis/redis_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,9 @@ func BenchmarkGetKeyVersion(b *testing.B) {
561561
}
562562
}
563563
}
564+
565+
func Test_KeyList(t *testing.T) {
566+
s := NewRedisStateStore(logger.NewLogger("test"))
567+
_, ok := s.(state.KeysLiker)
568+
require.True(t, ok)
569+
}

tests/config/state/tests.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
componentType: state
55
components:
66
- component: redis.v6
7-
operations: [ "transaction", "etag", "first-write", "query", "ttl", "actorStateStore" ]
7+
operations: [ "transaction", "etag", "first-write", "query", "ttl", "actorStateStore", "keyslike" ]
88
config:
99
# This component requires etags to be numeric
1010
badEtag: "9999999"
1111
- component: redis.v7
1212
# "query" is not included because redisjson hasn't been updated to Redis v7 yet
13-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
13+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
1414
config:
1515
# This component requires etags to be numeric
1616
badEtag: "9999999"
@@ -52,31 +52,31 @@ components:
5252
# This component requires etags to be hex-encoded numbers
5353
badEtag: "FFFF"
5454
- component: postgresql.v1.docker
55-
operations: [ "transaction", "etag", "first-write", "query", "ttl", "actorStateStore" ]
55+
operations: [ "transaction", "etag", "first-write", "query", "ttl", "actorStateStore", "keyslike" ]
5656
config:
5757
# This component requires etags to be numeric
5858
badEtag: "1"
5959
- component: postgresql.v1.azure
60-
operations: [ "transaction", "etag", "first-write", "query", "ttl", "actorStateStore" ]
60+
operations: [ "transaction", "etag", "first-write", "query", "ttl", "actorStateStore", "keyslike" ]
6161
config:
6262
# This component requires etags to be numeric
6363
badEtag: "1"
6464
- component: postgresql.v2.docker
65-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
65+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
6666
config:
6767
# This component requires etags to be UUIDs
6868
badEtag: "e9b9e142-74b1-4a2e-8e90-3f4ffeea2e70"
6969
- component: postgresql.v2.azure
70-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
70+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
7171
config:
7272
# This component requires etags to be UUIDs
7373
badEtag: "e9b9e142-74b1-4a2e-8e90-3f4ffeea2e70"
7474
- component: sqlite
75-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
75+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
7676
- component: mysql.mysql
77-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
77+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
7878
- component: mysql.mariadb
79-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
79+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
8080
- component: azure.tablestorage.storage
8181
operations: [ "etag", "first-write"]
8282
config:
@@ -95,28 +95,28 @@ components:
9595
# Although this component supports TTLs, the minimum TTL is 60s, which makes it not suitable for our conformance tests
9696
operations: []
9797
- component: cockroachdb.v1
98-
operations: [ "transaction", "etag", "first-write", "query", "ttl" ]
98+
operations: [ "transaction", "etag", "first-write", "query", "ttl", "keyslike" ]
9999
config:
100100
# This component requires etags to be numeric
101101
badEtag: "9999999"
102102
- component: cockroachdb.v2
103-
operations: [ "transaction", "etag", "first-write", "ttl" ]
103+
operations: [ "transaction", "etag", "first-write", "ttl", "keyslike" ]
104104
config:
105105
# This component requires etags to be UUIDs
106106
badEtag: "7b104dbd-1ae2-4772-bfa0-e29c7b89bc9b"
107107
- component: rethinkdb
108108
operations: []
109109
- component: in-memory
110-
operations: [ "transaction", "etag", "first-write", "ttl", "delete-with-prefix", "actorStateStore" ]
110+
operations: [ "transaction", "etag", "first-write", "ttl", "delete-with-prefix", "actorStateStore", "keyslike" ]
111111
- component: aws.dynamodb.docker
112112
# In the Docker variant, we do not set ttlAttributeName in the metadata, so TTLs are not enabled
113113
operations: [ "transaction", "etag", "first-write" ]
114114
- component: aws.dynamodb.terraform
115115
operations: [ "transaction", "etag", "first-write", "ttl" ]
116116
- component: etcd.v1
117-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
117+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
118118
- component: etcd.v2
119-
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore" ]
119+
operations: [ "transaction", "etag", "first-write", "ttl", "actorStateStore", "keyslike" ]
120120
- component: gcp.firestore.docker
121121
operations: []
122122
- component: gcp.firestore.cloud

0 commit comments

Comments
 (0)