Skip to content

Commit

Permalink
Use single namespace and isolate keys with prefix instead. #42618 (#18)
Browse files Browse the repository at this point in the history
* Use single namespace and isolate keys with prefix instead.

* Make KeyPrefix configurable from .toml file.

Co-authored-by: Matias Rasmussen <matias.rasmussen@vivino.com>
  • Loading branch information
matt-rocket and Matias Rasmussen committed Nov 19, 2021
1 parent bc0d2d4 commit b31f3ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion api/rankdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 15 additions & 6 deletions blobstore/aerostore/aerostore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ 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.
// Multiple hosts can be specified separated by ','.
// 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))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions conf/conf.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b31f3ee

Please sign in to comment.