Rename model.Model to record.Record

This commit is contained in:
Daniel 2018-09-05 17:19:45 +02:00
parent 9b7365376c
commit d76bfd55dc
23 changed files with 31 additions and 619 deletions

View file

@ -1,5 +1,9 @@
package database
import (
"github.com/Safing/portbase/database/record"
)
type Controller struct {
storage
writeLock sync.RWMutex
@ -13,14 +17,14 @@ func NewController() (*Controller, error) {
// Retrieve
func (c *Controller) Exists(key string) (bool, error) {}
func (c *Controller) Get(key string) (model.Model, error) {}
func (c *Controller) Get(key string) (record.Record, error) {}
// Modify
func (c *Controller) Create(model model.Model) error {}
func (c *Controller) Create(model record.Record) error {}
// create when not exists
func (c *Controller) Update(model model.Model) error {}
func (c *Controller) Update(model record.Record) error {}
// update, create if not exists.
func (c *Controller) UpdateOrCreate(model model.Model) error {}
func (c *Controller) UpdateOrCreate(model record.Record) error {}
func (c *Controller) Delete(key string) error {}
// Partial

View file

@ -19,9 +19,9 @@ func NewInterface(opts *Options) *Interface {
}
}
func (i *Interface) Get(key string) (model.Model, error) {
func (i *Interface) Get(key string) (record.Record, error) {
controller
controller
return nil, nil
}

View file

@ -1,18 +1,18 @@
package iterator
import (
"github.com/Safing/portbase/database/model"
"github.com/Safing/portbase/database/record"
)
// Iterator defines the iterator structure.
type Iterator struct {
Next chan model.Model
Next chan record.Record
Error error
}
// New creates a new Iterator.
func New() *Iterator {
return &Iterator{
Next: make(chan model.Model, 10),
Next: make(chan record.Record, 10),
}
}

View file

@ -4,36 +4,12 @@ package database
import (
"fmt"
"strings"
"sync"
"github.com/Safing/safing-core/database/dbutils"
"github.com/Safing/safing-core/formats/dsd"
)
type Model interface {
SetKey(*datastore.Key)
GetKey() *datastore.Key
FmtKey() string
// Type() string
// DefaultNamespace() datastore.Key
// Create(string) error
// CreateInLocation(datastore.Key, string) error
// CreateObject(*datastore.Key, string, Model) error
// Save() error
// Delete() error
// CastError(interface{}, interface{}) error
}
func getTypeName(model interface{}) string {
full := fmt.Sprintf("%T", model)
return full[strings.LastIndex(full, ".")+1:]
}
func TypeAssertError(model Model, object interface{}) error {
return fmt.Errorf("database: could not assert %s to type %T (is type %T)", model.FmtKey(), model, object)
}
// Model Registration
var (

View file

@ -1,507 +0,0 @@
package model
// Code generated by colf(1); DO NOT EDIT.
// The compiler used schema file meta.colf.
import (
"encoding/binary"
"fmt"
"io"
)
var intconv = binary.BigEndian
// Colfer configuration attributes
var (
// ColferSizeMax is the upper limit for serial byte sizes.
ColferSizeMax = 16 * 1024 * 1024
)
// ColferMax signals an upper limit breach.
type ColferMax string
// Error honors the error interface.
func (m ColferMax) Error() string { return string(m) }
// ColferError signals a data mismatch as as a byte index.
type ColferError int
// Error honors the error interface.
func (i ColferError) Error() string {
return fmt.Sprintf("colfer: unknown header at byte %d", i)
}
// ColferTail signals data continuation as a byte index.
type ColferTail int
// Error honors the error interface.
func (i ColferTail) Error() string {
return fmt.Sprintf("colfer: data continuation at byte %d", i)
}
type Course struct {
Created int64
Modified int64
Expires int64
Deleted int64
Secret bool
Cronjewel bool
}
// MarshalTo encodes o as Colfer into buf and returns the number of bytes written.
// If the buffer is too small, MarshalTo will panic.
func (o *Course) MarshalTo(buf []byte) int {
var i int
if v := o.Created; v != 0 {
x := uint64(v)
if v >= 0 {
buf[i] = 0
} else {
x = ^x + 1
buf[i] = 0 | 0x80
}
i++
for n := 0; x >= 0x80 && n < 8; n++ {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
}
if v := o.Modified; v != 0 {
x := uint64(v)
if v >= 0 {
buf[i] = 1
} else {
x = ^x + 1
buf[i] = 1 | 0x80
}
i++
for n := 0; x >= 0x80 && n < 8; n++ {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
}
if v := o.Expires; v != 0 {
x := uint64(v)
if v >= 0 {
buf[i] = 2
} else {
x = ^x + 1
buf[i] = 2 | 0x80
}
i++
for n := 0; x >= 0x80 && n < 8; n++ {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
}
if v := o.Deleted; v != 0 {
x := uint64(v)
if v >= 0 {
buf[i] = 3
} else {
x = ^x + 1
buf[i] = 3 | 0x80
}
i++
for n := 0; x >= 0x80 && n < 8; n++ {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
}
if o.Secret {
buf[i] = 4
i++
}
if o.Cronjewel {
buf[i] = 5
i++
}
buf[i] = 0x7f
i++
return i
}
// MarshalLen returns the Colfer serial byte size.
// The error return option is model.ColferMax.
func (o *Course) MarshalLen() (int, error) {
l := 1
if v := o.Created; v != 0 {
l += 2
x := uint64(v)
if v < 0 {
x = ^x + 1
}
for n := 0; x >= 0x80 && n < 8; n++ {
x >>= 7
l++
}
}
if v := o.Modified; v != 0 {
l += 2
x := uint64(v)
if v < 0 {
x = ^x + 1
}
for n := 0; x >= 0x80 && n < 8; n++ {
x >>= 7
l++
}
}
if v := o.Expires; v != 0 {
l += 2
x := uint64(v)
if v < 0 {
x = ^x + 1
}
for n := 0; x >= 0x80 && n < 8; n++ {
x >>= 7
l++
}
}
if v := o.Deleted; v != 0 {
l += 2
x := uint64(v)
if v < 0 {
x = ^x + 1
}
for n := 0; x >= 0x80 && n < 8; n++ {
x >>= 7
l++
}
}
if o.Secret {
l++
}
if o.Cronjewel {
l++
}
if l > ColferSizeMax {
return l, ColferMax(fmt.Sprintf("colfer: struct model.course exceeds %d bytes", ColferSizeMax))
}
return l, nil
}
// MarshalBinary encodes o as Colfer conform encoding.BinaryMarshaler.
// The error return option is model.ColferMax.
func (o *Course) MarshalBinary() (data []byte, err error) {
l, err := o.MarshalLen()
if err != nil {
return nil, err
}
data = make([]byte, l)
o.MarshalTo(data)
return data, nil
}
// Unmarshal decodes data as Colfer and returns the number of bytes read.
// The error return options are io.EOF, model.ColferError and model.ColferMax.
func (o *Course) Unmarshal(data []byte) (int, error) {
if len(data) == 0 {
return 0, io.EOF
}
header := data[0]
i := 1
if header == 0 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Created = int64(x)
header = data[i]
i++
} else if header == 0|0x80 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Created = int64(^x + 1)
header = data[i]
i++
}
if header == 1 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Modified = int64(x)
header = data[i]
i++
} else if header == 1|0x80 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Modified = int64(^x + 1)
header = data[i]
i++
}
if header == 2 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Expires = int64(x)
header = data[i]
i++
} else if header == 2|0x80 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Expires = int64(^x + 1)
header = data[i]
i++
}
if header == 3 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Deleted = int64(x)
header = data[i]
i++
} else if header == 3|0x80 {
if i+1 >= len(data) {
i++
goto eof
}
x := uint64(data[i])
i++
if x >= 0x80 {
x &= 0x7f
for shift := uint(7); ; shift += 7 {
b := uint64(data[i])
i++
if i >= len(data) {
goto eof
}
if b < 0x80 || shift == 56 {
x |= b << shift
break
}
x |= (b & 0x7f) << shift
}
}
o.Deleted = int64(^x + 1)
header = data[i]
i++
}
if header == 4 {
if i >= len(data) {
goto eof
}
o.Secret = true
header = data[i]
i++
}
if header == 5 {
if i >= len(data) {
goto eof
}
o.Cronjewel = true
header = data[i]
i++
}
if header != 0x7f {
return 0, ColferError(i - 1)
}
if i < ColferSizeMax {
return i, nil
}
eof:
if i >= ColferSizeMax {
return 0, ColferMax(fmt.Sprintf("colfer: struct model.course size exceeds %d bytes", ColferSizeMax))
}
return 0, io.EOF
}
// UnmarshalBinary decodes data as Colfer conform encoding.BinaryUnmarshaler.
// The error return options are io.EOF, model.ColferError, model.ColferTail and model.ColferMax.
func (o *Course) UnmarshalBinary(data []byte) error {
i, err := o.Unmarshal(data)
if i < len(data) && err == nil {
return ColferTail(i)
}
return err
}

