Commit graph

8 commits

Author SHA1 Message Date
Eng Zer Jun
537add3d70 jsondb: improve performance of list operation
Since we only need to retrieve the file names, we can use
`(*os.File).Readdirnames` to avoid reading the whole file info for
better performance.

Sample benchmark:

func Benchmark_Readdir(b *testing.B) {
	for i := 0; i < b.N; i++ {
		f, err := os.Open("/")
		if err != nil {
			b.Fatal(err)
		}

		_, err = f.Readdir(-1)
		if err != nil {
			f.Close()
			b.Fatal(err)
		}

		f.Close()
	}
}

func Benchmark_Readdirnames(b *testing.B) {
	for i := 0; i < b.N; i++ {
		f, err := os.Open("/")
		if err != nil {
			b.Fatal(err)
		}

		_, err = f.Readdirnames(-1)
		if err != nil {
			f.Close()
			b.Fatal(err)
		}

		f.Close()
	}
}

goos: linux
goarch: amd64
pkg: github.com/osbuild/osbuild-composer/internal/jsondb
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
Benchmark_Readdir-16         	   31304	     33551 ns/op	    5638 B/op	      70 allocs/op
Benchmark_Readdirnames-16    	  128443	     12124 ns/op	    1228 B/op	      30 allocs/op
PASS
ok  	github.com/osbuild/osbuild-composer/internal/jsondb	3.098s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-06-20 10:45:22 +02:00
Brian C. Lane
7a4bb863dd Update deprecated io/ioutil functions
ioutil has been deprecated since go 1.16, this fixes all of the
deprecated functions we are using:

ioutil.ReadFile -> os.ReadFile
ioutil.ReadAll -> io.ReadAll
ioutil.WriteFile -> os.WriteFile
ioutil.TempFile -> os.CreateTemp
ioutil.TempDir -> os.MkdirTemp

All of the above are a simple name change, the function arguments and
results are exactly the same as before.

ioutil.ReadDir -> os.ReadDir

now returns a os.DirEntry but the IsDir and Name functions work the
same. The difference is that the FileInfo must be retrieved with the
Info() function which can also return an error.

These were identified by running:
golangci-lint run --build-tags=integration ./...
2023-03-07 09:22:23 -08:00
Eng Zer Jun
00ea3eb285 test: use T.TempDir to create temporary test directory
The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-04-05 09:27:43 +02:00
Juan Abia
4f91a2cc81 gosec: G306-WriteFile permissions higher than 0600
lower WriteFile permissions to 0600 (could break something)
2021-12-13 12:17:30 +02:00
Lars Karlitski
7592e38d3d jsondb: Allow passing nil as document to Read()
This makes Read() usable in situations where only the existence of a
value has to be checked.
2020-05-13 16:45:09 +02:00
Lars Karlitski
2b7adb3200 jsondb: add List method
And add some additional tests.
2020-05-08 14:53:00 +02:00
Lars Karlitski
47df3820ce jsondb: don't use error wrapping (%w)
This is a Go 1.13 feature. We're trying to run on 1.12 for now.
2020-04-09 15:18:58 +02:00
Lars Karlitski
17f4281648 jsondb: introduce a simple JSON database
weldr's store is quite complex and handled serialization itself. Move
that part out into a separate package `jsondb`.

This package is more generic than the store needs: it can write an
arbitrary amount of JSON documents while the store only needs one:
state.json. This is in preparation for future work, which introduces a
queue package that builds on top of `jsondb`.
2020-04-09 08:52:31 +02:00