This commit was merged in pull request #1.
This commit is contained in:
2023-10-16 08:50:19 +00:00
parent ff8b87d6ea
commit 526196ef9d
50 changed files with 319 additions and 357 deletions

View File

@@ -3,6 +3,7 @@ package change
import (
"encoding/binary"
"io"
"git.crumpington.com/public/jldb/lib/errs"
)

View File

@@ -2,6 +2,7 @@ package change
import (
"io"
"git.crumpington.com/public/jldb/lib/errs"
)

View File

@@ -5,9 +5,10 @@ import (
"encoding/json"
"errors"
"hash/crc64"
"git.crumpington.com/public/jldb/lib/errs"
"unsafe"
"git.crumpington.com/public/jldb/lib/errs"
"github.com/google/btree"
)
@@ -189,7 +190,7 @@ func (c Collection[T]) Insert(tx *Snapshot, userItem *T) error {
for i := range c.uniqueIndices {
if c.uniqueIndices[i].insertConflict(tx, item) {
return ErrDuplicate.WithCollection(c.name).WithIndex(c.uniqueIndices[i].name)
return errs.Duplicate.WithCollection(c.name).WithIndex(c.uniqueIndices[i].name)
}
}
@@ -216,12 +217,12 @@ func (c Collection[T]) Update(tx *Snapshot, userItem *T) error {
old, ok := c.ByID.get(tx, item)
if !ok {
return ErrNotFound
return errs.NotFound
}
for i := range c.uniqueIndices {
if c.uniqueIndices[i].updateConflict(tx, item) {
return ErrDuplicate.WithCollection(c.name).WithIndex(c.uniqueIndices[i].name)
return errs.Duplicate.WithCollection(c.name).WithIndex(c.uniqueIndices[i].name)
}
}
@@ -239,7 +240,7 @@ func (c Collection[T]) Upsert(tx *Snapshot, item *T) error {
if err == nil {
return nil
}
if errors.Is(err, ErrDuplicate) {
if errors.Is(err, errs.Duplicate) {
return c.Update(tx, item)
}
return err
@@ -261,7 +262,7 @@ func (c Collection[T]) getByID(tx *Snapshot, itemID uint64) (*T, bool) {
func (c Collection[T]) ensureMutable(tx *Snapshot) error {
if !tx.writable() {
return ErrReadOnly
return errs.ReadOnly
}
state := c.getState(tx)
@@ -282,7 +283,7 @@ func (c Collection[T]) insertItem(tx *Snapshot, itemID uint64, data []byte) erro
// Check for insert conflict.
for _, index := range c.uniqueIndices {
if index.insertConflict(tx, item) {
return ErrDuplicate
return errs.Duplicate
}
}
@@ -297,7 +298,7 @@ func (c Collection[T]) insertItem(tx *Snapshot, itemID uint64, data []byte) erro
func (c Collection[T]) deleteItem(tx *Snapshot, itemID uint64) error {
item, ok := c.getByID(tx, itemID)
if !ok {
return ErrNotFound
return errs.NotFound
}
tx.delete(c.collectionID, itemID)

View File

@@ -1,12 +1,13 @@
package mdb
import (
"git.crumpington.com/public/jldb/lib/errs"
"log"
"os"
"os/exec"
"testing"
"time"
"git.crumpington.com/public/jldb/lib/errs"
)
func TestCrashConsistency(t *testing.T) {

View File

@@ -1,13 +1,14 @@
package mdb
import (
"log"
"net"
"os"
"git.crumpington.com/public/jldb/lib/errs"
"git.crumpington.com/public/jldb/lib/wal"
"git.crumpington.com/public/jldb/mdb/change"
"git.crumpington.com/public/jldb/mdb/pfile"
"log"
"net"
"os"
)
func (db *Database) repSendState(conn net.Conn) error {

View File

@@ -6,6 +6,8 @@ import (
"reflect"
"strings"
"testing"
"git.crumpington.com/public/jldb/lib/errs"
)
type DBTestCase struct {
@@ -54,7 +56,7 @@ var testDBTestCases = []DBTestCase{{
Update: func(t *testing.T, db TestDB, tx *Snapshot) error {
user, ok := db.Users.ByID.Get(tx, &User{ID: 1})
if !ok {
return ErrNotFound
return errs.NotFound
}
user.Name = "Bob"
user.Email = "b@c.com"
@@ -111,7 +113,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Insert(tx, user2)
},
ExpectedUpdateError: ErrDuplicate,
ExpectedUpdateError: errs.Duplicate,
State: DBState{},
}},
@@ -131,7 +133,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Insert(tx, user2)
},
ExpectedUpdateError: ErrDuplicate,
ExpectedUpdateError: errs.Duplicate,
State: DBState{},
}},
@@ -162,7 +164,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Insert(tx, user)
},
ExpectedUpdateError: ErrDuplicate,
ExpectedUpdateError: errs.Duplicate,
State: DBState{
UsersByID: []User{{ID: 1, Name: "Alice", Email: "a@b.com"}},
@@ -197,7 +199,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Insert(tx, user)
},
ExpectedUpdateError: ErrDuplicate,
ExpectedUpdateError: errs.Duplicate,
State: DBState{
UsersByID: []User{{ID: 1, Name: "Alice", Email: "a@b.com"}},
@@ -218,7 +220,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Insert(db.Snapshot(), user)
},
ExpectedUpdateError: ErrReadOnly,
ExpectedUpdateError: errs.ReadOnly,
}},
}, {
@@ -290,7 +292,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Update(tx, user)
},
ExpectedUpdateError: ErrNotFound,
ExpectedUpdateError: errs.NotFound,
State: DBState{
UsersByID: []User{{ID: 5, Name: "Alice", Email: "a@b.com"}},
@@ -323,14 +325,14 @@ var testDBTestCases = []DBTestCase{{
Update: func(t *testing.T, db TestDB, tx *Snapshot) error {
user, ok := db.Users.ByID.Get(tx, &User{ID: 1})
if !ok {
return ErrNotFound
return errs.NotFound
}
user.Name = "Bob"
user.Email = "b@c.com"
return db.Users.Update(db.Snapshot(), user)
},
ExpectedUpdateError: ErrReadOnly,
ExpectedUpdateError: errs.ReadOnly,
State: DBState{
UsersByID: []User{{ID: 1, Name: "Alice", Email: "a@b.com"}},
@@ -451,7 +453,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Update(tx, user2)
},
ExpectedUpdateError: ErrDuplicate,
ExpectedUpdateError: errs.Duplicate,
State: DBState{},
}},
@@ -493,14 +495,14 @@ var testDBTestCases = []DBTestCase{{
Update: func(t *testing.T, db TestDB, tx *Snapshot) error {
u, ok := db.Users.ByID.Get(tx, &User{ID: 2})
if !ok {
return ErrNotFound
return errs.NotFound
}
u.Email = "a@b.com"
return db.Users.Update(tx, u)
},
ExpectedUpdateError: ErrDuplicate,
ExpectedUpdateError: errs.Duplicate,
State: DBState{
UsersByID: []User{
@@ -542,7 +544,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Delete(db.Snapshot(), 1)
},
ExpectedUpdateError: ErrReadOnly,
ExpectedUpdateError: errs.ReadOnly,
State: DBState{
UsersByID: []User{{ID: 1, Name: "Alice", Email: "a@b.com"}},
@@ -575,7 +577,7 @@ var testDBTestCases = []DBTestCase{{
return db.Users.Delete(tx, 2)
},
ExpectedUpdateError: ErrNotFound,
ExpectedUpdateError: errs.NotFound,
State: DBState{
UsersByID: []User{{ID: 1, Name: "Alice", Email: "a@b.com"}},
@@ -609,7 +611,7 @@ var testDBTestCases = []DBTestCase{{
u, ok := db.Users.ByID.Get(tx, &User{ID: 1})
if !ok {
return ErrNotFound
return errs.NotFound
}
if !reflect.DeepEqual(u, expected) {
return errors.New("Not equal (id)")
@@ -617,7 +619,7 @@ var testDBTestCases = []DBTestCase{{
u, ok = db.Users.ByEmail.Get(tx, &User{Email: "a@b.com"})
if !ok {
return ErrNotFound
return errs.NotFound
}
if !reflect.DeepEqual(u, expected) {
return errors.New("Not equal (email)")

View File

@@ -2,6 +2,7 @@ package mdb
import (
"bytes"
"git.crumpington.com/public/jldb/mdb/change"
)

View File

@@ -2,15 +2,16 @@ package mdb
import (
"fmt"
"git.crumpington.com/public/jldb/lib/errs"
"git.crumpington.com/public/jldb/lib/rep"
"git.crumpington.com/public/jldb/mdb/change"
"git.crumpington.com/public/jldb/mdb/pfile"
"net/http"
"os"
"sync"
"sync/atomic"
"time"
"git.crumpington.com/public/jldb/lib/errs"
"git.crumpington.com/public/jldb/lib/rep"
"git.crumpington.com/public/jldb/mdb/change"
"git.crumpington.com/public/jldb/mdb/pfile"
)
type Config struct {

View File

@@ -1,11 +0,0 @@
package mdb
import (
"git.crumpington.com/public/jldb/lib/errs"
)
var (
ErrNotFound = errs.NotFound
ErrReadOnly = errs.ReadOnly
ErrDuplicate = errs.Duplicate
)

View File

@@ -2,8 +2,9 @@ package pfile
import (
crand "crypto/rand"
"git.crumpington.com/public/jldb/mdb/change"
"math/rand"
"git.crumpington.com/public/jldb/mdb/change"
)
func randomChangeList() (changes []change.Change) {

View File

@@ -3,10 +3,11 @@ package pfile
import (
"bytes"
crand "crypto/rand"
"git.crumpington.com/public/jldb/lib/wal"
"git.crumpington.com/public/jldb/mdb/change"
"path/filepath"
"testing"
"git.crumpington.com/public/jldb/lib/wal"
"git.crumpington.com/public/jldb/mdb/change"
)
func newForTesting(t *testing.T) (*File, *Index) {

View File

@@ -2,8 +2,9 @@ package pfile
import (
"hash/crc32"
"git.crumpington.com/public/jldb/lib/errs"
"unsafe"
"git.crumpington.com/public/jldb/lib/errs"
)
// ----------------------------------------------------------------------------
@@ -30,7 +31,7 @@ var emptyPage = func() dataPage {
type pageHeader struct {
CRC uint32 // IEEE CRC-32 checksum.
PageType uint32 // One of the PageType* constants.
CollectionID uint64 //
CollectionID uint64 //
ItemID uint64
DataSize uint64
NextPage uint64

View File

@@ -3,9 +3,10 @@ package pfile
import (
"bytes"
crand "crypto/rand"
"git.crumpington.com/public/jldb/lib/errs"
"math/rand"
"testing"
"git.crumpington.com/public/jldb/lib/errs"
)
func randomPage(t *testing.T) dataPage {

View File

@@ -6,12 +6,13 @@ import (
"compress/gzip"
"encoding/binary"
"io"
"git.crumpington.com/public/jldb/lib/errs"
"git.crumpington.com/public/jldb/mdb/change"
"net"
"os"
"sync"
"time"
"git.crumpington.com/public/jldb/lib/errs"
"git.crumpington.com/public/jldb/mdb/change"
)
type File struct {

View File

@@ -2,6 +2,7 @@ package pfile
import (
"bytes"
"git.crumpington.com/public/jldb/lib/wal"
"git.crumpington.com/public/jldb/mdb/change"
)

View File

@@ -3,8 +3,9 @@ package mdb
import (
"bytes"
"encoding/json"
"git.crumpington.com/public/jldb/mdb/change"
"sync/atomic"
"git.crumpington.com/public/jldb/mdb/change"
)
type Snapshot struct {

View File

@@ -4,7 +4,6 @@ import (
"crypto/rand"
"errors"
"hash/crc32"
"git.crumpington.com/public/jldb/mdb"
"log"
mrand "math/rand"
"os"
@@ -13,6 +12,8 @@ import (
"sync"
"sync/atomic"
"time"
"git.crumpington.com/public/jldb/mdb"
)
type DataItem struct {