Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducing issues #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 11 additions & 8 deletions collection.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package LibraDB
package main

import (
"bytes"
"encoding/binary"
)

type Collection struct {
name []byte
root pgnum
name []byte
root pgnum
counter uint64

// associated transaction
tx *tx

}

func newCollection(name []byte, root pgnum) *Collection {
Expand Down Expand Up @@ -124,6 +123,7 @@ func (c *Collection) Put(key []byte, value []byte) error {
newRoot = c.tx.writeNode(newRoot)

c.root = newRoot.pageNum
c.tx.updateCollection(c)
}

return nil
Expand Down Expand Up @@ -202,16 +202,19 @@ func (c *Collection) Remove(key []byte) error {
// If the root has no items after rebalancing, there's no need to save it because we ignore it.
if len(rootNode.items) == 0 && len(rootNode.childNodes) > 0 {
c.root = ancestors[1].pageNum
c.tx.updateCollection(c)
}

return nil
}

// getNodes returns a list of nodes based on their indexes (the breadcrumbs) from the root
// p
// / \
// a b
// / \ / \
//
// p
// / \
// a b
// / \ / \
//
// c d e f
// For [0,1,0] -> p,b,e
func (c *Collection) getNodes(indexes []int) ([]*Node, error) {
Expand Down
2 changes: 1 addition & 1 deletion collection_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"github.com/stretchr/testify/assert"
Expand Down
8 changes: 4 additions & 4 deletions const.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package LibraDB
package main

import "errors"

const (
magicNumberSize = 4
counterSize = 4
nodeHeaderSize = 3
counterSize = 4
nodeHeaderSize = 3

collectionSize = 16
pageNumSize = 8
)

var writeInsideReadTxErr = errors.New("can't perform a write operation inside a read transaction")
var writeInsideReadTxErr = errors.New("can't perform a write operation inside a read transaction")
2 changes: 1 addition & 1 deletion dal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion dal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"github.com/stretchr/testify/assert"
Expand Down
6 changes: 3 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package LibraDB
package main

import (
"os"
"sync"
)

type DB struct {
rwlock sync.RWMutex // Allows only one writer at a time
rwlock sync.RWMutex // Allows only one writer at a time
*dal
}

Expand Down Expand Up @@ -37,4 +37,4 @@ func (db *DB) ReadTx() *tx {
func (db *DB) WriteTx() *tx {
db.rwlock.Lock()
return newTx(db, true)
}
}
2 changes: 1 addition & 1 deletion db_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion freelist.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import "encoding/binary"

Expand Down
2 changes: 1 addition & 1 deletion freelist_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/amit-davidson/LibraDB
module github.com/amit-davidson/main

go 1.17

Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 h1:EFT6MH3igZK/dIVqgGbTqWVvkZ7wJ5iGN03SVtvvdd8=
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25/go.mod h1:sWkGw/wsaHtRsT9zGQ/WyJCotGWG/Anow/9hsAcBWRw=
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 h1:zm/Rb2OsnLWCY88Njoqgo4X6yt/lx3oBNWhepX0AOMU=
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975/go.mod h1:4Mct/lWCFf1jzQTTAaWtOI7sXqmG+wBeiBfT4CxoaJk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -17,5 +11,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
61 changes: 52 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
package LibraDB
package main

func LibraDB() {
import (
"bytes"
"fmt"
)

func main() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This main function tries to add 1000 collections with 200 item entries, which will fail.

path := "libra.db"
db, _ := Open(path, DefaultOptions)

tx := db.WriteTx()
name := []byte("test")
collection, _ := tx.CreateCollection(name)
for i := 0; i < 1000; i++ {
tx := db.WriteTx()
name := []byte(fmt.Sprintf("test_collection_%d", i))

collection, err := tx.CreateCollection(name)
if err != nil {
panic(fmt.Errorf("error: %s", err))
}

for j := 0; j < 200; j++ {
key, value := []byte(fmt.Sprintf("key_%d", j)), []byte(fmt.Sprintf("value_%d", j))
err = collection.Put(key, value)
if err != nil {
panic(fmt.Errorf("error: %s", err))
}
}

_ = tx.Commit()

tx = db.ReadTx()

collection, err = tx.GetCollection(name)
if err != nil {
panic(fmt.Errorf("error: %v", err))
}
if collection == nil {
panic(fmt.Errorf("no collection found: %s", name))
}

key, value := []byte("key1"), []byte("value1")
_ = collection.Put(key, value)
for j := 0; j < 200; j++ {
key, value := []byte(fmt.Sprintf("key_%d", j)), []byte(fmt.Sprintf("value_%d", j))
item, err := collection.Find(key)
if err != nil {
panic(fmt.Errorf("error: %v", err))
}
if item == nil {
panic(fmt.Errorf("item not found for key: %s in collection: %s", key, collection.name))
}
cmp := bytes.Compare(item.value, value)
if cmp != 0 {
panic(fmt.Errorf("value should be %s, found %s", item.value, value))
}
}

_ = tx.Commit()
}
_ = tx.Commit()
}
}
4 changes: 2 additions & 2 deletions meta.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package LibraDB
package main

import "encoding/binary"

const (
magicNumber uint32 = 0xD00DB00D
metaPageNum = 0
metaPageNum = 0
)

// meta is the meta page of the db
Expand Down
4 changes: 2 additions & 2 deletions meta_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -40,4 +40,4 @@ func TestMetaDeserialize(t *testing.T) {
expectedMeta.freelistPage = 4

assert.Equal(t, expectedMeta, actualMeta)
}
}
20 changes: 11 additions & 9 deletions node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"bytes"
Expand Down Expand Up @@ -226,7 +226,7 @@ func (n *Node) nodeSize() int {
// If the key isn't found, we have 2 options. If exact is true, it means we expect findKey
// to find the key, so a falsey answer. If exact is false, then findKey is used to locate where a new key should be
// inserted so the position is returned.
func (n *Node) findKey(key []byte, exact bool) (int, *Node, []int ,error) {
func (n *Node) findKey(key []byte, exact bool) (int, *Node, []int, error) {
ancestorsIndexes := []int{0} // index of root
index, node, err := findKeyHelper(n, key, exact, &ancestorsIndexes)
if err != nil {
Expand All @@ -235,7 +235,7 @@ func (n *Node) findKey(key []byte, exact bool) (int, *Node, []int ,error) {
return index, node, ancestorsIndexes, nil
}

func findKeyHelper(node *Node, key []byte, exact bool, ancestorsIndexes *[]int) (int, *Node ,error) {
func findKeyHelper(node *Node, key []byte, exact bool, ancestorsIndexes *[]int) (int, *Node, error) {
wasFound, index := node.findKeyInNode(key)
if wasFound {
return index, node, nil
Expand Down Expand Up @@ -291,17 +291,19 @@ func (n *Node) addItem(item *Item, insertionIndex int) int {
// is depicted in the graph below. If it's not a leaf node, then the children has to be moved as well as shown.
// This may leave the parent unbalanced by having too many items so rebalancing has to be checked for all the ancestors.
// The split is performed in a for loop to support splitting a node more than once. (Though in practice used only once).
// n n
// 3 3,6
// / \ ------> / | \
// a modifiedNode a modifiedNode newNode
// 1,2 4,5,6,7,8 1,2 4,5 7,8
//
// n n
// 3 3,6
// / \ ------> / | \
// a modifiedNode a modifiedNode newNode
// 1,2 4,5,6,7,8 1,2 4,5 7,8
func (n *Node) split(nodeToSplit *Node, nodeToSplitIndex int) {
// The first index where min amount of bytes to populate a page is achieved. Then add 1 so it will be split one
// index after.
splitIndex := nodeToSplit.tx.db.getSplitIndex(nodeToSplit)

middleItem := nodeToSplit.items[splitIndex]

var newNode *Node

if nodeToSplit.isLeaf() {
Expand Down Expand Up @@ -497,4 +499,4 @@ func (n *Node) merge(bNode *Node, bNodeIndex int) error {
n.writeNodes(aNode, n)
n.tx.deleteNode(bNode)
return nil
}
}
4 changes: 2 additions & 2 deletions node_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"bytes"
Expand Down Expand Up @@ -183,7 +183,7 @@ func Test_SplitAndMerge(t *testing.T) {
require.NoError(t, err)

removeTx := db.WriteTx()
collection , err = removeTx.GetCollection(collection.name)
collection, err = removeTx.GetCollection(collection.name)
require.NoError(t, err)

err = collection.Remove(val)
Expand Down
6 changes: 3 additions & 3 deletions testutils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LibraDB
package main

import (
"bytes"
Expand All @@ -17,7 +17,7 @@ const (
testMinPercentage = 0.2
testMaxPercentage = 0.55
testValSize = 255

mockNumberOfElements = 10
expectedFolderPath = "expected"
)
Expand Down Expand Up @@ -99,7 +99,7 @@ func getExpectedResultFileName(name string) string {

func getTempFileName() string {
var id = uuid.New()
return fmt.Sprintf("%s%c%s", os.TempDir(), os.PathSeparator,id)
return fmt.Sprintf("%s%c%s", os.TempDir(), os.PathSeparator, id)
}

func memset(buf []byte, count int) []byte {
Expand Down
Loading