Skip to content

Commit b82cc06

Browse files
committed
manifest: move InternalKeyBounds from manifest to base package
moved InternalKeyBounds from manifest to base added two new methods SmallestTrailer and LargestTrailer
1 parent b343079 commit b82cc06

File tree

4 files changed

+79
-68
lines changed

4 files changed

+79
-68
lines changed

internal/base/internal.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"strings"
1313
"sync/atomic"
14+
"unsafe"
1415

1516
"github.com/cockroachdb/errors"
1617
"github.com/cockroachdb/pebble/internal/invariants"
@@ -681,3 +682,77 @@ func (asn *AtomicSeqNum) Add(delta SeqNum) SeqNum {
681682
func (asn *AtomicSeqNum) CompareAndSwap(old, new SeqNum) bool {
682683
return asn.value.CompareAndSwap(uint64(old), uint64(new))
683684
}
685+
686+
// InternalKeyBounds represents set of keys (smallest, largest) used for the
687+
// in-memory and on-disk partial DBs that make up a pebble DB.
688+
//
689+
// It consists of the smallest, largest keys and their respective trailers.
690+
// The keys are represented as a single string; their individual representations
691+
// are given by the userKeySeparatorIdx as:
692+
// - smallest: [0, userKeySeparatorIdx)
693+
// - largest: [userKeySeparatorIdx, len(userKeyData))
694+
//
695+
// This format allows us to save a couple of bytes that will add up
696+
// proportionally to the amount of sstables we have.
697+
type InternalKeyBounds struct {
698+
userKeyData string
699+
userKeySeparatorIdx int
700+
smallestTrailer InternalKeyTrailer
701+
largestTrailer InternalKeyTrailer
702+
}
703+
704+
func (ikr *InternalKeyBounds) SetInternalKeyBounds(smallest, largest InternalKey) {
705+
ikr.userKeyData = string(smallest.UserKey) + string(largest.UserKey)
706+
ikr.smallestTrailer = smallest.Trailer
707+
ikr.largestTrailer = largest.Trailer
708+
ikr.userKeySeparatorIdx = len(smallest.UserKey)
709+
}
710+
711+
//gcassert:inline
712+
func (ikr *InternalKeyBounds) SmallestUserKey() []byte {
713+
return unsafe.Slice(unsafe.StringData(ikr.userKeyData), ikr.userKeySeparatorIdx)
714+
}
715+
716+
//gcassert:inline
717+
func (ikr *InternalKeyBounds) Smallest() InternalKey {
718+
return InternalKey{
719+
UserKey: ikr.SmallestUserKey(),
720+
Trailer: ikr.smallestTrailer,
721+
}
722+
}
723+
724+
//gcassert:inline
725+
func (ikr *InternalKeyBounds) LargestUserKey() []byte {
726+
largestStart := unsafe.StringData(ikr.userKeyData[ikr.userKeySeparatorIdx:])
727+
return unsafe.Slice(largestStart, len(ikr.userKeyData)-ikr.userKeySeparatorIdx)
728+
}
729+
730+
//gcassert:inline
731+
func (ikr *InternalKeyBounds) Largest() InternalKey {
732+
ik := InternalKey{
733+
UserKey: ikr.LargestUserKey(),
734+
Trailer: ikr.largestTrailer,
735+
}
736+
return ik
737+
}
738+
739+
func (ikr *InternalKeyBounds) SmallestTrailer() InternalKeyTrailer {
740+
return ikr.smallestTrailer
741+
}
742+
743+
func (ikr *InternalKeyBounds) LargestTrailer() InternalKeyTrailer {
744+
return ikr.largestTrailer
745+
}
746+
747+
func (ikr *InternalKeyBounds) SetSmallest(ik InternalKey) {
748+
ikr.userKeyData = string(ik.UserKey) + string(ikr.LargestUserKey())
749+
ikr.smallestTrailer = ik.Trailer
750+
ikr.userKeySeparatorIdx = len(ik.UserKey)
751+
}
752+
753+
func (ikr *InternalKeyBounds) SetLargest(ik InternalKey) {
754+
smallestUserKey := ikr.SmallestUserKey()
755+
ikr.userKeyData = string(smallestUserKey) + string(ik.UserKey)
756+
ikr.largestTrailer = ik.Trailer
757+
ikr.userKeySeparatorIdx = len(smallestUserKey)
758+
}

internal/manifest/level_metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ func (i *LevelIterator) SeekGE(cmp Compare, userKey []byte) *TableMetadata {
609609
ik = i.iter.n.items[h].RangeKeyBounds
610610
}
611611
c := cmp(userKey, ik.LargestUserKey())
612-
if c > 0 || (c == 0 && ik.largestTrailer.IsExclusiveSentinel()) {
612+
if c > 0 || (c == 0 && ik.LargestTrailer().IsExclusiveSentinel()) {
613613
j = h + 1 // preserves INVARIANT A
614614
} else {
615615
k = h // preserves INVARIANT B

internal/manifest/table_metadata.go

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
stdcmp "cmp"
1010
"fmt"
1111
"sync/atomic"
12-
"unsafe"
1312

1413
"github.com/cockroachdb/crlib/crmath"
1514
"github.com/cockroachdb/errors"
@@ -1178,72 +1177,6 @@ func (m *TableMetadata) Largest() InternalKey {
11781177
return x.Largest()
11791178
}
11801179

1181-
// InternalKeyBounds represents set of keys (smallest, largest) used for the
1182-
// in-memory and on-disk partial DBs that make up a pebble DB.
1183-
//
1184-
// It consists of the smallest, largest keys and their respective trailers.
1185-
// The keys are represented as a single string; their individual representations
1186-
// are given by the userKeySeparatorIdx as:
1187-
// - smallest: [0, userKeySeparatorIdx)
1188-
// - largest: [userKeySeparatorIdx, len(userKeyData))
1189-
//
1190-
// This format allows us to save a couple of bytes that will add up
1191-
// proportionally to the amount of sstables we have.
1192-
type InternalKeyBounds struct {
1193-
userKeyData string
1194-
userKeySeparatorIdx int
1195-
smallestTrailer base.InternalKeyTrailer
1196-
largestTrailer base.InternalKeyTrailer
1197-
}
1198-
1199-
func (ikr *InternalKeyBounds) SetInternalKeyBounds(smallest, largest InternalKey) {
1200-
ikr.userKeyData = string(smallest.UserKey) + string(largest.UserKey)
1201-
ikr.smallestTrailer = smallest.Trailer
1202-
ikr.largestTrailer = largest.Trailer
1203-
ikr.userKeySeparatorIdx = len(smallest.UserKey)
1204-
}
1205-
1206-
//gcassert:inline
1207-
func (ikr *InternalKeyBounds) SmallestUserKey() []byte {
1208-
return unsafe.Slice(unsafe.StringData(ikr.userKeyData), ikr.userKeySeparatorIdx)
1209-
}
1210-
1211-
//gcassert:inline
1212-
func (ikr *InternalKeyBounds) Smallest() InternalKey {
1213-
return InternalKey{
1214-
UserKey: ikr.SmallestUserKey(),
1215-
Trailer: ikr.smallestTrailer,
1216-
}
1217-
}
1218-
1219-
//gcassert:inline
1220-
func (ikr *InternalKeyBounds) LargestUserKey() []byte {
1221-
largestStart := unsafe.StringData(ikr.userKeyData[ikr.userKeySeparatorIdx:])
1222-
return unsafe.Slice(largestStart, len(ikr.userKeyData)-ikr.userKeySeparatorIdx)
1223-
}
1224-
1225-
//gcassert:inline
1226-
func (ikr *InternalKeyBounds) Largest() InternalKey {
1227-
ik := InternalKey{
1228-
UserKey: ikr.LargestUserKey(),
1229-
Trailer: ikr.largestTrailer,
1230-
}
1231-
return ik
1232-
}
1233-
1234-
func (ikr *InternalKeyBounds) SetSmallest(ik InternalKey) {
1235-
ikr.userKeyData = string(ik.UserKey) + string(ikr.LargestUserKey())
1236-
ikr.smallestTrailer = ik.Trailer
1237-
ikr.userKeySeparatorIdx = len(ik.UserKey)
1238-
}
1239-
1240-
func (ikr *InternalKeyBounds) SetLargest(ik InternalKey) {
1241-
smallestUserKey := ikr.SmallestUserKey()
1242-
ikr.userKeyData = string(smallestUserKey) + string(ik.UserKey)
1243-
ikr.largestTrailer = ik.Trailer
1244-
ikr.userKeySeparatorIdx = len(smallestUserKey)
1245-
}
1246-
12471180
// TableInfo contains the common information for table related events.
12481181
type TableInfo struct {
12491182
// FileNum is the internal DB identifier for the table.

internal/manifest/version.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type Compare = base.Compare
2525
// InternalKey exports the base.InternalKey type.
2626
type InternalKey = base.InternalKey
2727

28+
// InternalKeyBounds exports the base.InternalKeyBounds type.
29+
type InternalKeyBounds = base.InternalKeyBounds
30+
2831
// KeyRange returns the narrowest UserKeyBounds that encompass the bounds of all
2932
// the TableMetadata in iters.
3033
func KeyRange(ucmp Compare, iters ...iter.Seq[*TableMetadata]) base.UserKeyBounds {

0 commit comments

Comments
 (0)