-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a5ee2ef
Showing
8 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Build and test | ||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21.x' | ||
- name: Install dependencies | ||
run: go get . | ||
- name: Build | ||
run: go build -v ./... | ||
- name: Test with the Go CLI | ||
run: go test |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2023 Aert van de Hulsbeek | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/aertje/sparse-store | ||
|
||
go 1.21 | ||
|
||
require github.com/stretchr/testify v1.8.4 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package store | ||
|
||
import ( | ||
"sort" | ||
|
||
"slices" | ||
) | ||
|
||
const defaultMinContiguous = 16 << 10 // 16 KiB | ||
|
||
type entry[T any] struct { | ||
order int | ||
offset int | ||
data []T | ||
} | ||
|
||
type entries[T any] []entry[T] | ||
|
||
func (e entries[T]) Search(x int) int { | ||
return sort.Search(len(e), func(i int) bool { | ||
return e[i].offset >= x | ||
}) | ||
} | ||
|
||
type Store[T any] struct { | ||
minContiguous int | ||
|
||
entries entries[T] | ||
insertCount int | ||
occupancy int | ||
length int | ||
} | ||
|
||
type Option[T any] func(*Store[T]) | ||
|
||
func WithMinContiguous[T any](minContiguous int) Option[T] { | ||
return func(c *Store[T]) { | ||
c.minContiguous = minContiguous | ||
} | ||
} | ||
|
||
func NewStore[T any](opts ...Option[T]) *Store[T] { | ||
cache := &Store[T]{ | ||
minContiguous: defaultMinContiguous, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(cache) | ||
} | ||
|
||
return cache | ||
} | ||
|
||
func (c *Store[T]) Occupancy() int { | ||
return c.occupancy | ||
} | ||
|
||
func (c *Store[T]) Length() int { | ||
return c.length | ||
} | ||
|
||
// Has returns true if the cache contains data at `offset` with length | ||
// `length`. | ||
func (c *Store[T]) Has(offset int, length int) bool { | ||
if len(c.entries) == 0 && length > 0 { | ||
return false | ||
} | ||
|
||
lastOffset := offset | ||
for _, entry := range c.entries { | ||
if entry.offset+len(entry.data) < offset { | ||
continue | ||
} | ||
if entry.offset > offset+length { | ||
break | ||
} | ||
|
||
if lastOffset < entry.offset { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Get populates `p` with the data at `offset`. If the cache does not contain the | ||
// complete data for this range, Get returns false. | ||
func (c *Store[T]) Get(offset int, p []T) bool { | ||
if len(c.entries) == 0 && len(p) > 0 { | ||
return false | ||
} | ||
|
||
lastOffset := offset | ||
complete := true | ||
for _, entry := range c.entries { | ||
if entry.offset+len(entry.data) < offset { | ||
continue | ||
} | ||
if entry.offset > offset+len(p) { | ||
break | ||
} | ||
|
||
if lastOffset < entry.offset { | ||
complete = false | ||
} | ||
|
||
offsetDelta := entry.offset - offset | ||
if offsetDelta < 0 { | ||
copy(p, entry.data[-offsetDelta:]) | ||
} else { | ||
copy(p[offsetDelta:], entry.data) | ||
} | ||
|
||
lastOffset = entry.offset + len(entry.data) | ||
} | ||
|
||
return complete | ||
} | ||
|
||
// Set sets the cache data at `offset` to `p`. If the cache already contains | ||
// data at `offset`, it is overwritten. | ||
func (c *Store[T]) Set(offset int, p []T) { | ||
i := c.entries.Search(offset) | ||
c.entries = slices.Insert(c.entries, i, entry[T]{c.insertCount, offset, p}) | ||
c.insertCount++ | ||
|
||
// If the length increased, update it. | ||
if c.length < offset+len(p) { | ||
c.length = offset + len(p) | ||
} | ||
|
||
// Update the occupancy optimistically. If the entry is compacted, the | ||
// occupancy will be updated again. | ||
c.occupancy += len(p) | ||
|
||
c.compact() | ||
} | ||
|
||
// compact compacts the cache by merging adjacent entries and removing | ||
// overlapping entries. | ||
func (c *Store[T]) compact() { | ||
for i := 0; i < len(c.entries)-1; i++ { | ||
// We use references here as we want to update the entries in place | ||
// when reslicing. | ||
current := &c.entries[i] | ||
next := &c.entries[i+1] | ||
|
||
currentMin := current.offset | ||
currentMax := current.offset + len(current.data) | ||
nextMin := next.offset | ||
nextMax := next.offset + len(next.data) | ||
|
||
if nextMin < currentMax { | ||
// If the current entry encompasses the next entry, copy if needed. | ||
if nextMax <= currentMax { | ||
// If the next entry has a higher order, copy. | ||
if current.order < next.order { | ||
copy(current.data[nextMin-currentMin:], next.data) | ||
} | ||
|
||
c.entries = append(c.entries[:i+1], c.entries[i+2:]...) | ||
c.occupancy -= len(next.data) | ||
i-- | ||
continue | ||
} else { | ||
// If the entries overlap reslice so that they become contiguous. | ||
c.occupancy -= currentMax - nextMin | ||
if current.order < next.order { | ||
current.data = current.data[:nextMin-currentMin] | ||
currentMax = nextMin | ||
} else { | ||
next.data = next.data[currentMax-nextMin:] | ||
next.offset = currentMax | ||
nextMin = currentMax | ||
} | ||
} | ||
} | ||
|
||
// If the entries are contiguous and small enough, combine them. | ||
if currentMax == nextMin && nextMax-currentMin <= c.minContiguous { | ||
newData := make([]T, nextMax-currentMin) | ||
copy(newData, current.data) | ||
copy(newData[currentMax-currentMin:], next.data) | ||
c.entries[i] = entry[T]{current.order, currentMin, newData} | ||
c.entries = append(c.entries[:i+1], c.entries[i+2:]...) | ||
i-- | ||
} | ||
} | ||
} |
Oops, something went wrong.