Skip to content

Commit

Permalink
feat: namespace row ID sequences with owner guid
Browse files Browse the repository at this point in the history
  • Loading branch information
tauraamui committed Jul 11, 2023
1 parent d901bfd commit 041564a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions storage/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"fmt"
"strconv"
"strings"

Expand All @@ -22,7 +23,7 @@ func New(db kvs.KVDB) Store {
}

func (s Store) Save(owner kvs.UUID, value Value) error {
rowID, err := nextRowID(s.db, value.TableName(), s.pks)
rowID, err := nextRowID(s.db, owner, value.TableName(), s.pks)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,8 +169,8 @@ func (s Store) Close() (err error) {
return
}

func nextRowID(db kvs.KVDB, tableName string, pks map[string]*badger.Sequence) (uint32, error) {
seq, err := resolveSequence(db, tableName, pks)
func nextRowID(db kvs.KVDB, owner kvs.UUID, tableName string, pks map[string]*badger.Sequence) (uint32, error) {
seq, err := resolveSequence(db, fmt.Sprintf("%s.%s", owner, tableName), pks)
if err != nil {
return 0, err
}
Expand All @@ -189,15 +190,15 @@ func nextSequence(seq *badger.Sequence) (uint32, error) {
return uint32(s), nil
}

func resolveSequence(db kvs.KVDB, tableName string, pks map[string]*badger.Sequence) (*badger.Sequence, error) {
seq, ok := pks[tableName]
func resolveSequence(db kvs.KVDB, sequenceKey string, pks map[string]*badger.Sequence) (*badger.Sequence, error) {
seq, ok := pks[sequenceKey]
var err error
if !ok {
seq, err = db.GetSeq([]byte(tableName), 1)
seq, err = db.GetSeq([]byte(sequenceKey), 1)
if err != nil {
return nil, err
}
pks[tableName] = seq
pks[sequenceKey] = seq
}

return seq, nil
Expand Down

0 comments on commit 041564a

Please sign in to comment.