Skip to content

Commit 078580f

Browse files
committed
Fix build for GOARCH=wasm GOOS=js GOOS=wasip1
Fixes the following build failures: GOOS=js GOARCH=wasm go build ./... GOOS=wasip1 GOARCH=wasm go build ./... Depends on: hypermodeinc/ristretto#375 Signed-off-by: Christian Stewart <[email protected]>
1 parent c8f4b55 commit 078580f

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

dir_other.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//go:build js || wasip1
2+
// +build js wasip1
3+
4+
/*
5+
* Copyright 2017 Dgraph Labs, Inc. and Contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package badger
21+
22+
import (
23+
"fmt"
24+
"os"
25+
"path/filepath"
26+
27+
"github.com/dgraph-io/badger/v4/y"
28+
)
29+
30+
// directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part
31+
// of the locking mechanism, it's just advisory.
32+
type directoryLockGuard struct {
33+
// File handle on the directory, which we've flocked.
34+
f *os.File
35+
// The absolute path to our pid file.
36+
path string
37+
// Was this a shared lock for a read-only database?
38+
readOnly bool
39+
}
40+
41+
// acquireDirectoryLock gets a lock on the directory (using flock). If
42+
// this is not read-only, it will also write our pid to
43+
// dirPath/pidFileName for convenience.
44+
func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
45+
*directoryLockGuard, error) {
46+
// Convert to absolute path so that Release still works even if we do an unbalanced
47+
// chdir in the meantime.
48+
absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
49+
if err != nil {
50+
return nil, y.Wrapf(err, "cannot get absolute path for pid lock file")
51+
}
52+
f, err := os.Open(dirPath)
53+
if err != nil {
54+
return nil, y.Wrapf(err, "cannot open directory %q", dirPath)
55+
}
56+
57+
// NOTE: Here is where we would normally call flock.
58+
// This is not supported in js / wasm, so skip it.
59+
60+
if !readOnly {
61+
// Yes, we happily overwrite a pre-existing pid file. We're the
62+
// only read-write badger process using this directory.
63+
err = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
64+
if err != nil {
65+
f.Close()
66+
return nil, y.Wrapf(err,
67+
"Cannot write pid file %q", absPidFilePath)
68+
}
69+
}
70+
71+
return &directoryLockGuard{f, absPidFilePath, readOnly}, nil
72+
}
73+
74+
// Release deletes the pid file and releases our lock on the directory.
75+
func (guard *directoryLockGuard) release() error {
76+
var err error
77+
if !guard.readOnly {
78+
// It's important that we remove the pid file first.
79+
err = os.Remove(guard.path)
80+
}
81+
82+
if closeErr := guard.f.Close(); err == nil {
83+
err = closeErr
84+
}
85+
guard.path = ""
86+
guard.f = nil
87+
88+
return err
89+
}
90+
91+
// openDir opens a directory for syncing.
92+
func openDir(path string) (*os.File, error) { return os.Open(path) }
93+
94+
// When you create or delete a file, you have to ensure the directory entry for the file is synced
95+
// in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
96+
// or see https://github.com/coreos/etcd/issues/6368 for an example.)
97+
func syncDir(dir string) error {
98+
f, err := openDir(dir)
99+
if err != nil {
100+
return y.Wrapf(err, "While opening directory: %s.", dir)
101+
}
102+
103+
err = f.Sync()
104+
closeErr := f.Close()
105+
if err != nil {
106+
return y.Wrapf(err, "While syncing directory: %s.", dir)
107+
}
108+
return y.Wrapf(closeErr, "While closing directory: %s.", dir)
109+
}

dir_unix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !windows && !plan9
2-
// +build !windows,!plan9
1+
//go:build !windows && !plan9 && !js && !wasip1
2+
// +build !windows,!plan9,!js,!wasip1
33

44
/*
55
* Copyright 2017 Dgraph Labs, Inc. and Contributors

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module github.com/dgraph-io/badger/v4
22

33
go 1.19
44

5+
// https://github.com/dgraph-io/ristretto/pull/375
6+
replace github.com/dgraph-io/ristretto => github.com/paralin/ristretto v0.1.2-0.20240221033725-e9838e36e9d8 // fix-wasm-1
7+
58
require (
69
github.com/cespare/xxhash/v2 v2.2.0
710
github.com/dgraph-io/ristretto v0.1.2-0.20240116140435-c67e07994f91

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
77
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10-
github.com/dgraph-io/ristretto v0.1.2-0.20240116140435-c67e07994f91 h1:Pux6+xANi0I7RRo5E1gflI4EZ2yx3BGZ75JkAIvGEOA=
11-
github.com/dgraph-io/ristretto v0.1.2-0.20240116140435-c67e07994f91/go.mod h1:swkazRqnUf1N62d0Nutz7KIj2UKqsm/H8tD0nBJAXqM=
1210
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
1311
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
1412
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
@@ -34,6 +32,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
3432
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
3533
github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw=
3634
github.com/klauspost/compress v1.15.15/go.mod h1:ZcK2JAFqKOpnBlxcLsJzYfrS9X1akm9fHZNnD9+Vo/4=
35+
github.com/paralin/ristretto v0.1.2-0.20240221033725-e9838e36e9d8 h1:EfAnHCpOmoIZXrAV8Ov3Z32tJw3K62rFdlQOSNYAU04=
36+
github.com/paralin/ristretto v0.1.2-0.20240221033725-e9838e36e9d8/go.mod h1:swkazRqnUf1N62d0Nutz7KIj2UKqsm/H8tD0nBJAXqM=
3737
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3838
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3939
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

y/file_dsync.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !dragonfly && !freebsd && !windows && !plan9
2-
// +build !dragonfly,!freebsd,!windows,!plan9
1+
//go:build !dragonfly && !freebsd && !windows && !plan9 && !js && !wasip1
2+
// +build !dragonfly,!freebsd,!windows,!plan9,!js,!wasip1
33

44
/*
55
* Copyright 2017 Dgraph Labs, Inc. and Contributors

0 commit comments

Comments
 (0)