Skip to content

Commit

Permalink
Add support more data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
xujiajun committed Feb 26, 2019
1 parent a67ebdf commit 1d184e4
Show file tree
Hide file tree
Showing 32 changed files with 6,564 additions and 1,250 deletions.
43 changes: 7 additions & 36 deletions bptree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package nutsdb
import (
"bytes"
"errors"
"time"
)

var (
Expand Down Expand Up @@ -49,7 +48,8 @@ type (
// BPTree records root node and valid key number.
BPTree struct {
root *Node
validKeyCount int // the number of the key that not expired or deleted
ValidKeyCount int // the number of the key that not expired or deleted
idxType int
}

// Records records multi-records as result when is called Range or PrefixScan.
Expand All @@ -63,29 +63,8 @@ type (
isLeaf bool
KeysNum int
}

// Record records entry and hint.
Record struct {
H *Hint
E *Entry
}
)

// isExpired returns the record if expired or not.
func (r *Record) isExpired() bool {
return IsExpired(r.H.meta.TTL, r.H.meta.timestamp)
}

// IsExpired checks the ttl if expired or not.
func IsExpired(ttl uint32, timestamp uint64) bool {
now := time.Now().Unix()
if ttl > 0 && uint64(ttl)+timestamp > uint64(now) || ttl == Persistent {
return false
}

return true
}

// newNode returns a newly initialized Node object that implements the Node.
func newNode() *Node {
return &Node{
Expand Down Expand Up @@ -275,14 +254,6 @@ func (t *BPTree) Find(key []byte) (*Record, error) {
return leaf.pointers[i].(*Record), nil
}

// updateRecord updates the record.
func (r *Record) updateRecord(h *Hint, e *Entry) error {
r.E = e
r.H = h

return nil
}

// startNewTree returns a start new tree.
func (t *BPTree) startNewTree(key []byte, pointer *Record) error {
t.root = newLeaf()
Expand All @@ -297,22 +268,22 @@ func (t *BPTree) startNewTree(key []byte, pointer *Record) error {
// and if the key exists, update the record and the counter(if countFlag set true,it will start count).
func (t *BPTree) Insert(key []byte, e *Entry, h *Hint, countFlag bool) error {
if r, err := t.Find(key); err == nil && r != nil {
if countFlag && h.meta.Flag == DataDeleteFlag && r.H.meta.Flag != DataDeleteFlag && t.validKeyCount > 0 {
t.validKeyCount--
if countFlag && h.meta.Flag == DataDeleteFlag && r.H.meta.Flag != DataDeleteFlag && t.ValidKeyCount > 0 {
t.ValidKeyCount--
}

if countFlag && h.meta.Flag != DataDeleteFlag && r.H.meta.Flag == DataDeleteFlag {
t.validKeyCount++
t.ValidKeyCount++
}

return r.updateRecord(h, e)
return r.UpdateRecord(h, e)
}

// Initialize the Record object When key does not exist.
pointer := &Record{H: h, E: e}

// Update the validKeyCount number
t.validKeyCount++
t.ValidKeyCount++

// Check if the root node is nil or not
// if nil build a start new tree for insert.
Expand Down
4 changes: 2 additions & 2 deletions bptree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ func TestIsExpired(t *testing.T) {
},
E: nil,
}
if !record.isExpired() {
if !record.IsExpired() {
t.Error("err TestIsExpired")
}

record.H.meta.TTL = Persistent
if record.isExpired() {
if record.IsExpired() {
t.Error("err TestIsExpired")
}
}
Expand Down
15 changes: 8 additions & 7 deletions datafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (

const (
DataSuffix = ".dat"
DataEntryHeaderSize = 40
DataEntryHeaderSize = 42
)

// DataFile records about data file information.
Expand All @@ -47,23 +47,23 @@ type DataFile struct {
}

// NewDataFile returns a newly initialized DataFile object.
func NewDataFile(path string, capacity int64) (*DataFile,error) {
func NewDataFile(path string, capacity int64) (*DataFile, error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return nil,err
return nil, err
}
defer f.Close()

fileInfo, _ := os.Stat(path)
if fileInfo.Size() < capacity {
if err := f.Truncate(capacity); err != nil {
return nil,err
return nil, err
}
}

m, err := mmap.NewSharedFileMmap(f, 0, int(capacity), syscall.PROT_READ|syscall.PROT_WRITE)
if err != nil {
return nil,err
return nil, err
}

return &DataFile{
Expand All @@ -72,7 +72,7 @@ func NewDataFile(path string, capacity int64) (*DataFile,error) {
path: path,
writeOff: 0,
ActualSize: 0,
},nil
}, nil
}

// ReadAt returns entry at the given off(offset).
Expand Down Expand Up @@ -146,6 +146,7 @@ func readMetaData(buf []byte) *MetaData {
TTL: binary.LittleEndian.Uint32(buf[22:26]),
bucketSize: binary.LittleEndian.Uint32(buf[26:30]),
status: binary.LittleEndian.Uint16(buf[30:32]),
txId: binary.LittleEndian.Uint64(buf[32:40]),
ds: binary.LittleEndian.Uint16(buf[32:34]),
txId: binary.LittleEndian.Uint64(buf[34:42]),
}
}
5 changes: 5 additions & 0 deletions datafile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestDataFile_All(t *testing.T) {
t.Error("err ReadAt")
}

e, err = df.ReadAt(1025)
if err == nil && e != nil {
t.Error("err ReadAt")
}

entry := Entry{
Key: []byte("key_0001"),
Value: []byte("val_0001"),
Expand Down
Loading

0 comments on commit 1d184e4

Please sign in to comment.