mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Delete copy of dsd formats in database.record package
This commit is contained in:
parent
40b25a0af7
commit
462570f0c9
7 changed files with 23 additions and 34 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
|
|
||||||
"github.com/safing/portbase/database/iterator"
|
"github.com/safing/portbase/database/iterator"
|
||||||
|
"github.com/safing/portbase/formats/dsd"
|
||||||
"github.com/safing/portbase/formats/varint"
|
"github.com/safing/portbase/formats/varint"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
@ -534,9 +535,9 @@ func (api *DatabaseAPI) handlePut(opID []byte, key string, data []byte, create b
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO - staged for deletion: remove transition code
|
// TODO - staged for deletion: remove transition code
|
||||||
// if data[0] != record.JSON {
|
// if data[0] != dsd.JSON {
|
||||||
// typedData := make([]byte, len(data)+1)
|
// typedData := make([]byte, len(data)+1)
|
||||||
// typedData[0] = record.JSON
|
// typedData[0] = dsd.JSON
|
||||||
// copy(typedData[1:], data)
|
// copy(typedData[1:], data)
|
||||||
// data = typedData
|
// data = typedData
|
||||||
// }
|
// }
|
||||||
|
@ -631,13 +632,13 @@ func marshalRecord(r record.Record, withDSDIdentifier bool) ([]byte, error) {
|
||||||
defer r.Unlock()
|
defer r.Unlock()
|
||||||
|
|
||||||
// Pour record into JSON.
|
// Pour record into JSON.
|
||||||
jsonData, err := r.Marshal(r, record.JSON)
|
jsonData, err := r.Marshal(r, dsd.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove JSON identifier for manual editing.
|
// Remove JSON identifier for manual editing.
|
||||||
jsonData = bytes.TrimPrefix(jsonData, varint.Pack8(record.JSON))
|
jsonData = bytes.TrimPrefix(jsonData, varint.Pack8(dsd.JSON))
|
||||||
|
|
||||||
// Add metadata.
|
// Add metadata.
|
||||||
jsonData, err = sjson.SetBytes(jsonData, "_meta", r.Meta())
|
jsonData, err = sjson.SetBytes(jsonData, "_meta", r.Meta())
|
||||||
|
@ -653,7 +654,7 @@ func marshalRecord(r record.Record, withDSDIdentifier bool) ([]byte, error) {
|
||||||
|
|
||||||
// Add JSON identifier again.
|
// Add JSON identifier again.
|
||||||
if withDSDIdentifier {
|
if withDSDIdentifier {
|
||||||
formatID := varint.Pack8(record.JSON)
|
formatID := varint.Pack8(dsd.JSON)
|
||||||
finalData := make([]byte, 0, len(formatID)+len(jsonData))
|
finalData := make([]byte, 0, len(formatID)+len(jsonData))
|
||||||
finalData = append(finalData, formatID...)
|
finalData = append(finalData, formatID...)
|
||||||
finalData = append(finalData, jsonData...)
|
finalData = append(finalData, jsonData...)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
|
|
||||||
"github.com/safing/portbase/database/record"
|
"github.com/safing/portbase/database/record"
|
||||||
|
"github.com/safing/portbase/formats/dsd"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OptionType defines the value type of an option.
|
// OptionType defines the value type of an option.
|
||||||
|
@ -303,7 +304,7 @@ func (option *Option) export() (record.Record, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := record.NewWrapper(fmt.Sprintf("config:%s", option.Key), nil, record.JSON, data)
|
r, err := record.NewWrapper(fmt.Sprintf("config:%s", option.Key), nil, dsd.JSON, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,14 +122,14 @@ func (b *Base) MarshalRecord(self Record) ([]byte, error) {
|
||||||
c := container.New([]byte{1})
|
c := container.New([]byte{1})
|
||||||
|
|
||||||
// meta encoding
|
// meta encoding
|
||||||
metaSection, err := dsd.Dump(b.meta, GenCode)
|
metaSection, err := dsd.Dump(b.meta, dsd.GenCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.AppendAsBlock(metaSection)
|
c.AppendAsBlock(metaSection)
|
||||||
|
|
||||||
// data
|
// data
|
||||||
dataSection, err := b.Marshal(self, JSON)
|
dataSection, err := b.Marshal(self, dsd.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
package record
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/safing/portbase/formats/dsd"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reimport DSD storage types
|
|
||||||
const (
|
|
||||||
AUTO = dsd.AUTO
|
|
||||||
STRING = dsd.STRING // S
|
|
||||||
BYTES = dsd.BYTES // X
|
|
||||||
JSON = dsd.JSON // J
|
|
||||||
BSON = dsd.BSON // B
|
|
||||||
GenCode = dsd.GenCode // G
|
|
||||||
)
|
|
|
@ -432,7 +432,7 @@ func BenchmarkMetaUnserializeWithCodegen(b *testing.B) {
|
||||||
func BenchmarkMetaSerializeWithDSDJSON(b *testing.B) {
|
func BenchmarkMetaSerializeWithDSDJSON(b *testing.B) {
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_, err := dsd.Dump(testMeta, JSON)
|
_, err := dsd.Dump(testMeta, dsd.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Errorf("failed to serialize with DSD/JSON: %s", err)
|
b.Errorf("failed to serialize with DSD/JSON: %s", err)
|
||||||
return
|
return
|
||||||
|
@ -444,7 +444,7 @@ func BenchmarkMetaSerializeWithDSDJSON(b *testing.B) {
|
||||||
func BenchmarkMetaUnserializeWithDSDJSON(b *testing.B) {
|
func BenchmarkMetaUnserializeWithDSDJSON(b *testing.B) {
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
encodedData, err := dsd.Dump(testMeta, JSON)
|
encodedData, err := dsd.Dump(testMeta, dsd.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Errorf("failed to serialize with DSD/JSON: %s", err)
|
b.Errorf("failed to serialize with DSD/JSON: %s", err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -42,7 +42,7 @@ func NewRawWrapper(database, key string, data []byte) (*Wrapper, error) {
|
||||||
return nil, fmt.Errorf("could not unmarshal meta section: %s", err)
|
return nil, fmt.Errorf("could not unmarshal meta section: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var format uint8 = dsd.NONE
|
var format uint8 = dsd.RAW
|
||||||
if !newMeta.IsDeleted() {
|
if !newMeta.IsDeleted() {
|
||||||
format, n, err = varint.Unpack8(data[offset:])
|
format, n, err = varint.Unpack8(data[offset:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -89,7 +89,7 @@ func (w *Wrapper) Marshal(r Record, format uint8) ([]byte, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if format != AUTO && format != w.Format {
|
if format != dsd.AUTO && format != w.Format {
|
||||||
return nil, errors.New("could not dump model, wrapped object format mismatch")
|
return nil, errors.New("could not dump model, wrapped object format mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,14 +112,14 @@ func (w *Wrapper) MarshalRecord(r Record) ([]byte, error) {
|
||||||
c := container.New([]byte{1})
|
c := container.New([]byte{1})
|
||||||
|
|
||||||
// meta
|
// meta
|
||||||
metaSection, err := dsd.Dump(w.meta, GenCode)
|
metaSection, err := dsd.Dump(w.meta, dsd.GenCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.AppendAsBlock(metaSection)
|
c.AppendAsBlock(metaSection)
|
||||||
|
|
||||||
// data
|
// data
|
||||||
dataSection, err := w.Marshal(r, JSON)
|
dataSection, err := w.Marshal(r, dsd.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -140,7 +140,7 @@ func Unwrap(wrapped, new Record) error {
|
||||||
return fmt.Errorf("cannot unwrap %T", wrapped)
|
return fmt.Errorf("cannot unwrap %T", wrapped)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := dsd.LoadAsFormat(wrapper.Data, wrapper.Format, new)
|
err := dsd.LoadAsFormat(wrapper.Data, wrapper.Format, new)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to unwrap %T: %s", new, err)
|
return fmt.Errorf("failed to unwrap %T: %s", new, err)
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ func Unwrap(wrapped, new Record) error {
|
||||||
|
|
||||||
// GetAccessor returns an accessor for this record, if available.
|
// GetAccessor returns an accessor for this record, if available.
|
||||||
func (w *Wrapper) GetAccessor(self Record) accessor.Accessor {
|
func (w *Wrapper) GetAccessor(self Record) accessor.Accessor {
|
||||||
if w.Format == JSON && len(w.Data) > 0 {
|
if w.Format == dsd.JSON && len(w.Data) > 0 {
|
||||||
return accessor.NewJSONBytesAccessor(&w.Data)
|
return accessor.NewJSONBytesAccessor(&w.Data)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -3,6 +3,8 @@ package record
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/formats/dsd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWrapper(t *testing.T) {
|
func TestWrapper(t *testing.T) {
|
||||||
|
@ -18,18 +20,18 @@ func TestWrapper(t *testing.T) {
|
||||||
encodedTestData := []byte(`J{"a": "b"}`)
|
encodedTestData := []byte(`J{"a": "b"}`)
|
||||||
|
|
||||||
// test wrapper
|
// test wrapper
|
||||||
wrapper, err := NewWrapper("test:a", &Meta{}, JSON, testData)
|
wrapper, err := NewWrapper("test:a", &Meta{}, dsd.JSON, testData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if wrapper.Format != JSON {
|
if wrapper.Format != dsd.JSON {
|
||||||
t.Error("format mismatch")
|
t.Error("format mismatch")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(testData, wrapper.Data) {
|
if !bytes.Equal(testData, wrapper.Data) {
|
||||||
t.Error("data mismatch")
|
t.Error("data mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
encoded, err := wrapper.Marshal(wrapper, JSON)
|
encoded, err := wrapper.Marshal(wrapper, dsd.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue