json marshal and unmarshal moved under connection
This commit is contained in:
@@ -113,7 +113,7 @@ func (connection *DbConnection) ExportRaw(filename string) error {
|
||||
}
|
||||
|
||||
// TODO: Put it behind a debug feature flag
|
||||
b, err := exportJson(databasePath, connection.getEncryptionKey())
|
||||
b, err := connection.exportJson(databasePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -162,7 +162,7 @@ func (connection *DbConnection) GetObject(bucketName string, key []byte, object
|
||||
return err
|
||||
}
|
||||
|
||||
return UnmarshalObject(data, object, connection.getEncryptionKey())
|
||||
return connection.UnmarshalObject(data, object)
|
||||
}
|
||||
|
||||
func (connection *DbConnection) getEncryptionKey() string {
|
||||
@@ -180,7 +180,7 @@ func (connection *DbConnection) UpdateObject(bucketName string, key []byte, obje
|
||||
return connection.Update(func(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket([]byte(bucketName))
|
||||
|
||||
data, err := MarshalObject(object, connection.getEncryptionKey())
|
||||
data, err := connection.MarshalObject(object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -215,7 +215,7 @@ func (connection *DbConnection) DeleteAllObjects(bucketName string, matching fun
|
||||
cursor := bucket.Cursor()
|
||||
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||||
var obj interface{}
|
||||
err := UnmarshalObject(v, &obj, connection.getEncryptionKey())
|
||||
err := connection.UnmarshalObject(v, &obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func (connection *DbConnection) CreateObject(bucketName string, fn func(uint64)
|
||||
seqId, _ := bucket.NextSequence()
|
||||
id, obj := fn(seqId)
|
||||
|
||||
data, err := MarshalObject(obj, connection.getEncryptionKey())
|
||||
data, err := connection.MarshalObject(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -275,7 +275,7 @@ func (connection *DbConnection) CreateObjectWithId(bucketName string, id int, ob
|
||||
return connection.Update(func(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket([]byte(bucketName))
|
||||
|
||||
data, err := MarshalObject(obj, connection.getEncryptionKey())
|
||||
data, err := connection.MarshalObject(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -296,7 +296,7 @@ func (connection *DbConnection) CreateObjectWithSetSequence(bucketName string, i
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := MarshalObject(obj, connection.getEncryptionKey())
|
||||
data, err := connection.MarshalObject(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -313,7 +313,7 @@ func (connection *DbConnection) GetAll(bucketName string, obj interface{}, appen
|
||||
|
||||
cursor := bucket.Cursor()
|
||||
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||||
err := UnmarshalObject(v, obj, connection.getEncryptionKey())
|
||||
err := connection.UnmarshalObject(v, obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -337,7 +337,7 @@ func (connection *DbConnection) GetAllWithJsoniter(bucketName string, obj interf
|
||||
|
||||
cursor := bucket.Cursor()
|
||||
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||||
err := UnmarshalObjectWithJsoniter(v, obj, connection.getEncryptionKey())
|
||||
err := connection.UnmarshalObjectWithJsoniter(v, obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
// inspired by github.com/konoui/boltdb-exporter (which has no license)
|
||||
// but very much simplified, based on how we use boltdb
|
||||
|
||||
func exportJson(databasePath, passphrase string) ([]byte, error) {
|
||||
func (c *DbConnection) exportJson(databasePath string) ([]byte, error) {
|
||||
logrus.WithField("databasePath", databasePath).Infof("exportJson")
|
||||
|
||||
connection, err := bolt.Open(databasePath, 0600, &bolt.Options{Timeout: 1 * time.Second, ReadOnly: true})
|
||||
@@ -33,12 +33,15 @@ func exportJson(databasePath, passphrase string) ([]byte, error) {
|
||||
continue
|
||||
}
|
||||
var obj interface{}
|
||||
err := UnmarshalObject(v, &obj, passphrase)
|
||||
err := c.UnmarshalObject(v, &obj)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("Failed to unmarshal (bucket %s): %v", bucketName, string(v))
|
||||
obj = v
|
||||
}
|
||||
if bucketName == "version" {
|
||||
if string(k) == "DB_UPDATING" {
|
||||
continue
|
||||
}
|
||||
version[string(k)] = obj.(string)
|
||||
} else {
|
||||
list = append(list, obj)
|
||||
|
||||
@@ -16,26 +16,26 @@ import (
|
||||
var encryptedStringTooShort = fmt.Errorf("encrypted string too short")
|
||||
|
||||
// MarshalObject encodes an object to binary format
|
||||
func MarshalObject(object interface{}, passphrase string) ([]byte, error) {
|
||||
func (connection *DbConnection) MarshalObject(object interface{}) ([]byte, error) {
|
||||
data, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("failed marshaling object")
|
||||
return data, err
|
||||
}
|
||||
if passphrase == "" {
|
||||
if connection.getEncryptionKey() == "" {
|
||||
logrus.Infof("no encryption passphrase")
|
||||
return data, nil
|
||||
}
|
||||
return encrypt(data, passphrase)
|
||||
return encrypt(data, connection.getEncryptionKey())
|
||||
}
|
||||
|
||||
// UnmarshalObject decodes an object from binary data
|
||||
func UnmarshalObject(data []byte, object interface{}, passphrase string) error {
|
||||
func (connection *DbConnection) UnmarshalObject(data []byte, object interface{}) error {
|
||||
var err error
|
||||
if passphrase == "" {
|
||||
if connection.getEncryptionKey() == "" {
|
||||
logrus.Infof("no encryption passphrase")
|
||||
} else {
|
||||
data, err = decrypt(data, passphrase)
|
||||
data, err = decrypt(data, connection.getEncryptionKey())
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("failed decrypting object")
|
||||
}
|
||||
@@ -50,12 +50,12 @@ func UnmarshalObject(data []byte, object interface{}, passphrase string) error {
|
||||
// UnmarshalObjectWithJsoniter decodes an object from binary data
|
||||
// using the jsoniter library. It is mainly used to accelerate environment(endpoint)
|
||||
// decoding at the moment.
|
||||
func UnmarshalObjectWithJsoniter(data []byte, object interface{}, passphrase string) error {
|
||||
if passphrase == "" {
|
||||
func (connection *DbConnection) UnmarshalObjectWithJsoniter(data []byte, object interface{}) error {
|
||||
if connection.getEncryptionKey() == "" {
|
||||
logrus.Infof("no encryption passphrase")
|
||||
} else {
|
||||
var err error
|
||||
data, err = decrypt(data, passphrase)
|
||||
data, err = decrypt(data, connection.getEncryptionKey())
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("failed decrypting object")
|
||||
return err
|
||||
|
||||
@@ -63,7 +63,7 @@ func (store *Store) Open() (bool, error) {
|
||||
// if we have DBVersion in the database then ensure we flag this as NOT a new store
|
||||
version, err := store.VersionService.DBVersion()
|
||||
logrus.Infof("database version: %d", version)
|
||||
|
||||
|
||||
if err == nil {
|
||||
newStore = true
|
||||
logrus.WithField("version", version).Infof("Opened existing store")
|
||||
@@ -74,7 +74,7 @@ func (store *Store) Open() (bool, error) {
|
||||
if store.IsErrObjectNotFound(err) {
|
||||
logrus.WithError(err).Debugf("open db failed - object not found")
|
||||
} else {
|
||||
logrus.WithError(err).Debugf("open db failed - unknown")
|
||||
logrus.WithError(err).Debugf("open db failed - other")
|
||||
}
|
||||
return newStore, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user