Commit 3df8a3a8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 9cbf4ac2
...@@ -147,9 +147,23 @@ out: ...@@ -147,9 +147,23 @@ out:
return &IndexSaveError{err} return &IndexSaveError{err}
} }
// XXX do we need it? func (fsi *fsIndex) SaveFile(topPos int64, path string) (err error) {
// func (fsi *fsIndex) SaveFile(topPos int64, path string) error { f, err := os.Create(path)
// } if err != nil {
return &IndexSaveError{err}
}
defer func() {
err2 := f.Close()
if err2 != nil && err == nil {
err = &IndexSaveError{err2}
}
}()
err = fsi.Save(topPos, f)
return
}
// IndexLoadError is the error type returned by index load routines // IndexLoadError is the error type returned by index load routines
type IndexLoadError struct { type IndexLoadError struct {
...@@ -159,12 +173,13 @@ type IndexLoadError struct { ...@@ -159,12 +173,13 @@ type IndexLoadError struct {
} }
func (e *IndexLoadError) Error() string { func (e *IndexLoadError) Error() string {
s := e.Filename s := "index load: "
if s != "" { if e.Filename != "" {
s += ": " s += e.Filename + ": "
}
if e.Pos != -1 {
s += "pickle @" + strconv.FormatInt(e.Pos, 10) + ": "
} }
s += "index load: "
s += "pickle @" + strconv.FormatInt(e.Pos, 10) + ": "
s += e.Err.Error() s += e.Err.Error()
return s return s
} }
...@@ -276,7 +291,22 @@ out: ...@@ -276,7 +291,22 @@ out:
return 0, nil, &IndexLoadError{IOName(r), picklePos, err} return 0, nil, &IndexLoadError{IOName(r), picklePos, err}
} }
// XXX LoadIndexFile - do we need it ? func LoadIndexFile(path string) (topPos int64, fsi *fsIndex, err error) {
f, err := os.Open(path)
if err != nil {
return 0, nil, &IndexLoadError{path, -1, err}
}
defer func() {
err2 := f.Close()
if err2 != nil && err == nil {
err = &IndexLoadError{path, -1, err}
topPos, fsi = 0, nil
}
}()
return LoadIndex(f)
}
// CountReader is an io.Reader that count total bytes read // CountReader is an io.Reader that count total bytes read
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
package fs1 package fs1
import ( import (
"io/ioutil"
"os"
"sort" "sort"
"testing" "testing"
...@@ -20,6 +22,25 @@ func (p byOid) Len() int { return len(p) } ...@@ -20,6 +22,25 @@ func (p byOid) Len() int { return len(p) }
func (p byOid) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p byOid) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p byOid) Less(i, j int) bool { return p[i].oid < p[j].oid } func (p byOid) Less(i, j int) bool { return p[i].oid < p[j].oid }
var indexTest1 = [...]indexEntry {
{0x0000000000000000, 111},
{0x0000000000000001, 222},
{0x000000000000ffff, 333},
{0x0000000000001234, 444},
{0x0000000000010002, 555},
{0x0000000000010001, 665},
{0xffffffffffffffff, 777},
{0xfffffffffffffff0, 888},
{0x8000000000000000, 999},
{0xa000000000000000, 0x7fffffffffffffff},
}
func setIndex(fsi *fsIndex, kv []indexEntry) {
for _, entry := range kv {
fsi.Set(entry.oid, entry.pos)
}
}
func TestIndexLookup(t *testing.T) { func TestIndexLookup(t *testing.T) {
// the lookup is tested in cznic.b itself // the lookup is tested in cznic.b itself
// here we only lightly exercise it // here we only lightly exercise it
...@@ -30,23 +51,10 @@ func TestIndexLookup(t *testing.T) { ...@@ -30,23 +51,10 @@ func TestIndexLookup(t *testing.T) {
} }
tt := [...]indexEntry { tt := indexTest1
{0x0000000000000000, 111},
{0x0000000000000001, 222},
{0x000000000000ffff, 333},
{0x0000000000001234, 444},
{0x0000000000010002, 555},
{0x0000000000010001, 665},
{0xffffffffffffffff, 777},
{0xfffffffffffffff0, 888},
{0x8000000000000000, 999},
{0xa000000000000000, 0x7fffffffffffffff},
}
// set // set
for _, entry := range tt { setIndex(fsi, tt[:])
fsi.Set(entry.oid, entry.pos)
}
// get // get
for _, entry := range tt { for _, entry := range tt {
...@@ -91,6 +99,31 @@ func TestIndexLookup(t *testing.T) { ...@@ -91,6 +99,31 @@ func TestIndexLookup(t *testing.T) {
} }
} }
//
func TestIndexSaveLoad(t *testing.T) { func TestIndexSaveLoad(t *testing.T) {
workdir, err := ioutil.TempDir("", "t-index")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(workdir)
topPos := int64(786)
fsi := fsIndexNew()
setIndex(fsi, indexTest1[:])
err = fsi.SaveFile(topPos, workdir + "/1.fs.index")
if err != nil {
t.Fatal(err)
}
topPos2, fsi2, err := LoadIndexFile(workdir + "/1.fs.index")
if err != nil {
t.Fatal(err)
}
if topPos2 != topPos {
t.Errorf("index load: topPos mismatch: %v ; want %v", topPos2, topPos)
}
_ = fsi2
} }
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