Skip to content

Commit

Permalink
stop nesting []bytes in rdb compiler
Browse files Browse the repository at this point in the history
Summary: Just a code cleanup, no idea why we used slice of `[]byte` but it is clearly not needed anymore.

Reviewed By: deathowl

Differential Revision: D50646003

fbshipit-source-id: 77304b10525bf4b2263c67026324819ed2a98c73
  • Loading branch information
abulimov authored and facebook-github-bot committed Oct 25, 2023
1 parent 0f874e4 commit 5fc1078
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 43 deletions.
8 changes: 4 additions & 4 deletions dnsrocks/dnsdata/rdb/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (rdb *RDB) Add(key, value []byte) error {
return err
}

return rdb.db.Put(rdb.writeOptions, key, appendValues(oldData, [][]byte{value}))
return rdb.db.Put(rdb.writeOptions, key, appendValues(oldData, value))
}

// GetStats reports main memory stats from RocksDB.
Expand Down Expand Up @@ -472,7 +472,7 @@ func (batch *Batch) Add(key, value []byte) {
batch.addedPairs,
keyValues{
key: copyBytes(key),
values: [][]byte{copyBytes(value)},
values: copyBytes(value),
},
)
batch.sorted = false
Expand All @@ -486,7 +486,7 @@ func (batch *Batch) Del(key, value []byte) {
batch.deletedPairs,
keyValues{
key: copyBytes(key),
values: [][]byte{copyBytes(value)},
values: copyBytes(value),
},
)
batch.sorted = false
Expand Down Expand Up @@ -570,7 +570,7 @@ func (batch *Batch) integrate(uniqueKeys [][]byte, dbValues *[][]byte) error {
}
for ; dOffset < len(batch.deletedPairs) && bytes.Equal(batch.deletedPairs[dOffset].key, key); dOffset++ {
var err error
(*dbValues)[i], err = delValue((*dbValues)[i], batch.deletedPairs[dOffset].values[0])
(*dbValues)[i], err = delValue((*dbValues)[i], batch.deletedPairs[dOffset].values)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion dnsrocks/dnsdata/rdb/rdb_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (b *Builder) ScheduleAdd(key, value []byte) {
b.values,
keyValues{
key: copyBytes(key),
values: [][]byte{copyBytes(value)},
values: copyBytes(value),
},
)
}
Expand Down
5 changes: 1 addition & 4 deletions dnsrocks/dnsdata/rdb/rdb_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ func TestScheduleAdd(t *testing.T) {
if !bytes.Equal(key, targ.key) {
t.Errorf("expected key %v, got %v", targ.key, key)
}
if len(b.values[0].values) != 1 {
t.Fatalf("expected 1 value, got %d", len(b.values[0].values))
}
val := b.values[0].values[0]
val := b.values[0].values
if !bytes.Equal(val, targ.val) {
t.Errorf("expected value %v, got %v", targ.val, val)
}
Expand Down
44 changes: 19 additions & 25 deletions dnsrocks/dnsdata/rdb/rdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,15 +857,15 @@ func TestRDBBatchIntegrate(t *testing.T) {
added: kvList{
keyValues{
key: []byte{0, 47},
values: [][]byte{{1}},
values: []byte{1},
},
keyValues{
key: []byte{0, 52},
values: [][]byte{{2, 3}},
values: []byte{2, 3},
},
keyValues{
key: []byte{0, 52},
values: [][]byte{{4, 5, 6}},
values: []byte{4, 5, 6},
},
},
deleted: kvList{},
Expand All @@ -888,11 +888,11 @@ func TestRDBBatchIntegrate(t *testing.T) {
deleted: kvList{
keyValues{
key: []byte{0, 47},
values: [][]byte{{1}},
values: []byte{1},
},
keyValues{
key: []byte{0, 52},
values: [][]byte{{2, 3}},
values: []byte{2, 3},
},
},
uniqueKeys: [][]byte{
Expand All @@ -915,29 +915,29 @@ func TestRDBBatchIntegrate(t *testing.T) {
added: kvList{
keyValues{
key: []byte{0, 46},
values: [][]byte{{3, 2, 1}},
values: []byte{3, 2, 1},
},
keyValues{
key: []byte{0, 47},
values: [][]byte{{9, 10}},
values: []byte{9, 10},
},
keyValues{
key: []byte{0, 53},
values: [][]byte{{2, 3}},
values: []byte{2, 3},
},
keyValues{
key: []byte{0, 53},
values: [][]byte{{4, 5, 6}},
values: []byte{4, 5, 6},
},
},
deleted: kvList{
keyValues{
key: []byte{0, 47},
values: [][]byte{{2, 3}},
values: []byte{2, 3},
},
keyValues{
key: []byte{0, 52},
values: [][]byte{{9, 3}},
values: []byte{9, 3},
},
},
uniqueKeys: [][]byte{
Expand All @@ -962,29 +962,29 @@ func TestRDBBatchIntegrate(t *testing.T) {
added: kvList{
keyValues{
key: []byte{0, 46},
values: [][]byte{{3, 2, 1}},
values: []byte{3, 2, 1},
},
keyValues{
key: []byte{0, 47},
values: [][]byte{{9, 10}},
values: []byte{9, 10},
},
keyValues{
key: []byte{0, 53},
values: [][]byte{{2, 3}},
values: []byte{2, 3},
},
keyValues{
key: []byte{0, 53},
values: [][]byte{{4, 5, 6}},
values: []byte{4, 5, 6},
},
},
deleted: kvList{
keyValues{
key: []byte{0, 47},
values: [][]byte{{2, 3}},
values: []byte{2, 3},
},
keyValues{
key: []byte{0, 52},
values: [][]byte{{9, 3}},
values: []byte{9, 3},
},
},
uniqueKeys: [][]byte{
Expand Down Expand Up @@ -1035,10 +1035,7 @@ func TestBatchAdd(t *testing.T) {
if !bytes.Equal(key, targ.key) {
t.Errorf("expected key %v, got %v", targ.key, key)
}
if len(b.addedPairs[0].values) != 1 {
t.Fatalf("expected 1 value, got %d", len(b.addedPairs[0].values))
}
val := b.addedPairs[0].values[0]
val := b.addedPairs[0].values
if !bytes.Equal(val, targ.val) {
t.Errorf("expected value %v, got %v", targ.val, val)
}
Expand Down Expand Up @@ -1072,10 +1069,7 @@ func TestBatchDel(t *testing.T) {
if !bytes.Equal(key, targ.key) {
t.Errorf("expected key %v, got %v", targ.key, key)
}
if len(b.deletedPairs[0].values) != 1 {
t.Fatalf("expected 1 value, got %d", len(b.deletedPairs[0].values))
}
val := b.deletedPairs[0].values[0]
val := b.deletedPairs[0].values
if !bytes.Equal(val, targ.val) {
t.Errorf("expected value %v, got %v", targ.val, val)
}
Expand Down
14 changes: 6 additions & 8 deletions dnsrocks/dnsdata/rdb/rdb_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (

type keyValues struct {
key []byte
values [][]byte
values []byte
}

type kvList []keyValues
Expand All @@ -56,15 +56,13 @@ func (kv *kvList) Sort() {
// RFC-1035 RDLENGTH (uint16) + header requires more than 16 bytes; rounding
// this up to uint32. Potentially that wastes about 1 byte, so there is a room
// for trading off between space and computation.
func appendValues(data []byte, newVals [][]byte) []byte {
func appendValues(data []byte, newVals []byte) []byte {
var b [4]byte

for _, v := range newVals {
vlen := len(v)
binary.LittleEndian.PutUint32(b[:], uint32(vlen))
data = append(data, b[:]...)
data = append(data, v...)
}
vlen := len(newVals)
binary.LittleEndian.PutUint32(b[:], uint32(vlen))
data = append(data, b[:]...)
data = append(data, newVals...)
return data
}

Expand Down
6 changes: 5 additions & 1 deletion dnsrocks/dnsdata/rdb/rdb_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ func TestRDBappendValues(t *testing.T) {
}
for i := 0; i < len(testCases); i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
data := appendValues(testCases[i].dataBefore, testCases[i].newVals[0])
for _, nv := range testCases[i].newVals[1:] {
data = appendValues(data, nv)
}
require.Equal(
t,
testCases[i].dataAfter,
appendValues(testCases[i].dataBefore, testCases[i].newVals),
data,
)
})
}
Expand Down

0 comments on commit 5fc1078

Please sign in to comment.