swarm/shed: replace JSONField with StructField and rlp encoding

This commit is contained in:
Janos Guljas 2018-11-15 17:24:13 +01:00
parent a919a3214d
commit ba8d0f0e50
2 changed files with 18 additions and 19 deletions

View file

@ -17,26 +17,25 @@
package shed package shed
import ( import (
"encoding/json" "github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
) )
// JSONField is a helper to store complex structure by // StructField is a helper to store complex structure by
// encoding it in JSON format. // encoding it in RLP format.
type JSONField struct { type StructField struct {
db *DB db *DB
key []byte key []byte
} }
// NewJSONField returns a new JSONField. // NewStructField returns a new StructField.
// It validates its name and type against the database schema. // It validates its name and type against the database schema.
func (db *DB) NewJSONField(name string) (f JSONField, err error) { func (db *DB) NewStructField(name string) (f StructField, err error) {
key, err := db.schemaFieldKey(name, "json") key, err := db.schemaFieldKey(name, "struct-rlp")
if err != nil { if err != nil {
return f, err return f, err
} }
return JSONField{ return StructField{
db: db, db: db,
key: key, key: key,
}, nil }, nil
@ -44,17 +43,17 @@ func (db *DB) NewJSONField(name string) (f JSONField, err error) {
// Unmarshal unmarshals data fromt he database to a provided val. // Unmarshal unmarshals data fromt he database to a provided val.
// If the data is not found leveldb.ErrNotFound is returned. // If the data is not found leveldb.ErrNotFound is returned.
func (f JSONField) Unmarshal(val interface{}) (err error) { func (f StructField) Unmarshal(val interface{}) (err error) {
b, err := f.db.Get(f.key) b, err := f.db.Get(f.key)
if err != nil { if err != nil {
return err return err
} }
return json.Unmarshal(b, val) return rlp.DecodeBytes(b, val)
} }
// Put marshals provided val and saves it to the database. // Put marshals provided val and saves it to the database.
func (f JSONField) Put(val interface{}) (err error) { func (f StructField) Put(val interface{}) (err error) {
b, err := json.Marshal(val) b, err := rlp.EncodeToBytes(val)
if err != nil { if err != nil {
return err return err
} }
@ -62,8 +61,8 @@ func (f JSONField) Put(val interface{}) (err error) {
} }
// PutInBatch marshals provided val and puts it into the batch. // PutInBatch marshals provided val and puts it into the batch.
func (f JSONField) PutInBatch(batch *leveldb.Batch, val interface{}) (err error) { func (f StructField) PutInBatch(batch *leveldb.Batch, val interface{}) (err error) {
b, err := json.Marshal(val) b, err := rlp.EncodeToBytes(val)
if err != nil { if err != nil {
return err return err
} }

View file

@ -22,13 +22,13 @@ import (
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
) )
// TestJSONField validates put and unmarshal operations // TestStructField validates put and unmarshal operations
// of the JSONField. // of the StructField.
func TestJSONField(t *testing.T) { func TestStructField(t *testing.T) {
db, cleanupFunc := newTestDB(t) db, cleanupFunc := newTestDB(t)
defer cleanupFunc() defer cleanupFunc()
complexField, err := db.NewJSONField("complex-field") complexField, err := db.NewStructField("complex-field")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }