diff --git a/database/controller.go b/database/controller.go index 6f70177..d228d64 100644 --- a/database/controller.go +++ b/database/controller.go @@ -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() defer c.writeLock.RUnlock() diff --git a/database/interface.go b/database/interface.go index 69063bc..d67aaff 100644 --- a/database/interface.go +++ b/database/interface.go @@ -36,9 +36,7 @@ type Options struct { // Apply applies options to the record metadata. func (o *Options) Apply(r record.Record) { - if r.Meta() == nil { - r.SetMeta(&record.Meta{}) - } + r.UpdateMeta() if o.AlwaysMakeSecret { r.Meta().MakeSecret() } @@ -198,7 +196,7 @@ func (i *Interface) PutNew(r record.Record) error { defer r.Unlock() if r.Meta() == nil { - r.SetMeta(&record.Meta{}) + r.CreateMeta() } r.Meta().Reset() i.options.Apply(r) diff --git a/database/record/base.go b/database/record/base.go index 340ef37..6328437 100644 --- a/database/record/base.go +++ b/database/record/base.go @@ -57,6 +57,14 @@ func (b *Base) CreateMeta() { 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. func (b *Base) SetMeta(meta *Meta) { b.meta = meta diff --git a/database/record/meta.go b/database/record/meta.go index 33a529b..4e09c48 100644 --- a/database/record/meta.go +++ b/database/record/meta.go @@ -106,3 +106,15 @@ func (m *Meta) CheckPermission(local, internal bool) (permitted bool) { 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, + } +} diff --git a/database/record/record.go b/database/record/record.go index 68082de..3733745 100644 --- a/database/record/record.go +++ b/database/record/record.go @@ -15,6 +15,8 @@ type Record interface { MoveTo(key string) // test:config Meta() *Meta SetMeta(meta *Meta) + CreateMeta() + UpdateMeta() Marshal(self Record, format uint8) ([]byte, error) MarshalRecord(self Record) ([]byte, error)