Skip to content

Commit

Permalink
Remove base64 conversion for checking duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
ostafen committed Apr 15, 2024
1 parent bdb2c0b commit 03213dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 5 additions & 6 deletions embedded/store/immustore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"container/list"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
Expand All @@ -39,11 +38,11 @@ import (
"github.com/codenotary/immudb/embedded/appendable/singleapp"
"github.com/codenotary/immudb/embedded/cache"
"github.com/codenotary/immudb/embedded/htree"
"github.com/codenotary/immudb/embedded/logger"
"github.com/codenotary/immudb/embedded/multierr"
"github.com/codenotary/immudb/embedded/tbtree"
"github.com/codenotary/immudb/embedded/watchers"

"github.com/codenotary/immudb/embedded/logger"
"github.com/codenotary/immudb/pkg/helpers/slices"
)

var ErrIllegalArguments = embedded.ErrIllegalArguments
Expand Down Expand Up @@ -3212,11 +3211,11 @@ func (s *ImmuStore) validateEntries(entries []*EntrySpec) error {
return ErrMaxValueLenExceeded
}

b64k := base64.StdEncoding.EncodeToString(kv.Key)
if _, ok := m[b64k]; ok {
ks := slices.BytesToString(kv.Key)
if _, ok := m[ks]; ok {
return ErrDuplicatedKey
}
m[b64k] = struct{}{}
m[ks] = struct{}{}
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/helpers/slices/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package slices

import (
"unsafe"
)

// BytesToString converts bytes to a string without memory allocation.
// NOTE: The given bytes MUST NOT be modified since they share the same backing array
// with the returned string.
func BytesToString(bs []byte) string {
return *(*string)(unsafe.Pointer(&bs))
}

0 comments on commit 03213dc

Please sign in to comment.