Commit bc535848 authored by ale's avatar ale

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

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