Browse Source

Cleanup

master
jdl 1 year ago
parent
commit
d1f4ca7b81
4 changed files with 15 additions and 8 deletions
  1. +4
    -1
      fsstorage/record.go
  2. +4
    -3
      fsstorage/record_test.go
  3. +1
    -2
      fsstorage/segmentcursor.go
  4. +6
    -2
      fsstorage/segmenttx.go

+ 4
- 1
fsstorage/record.go View File

@ -17,10 +17,11 @@ type Record struct {
}
// Read WALRecord from the buffer at the given offset.
func readWALRecord(buf []byte, offset int64, rec *Record) *Record {
func readWALRecord(segID int64, buf []byte, offset int64, rec *Record) *Record {
if rec == nil {
rec = &Record{}
}
rec.segID = segID
rec.offset = offset
rec.raw = buf[offset:]
rec.raw = rec.raw[:rec.size()]
@ -29,6 +30,7 @@ func readWALRecord(buf []byte, offset int64, rec *Record) *Record {
// Write WALRecord to the buffer at the given offset.
func writeWALRecord(
segID int64,
buf []byte,
offset int64,
walID int64,
@ -43,6 +45,7 @@ func writeWALRecord(
copy(raw[recOffsetData:], data)
return &Record{
segID: segID,
offset: offset,
raw: raw,
}


+ 4
- 3
fsstorage/record_test.go View File

@ -12,20 +12,21 @@ func TestRecord_readAndWrite(t *testing.T) {
offset := rand.Int63n(128)
walID := rand.Int63()
data := make([]byte, rand.Intn(128))
segID := rand.Int63()
buf := make([]byte, recOffsetData+len(data)+int(offset))
rec1 := writeWALRecord(buf, offset, walID, data)
rec1 := writeWALRecord(segID, buf, offset, walID, data)
// Expected values.
nextOffset := int64(recOffsetData+len(data)) + offset
dataLen := uint32(len(data))
size := uint32(recOffsetData + len(data))
rec2 := readWALRecord(buf, offset, nil)
rec2 := readWALRecord(segID, buf, offset, nil)
for _, rec := range []*Record{rec1, rec2} {
if rec.segID != 0 {
if rec.segID != segID {
t.Fatal(rec.segID)
}
if rec.offset != offset {


+ 1
- 2
fsstorage/segmentcursor.go View File

@ -11,8 +11,7 @@ func (cur segmentCursor) Loaded() bool {
}
func (cur segmentCursor) loadRecord(offset int64, rec *Record) *Record {
rec = readWALRecord(cur.mapped, offset, rec)
rec.segID = cur.segID
rec = readWALRecord(cur.segID, cur.mapped, offset, rec)
return rec
}


+ 6
- 2
fsstorage/segmenttx.go View File

@ -16,10 +16,14 @@ func (tx *segmentTX) Append(walID int64, data []byte) *Record {
}
tx.nextID = walID + 1
rec := writeWALRecord(tx.seg.mapped, int64(tx.insertAt), walID, data)
rec := writeWALRecord(
tx.seg.ID(),
tx.seg.mapped,
int64(tx.insertAt),
walID,
data)
tx.tailOffset = rec.offset
tx.insertAt = int(rec.nextOffset())
rec.segID = tx.seg.ID()
return rec
}


Loading…
Cancel
Save