Skip to content

Commit ff249e1

Browse files
committed
fix: Replace renameio with natefinch/atomic for Windows compatibility
- Remove dependency on google/renameio which doesn't support Windows - Use natefinch/atomic which provides cross-platform atomic file operations - Fixes issue #9: Windows Compatibility This change enables the hnsw library to work on Windows platforms, allowing projects that depend on it to be cross-compiled for Windows.
1 parent ff889c9 commit ff249e1

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

encode.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package hnsw
22

33
import (
44
"bufio"
5+
"bytes"
56
"cmp"
67
"encoding/binary"
78
"fmt"
89
"io"
910
"os"
1011

11-
"github.com/google/renameio"
12+
"github.com/natefinch/atomic"
1213
)
1314

1415
// errorEncoder is a helper type to encode multiple values
@@ -301,14 +302,11 @@ func LoadSavedGraph[K cmp.Ordered](path string) (*SavedGraph[K], error) {
301302

302303
// Save writes the graph to the file.
303304
func (g *SavedGraph[K]) Save() error {
304-
tmp, err := renameio.TempFile("", g.Path)
305-
if err != nil {
306-
return err
307-
}
308-
defer tmp.Cleanup()
309-
310-
wr := bufio.NewWriter(tmp)
311-
err = g.Export(wr)
305+
// Create a buffer to write the data
306+
var buf bytes.Buffer
307+
wr := bufio.NewWriter(&buf)
308+
309+
err := g.Export(wr)
312310
if err != nil {
313311
return fmt.Errorf("exporting: %w", err)
314312
}
@@ -318,9 +316,10 @@ func (g *SavedGraph[K]) Save() error {
318316
return fmt.Errorf("flushing: %w", err)
319317
}
320318

321-
err = tmp.CloseAtomicallyReplace()
319+
// Use atomic.WriteFile to write the buffer contents atomically
320+
err = atomic.WriteFile(g.Path, &buf)
322321
if err != nil {
323-
return fmt.Errorf("closing atomically: %w", err)
322+
return fmt.Errorf("writing atomically: %w", err)
324323
}
325324

326325
return nil

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require github.com/google/renameio v1.0.1
88

99
require (
1010
github.com/chewxy/math32 v1.10.1 // indirect
11+
github.com/natefinch/atomic v1.0.1 // indirect
1112
github.com/viterin/partial v1.1.0 // indirect
1213
github.com/viterin/vek v0.4.2 // indirect
1314
golang.org/x/sys v0.11.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
66
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
7+
github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A=
8+
github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM=
79
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
810
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
911
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

0 commit comments

Comments
 (0)