Skip to content
Snippets Groups Projects
Commit bc535848 authored by ale's avatar ale
Browse files

use Gob for database blobs, to avoid loss of numerical precision on uint64 with JSON

parent c2b68b72
Branches
Tags
No related merge requests found
......@@ -4,7 +4,7 @@ import (
"bytes"
cryptorand "crypto/rand"
"encoding/binary"
"encoding/json"
"encoding/gob"
"errors"
"math/rand"
"os"
......@@ -196,7 +196,7 @@ func (db *Database) GetBookFiles(bookid BookId) ([]*File, error) {
var out []*File
for it.Seek(start); it.Valid() && bytes.Compare(it.Key(), end) < 0; it.Next() {
var filepath string
if json.Unmarshal(it.Value(), &filepath) == nil {
if gob.NewDecoder(bytes.NewReader(it.Value())).Decode(&filepath) == nil {
if file, err := db.GetFile(filepath); err == nil {
out = append(out, file)
}
......@@ -212,7 +212,7 @@ func (db *Database) Get(bucket, key []byte, obj interface{}) error {
if err != nil {
return err
}
return json.Unmarshal(data, obj)
return gob.NewDecoder(bytes.NewReader(data)).Decode(obj)
}
func (db *Database) PutBook(b *Book) error {
......@@ -237,15 +237,15 @@ func (db *Database) PutFile(f *File) error {
}
func (db *Database) Put(bucket, key []byte, obj interface{}) error {
data, err := json.Marshal(obj)
if err != nil {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(obj); err != nil {
return err
}
wo := levigo.NewWriteOptions()
defer wo.Close()
return db.leveldb.Put(wo, bktToKey(bucket, key), data)
return db.leveldb.Put(wo, bktToKey(bucket, key), buf.Bytes())
}
func (db *Database) DeleteBook(bookid BookId) error {
......@@ -303,7 +303,7 @@ func (i *DatabaseIterator) Id() BookId {
}
func (i *DatabaseIterator) Value(obj interface{}) error {
return json.Unmarshal(i.iter.Value(), obj)
return gob.NewDecoder(bytes.NewReader(i.iter.Value())).Decode(obj)
}
// Scan an entire bucket.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment