Improve database meta handling

This commit is contained in:
Daniel 2019-03-08 23:16:36 +01:00
parent 0e2733a342
commit 1133c78f85
5 changed files with 24 additions and 9 deletions

View file

@ -116,11 +116,6 @@ func (c *Controller) Put(r record.Record) (err error) {
} }
} }
if r.Meta() == nil {
r.SetMeta(&record.Meta{})
}
r.Meta().Update()
c.writeLock.RLock() c.writeLock.RLock()
defer c.writeLock.RUnlock() defer c.writeLock.RUnlock()

View file

@ -36,9 +36,7 @@ type Options struct {
// Apply applies options to the record metadata. // Apply applies options to the record metadata.
func (o *Options) Apply(r record.Record) { func (o *Options) Apply(r record.Record) {
if r.Meta() == nil { r.UpdateMeta()
r.SetMeta(&record.Meta{})
}
if o.AlwaysMakeSecret { if o.AlwaysMakeSecret {
r.Meta().MakeSecret() r.Meta().MakeSecret()
} }
@ -198,7 +196,7 @@ func (i *Interface) PutNew(r record.Record) error {
defer r.Unlock() defer r.Unlock()
if r.Meta() == nil { if r.Meta() == nil {
r.SetMeta(&record.Meta{}) r.CreateMeta()
} }
r.Meta().Reset() r.Meta().Reset()
i.options.Apply(r) i.options.Apply(r)

View file

@ -57,6 +57,14 @@ func (b *Base) CreateMeta() {
b.meta = &Meta{} b.meta = &Meta{}
} }
// UpdateMeta creates the metadata if it does not exist and updates it.
func (b *Base) UpdateMeta() {
if b.meta == nil {
b.meta = &Meta{}
}
b.meta.Update()
}
// SetMeta sets the metadata on the database record, it should only be called after loading the record. Use MoveTo to save the record with another key. // SetMeta sets the metadata on the database record, it should only be called after loading the record. Use MoveTo to save the record with another key.
func (b *Base) SetMeta(meta *Meta) { func (b *Base) SetMeta(meta *Meta) {
b.meta = meta b.meta = meta

View file

@ -106,3 +106,15 @@ func (m *Meta) CheckPermission(local, internal bool) (permitted bool) {
return true return true
} }
} }
// Duplicate returns a new copy of Meta.
func (m *Meta) Duplicate() *Meta {
return &Meta{
Created: m.Created,
Modified: m.Modified,
Expires: m.Expires,
Deleted: m.Deleted,
secret: m.secret,
cronjewel: m.cronjewel,
}
}

View file

@ -15,6 +15,8 @@ type Record interface {
MoveTo(key string) // test:config MoveTo(key string) // test:config
Meta() *Meta Meta() *Meta
SetMeta(meta *Meta) SetMeta(meta *Meta)
CreateMeta()
UpdateMeta()
Marshal(self Record, format uint8) ([]byte, error) Marshal(self Record, format uint8) ([]byte, error)
MarshalRecord(self Record) ([]byte, error) MarshalRecord(self Record) ([]byte, error)