Skip to content

Commit

Permalink
added timestamp to index file
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Franzén committed Jun 29, 2017
1 parent 3b020fb commit 52a9324
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
31 changes: 27 additions & 4 deletions hnsw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/rand"
"os"
"sync"
"time"

"github.com/Bithack/go-hnsw/bitsetpool"
"github.com/Bithack/go-hnsw/distqueue"
Expand Down Expand Up @@ -48,16 +49,19 @@ type Hnsw struct {
enterpoint uint32
}

func Load(filename string) (*Hnsw, error) {
// Load opens a index file previously written by Save(). Returnes a new index and the timestamp the file was written
func Load(filename string) (*Hnsw, int64, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
return nil, 0, err
}
z, err := gzip.NewReader(f)
if err != nil {
return nil, err
return nil, 0, err
}

timestamp := readInt64(z)

h := new(Hnsw)
h.M = readInt32(z)
h.M0 = readInt32(z)
Expand Down Expand Up @@ -102,7 +106,7 @@ func Load(filename string) (*Hnsw, error) {
z.Close()
f.Close()

return h, nil
return h, timestamp, nil
}

// Save writes to current index to a gzipped binary data file
Expand All @@ -113,6 +117,10 @@ func (h *Hnsw) Save(filename string) error {
}
z := gzip.NewWriter(f)

timestamp := time.Now().Unix()

writeInt64(timestamp, z)

writeInt32(h.M, z)
writeInt32(h.M0, z)
writeInt32(h.efConstruction, z)
Expand Down Expand Up @@ -155,6 +163,13 @@ func (h *Hnsw) Save(filename string) error {
return nil
}

func writeInt64(v int64, w io.Writer) {
err := binary.Write(w, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
}

func writeInt32(v int, w io.Writer) {
i := int32(v)
err := binary.Write(w, binary.LittleEndian, &i)
Expand All @@ -179,6 +194,14 @@ func writeFloat64(v float64, w io.Writer) {
}
}

func readInt64(r io.Reader) (v int64) {
err := binary.Read(r, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
return
}

func readFloat64(r io.Reader) (v float64) {
err := binary.Read(r, binary.LittleEndian, &v)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion hnsw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ func TestSaveLoad(t *testing.T) {
assert.Nil(t, err)

fmt.Printf("Loading from index.dat\n")
h2, err := Load("index.dat")
h2, timestamp, err := Load("index.dat")
assert.Nil(t, err)

fmt.Printf("Index loaded, time saved was %v", time.Unix(timestamp, 0))

fmt.Printf(h2.Stats())
testSearch(h2)
}
Expand Down

0 comments on commit 52a9324

Please sign in to comment.