Skip to content

Commit

Permalink
refactor: slimtrie: generate marshaled data for a version and add tes…
Browse files Browse the repository at this point in the history
…t of unmarshal old data
  • Loading branch information
drmingdrmer committed Jun 1, 2019
1 parent 223d221 commit 973a4b6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
54 changes: 54 additions & 0 deletions trie/slimtrie_make_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package trie

import (
"fmt"
"os"

"github.com/golang/protobuf/proto"
"github.com/openacid/slim/encode"
)

func MakeMarshaledData(fn string, keys []string) {

if len(keys) == 0 {
keys = keys50k
}

n := len(keys)
values := make([]int32, n)
for i := 0; i < n; i++ {
values[i] = int32(i)
}

st, err := NewSlimTrie(encode.I32{}, keys, values)
if err != nil {
panic(err)
}

b, err := proto.Marshal(st)
if err != nil {
panic(err)
}

fn = fmt.Sprintf(fn, st.GetVersion())
f := newFile(fn)
defer f.Close()

_, err = f.Write(b)
if err != nil {
panic(err)
}
}

func newFile(fn string) *os.File {
f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
panic(err)
}

err = f.Truncate(0)
if err != nil {
panic(err)
}
return f
}
38 changes: 38 additions & 0 deletions trie/slimtrie_marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package trie

import (
"io/ioutil"
"path/filepath"
"strings"
"testing"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -40,3 +43,38 @@ func TestSlimTrie_Unmarshal_incompatible(t *testing.T) {
ta.Equal(c.want, errors.Cause(err), "%d-th: case: %+v", i+1, c)
}
}

func TestSlimTrie_Unmarshal_old_data(t *testing.T) {

ta := require.New(t)

folder := "testdata/"
finfos, err := ioutil.ReadDir(folder)
ta.Nil(err)

for _, finfo := range finfos {

fn := finfo.Name()

if !strings.HasPrefix(fn, "slimtrie-data-") {
continue
}

path := filepath.Join(folder, fn)
b, err := ioutil.ReadFile(path)
ta.Nil(err)

st, err := NewSlimTrie(encode.I32{}, nil, nil)
ta.Nil(err)

err = proto.Unmarshal(b, st)
ta.Nil(err)

keys := keys50k
for i, key := range keys {
v, found := st.Get(key)
ta.True(found)
ta.Equal(int32(i), v)
}
}
}
7 changes: 7 additions & 0 deletions trie/testdata/makedata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/openacid/slim/trie"

func main() {
trie.MakeMarshaledData("slimtrie-data-%s", nil)
}
Binary file added trie/testdata/slimtrie-data-0.5.8
Binary file not shown.
File renamed without changes.

0 comments on commit 973a4b6

Please sign in to comment.