View file

@ -1,61 +0,0 @@
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
package database
import datastore "github.com/ipfs/go-datastore"
var (
// Persistent data that is fetched or gathered, entries may be deleted
Cache = datastore.NewKey("/Cache")
DNSCache = Cache.ChildString("Dns")
IntelCache = Cache.ChildString("Intel")
FileInfoCache = Cache.ChildString("FileInfo")
ProfileCache = Cache.ChildString("Profile")
IPInfoCache = Cache.ChildString("IPInfo")
CertCache = Cache.ChildString("Cert")
CARevocationInfoCache = Cache.ChildString("CARevocationInfo")
// Volatile, in-memory (recommended) namespace for storing runtime information, cleans itself
Run = datastore.NewKey("/Run")
Processes = Run.ChildString("Processes")
OrphanedConnection = Run.ChildString("OrphanedConnections")
OrphanedLink = Run.ChildString("OrphanedLinks")
Api = Run.ChildString("Api")
ApiSessions = Api.ChildString("ApiSessions")
// Namespace for current device, will be mounted into /Devices/[device]
Me = datastore.NewKey("/Me")
// Holds data of all Devices
Devices = datastore.NewKey("/Devices")
// Holds persistent data
Data = datastore.NewKey("/Data")
Profiles = Data.ChildString("Profiles")
// Holds data distributed by the System (coming from the Community and Devs)
Dist = datastore.NewKey("/Dist")
DistProfiles = Dist.ChildString("Profiles")
DistUpdates = Dist.ChildString("Updates")
// Holds data issued by company
Company = datastore.NewKey("/Company")
CompanyProfiles = Company.ChildString("Profiles")
CompanyUpdates = Company.ChildString("Updates")
// Server
// The Authority namespace is used by authoritative servers (Safing or Company) to store data (Intel, Profiles, ...) to be served to clients
Authority = datastore.NewKey("/Authority")
AthoritativeIntel = Authority.ChildString("Intel")
AthoritativeProfiles = Authority.ChildString("Profiles")
// The Staging namespace is the same as the Authority namespace, but for rolling out new things first to a selected list of clients for testing
AuthorityStaging = datastore.NewKey("/Staging")
AthoritativeStagingProfiles = AuthorityStaging.ChildString("Profiles")
// Holds data of Apps
Apps = datastore.NewKey("/Apps")
// Test & Invalid namespace
Tests = datastore.NewKey("/Tests")
Invalid = datastore.NewKey("/Invalid")
)

View file

@ -2,11 +2,11 @@ package model
import "testing"
func TestBaseModel(t *testing.T) {
func TestBaseRecord(t *testing.T) {
// check model interface compliance
var m Model
b := &TestModel{}
var m Record
b := &TestRecord{}
m = b
_ = m

View file

@ -1,7 +1,7 @@
package model
// Model provides an interface for uniformally handling database records.
type Model interface {
// Record provides an interface for uniformally handling database records.
type Record interface {
Key() string // test:config
DatabaseName() string // test
DatabaseKey() string // config

View file

@ -2,15 +2,15 @@ package model
import "sync"
type TestModel struct {
type TestRecord struct {
Base
lock sync.Mutex
}
func (tm *TestModel) Lock() {
func (tm *TestRecord) Lock() {
tm.lock.Lock()
}
func (tm *TestModel) Unlock() {
func (tm *TestRecord) Unlock() {
tm.lock.Unlock()
}

View file

@ -10,7 +10,7 @@ import (
func TestWrapper(t *testing.T) {
// check model interface compliance
var m Model
var m Record
w := &Wrapper{}
m = w
_ = m

View file

@ -7,7 +7,7 @@ import (
"github.com/dgraph-io/badger"
"github.com/Safing/portbase/database/iterator"
"github.com/Safing/portbase/database/model"
"github.com/Safing/portbase/database/record"
"github.com/Safing/portbase/database/query"
"github.com/Safing/portbase/database/storage"
)
@ -58,7 +58,7 @@ func (b *Badger) Exists(key string) (bool, error) {
}
// Get returns a database record.
func (b *Badger) Get(key string) (model.Model, error) {
func (b *Badger) Get(key string) (record.Record, error) {
var item *badger.Item
err := b.db.View(func(txn *badger.Txn) error {
@ -93,7 +93,7 @@ func (b *Badger) Get(key string) (model.Model, error) {
}
// Put stores a record in the database.
func (b *Badger) Put(m model.Model) error {
func (b *Badger) Put(m record.Record) error {
data, err := m.MarshalRecord()
if err != nil {
return err

View file

@ -2,15 +2,15 @@ package storage
import (
"github.com/Safing/portbase/database/iterator"
"github.com/Safing/portbase/database/model"
"github.com/Safing/portbase/database/record"
"github.com/Safing/portbase/database/query"
)
// Interface defines the database storage API.
type Interface interface {
Exists(key string) (bool, error)
Get(key string) (model.Model, error)
Put(m model.Model) error
Get(key string) (record.Record, error)
Put(m record.Record) error
Delete(key string) error
Query(q *query.Query) (*iterator.Iterator, error)

View file

@ -4,7 +4,7 @@ import (
"errors"
"github.com/Safing/portbase/database/iterator"
"github.com/Safing/portbase/database/model"
"github.com/Safing/portbase/database/record"
"github.com/Safing/portbase/database/query"
"github.com/Safing/portbase/database/storage"
)
@ -31,12 +31,12 @@ func (s *Sinkhole) Exists(key string) (bool, error) {
}
// Get returns a database record.
func (s *Sinkhole) Get(key string) (model.Model, error) {
func (s *Sinkhole) Get(key string) (record.Record, error) {
return nil, storage.ErrNotFound
}
// Put stores a record in the database.
func (s *Sinkhole) Put(m model.Model) error {
func (s *Sinkhole) Put(m record.Record) error {
return nil
}