From b31f3ee6d1ef6d01ca0cb6db2868647255c87a18 Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 19 Nov 2021 11:40:29 +0100 Subject: [PATCH] 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 --- api/rankdb.go | 3 ++- blobstore/aerostore/aerostore.go | 21 +++++++++++++++------ conf/conf.test.toml | 2 ++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/api/rankdb.go b/api/rankdb.go index e9c41d3..341249b 100644 --- a/api/rankdb.go +++ b/api/rankdb.go @@ -71,6 +71,7 @@ var ( Aerospike struct { Hosts string Namespace string + KeyPrefix string WriteBlockSize int // See http://www.aerospike.com/docs/reference/configuration#write-block-size } @@ -300,7 +301,7 @@ func newBlobstore(ctx context.Context, s string) (store blobstore.Store, closers case "Memory": store = memstore.NewMemStore() case "Aerospike": - as, err := aerostore.New(config.Aerospike.Namespace, config.Aerospike.Hosts) + as, err := aerostore.New(config.Aerospike.Namespace, config.Aerospike.KeyPrefix, config.Aerospike.Hosts) if err != nil { return nil, nil, err } diff --git a/blobstore/aerostore/aerostore.go b/blobstore/aerostore/aerostore.go index fa65581..17a0d53 100644 --- a/blobstore/aerostore/aerostore.go +++ b/blobstore/aerostore/aerostore.go @@ -24,8 +24,9 @@ type AeroStore struct { WritePolicy *as.WritePolicy BasePolicy *as.BasePolicy - c *as.Client - ns string + c *as.Client + ns string + keyPrefix string } // New creates a storage with the supplied namespace. @@ -33,7 +34,7 @@ type AeroStore struct { // If no port is given on hosts, port 3000 is assumed. // It is highly recommended to include a MaxSizeStore prior to this // matching maximum storage size. -func New(namespace, hosts string) (*AeroStore, error) { +func New(namespace, keyPrefix, hosts string) (*AeroStore, error) { var h []*as.Host for _, hwp := range parseHosts(hosts, 3000) { h = append(h, as.NewHost(hwp.Name, hwp.Port)) @@ -54,11 +55,19 @@ func New(namespace, hosts string) (*AeroStore, error) { BasePolicy: as.NewPolicy(), c: cl, ns: namespace, + keyPrefix: keyPrefix, }, nil } +func (a *AeroStore) prefix(key string) string { + if a.keyPrefix == "" { + return key + } + return fmt.Sprintf("%s_%s", a.keyPrefix, key) +} + func (a *AeroStore) Get(ctx context.Context, set, key string) ([]byte, error) { - k, err := as.NewKey(a.ns, set, key) + k, err := as.NewKey(a.ns, set, a.prefix(key)) if err != nil { log.Error(ctx, err.Error()) return nil, err @@ -88,7 +97,7 @@ func (a *AeroStore) Get(ctx context.Context, set, key string) ([]byte, error) { } func (a *AeroStore) Delete(ctx context.Context, set, key string) error { - k, err := as.NewKey(a.ns, set, key) + k, err := as.NewKey(a.ns, set, a.prefix(key)) if err != nil { log.Error(ctx, err.Error()) return err @@ -101,7 +110,7 @@ func (a *AeroStore) Set(ctx context.Context, set, key string, val []byte) error if len(val) > 1<<20 { return blobstore.ErrBlobTooBig } - k, err := as.NewKey(a.ns, set, key) + k, err := as.NewKey(a.ns, set, a.prefix(key)) if err != nil { return err } diff --git a/conf/conf.test.toml b/conf/conf.test.toml index 20e7d5a..eba0eb7 100644 --- a/conf/conf.test.toml +++ b/conf/conf.test.toml @@ -84,6 +84,8 @@ BackupEvery = 10 Hosts = "" # Namespace used for storage. Namespace = "api" +# KeyPrefix used to isolate keys in namespaces +KeyPrefix = "" # Must match write-block-size of server. # See http://www.aerospike.com/docs/reference/configuration#write-block-size WriteBlockSize = 1048576