2023-10-13 09:43:27 +00:00
|
|
|
package mdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"hash/crc64"
|
|
|
|
"unsafe"
|
|
|
|
|
2023-10-16 08:50:19 +00:00
|
|
|
"git.crumpington.com/public/jldb/lib/errs"
|
|
|
|
|
2023-10-13 09:43:27 +00:00
|
|
|
"github.com/google/btree"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Collection[T any] struct {
|
|
|
|
db *Database
|
|
|
|
name string
|
|
|
|
collectionID uint64
|
|
|
|
|
|
|
|
copy func(*T) *T
|
|
|
|
sanitize func(*T)
|
|
|
|
validate func(*T) error
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
indices []*Index[T]
|
|
|
|
uniqueIndices []*Index[T]
|
2023-10-13 09:43:27 +00:00
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
ByID *Index[T]
|
2023-10-13 09:43:27 +00:00
|
|
|
|
|
|
|
buf *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
type CollectionConfig[T any] struct {
|
|
|
|
Copy func(*T) *T
|
|
|
|
Sanitize func(*T)
|
|
|
|
Validate func(*T) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCollection[T any](db *Database, name string, conf *CollectionConfig[T]) *Collection[T] {
|
|
|
|
if conf == nil {
|
|
|
|
conf = &CollectionConfig[T]{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Copy == nil {
|
|
|
|
conf.Copy = func(from *T) *T {
|
|
|
|
to := new(T)
|
|
|
|
*to = *from
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Sanitize == nil {
|
|
|
|
conf.Sanitize = func(*T) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Validate == nil {
|
|
|
|
conf.Validate = func(*T) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Collection[T]{
|
|
|
|
db: db,
|
|
|
|
name: name,
|
|
|
|
collectionID: crc64.Checksum([]byte(name), crc64Table),
|
|
|
|
copy: conf.Copy,
|
|
|
|
sanitize: conf.Sanitize,
|
|
|
|
validate: conf.Validate,
|
2023-11-17 10:06:56 +00:00
|
|
|
indices: []*Index[T]{},
|
|
|
|
uniqueIndices: []*Index[T]{},
|
2023-10-13 09:43:27 +00:00
|
|
|
buf: &bytes.Buffer{},
|
|
|
|
}
|
|
|
|
|
|
|
|
db.addCollection(c.collectionID, c, &collectionState[T]{
|
|
|
|
Indices: []indexState[T]{},
|
|
|
|
})
|
|
|
|
|
|
|
|
c.ByID = c.addIndex(indexConfig[T]{
|
|
|
|
Name: "ByID",
|
|
|
|
Unique: true,
|
|
|
|
Compare: func(lhs, rhs *T) int {
|
|
|
|
l := c.getID(lhs)
|
|
|
|
r := c.getID(rhs)
|
|
|
|
if l < r {
|
|
|
|
return -1
|
|
|
|
} else if l > r {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) Name() string {
|
2023-10-13 09:43:27 +00:00
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexConfig[T any] struct {
|
|
|
|
Name string
|
|
|
|
Unique bool
|
|
|
|
|
|
|
|
// If an index isn't unique, an additional comparison by ID is added if
|
|
|
|
// two items are otherwise equal.
|
|
|
|
Compare func(lhs, rhs *T) int
|
|
|
|
|
|
|
|
// If not nil, indicates if a given item should be in the index.
|
|
|
|
Include func(item *T) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddIndex: Add an index to the collection.
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) addIndex(conf indexConfig[T]) *Index[T] {
|
2023-10-13 09:43:27 +00:00
|
|
|
var less func(*T, *T) bool
|
|
|
|
|
|
|
|
if conf.Unique {
|
|
|
|
less = func(lhs, rhs *T) bool {
|
|
|
|
return conf.Compare(lhs, rhs) == -1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
less = func(lhs, rhs *T) bool {
|
|
|
|
switch conf.Compare(lhs, rhs) {
|
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 1:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return c.getID(lhs) < c.getID(rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
indexState := indexState[T]{
|
|
|
|
BTree: btree.NewG(256, less),
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
index := &Index[T]{
|
2023-11-21 16:41:55 +00:00
|
|
|
db: c.db,
|
2023-10-13 09:43:27 +00:00
|
|
|
collectionID: c.collectionID,
|
|
|
|
name: conf.Name,
|
|
|
|
indexID: c.getState(c.db.Snapshot()).addIndex(indexState),
|
|
|
|
include: conf.Include,
|
|
|
|
copy: c.copy,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.indices = append(c.indices, index)
|
|
|
|
if conf.Unique {
|
|
|
|
c.uniqueIndices = append(c.uniqueIndices, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2023-11-21 16:41:55 +00:00
|
|
|
func (c *Collection[T]) Get(tx *Snapshot, id uint64) *T {
|
|
|
|
if tx == nil {
|
|
|
|
tx = c.db.Snapshot()
|
|
|
|
}
|
|
|
|
item := new(T)
|
|
|
|
c.setID(item, id)
|
|
|
|
return c.ByID.Get(tx, item)
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) Insert(tx *Snapshot, userItem *T) error {
|
2023-11-21 16:41:55 +00:00
|
|
|
if tx == nil {
|
|
|
|
return c.db.Update(func(tx *Snapshot) error {
|
|
|
|
return c.insert(tx, userItem)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.insert(tx, userItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection[T]) insert(tx *Snapshot, userItem *T) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
if err := c.ensureMutable(tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
item := c.copy(userItem)
|
|
|
|
c.sanitize(item)
|
|
|
|
|
|
|
|
if err := c.validate(item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range c.uniqueIndices {
|
|
|
|
if c.uniqueIndices[i].insertConflict(tx, item) {
|
2023-10-16 08:50:19 +00:00
|
|
|
return errs.Duplicate.WithCollection(c.name).WithIndex(c.uniqueIndices[i].name)
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.store(c.collectionID, c.getID(item), item)
|
|
|
|
|
|
|
|
for i := range c.indices {
|
|
|
|
c.indices[i].insert(tx, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) Update(tx *Snapshot, userItem *T) error {
|
2023-11-21 16:41:55 +00:00
|
|
|
if tx == nil {
|
|
|
|
return c.db.Update(func(tx *Snapshot) error {
|
|
|
|
return c.update(tx, userItem)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return c.update(tx, userItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection[T]) update(tx *Snapshot, userItem *T) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
if err := c.ensureMutable(tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
item := c.copy(userItem)
|
|
|
|
c.sanitize(item)
|
|
|
|
|
|
|
|
if err := c.validate(item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
old, ok := c.ByID.get(tx, item)
|
|
|
|
if !ok {
|
2023-10-16 08:50:19 +00:00
|
|
|
return errs.NotFound
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range c.uniqueIndices {
|
|
|
|
if c.uniqueIndices[i].updateConflict(tx, item) {
|
2023-10-16 08:50:19 +00:00
|
|
|
return errs.Duplicate.WithCollection(c.name).WithIndex(c.uniqueIndices[i].name)
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.store(c.collectionID, c.getID(item), item)
|
|
|
|
|
|
|
|
for i := range c.indices {
|
|
|
|
c.indices[i].update(tx, old, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 16:41:55 +00:00
|
|
|
func (c *Collection[T]) Upsert(tx *Snapshot, item *T) error {
|
|
|
|
if tx == nil {
|
|
|
|
return c.db.Update(func(tx *Snapshot) error {
|
|
|
|
return c.upsert(tx, item)
|
|
|
|
})
|
2023-11-20 19:23:44 +00:00
|
|
|
}
|
2023-11-21 16:41:55 +00:00
|
|
|
return c.upsert(tx, item)
|
2023-11-20 19:23:44 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 16:41:55 +00:00
|
|
|
func (c *Collection[T]) upsert(tx *Snapshot, item *T) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
err := c.Insert(tx, item)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-16 08:50:19 +00:00
|
|
|
if errors.Is(err, errs.Duplicate) {
|
2023-10-13 09:43:27 +00:00
|
|
|
return c.Update(tx, item)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 16:41:55 +00:00
|
|
|
func (c *Collection[T]) Delete(tx *Snapshot, itemID uint64) error {
|
|
|
|
if tx == nil {
|
|
|
|
return c.db.Update(func(tx *Snapshot) error {
|
|
|
|
return c.delete(tx, itemID)
|
|
|
|
})
|
2023-11-20 19:23:44 +00:00
|
|
|
}
|
2023-11-21 16:41:55 +00:00
|
|
|
return c.delete(tx, itemID)
|
2023-11-20 19:23:44 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 16:41:55 +00:00
|
|
|
func (c *Collection[T]) delete(tx *Snapshot, itemID uint64) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
if err := c.ensureMutable(tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.deleteItem(tx, itemID)
|
|
|
|
}
|
|
|
|
|
2023-11-21 17:35:37 +00:00
|
|
|
func (c *Collection[T]) Count(tx *Snapshot) int {
|
|
|
|
return c.ByID.Count(tx)
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) getByID(tx *Snapshot, itemID uint64) (*T, bool) {
|
2023-10-13 09:43:27 +00:00
|
|
|
x := new(T)
|
|
|
|
c.setID(x, itemID)
|
|
|
|
return c.ByID.get(tx, x)
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) ensureMutable(tx *Snapshot) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
if !tx.writable() {
|
2023-10-16 08:50:19 +00:00
|
|
|
return errs.ReadOnly
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state := c.getState(tx)
|
|
|
|
if state.Version != tx.version {
|
|
|
|
tx.collections[c.collectionID] = state.clone(tx.version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// For initial data loading.
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) insertItem(tx *Snapshot, itemID uint64, data []byte) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
item := new(T)
|
|
|
|
if err := json.Unmarshal(data, item); err != nil {
|
|
|
|
return errs.Encoding.WithErr(err).WithCollection(c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for insert conflict.
|
|
|
|
for _, index := range c.uniqueIndices {
|
|
|
|
if index.insertConflict(tx, item) {
|
2023-10-16 08:50:19 +00:00
|
|
|
return errs.Duplicate
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the insert.
|
|
|
|
for _, index := range c.indices {
|
|
|
|
index.insert(tx, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) deleteItem(tx *Snapshot, itemID uint64) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
item, ok := c.getByID(tx, itemID)
|
|
|
|
if !ok {
|
2023-10-16 08:50:19 +00:00
|
|
|
return errs.NotFound
|
2023-10-13 09:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.delete(c.collectionID, itemID)
|
|
|
|
|
|
|
|
for i := range c.indices {
|
|
|
|
c.indices[i].delete(tx, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsertItem inserts or updates the item with itemID and the given serialized
|
|
|
|
// form. It's called by
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) upsertItem(tx *Snapshot, itemID uint64, data []byte) error {
|
2023-10-13 09:43:27 +00:00
|
|
|
item, ok := c.getByID(tx, itemID)
|
|
|
|
if ok {
|
|
|
|
tx.delete(c.collectionID, itemID)
|
|
|
|
|
|
|
|
for i := range c.indices {
|
|
|
|
c.indices[i].delete(tx, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item = new(T)
|
|
|
|
if err := json.Unmarshal(data, item); err != nil {
|
|
|
|
return errs.Encoding.WithErr(err).WithCollection(c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the insert.
|
|
|
|
for _, index := range c.indices {
|
|
|
|
index.insert(tx, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) getID(t *T) uint64 {
|
2023-10-13 09:43:27 +00:00
|
|
|
return *((*uint64)(unsafe.Pointer(t)))
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) setID(t *T, id uint64) {
|
2023-10-13 09:43:27 +00:00
|
|
|
*((*uint64)(unsafe.Pointer(t))) = id
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:06:56 +00:00
|
|
|
func (c *Collection[T]) getState(tx *Snapshot) *collectionState[T] {
|
2023-10-13 09:43:27 +00:00
|
|
|
return tx.collections[c.collectionID].(*collectionState[T])
|
|
|
|
}
|