Skip to content

Commit

Permalink
feat: add crc32 check
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan committed Dec 6, 2022
1 parent 1edca6c commit 8f51c92
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
14 changes: 6 additions & 8 deletions pkg/barrel/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import (
"encoding/binary"
)

const (
HEADER_SIZE = 12
)

/*
Record is a binary representation of how each record is persisted in the disk.
The first three fields have a fixed size of 4 bytes (so 4+4+4=12 bytes fixed width "Header").
The first three fields have a fixed size of 4 bytes (so 4+4+4+4=16 bytes fixed width "Header").
Key size = 4 bytes which means tha max size of key can be (2^32)-1 = ~4.29GB.
Key size = 4 bytes which means tha max size of value can be (2^32)-1 = ~4.29GB.
Value size = 4 bytes which means tha max size of value can be (2^32)-1 = ~4.29GB.
Each entry cannot exceed more than ~8.6GB as a theoretical limit.
In a practical sense, this is also constrained by the memory of the underlying VM
where this program would run.
------------------------------------------------------
| time(4) | key_size(4) | val_size(4) | key | val |
| crc(4) | time(4) | key_size(4) | val_size(4) | key | val |
------------------------------------------------------
*/
type Record struct {
Expand All @@ -29,7 +27,7 @@ type Record struct {

// Header represents the fixed width fields present at the start of every record.
type Header struct {
// TODO: Add Expiry and CRC.
Checksum uint32
Timestamp uint32
KeySize uint32
ValSize uint32
Expand Down
2 changes: 1 addition & 1 deletion pkg/barrel/keydir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Meta struct {
Timestamp int
RecordSize int
RecordPos int
FileID string
FileID int
}

type KeyDir map[string]Meta
16 changes: 13 additions & 3 deletions pkg/barrel/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package barrel
import (
"bytes"
"fmt"
"hash/crc32"
"time"
)

Expand Down Expand Up @@ -30,12 +31,21 @@ func (b *Barrel) get(k string) ([]byte, error) {
// Get the offset position in record to start reading the value from.
valPos := meta.RecordSize - int(header.ValSize)

return record[valPos:], nil
// Read the value from the record.
val := record[valPos:]

// Validate the checksum.
if crc32.ChecksumIEEE(val) != header.Checksum {
return nil, fmt.Errorf("invalid data: checksum does not match")
}

return val, nil
}

func (b *Barrel) put(k string, val []byte) error {
func (b *Barrel) put(k string, val []byte, expiry time.Time) error {
// Prepare header.
header := Header{
Checksum: crc32.ChecksumIEEE(val),
Timestamp: uint32(time.Now().Unix()),
KeySize: uint32(len(k)),
ValSize: uint32(len(val)),
Expand Down Expand Up @@ -71,7 +81,7 @@ func (b *Barrel) put(k string, val []byte) error {
Timestamp: int(record.Header.Timestamp),
RecordSize: len(buf.Bytes()),
RecordPos: offset + len(buf.Bytes()),
FileID: "TODO",
FileID: b.df.ID(),
}

// Ensure filesystem's in memory buffer is flushed to disk.
Expand Down

0 comments on commit 8f51c92

Please sign in to comment.