diff --git a/christmasdb.go b/christmasdb.go new file mode 100644 index 0000000..0ed453a --- /dev/null +++ b/christmasdb.go @@ -0,0 +1,7 @@ +package christmasdb + +import "github.com/DevourTech/christmasdb/raft" + +func Launch() { + raft.StartServer() +} diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..24e32ad --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,7 @@ +package main + +import christmasdb "github.com/DevourTech/christmasdb" + +func main() { + christmasdb.Launch() +} diff --git a/go.mod b/go.mod index 09df640..930c137 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/DevourTech/devourKV +module github.com/DevourTech/christmasdb go 1.15 diff --git a/internal/bplustree/bplustree.go b/internal/bplustree/bplustree.go index 0128cc8..c7e0d52 100644 --- a/internal/bplustree/bplustree.go +++ b/internal/bplustree/bplustree.go @@ -1,13 +1,13 @@ package bplustree import ( - "github.com/DevourTech/devourKV/internal/apis" - "github.com/DevourTech/devourKV/internal/errors" + "github.com/DevourTech/christmasdb/internal/apis" + "github.com/DevourTech/christmasdb/internal/errors" ) const ( - //indexFilePath = "./db/index.db" - //recordFilePath = "./db/records.db" +//indexFilePath = "./db/index.db" +//recordFilePath = "./db/records.db" ) // Node is the structure used to represent a BPlusTree node @@ -40,8 +40,8 @@ type BPlusTree struct { t int } -// Construct creates a tree either from the file or an entirely new tree -func Construct(degree int) apis.Tree { +// New creates a tree either from the file or an entirely new tree +func New(degree int) apis.Tree { // TODO: Check if an entry exists in the file for the root. r := &Node{ keys: nil, @@ -59,7 +59,7 @@ func (tree *BPlusTree) Search(key uint64) (interface{}, error) { return tree.search(tree.root, key) } -// search searches for key in node x +// search looks for key in node x func (tree *BPlusTree) search(x *Node, key uint64) (interface{}, error) { if !x.leaf { i := 0 diff --git a/internal/bplustree/bplustree_test.go b/internal/bplustree/bplustree_test.go index 7cc4f22..57716e1 100644 --- a/internal/bplustree/bplustree_test.go +++ b/internal/bplustree/bplustree_test.go @@ -1 +1,13 @@ package bplustree + +import ( + "fmt" + "reflect" + "testing" +) + +func TestNew(t *testing.T) { + n := Node{} + typ := reflect.TypeOf(n) + fmt.Println(typ.Size()) +} diff --git a/internal/btree/btree.go b/internal/btree/btree.go deleted file mode 100644 index 3c38a25..0000000 --- a/internal/btree/btree.go +++ /dev/null @@ -1 +0,0 @@ -package btree diff --git a/internal/skiplist/skiplist.go b/internal/skiplist/skiplist.go new file mode 100644 index 0000000..fff48e8 --- /dev/null +++ b/internal/skiplist/skiplist.go @@ -0,0 +1 @@ +package skiplist diff --git a/raft/server.go b/raft/server.go new file mode 100644 index 0000000..ead93f9 --- /dev/null +++ b/raft/server.go @@ -0,0 +1,16 @@ +package raft + +import ( + "fmt" + "time" +) + +func StartServer() { + et := NewTimer() + go et.WaitTillElection() + + // TODO: Use wait group maybe + time.Sleep(10 * time.Second) + et.Stop() + fmt.Println("some timer impl") +} diff --git a/raft/timer.go b/raft/timer.go new file mode 100644 index 0000000..2ad4c37 --- /dev/null +++ b/raft/timer.go @@ -0,0 +1,44 @@ +package raft + +import ( + "fmt" + "time" +) + +type Timer interface { + WaitTillElection() + Stop() +} + +func NewTimer() Timer { + return &electionTimer{ + ticker: time.NewTicker(getRandomTimeout()), + done: make(chan bool), + } +} + +type electionTimer struct { + ticker *time.Ticker + done chan bool +} + +func (et *electionTimer) WaitTillElection() { + for { + select { + case <-et.done: + return + case t := <-et.ticker.C: + fmt.Println("Ticked at ", t) + // TODO: Invoke the TCP Client to send heartbeat to all other servers + } + } +} + +func (et *electionTimer) Stop() { + et.done <- true +} + +// TODO: Make this randomized +func getRandomTimeout() time.Duration { + return time.Second * 3 +}