diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..09658ca --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,11 @@ +name: Test +on: + push: + branches: + - main + pull_request: + branches: + - main +jobs: + go: + uses: bsm/misc/.github/workflows/test-go.yml@main diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fb20c3c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: go -go: - - 1.11.x - - 1.12.x -env: - - GO111MODULE=on GOPROXY=https://proxy.golang.org -cache: - directories: - - $GOPATH/pkg/mod diff --git a/Makefile b/Makefile index 543cc16..fe39b9b 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,13 @@ -default: vet test - -vet: - go vet ./... +default: test test: go test ./... -.PHONY: vet test +bench: + go test ./... -run=NONE -bench=. -benchmem + +lint: + golangci-lint run -# go get -u github.com/davelondon/rebecca/cmd/becca README.md: README.md.tpl becca -package github.com/bsm/sntable diff --git a/README.md b/README.md index bb1132a..eb7407c 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # SNTable -[![GoDoc](https://godoc.org/github.com/bsm/sntable?status.svg)](https://godoc.org/github.com/bsm/sntable) -[![Build Status](https://travis-ci.org/bsm/sntable.png?branch=master)](https://travis-ci.org/bsm/sntable) +[![Go Reference](https://pkg.go.dev/badge/github.com/bsm/sntable.svg)](https://pkg.go.dev/github.com/bsm/sntable) +[![Test](https://github.com/bsm/sntable/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/sntable/actions/workflows/test.yml) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) Custom [SSTable](https://www.igvita.com/2012/02/06/sstable-and-log-structured-storage-leveldb/) implementation for [Go](https://golang.org). Instead of arbitrary bytes strings, this implementation assumes numeric 8-byte (`uint64`) -keys. +keys. Blocks can be stored uncompressed or compressed with [Snappy](https://github.com/klauspost/compress) (default) +or [Zstandard](https://github.com/klauspost/compress). ## Examples @@ -16,7 +17,6 @@ keys. package main import ( - "io/ioutil" "log" "os" @@ -25,7 +25,7 @@ import ( func main() { // create a file - f, err := ioutil.TempFile("", "sntable-example") + f, err := os.CreateTemp("", "sntable-example") if err != nil { log.Fatalln(err) } @@ -56,7 +56,6 @@ func main() { package main import ( - "io/ioutil" "log" "os" diff --git a/README.md.tpl b/README.md.tpl index 9689589..919ef07 100644 --- a/README.md.tpl +++ b/README.md.tpl @@ -1,12 +1,13 @@ # SNTable -[![GoDoc](https://godoc.org/github.com/bsm/sntable?status.svg)](https://godoc.org/github.com/bsm/sntable) -[![Build Status](https://travis-ci.org/bsm/sntable.png?branch=master)](https://travis-ci.org/bsm/sntable) +[![Go Reference](https://pkg.go.dev/badge/github.com/bsm/sntable.svg)](https://pkg.go.dev/github.com/bsm/sntable) +[![Test](https://github.com/bsm/sntable/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/sntable/actions/workflows/test.yml) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) Custom [SSTable](https://www.igvita.com/2012/02/06/sstable-and-log-structured-storage-leveldb/) implementation for [Go](https://golang.org). Instead of arbitrary bytes strings, this implementation assumes numeric 8-byte (`uint64`) -keys. +keys. Blocks can be stored uncompressed or compressed with [Snappy](https://github.com/klauspost/compress) (default) +or [Zstandard](https://github.com/klauspost/compress). ## Examples @@ -16,7 +17,6 @@ keys. package main import ( - "io/ioutil" "log" "os" @@ -33,7 +33,6 @@ func main() {{ "ExampleWriter" | code }} package main import ( - "io/ioutil" "log" "os" diff --git a/bench/go.mod b/bench/go.mod index 71456ff..91d0de5 100644 --- a/bench/go.mod +++ b/bench/go.mod @@ -1,6 +1,6 @@ module github.com/bsm/sntable/bench -go 1.12 +go 1.26 replace github.com/bsm/sntable => ../ @@ -8,6 +8,11 @@ require ( github.com/bsm/sntable v0.1.1 github.com/golang/leveldb v0.0.0-20170107010102-259d9253d719 github.com/syndtr/goleveldb v1.0.0 +) + +require ( + github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect + github.com/klauspost/compress v1.19.0 // indirect golang.org/x/net v0.0.0-20190607181551-461777fb6f67 // indirect golang.org/x/sys v0.0.0-20190610081024-1e42afee0f76 // indirect ) diff --git a/bench/go.sum b/bench/go.sum index 5d9194f..1b7be74 100644 --- a/bench/go.sum +++ b/bench/go.sum @@ -1,52 +1,35 @@ -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/golang/leveldb v0.0.0-20170107010102-259d9253d719 h1:yahFtfWlyALYDkXw2ETowZqG4vi8hiE0yOEBOkpaXl0= github.com/golang/leveldb v0.0.0-20170107010102-259d9253d719/go.mod h1:etEpE0xVqxA0N3WNUa5wic5HCNSsQvYm+PFNmOnx2iU= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/klauspost/compress v1.19.0 h1:sXLILfc9jV2QYWkzFOPWStmcUVH2RHEB1JCdY2oVvCQ= +github.com/klauspost/compress v1.19.0/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w= -github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo= -github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190607181551-461777fb6f67 h1:rJJxsykSlULwd2P2+pg/rtnwN2FrWp4IuCxOSyS0V00= golang.org/x/net v0.0.0-20190607181551-461777fb6f67/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190610081024-1e42afee0f76 h1:QSmW7Q3mFdAGjtAd0byXmFJ55inUydyZ4WQmiuItAIQ= golang.org/x/sys v0.0.0-20190610081024-1e42afee0f76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/doc.go b/doc.go index 0c6b2c1..a3935ae 100644 --- a/doc.go +++ b/doc.go @@ -2,50 +2,50 @@ Package sntable contains a custom SSTable implementation which, instead of arbitrary bytes strings, assumes 8-byte numeric keys. -Data Structure Documentation +# Data Structure Documentation -Store +# Store A store contains a series of data blocks followed by an index and a store footer. - Store layout: - +---------+---------+---------+-------------+--------------+ - | block 1 | ... | block n | block index | store footer | - +---------+---------+---------+-------------+--------------+ + Store layout: + +---------+---------+---------+-------------+--------------+ + | block 1 | ... | block n | block index | store footer | + +---------+---------+---------+-------------+--------------+ - Block index: - +----------------------------+--------------------+----------------------------------+--------------------------+--------+ - | last cell block 1 (varint) | offset 2 (varint) | last cell block 2 (varint,delta) | offset 2 (varint,delta) | ... | - +----------------------------+--------------------+----------------------------------+--------------------------+--------+ + Block index: + +----------------------------+--------------------+----------------------------------+--------------------------+--------+ + | last cell block 1 (varint) | offset 2 (varint) | last cell block 2 (varint,delta) | offset 2 (varint,delta) | ... | + +----------------------------+--------------------+----------------------------------+--------------------------+--------+ - Store footer: - +------------------------+------------------+ - | index offset (8 bytes) | magic (8 bytes) | - +------------------------+------------------+ + Store footer: + +------------------------+------------------+ + | index offset (8 bytes) | magic (8 bytes) | + +------------------------+------------------+ -Block +# Block A block comprises of a series of sections, followed by a section index and a single-byte compression type indicator. - Block layout: - +-----------+---------+-----------+---------------+---------------------------+ - | section 1 | ... | section n | section index | compression type (1-byte) | - +-----------+---------+-----------+---------------+---------------------------+ + Block layout: + +-----------+---------+-----------+---------------+---------------------------+ + | section 1 | ... | section n | section index | compression type (1-byte) | + +-----------+---------+-----------+---------------+---------------------------+ - Section index: - +----------------------------+-------+----------------------------+-------------------------------+ - | section offset 2 (4 bytes) | ... | section offset n (4 bytes) | number of sections (4 bytes) | - +----------------------------+-------+----------------------------+-------------------------------+ + Section index: + +----------------------------+-------+----------------------------+-------------------------------+ + | section offset 2 (4 bytes) | ... | section offset n (4 bytes) | number of sections (4 bytes) | + +----------------------------+-------+----------------------------+-------------------------------+ -Section +# Section A section is a series of key/value pairs where the first key is stored as a full uint64 while subsequent keys are delta encoded. - +----------------+----------------------+------------------+----------------------+----------------------+------------------+-------+ - | key 1 (varint) | value len 1 (varint) | value 1 (varlen) | key 2 (varint,delta) | value len 2 (varint) | value 2 (varlen) | ... | - +----------------+----------------------+------------------+----------------------+----------------------+------------------+-------+ + +----------------+----------------------+------------------+----------------------+----------------------+------------------+-------+ + | key 1 (varint) | value len 1 (varint) | value 1 (varlen) | key 2 (varint,delta) | value len 2 (varint) | value 2 (varlen) | ... | + +----------------+----------------------+------------------+----------------------+----------------------+------------------+-------+ */ package sntable diff --git a/example_test.go b/example_test.go index e63837b..2acfb89 100644 --- a/example_test.go +++ b/example_test.go @@ -1,7 +1,6 @@ package sntable_test import ( - "io/ioutil" "log" "os" @@ -10,11 +9,11 @@ import ( func ExampleWriter() { // create a file - f, err := ioutil.TempFile("", "sntable-example") + f, err := os.CreateTemp("", "sntable-example") if err != nil { log.Fatalln(err) } - defer f.Close() + defer func() { _ = f.Close() }() // wrap writer around file, append (neglecting errors for demo purposes) w := sntable.NewWriter(f, nil) @@ -39,7 +38,7 @@ func ExampleReader() { if err != nil { log.Fatalln(err) } - defer f.Close() + defer func() { _ = f.Close() }() // get file size fs, err := f.Stat() diff --git a/go.mod b/go.mod index 13c265c..eb842ef 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,5 @@ module github.com/bsm/sntable -go 1.12 +go 1.26 -require ( - github.com/golang/snappy v0.0.1 - github.com/onsi/ginkgo v1.10.3 - github.com/onsi/gomega v1.7.1 -) +require github.com/klauspost/compress v1.19.0 diff --git a/go.sum b/go.sum index eeae621..8916777 100644 --- a/go.sum +++ b/go.sum @@ -1,29 +1,2 @@ -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY= -github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ= -github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= -gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +github.com/klauspost/compress v1.19.0 h1:sXLILfc9jV2QYWkzFOPWStmcUVH2RHEB1JCdY2oVvCQ= +github.com/klauspost/compress v1.19.0/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= diff --git a/helper_test.go b/helper_test.go new file mode 100644 index 0000000..550acc9 --- /dev/null +++ b/helper_test.go @@ -0,0 +1,79 @@ +package sntable_test + +import ( + "bytes" + "errors" + "testing" +) + +func equal[T comparable](t *testing.T, got, want T, ctx ...any) { + t.Helper() + if got != want { + t.Fatalf("got %v, want %v%s", got, want, ctxMsg(ctx)) + } +} + +func isTrue(t *testing.T, got bool, ctx ...any) { + t.Helper() + if !got { + t.Fatalf("expected true%s", ctxMsg(ctx)) + } +} + +func isFalse(t *testing.T, got bool, ctx ...any) { + t.Helper() + if got { + t.Fatalf("expected false%s", ctxMsg(ctx)) + } +} + +func ok(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } +} + +func errIs(t *testing.T, err, target error) { + t.Helper() + if !errors.Is(err, target) { + t.Fatalf("got error %v, want %v", err, target) + } +} + +func errMsg(t *testing.T, err error, msg string) { + t.Helper() + if err == nil || err.Error() != msg { + t.Fatalf("got error %v, want %q", err, msg) + } +} + +func hasSuffix(t *testing.T, got []byte, sfx string, ctx ...any) { + t.Helper() + if !bytes.HasSuffix(got, []byte(sfx)) { + t.Fatalf("%q does not have suffix %q%s", got, sfx, ctxMsg(ctx)) + } +} + +// approx asserts got is within tol of want. +func approx(t *testing.T, got, want, tol int) { + t.Helper() + if d := got - want; d < -tol || d > tol { + t.Fatalf("got %d, want %d ±%d", got, want, tol) + } +} + +func hasPos(t *testing.T, x interface{ Pos() int }, want int, ctx ...any) { + t.Helper() + equal(t, x.Pos(), want, ctx...) +} + +func ctxMsg(ctx []any) string { + if len(ctx) == 0 { + return "" + } + if s, ok := ctx[0].(string); ok { + return " (" + s + ")" + } + return "" +} diff --git a/reader.go b/reader.go index 196880a..910d79b 100644 --- a/reader.go +++ b/reader.go @@ -7,7 +7,7 @@ import ( "sort" "sync" - "github.com/golang/snappy" + "github.com/klauspost/compress/snappy" ) // Reader instances can seek and iterate across data in tables. @@ -161,6 +161,15 @@ func (r *Reader) readBlock(bpos int) (*BlockReader, error) { releaseBuffer(plain) return nil, err } + case blockZstdCompression: + defer releaseBuffer(raw) + + // ponytail: DecodeAll allocates a fresh buffer per block (no pooled sizing + // like snappy's DecodedLen); the result is returned to bufPool on Release. + var err error + if block, err = zstdDec.DecodeAll(raw[:cBitPos], nil); err != nil { + return nil, err + } default: releaseBuffer(raw) return nil, errBadCompression @@ -220,7 +229,7 @@ func (r *BlockReader) SeekSection(key uint64) *SectionReader { // Release releases the block reader and frees up resources. The reader must not be used // after this method is called. -func (r *BlockReader) Release() { bufPool.Put(r.block) } +func (r *BlockReader) Release() { releaseBuffer(r.block) } // The starting offset of the section within the block. func (r *BlockReader) sectionOffset(spos int) int { @@ -389,8 +398,8 @@ var bufPool sync.Pool func fetchBuffer(sz int) []byte { if v := bufPool.Get(); v != nil { - if p := v.([]byte); sz <= cap(p) { - return p[:sz] + if p := v.(*[]byte); sz <= cap(*p) { + return (*p)[:sz] } } return make([]byte, sz) @@ -398,6 +407,6 @@ func fetchBuffer(sz int) []byte { func releaseBuffer(p []byte) { if cap(p) != 0 { - bufPool.Put(p) + bufPool.Put(&p) } } diff --git a/reader_test.go b/reader_test.go index 2d1148a..36be7ff 100644 --- a/reader_test.go +++ b/reader_test.go @@ -2,234 +2,227 @@ package sntable_test import ( "fmt" + "testing" "github.com/bsm/sntable" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "github.com/onsi/gomega/types" ) -var _ = Describe("Reader", func() { - var subject *sntable.Reader - - HavePos := func(n int) types.GomegaMatcher { - return WithTransform(func(x interface{ Pos() int }) int { - return x.Pos() - }, Equal(n)) - } - - // The following will seed 100 keys into 4 blocks: - // - // B0: 0..120 - // B1: 124..244 - // B2: 248..368 - // B3: 372..396 - // - BeforeEach(func() { - var err error - subject, err = seedReader(100) - Expect(err).NotTo(HaveOccurred()) +// seedReader(100) produces 100 keys across 4 blocks: +// +// B0: 0..120 +// B1: 124..244 +// B2: 248..368 +// B3: 372..396 +func mustSeedReader(t *testing.T, sz int) *sntable.Reader { + t.Helper() + r, err := seedReader(sz) + ok(t, err) + return r +} + +func TestReader(t *testing.T) { + t.Run("init", func(t *testing.T) { + subject := mustSeedReader(t, 100) + equal(t, subject.NumBlocks(), 4) + + tr10k := mustSeedReader(t, 10000) + equal(t, tr10k.NumBlocks(), 323) }) - It("should init", func() { - Expect(subject.NumBlocks()).To(Equal(4)) - - tr10k, err := seedReader(10000) - Expect(err).NotTo(HaveOccurred()) - Expect(tr10k.NumBlocks()).To(Equal(323)) - }) - - It("should Get/Append", func() { + t.Run("Get/Append", func(t *testing.T) { + subject := mustSeedReader(t, 100) for i := uint64(0); i <= 396; i += 4 { - sfx := fmt.Sprintf("%04d", i) - Expect(subject.Get(i)).To(HaveSuffix(sfx), "for %d", i) + val, err := subject.Get(i) + ok(t, err) + hasSuffix(t, val, fmt.Sprintf("%04d", i), fmt.Sprintf("for %d", i)) } _, err := subject.Get(1) - Expect(err).To(MatchError(sntable.ErrNotFound)) + errIs(t, err, sntable.ErrNotFound) _, err = subject.Get(395) - Expect(err).To(MatchError(sntable.ErrNotFound)) + errIs(t, err, sntable.ErrNotFound) _, err = subject.Get(400) - Expect(err).To(MatchError(sntable.ErrNotFound)) + errIs(t, err, sntable.ErrNotFound) }) - It("should retrieve blocks", func() { + t.Run("retrieve blocks", func(t *testing.T) { + subject := mustSeedReader(t, 100) + b0, err := subject.GetBlock(0) - Expect(err).NotTo(HaveOccurred()) - Expect(b0.Pos()).To(Equal(0)) + ok(t, err) + equal(t, b0.Pos(), 0) b1, err := subject.GetBlock(1) - Expect(err).NotTo(HaveOccurred()) - Expect(b1.Pos()).To(Equal(1)) + ok(t, err) + equal(t, b1.Pos(), 1) b0, err = subject.GetBlock(-1) - Expect(err).NotTo(HaveOccurred()) - Expect(b0.Pos()).To(Equal(0)) + ok(t, err) + equal(t, b0.Pos(), 0) }) - It("should seek blocks", func() { - Expect(subject.SeekBlock(50)).To(HavePos(0)) - Expect(subject.SeekBlock(120)).To(HavePos(0)) - Expect(subject.SeekBlock(121)).To(HavePos(1)) - Expect(subject.SeekBlock(360)).To(HavePos(2)) - Expect(subject.SeekBlock(370)).To(HavePos(3)) - Expect(subject.SeekBlock(396)).To(HavePos(3)) - Expect(subject.SeekBlock(397)).To(HavePos(4)) - Expect(subject.SeekBlock(1000)).To(HavePos(4)) + t.Run("seek blocks", func(t *testing.T) { + subject := mustSeedReader(t, 100) + for _, tc := range []struct{ key, pos int }{ + {50, 0}, {120, 0}, {121, 1}, {360, 2}, + {370, 3}, {396, 3}, {397, 4}, {1000, 4}, + } { + b, err := subject.SeekBlock(uint64(tc.key)) + ok(t, err) + hasPos(t, b, tc.pos, fmt.Sprintf("key %d", tc.key)) + } }) - Describe("BlockReader", func() { - var block *sntable.BlockReader - - // B1 (124..244) is split into 2 sections: - // S0: 124..184 - // S1: 188..244 - BeforeEach(func() { - var err error - block, err = subject.GetBlock(1) - Expect(err).NotTo(HaveOccurred()) - }) - - It("should have pos", func() { - Expect(block.Pos()).To(Equal(1)) + // B1 (124..244) is split into 2 sections: + // S0: 124..184 + // S1: 188..244 + t.Run("BlockReader", func(t *testing.T) { + t.Run("pos", func(t *testing.T) { + block, err := mustSeedReader(t, 100).GetBlock(1) + ok(t, err) + equal(t, block.Pos(), 1) }) - It("should have sections", func() { - Expect(block.NumSections()).To(Equal(2)) - Expect(block.GetSection(0).Pos()).To(Equal(0)) - Expect(block.GetSection(1).Pos()).To(Equal(1)) - Expect(block.GetSection(2).Pos()).To(Equal(2)) - Expect(block.GetSection(3).Pos()).To(Equal(2)) - Expect(block.GetSection(-1).Pos()).To(Equal(0)) + t.Run("sections", func(t *testing.T) { + block, err := mustSeedReader(t, 100).GetBlock(1) + ok(t, err) + equal(t, block.NumSections(), 2) + equal(t, block.GetSection(0).Pos(), 0) + equal(t, block.GetSection(1).Pos(), 1) + equal(t, block.GetSection(2).Pos(), 2) + equal(t, block.GetSection(3).Pos(), 2) + equal(t, block.GetSection(-1).Pos(), 0) }) - It("should seek sections", func() { - Expect(block.SeekSection(0).Pos()).To(Equal(0)) - Expect(block.SeekSection(120).Pos()).To(Equal(0)) - Expect(block.SeekSection(184).Pos()).To(Equal(0)) - Expect(block.SeekSection(187).Pos()).To(Equal(0)) - Expect(block.SeekSection(188).Pos()).To(Equal(1)) - Expect(block.SeekSection(244).Pos()).To(Equal(1)) - Expect(block.SeekSection(245).Pos()).To(Equal(2)) + t.Run("seek sections", func(t *testing.T) { + block, err := mustSeedReader(t, 100).GetBlock(1) + ok(t, err) + for _, tc := range []struct{ key, pos int }{ + {0, 0}, {120, 0}, {184, 0}, {187, 0}, + {188, 1}, {244, 1}, {245, 2}, + } { + equal(t, block.SeekSection(uint64(tc.key)).Pos(), tc.pos, fmt.Sprintf("key %d", tc.key)) + } }) }) - Describe("SectionReader", func() { - var section *sntable.SectionReader - - // S1: 188..244 - BeforeEach(func() { - block, err := subject.GetBlock(1) - Expect(err).NotTo(HaveOccurred()) - - section = block.GetSection(1) - }) + // S1: 188..244 + t.Run("SectionReader", func(t *testing.T) { + getSection := func(t *testing.T) *sntable.SectionReader { + t.Helper() + block, err := mustSeedReader(t, 100).GetBlock(1) + ok(t, err) + return block.GetSection(1) + } - It("should have pos", func() { - Expect(section.Pos()).To(Equal(1)) + t.Run("pos", func(t *testing.T) { + equal(t, getSection(t).Pos(), 1) }) - It("should seek", func() { - Expect(section.Seek(200)).To(BeTrue()) - Expect(section.Next()).To(BeTrue()) - Expect(section.Key()).To(Equal(uint64(200))) + t.Run("seek", func(t *testing.T) { + section := getSection(t) + isTrue(t, section.Seek(200)) + isTrue(t, section.Next()) + equal(t, section.Key(), uint64(200)) - Expect(section.Seek(229)).To(BeTrue()) - Expect(section.Next()).To(BeTrue()) - Expect(section.Key()).To(Equal(uint64(232))) + isTrue(t, section.Seek(229)) + isTrue(t, section.Next()) + equal(t, section.Key(), uint64(232)) }) - It("should iterate", func() { - Expect(section.More()).To(BeTrue()) - Expect(section.Next()).To(BeTrue()) - Expect(section.Key()).To(Equal(uint64(188))) - Expect(section.Value()).To(HaveSuffix("0188")) + t.Run("iterate", func(t *testing.T) { + section := getSection(t) + + isTrue(t, section.More()) + isTrue(t, section.Next()) + equal(t, section.Key(), uint64(188)) + hasSuffix(t, section.Value(), "0188") - Expect(section.More()).To(BeTrue()) - Expect(section.Next()).To(BeTrue()) - Expect(section.Key()).To(Equal(uint64(192))) - Expect(section.Value()).To(HaveSuffix("0192")) + isTrue(t, section.More()) + isTrue(t, section.Next()) + equal(t, section.Key(), uint64(192)) + hasSuffix(t, section.Value(), "0192") for i := 0; i < 12; i++ { - Expect(section.More()).To(BeTrue()) - Expect(section.Next()).To(BeTrue()) + isTrue(t, section.More()) + isTrue(t, section.Next()) } - Expect(section.Key()).To(Equal(uint64(240))) - Expect(section.Value()).To(HaveSuffix("0240")) + equal(t, section.Key(), uint64(240)) + hasSuffix(t, section.Value(), "0240") - Expect(section.More()).To(BeTrue()) - Expect(section.Next()).To(BeTrue()) - Expect(section.Key()).To(Equal(uint64(244))) - Expect(section.Value()).To(HaveSuffix("0244")) + isTrue(t, section.More()) + isTrue(t, section.Next()) + equal(t, section.Key(), uint64(244)) + hasSuffix(t, section.Value(), "0244") - Expect(section.More()).To(BeFalse()) - Expect(section.Next()).To(BeFalse()) + isFalse(t, section.More()) + isFalse(t, section.Next()) }) }) - Describe("Iterator", func() { - It("should iterate from beginning", func() { - iter, err := subject.Seek(0) - Expect(err).NotTo(HaveOccurred()) + t.Run("Iterator", func(t *testing.T) { + t.Run("iterate from beginning", func(t *testing.T) { + iter, err := mustSeedReader(t, 100).Seek(0) + ok(t, err) defer iter.Release() - Expect(iter.More()).To(BeTrue()) - Expect(iter.Next()).To(BeTrue()) - Expect(iter.Key()).To(Equal(uint64(0))) - Expect(iter.Value()).To(HaveSuffix("0000")) + isTrue(t, iter.More()) + isTrue(t, iter.Next()) + equal(t, iter.Key(), uint64(0)) + hasSuffix(t, iter.Value(), "0000") - Expect(iter.More()).To(BeTrue()) - Expect(iter.Next()).To(BeTrue()) - Expect(iter.Key()).To(Equal(uint64(4))) - Expect(iter.Value()).To(HaveSuffix("0004")) + isTrue(t, iter.More()) + isTrue(t, iter.Next()) + equal(t, iter.Key(), uint64(4)) + hasSuffix(t, iter.Value(), "0004") for i := 0; i < 97; i++ { - Expect(iter.More()).To(BeTrue()) - Expect(iter.Next()).To(BeTrue()) + isTrue(t, iter.More()) + isTrue(t, iter.Next()) } - Expect(iter.More()).To(BeTrue()) - Expect(iter.Next()).To(BeTrue()) - Expect(iter.Key()).To(Equal(uint64(396))) - Expect(iter.Value()).To(HaveSuffix("0396")) + isTrue(t, iter.More()) + isTrue(t, iter.Next()) + equal(t, iter.Key(), uint64(396)) + hasSuffix(t, iter.Value(), "0396") - Expect(iter.More()).To(BeFalse()) - Expect(iter.Next()).To(BeFalse()) - Expect(iter.Err()).NotTo(HaveOccurred()) + isFalse(t, iter.More()) + isFalse(t, iter.Next()) + ok(t, iter.Err()) }) - It("should iterate from middle", func() { - iter, err := subject.Seek(200) - Expect(err).NotTo(HaveOccurred()) + t.Run("iterate from middle", func(t *testing.T) { + iter, err := mustSeedReader(t, 100).Seek(200) + ok(t, err) defer iter.Release() - Expect(iter.Next()).To(BeTrue()) - Expect(iter.Key()).To(Equal(uint64(200))) + isTrue(t, iter.Next()) + equal(t, iter.Key(), uint64(200)) }) - It("should iterate from last entry", func() { - iter, err := subject.Seek(396) - Expect(err).NotTo(HaveOccurred()) + t.Run("iterate from last entry", func(t *testing.T) { + iter, err := mustSeedReader(t, 100).Seek(396) + ok(t, err) defer iter.Release() - Expect(iter.More()).To(BeTrue()) - Expect(iter.Next()).To(BeTrue()) - Expect(iter.Key()).To(Equal(uint64(396))) - Expect(iter.Value()).To(HaveSuffix("0396")) + isTrue(t, iter.More()) + isTrue(t, iter.Next()) + equal(t, iter.Key(), uint64(396)) + hasSuffix(t, iter.Value(), "0396") - Expect(iter.More()).To(BeFalse()) - Expect(iter.Next()).To(BeFalse()) - Expect(iter.Err()).NotTo(HaveOccurred()) + isFalse(t, iter.More()) + isFalse(t, iter.Next()) + ok(t, iter.Err()) }) - It("should not iterate when past the end", func() { - iter, err := subject.Seek(1000) - Expect(err).NotTo(HaveOccurred()) + t.Run("not iterate when past the end", func(t *testing.T) { + iter, err := mustSeedReader(t, 100).Seek(1000) + ok(t, err) defer iter.Release() - Expect(iter.More()).To(BeFalse()) - Expect(iter.Next()).To(BeFalse()) - Expect(iter.Err()).NotTo(HaveOccurred()) + isFalse(t, iter.More()) + isFalse(t, iter.Next()) + ok(t, iter.Err()) }) }) -}) +} diff --git a/sntable.go b/sntable.go index dfe79db..f8a0b0f 100644 --- a/sntable.go +++ b/sntable.go @@ -1,12 +1,23 @@ package sntable -import "errors" +import ( + "errors" + + "github.com/klauspost/compress/zstd" +) var magic = []byte{71, 39, 134, 190, 31, 122, 101, 219} const ( blockNoCompression = 0 blockSnappyCompression = 1 + blockZstdCompression = 2 +) + +// Shared, concurrency-safe zstd codec instances for the block EncodeAll/DecodeAll APIs. +var ( + zstdEnc, _ = zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedDefault)) + zstdDec, _ = zstd.NewReader(nil) ) // ErrNotFound is returned by the reader when a key cannot be found. @@ -30,12 +41,13 @@ type blockInfo struct { type Compression byte func (c Compression) isValid() bool { - return c >= SnappyCompression && c <= unknownCompression + return c >= SnappyCompression && c < unknownCompression } // Supported compression codecs const ( SnappyCompression Compression = iota NoCompression + ZstdCompression unknownCompression ) diff --git a/sntable_test.go b/sntable_test.go index 3176921..02c4762 100644 --- a/sntable_test.go +++ b/sntable_test.go @@ -4,20 +4,10 @@ import ( "bytes" "fmt" "math/rand" - "testing" "github.com/bsm/sntable" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" ) -func TestSuite(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "sntable") -} - -// -------------------------------------------------------------------- - func seedReader(sz int) (*sntable.Reader, error) { buf := new(bytes.Buffer) if err := seedTable(buf, sz); err != nil { diff --git a/writer.go b/writer.go index d32df8a..db133e6 100644 --- a/writer.go +++ b/writer.go @@ -5,7 +5,7 @@ import ( "fmt" "io" - "github.com/golang/snappy" + "github.com/klauspost/compress/snappy" ) // WriterOptions define writer specific options. @@ -54,7 +54,7 @@ type Writer struct { soffs []int // section offsets in the current block buf []byte // plain buffer - snp []byte // snappy buffer + cmp []byte // compression buffer tmp []byte // scratch buffer index []blockInfo @@ -180,12 +180,11 @@ func (w *Writer) flush() error { var block []byte switch w.o.Compression { case SnappyCompression: - w.snp = snappy.Encode(w.snp[:cap(w.snp)], w.buf) - if len(w.snp) < len(w.buf)-len(w.buf)/4 { - block = append(w.snp, blockSnappyCompression) - } else { - block = append(w.buf, blockNoCompression) - } + w.cmp = snappy.Encode(w.cmp[:cap(w.cmp)], w.buf) + block = w.pickBlock(blockSnappyCompression) + case ZstdCompression: + w.cmp = zstdEnc.EncodeAll(w.buf, w.cmp[:0]) + block = w.pickBlock(blockZstdCompression) default: block = append(w.buf, blockNoCompression) } @@ -197,3 +196,12 @@ func (w *Writer) flush() error { return w.writeRaw(block) } + +// pickBlock returns w.cmp tagged with codec if it saved at least 25%, +// otherwise the uncompressed buffer. +func (w *Writer) pickBlock(codec byte) []byte { + if len(w.cmp) < len(w.buf)-len(w.buf)/4 { + return append(w.cmp, codec) + } + return append(w.buf, blockNoCompression) +} diff --git a/writer_test.go b/writer_test.go index 2aedc48..a742285 100644 --- a/writer_test.go +++ b/writer_test.go @@ -3,62 +3,78 @@ package sntable_test import ( "bytes" "math/rand" + "testing" "github.com/bsm/sntable" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" ) -var _ = Describe("Writer", func() { - var buf *bytes.Buffer - var subject *sntable.Writer - var testdata = []byte("testdata") +func TestWriter(t *testing.T) { + testdata := []byte("testdata") - BeforeEach(func() { - buf = new(bytes.Buffer) - subject = sntable.NewWriter(buf, nil) + t.Run("write empty", func(t *testing.T) { + buf := new(bytes.Buffer) + w := sntable.NewWriter(buf, nil) + ok(t, w.Close()) + equal(t, buf.Len(), 16) }) - AfterEach(func() { - _ = subject.Close() - }) + t.Run("prevent out-of-order appends", func(t *testing.T) { + buf := new(bytes.Buffer) + w := sntable.NewWriter(buf, nil) + defer func() { _ = w.Close() }() - It("should write empty", func() { - Expect(subject.Close()).To(Succeed()) - Expect(buf.Len()).To(Equal(16)) + ok(t, w.Append(20, testdata)) + errMsg(t, w.Append(19, testdata), `sntable: attempted an out-of-order append, 19 must be > 20`) + ok(t, w.Append(22, testdata)) + errMsg(t, w.Append(20, testdata), `sntable: attempted an out-of-order append, 20 must be > 22`) + ok(t, w.Append(23, testdata)) + errMsg(t, w.Append(23, testdata), `sntable: attempted an out-of-order append, 23 must be > 23`) + ok(t, w.Append(24, testdata)) }) - It("should prevent out-of-order appends", func() { - Expect(subject.Append(20, testdata)).To(Succeed()) - Expect(subject.Append(19, testdata)).To(MatchError(`sntable: attempted an out-of-order append, 19 must be > 20`)) - Expect(subject.Append(22, testdata)).To(Succeed()) - Expect(subject.Append(20, testdata)).To(MatchError(`sntable: attempted an out-of-order append, 20 must be > 22`)) - Expect(subject.Append(23, testdata)).To(Succeed()) - Expect(subject.Append(23, testdata)).To(MatchError(`sntable: attempted an out-of-order append, 23 must be > 23`)) - Expect(subject.Append(24, testdata)).To(Succeed()) - }) + t.Run("write (non-compressable)", func(t *testing.T) { + buf := new(bytes.Buffer) + w := sntable.NewWriter(buf, nil) - It("should write (non-compressable)", func() { rnd := rand.New(rand.NewSource(1)) val := make([]byte, 128) - for key := uint64(0); key < 100000; key += 2 { _, err := rnd.Read(val) - Expect(err).NotTo(HaveOccurred()) - Expect(subject.Append(key, val)).To(Succeed()) + ok(t, err) + ok(t, w.Append(key, val)) } - Expect(subject.Close()).To(Succeed()) - Expect(buf.Len()).To(BeNumerically("~", 6575289, 1024)) - Expect(buf.String()[buf.Len()-8:]).To(Equal("\x47\x27\x86\xBE\x1F\x7a\x65\xDB")) + ok(t, w.Close()) + approx(t, buf.Len(), 6575289, 1024) + equal(t, buf.String()[buf.Len()-8:], "\x47\x27\x86\xBE\x1F\x7a\x65\xDB") }) - It("should write (well-compressable)", func() { + t.Run("write (well-compressable)", func(t *testing.T) { + buf := new(bytes.Buffer) + w := sntable.NewWriter(buf, nil) + val := bytes.Repeat(testdata, 16) for key := uint64(0); key < 100000; key += 2 { - Expect(subject.Append(key, val)).To(Succeed()) + ok(t, w.Append(key, val)) + } + ok(t, w.Close()) + approx(t, buf.Len(), 382311, 1024) + equal(t, buf.String()[buf.Len()-8:], "\x47\x27\x86\xBE\x1F\x7a\x65\xDB") + }) + + t.Run("round-trip with zstd", func(t *testing.T) { + buf := new(bytes.Buffer) + w := sntable.NewWriter(buf, &sntable.WriterOptions{Compression: sntable.ZstdCompression}) + + val := bytes.Repeat(testdata, 16) + for key := uint64(0); key < 10000; key += 2 { + ok(t, w.Append(key, val)) } - Expect(subject.Close()).To(Succeed()) - Expect(buf.Len()).To(BeNumerically("~", 362538, 1024)) - Expect(buf.String()[buf.Len()-8:]).To(Equal("\x47\x27\x86\xBE\x1F\x7a\x65\xDB")) + ok(t, w.Close()) + + r, err := sntable.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) + ok(t, err) + got, err := r.Get(4242) + ok(t, err) + equal(t, string(got), string(val)) }) -}) +}