Skip to content

Commit

Permalink
[remake?] sort of reorgniaze.
Browse files Browse the repository at this point in the history
  • Loading branch information
waldoweng committed Aug 11, 2019
1 parent 6a0c7e2 commit 4505e5d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 71 deletions.
71 changes: 71 additions & 0 deletions storage/bitcask.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package storage

import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"log"
Expand All @@ -18,6 +20,14 @@ const dataFileNamePattern string = "dataFile.%d.dat"
const dataFileNameMatchPattern string = "dataFile.*.dat"
const compationFileName string = "compactionFile.dat"

// HashItem struct of hash table entry
type HashItem struct {
Wal WALFile
Len int
Offset int64
Tmstamp int64
}

// BHashTable interface for bitcask hash table. it's used to find the location of
// the record in current version in wal file on disk, and to support the read
// request with only one disk seek and one disk read per request
Expand All @@ -32,6 +42,67 @@ type BHashTable interface {
}
}

// Record struct of record of the bitcask wal file
type Record struct {
Crc uint32
Tmstamp int64
Ksz int64
ValSz int64
Key []byte
Value []byte
}

// CreateTomStoneRecord create a tomb stone record for key
func CreateTomStoneRecord(key string) Record {
return Record{
Crc: 0,
Tmstamp: time.Now().UnixNano(),
Ksz: int64(len(key)),
ValSz: 0,
Key: []byte(key),
Value: []byte(""),
}
}

func (r *Record) fromBuffer(buf *bytes.Buffer) error {
err := binary.Read(buf, binary.LittleEndian, &r.Crc)
err = binary.Read(buf, binary.LittleEndian, &r.Tmstamp)
err = binary.Read(buf, binary.LittleEndian, &r.Ksz)
err = binary.Read(buf, binary.LittleEndian, &r.ValSz)

r.Key = make([]byte, r.Ksz)
r.Value = make([]byte, r.ValSz)
err = binary.Read(buf, binary.LittleEndian, &r.Key)
err = binary.Read(buf, binary.LittleEndian, &r.Value)

if err != nil {
return err
}
return nil
}

func (r *Record) toBuffer(buf *bytes.Buffer) error {
err := binary.Write(buf, binary.LittleEndian, r.Crc)
err = binary.Write(buf, binary.LittleEndian, r.Tmstamp)
err = binary.Write(buf, binary.LittleEndian, r.Ksz)
err = binary.Write(buf, binary.LittleEndian, r.ValSz)
err = binary.Write(buf, binary.LittleEndian, r.Key[0:r.Ksz])
err = binary.Write(buf, binary.LittleEndian, r.Value[0:r.ValSz])

if err != nil {
return err
}
return nil
}

func (r *Record) size() int64 {
return 28 + r.Ksz + r.ValSz
}

func (r *Record) isTomeStone() bool {
return r.ValSz == 0
}

// WALFile interface for bitcask wal file. It's used to contain the record
// on the disk to support durability and large writing throughput by appending
// new value to the end of itself without modifing the old value. when it gets
Expand Down
8 changes: 0 additions & 8 deletions storage/hash_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import (
"sync"
)

// HashItem struct of hash table entry
type HashItem struct {
Wal WALFile
Len int
Offset int64
Tmstamp int64
}

// CreateSimpleHashTable create a new hash table
func CreateSimpleHashTable() *SimpleHashTable {
return &SimpleHashTable{}
Expand Down
63 changes: 0 additions & 63 deletions storage/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,14 @@ package storage

import (
"bytes"
"encoding/binary"
"io"
"log"
"os"
"sync"
"time"

beancaskError "github.com/waldoweng/beancask/errors"
)

// Record struct of record of the bitcask wal file
type Record struct {
Crc uint32
Tmstamp int64
Ksz int64
ValSz int64
Key []byte
Value []byte
}

// CreateTomStoneRecord create a tomb stone record for key
func CreateTomStoneRecord(key string) Record {
return Record{
Crc: 0,
Tmstamp: time.Now().UnixNano(),
Ksz: int64(len(key)),
ValSz: 0,
Key: []byte(key),
Value: []byte(""),
}
}

func (r *Record) fromBuffer(buf *bytes.Buffer) error {
err := binary.Read(buf, binary.LittleEndian, &r.Crc)
err = binary.Read(buf, binary.LittleEndian, &r.Tmstamp)
err = binary.Read(buf, binary.LittleEndian, &r.Ksz)
err = binary.Read(buf, binary.LittleEndian, &r.ValSz)

r.Key = make([]byte, r.Ksz)
r.Value = make([]byte, r.ValSz)
err = binary.Read(buf, binary.LittleEndian, &r.Key)
err = binary.Read(buf, binary.LittleEndian, &r.Value)

if err != nil {
return err
}
return nil
}

func (r *Record) toBuffer(buf *bytes.Buffer) error {
err := binary.Write(buf, binary.LittleEndian, r.Crc)
err = binary.Write(buf, binary.LittleEndian, r.Tmstamp)
err = binary.Write(buf, binary.LittleEndian, r.Ksz)
err = binary.Write(buf, binary.LittleEndian, r.ValSz)
err = binary.Write(buf, binary.LittleEndian, r.Key[0:r.Ksz])
err = binary.Write(buf, binary.LittleEndian, r.Value[0:r.ValSz])

if err != nil {
return err
}
return nil
}

func (r *Record) size() int64 {
return 28 + r.Ksz + r.ValSz
}

func (r *Record) isTomeStone() bool {
return r.ValSz == 0
}

// BitCaskLogFile struct of the BitcaskLogFile
// implementation of WALFile interface
type BitCaskLogFile struct {
Expand Down

0 comments on commit 4505e5d

Please sign in to comment.