Skip to content

Commit b31f3ee

Browse files
matt-rocketMatias Rasmussen
andauthored
Use single namespace and isolate keys with prefix instead. #42618 (#18)
* Use single namespace and isolate keys with prefix instead. * Make KeyPrefix configurable from .toml file. Co-authored-by: Matias Rasmussen <matias.rasmussen@vivino.com>
1 parent bc0d2d4 commit b31f3ee

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

api/rankdb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var (
7171
Aerospike struct {
7272
Hosts string
7373
Namespace string
74+
KeyPrefix string
7475
WriteBlockSize int // See http://www.aerospike.com/docs/reference/configuration#write-block-size
7576
}
7677

@@ -300,7 +301,7 @@ func newBlobstore(ctx context.Context, s string) (store blobstore.Store, closers
300301
case "Memory":
301302
store = memstore.NewMemStore()
302303
case "Aerospike":
303-
as, err := aerostore.New(config.Aerospike.Namespace, config.Aerospike.Hosts)
304+
as, err := aerostore.New(config.Aerospike.Namespace, config.Aerospike.KeyPrefix, config.Aerospike.Hosts)
304305
if err != nil {
305306
return nil, nil, err
306307
}

blobstore/aerostore/aerostore.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ type AeroStore struct {
2424
WritePolicy *as.WritePolicy
2525
BasePolicy *as.BasePolicy
2626

27-
c *as.Client
28-
ns string
27+
c *as.Client
28+
ns string
29+
keyPrefix string
2930
}
3031

3132
// New creates a storage with the supplied namespace.
3233
// Multiple hosts can be specified separated by ','.
3334
// If no port is given on hosts, port 3000 is assumed.
3435
// It is highly recommended to include a MaxSizeStore prior to this
3536
// matching maximum storage size.
36-
func New(namespace, hosts string) (*AeroStore, error) {
37+
func New(namespace, keyPrefix, hosts string) (*AeroStore, error) {
3738
var h []*as.Host
3839
for _, hwp := range parseHosts(hosts, 3000) {
3940
h = append(h, as.NewHost(hwp.Name, hwp.Port))
@@ -54,11 +55,19 @@ func New(namespace, hosts string) (*AeroStore, error) {
5455
BasePolicy: as.NewPolicy(),
5556
c: cl,
5657
ns: namespace,
58+
keyPrefix: keyPrefix,
5759
}, nil
5860
}
5961

62+
func (a *AeroStore) prefix(key string) string {
63+
if a.keyPrefix == "" {
64+
return key
65+
}
66+
return fmt.Sprintf("%s_%s", a.keyPrefix, key)
67+
}
68+
6069
func (a *AeroStore) Get(ctx context.Context, set, key string) ([]byte, error) {
61-
k, err := as.NewKey(a.ns, set, key)
70+
k, err := as.NewKey(a.ns, set, a.prefix(key))
6271
if err != nil {
6372
log.Error(ctx, err.Error())
6473
return nil, err
@@ -88,7 +97,7 @@ func (a *AeroStore) Get(ctx context.Context, set, key string) ([]byte, error) {
8897
}
8998

9099
func (a *AeroStore) Delete(ctx context.Context, set, key string) error {
91-
k, err := as.NewKey(a.ns, set, key)
100+
k, err := as.NewKey(a.ns, set, a.prefix(key))
92101
if err != nil {
93102
log.Error(ctx, err.Error())
94103
return err
@@ -101,7 +110,7 @@ func (a *AeroStore) Set(ctx context.Context, set, key string, val []byte) error
101110
if len(val) > 1<<20 {
102111
return blobstore.ErrBlobTooBig
103112
}
104-
k, err := as.NewKey(a.ns, set, key)
113+
k, err := as.NewKey(a.ns, set, a.prefix(key))
105114
if err != nil {
106115
return err
107116
}

conf/conf.test.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ BackupEvery = 10
8484
Hosts = ""
8585
# Namespace used for storage.
8686
Namespace = "api"
87+
# KeyPrefix used to isolate keys in namespaces
88+
KeyPrefix = ""
8789
# Must match write-block-size of server.
8890
# See http://www.aerospike.com/docs/reference/configuration#write-block-size
8991
WriteBlockSize = 1048576

0 commit comments

Comments
 (0)