Created
July 8, 2016 16:49
-
-
Save mdouchement/54a2081d9132367c3f43fd6fec0cfc16 to your computer and use it in GitHub Desktop.
Diskv serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/gob" | |
"fmt" | |
"github.com/peterbourgon/diskv" | |
) | |
type Storage struct { | |
disk *diskv.Diskv | |
} | |
func NewStorage() *Storage { | |
return &Storage{ | |
disk: diskv.New(diskv.Options{ | |
BasePath: "diskv-storage", | |
Compression: diskv.NewGzipCompression(), | |
Transform: func(s string) []string { return []string{} }, | |
CacheSizeMax: 1024 * 1024, | |
}), | |
} | |
} | |
func (s *Storage) Marshal(key string, v interface{}) error { | |
var buf bytes.Buffer | |
enc := gob.NewEncoder(&buf) | |
if err := enc.Encode(v); err != nil { | |
return fmt.Errorf("Marshal: %s", err.Error()) | |
} | |
if err := s.disk.Write(key, buf.Bytes()); err != nil { | |
return fmt.Errorf("Marshal: %s", err.Error()) | |
} | |
return nil | |
} | |
func (s *Storage) Unmarshal(key string, v interface{}) error { | |
serialized, err := s.disk.Read(key) | |
if err != nil { | |
return fmt.Errorf("Unmarshal: %s", err.Error()) | |
} | |
r := bytes.NewReader(serialized) | |
dec := gob.NewDecoder(r) | |
if err := dec.Decode(v); err != nil { | |
return fmt.Errorf("Unmarshal: %s", err.Error()) | |
} | |
return nil | |
} | |
// ============================================================================= | |
func main() { | |
storage := NewStorage() | |
test1(storage) | |
test2(storage) | |
test3(storage) | |
} | |
func test1(storage *Storage) { | |
input := "Testouille" | |
err := storage.Marshal("test1", input) | |
check(err) | |
var output string | |
err = storage.Unmarshal("test1", &output) | |
check(err) | |
fmt.Printf("%v\n", output) | |
} | |
func test2(storage *Storage) { | |
type DataLine struct { | |
Offset uint64 `json:"offset"` | |
Limit uint32 `json:"limit"` | |
} | |
input := DataLine{Offset: 5, Limit: 42} | |
err := storage.Marshal("test2", input) | |
check(err) | |
var output DataLine | |
err = storage.Unmarshal("test2", &output) | |
check(err) | |
fmt.Printf("%v - %#[1]v\n", output) | |
} | |
func test3(storage *Storage) { | |
type DataLine struct { | |
Offset uint64 `json:"offset"` | |
Limit uint32 `json:"limit"` | |
} | |
input := []DataLine{ | |
DataLine{Offset: 5, Limit: 42}, | |
DataLine{Offset: 5, Limit: 42}, | |
DataLine{Offset: 5, Limit: 42}, | |
} | |
err := storage.Marshal("test3", input) | |
check(err) | |
var output []DataLine | |
err = storage.Unmarshal("test3", &output) | |
check(err) | |
fmt.Printf("%v - %#[1]v\n", output) | |
} | |
func check(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment