Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bitcask-intro.pdf
Binary file not shown.
66 changes: 66 additions & 0 deletions entity/entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package entity

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestVerifyRecordCRC(t *testing.T) {
tests := []struct {
name string
mutate func([]byte)
wantOK bool
}{
{
name: "valid_roundtrip",
mutate: func([]byte) {},
wantOK: true,
},
{
name: "flip_payload_byte",
mutate: func(b []byte) {
if len(b) > MetaSize {
b[len(b)-1] ^= 0xFF
}
},
wantOK: false,
},
{
name: "flip_crc_byte",
mutate: func(b []byte) {
b[0] ^= 0xFF
},
wantOK: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := NewEntryWithData([]byte("k"), []byte("v"))
buf := e.Encode()
tt.mutate(buf)
assert.Equal(t, tt.wantOK, VerifyRecordCRC(buf))
})
}
}

func TestNewTombstoneEntry_RoundTrip(t *testing.T) {
tests := []struct {
name string
key []byte
}{
{name: "short", key: []byte("k")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := NewTombstoneEntry(tt.key)
require.Equal(t, uint8(DeleteFlag), e.Meta.Flag)
require.Equal(t, uint32(len(tt.key)), e.Meta.KeySize)
require.Equal(t, uint32(0), e.Meta.ValueSize)
buf := e.Encode()
require.True(t, VerifyRecordCRC(buf))
require.Equal(t, int64(len(buf)), e.Size())
})
}
}
10 changes: 10 additions & 0 deletions lock_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !unix

package tiny_bitcask

import "os"

// acquireDBLock is a no-op on non-Unix platforms; ExclusiveLock does not enforce single-writer access.
func acquireDBLock(_ string, _, _ bool) (*os.File, error) {
return nil, nil
}
30 changes: 30 additions & 0 deletions lock_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build unix

package tiny_bitcask

import (
"fmt"
"os"
"path/filepath"
"syscall"
)

func acquireDBLock(dir string, readOnly, exclusive bool) (*os.File, error) {
if !exclusive {
return nil, nil
}
path := filepath.Join(dir, ".tiny-bitcask.lock")
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, err
}
how := syscall.LOCK_EX
if readOnly {
how = syscall.LOCK_SH
}
if err := syscall.Flock(int(f.Fd()), how|syscall.LOCK_NB); err != nil {
_ = f.Close()
return nil, fmt.Errorf("tiny-bitcask: database lock: %w", err)
}
return f, nil
}
Loading