-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
5 changed files
with
168 additions
and
14 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,52 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/sync/singleflight" | ||
) | ||
|
||
type boundary struct { | ||
minLat float32 | ||
maxLat float32 | ||
minLon float32 | ||
maxLon float32 | ||
|
||
cache map[int]float32 | ||
datasource GridDataSource | ||
sfg singleflight.Group | ||
} | ||
|
||
func NewBoundary(minLat, maxLat, minLon, maxLon float32, datasource GridDataSource) GridCache { | ||
return &boundary{ | ||
minLat: minLat, | ||
maxLat: maxLat, | ||
minLon: minLon, | ||
maxLon: maxLon, | ||
datasource: datasource, | ||
cache: make(map[int]float32), | ||
} | ||
} | ||
|
||
func (b *boundary) ReadGridAt(grid int, lat, lon float32) (float32, error) { | ||
if lat < b.minLat || lat > b.maxLat || lon < b.minLon || lon > b.maxLon { | ||
return b.datasource.ReadGridAt(grid) | ||
} | ||
|
||
v, err, _ := b.sfg.Do(fmt.Sprintf("%d", grid), func() (interface{}, error) { | ||
vFromCache, ok := b.cache[grid] | ||
if !ok { | ||
vFromSource, err := b.datasource.ReadGridAt(grid) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
b.cache[grid] = vFromSource | ||
vFromCache = vFromSource | ||
} | ||
|
||
return vFromCache, nil | ||
}) | ||
|
||
return v.(float32), err | ||
} |
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,26 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/scorix/grib-go/pkg/grib2/cache" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBoundary(t *testing.T) { | ||
ds := &mockGridDataSource{gridValue: 100} | ||
bc := cache.NewBoundary(0, 10, 0, 10, ds) | ||
|
||
// first read should be from source | ||
v, err := bc.ReadGridAt(1, 1, 1) | ||
require.NoError(t, err) | ||
assert.Equal(t, float32(100), v) | ||
assert.Equal(t, 1, ds.readCount) | ||
|
||
// second read should be cached | ||
v, err = bc.ReadGridAt(1, 1, 1) | ||
require.NoError(t, err) | ||
assert.Equal(t, float32(100), v) | ||
assert.Equal(t, 1, ds.readCount) | ||
} |
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,21 @@ | ||
package cache | ||
|
||
type GridDataSource interface { | ||
ReadGridAt(grid int) (float32, error) | ||
} | ||
|
||
type GridCache interface { | ||
ReadGridAt(grid int, lat, lon float32) (float32, error) | ||
} | ||
|
||
type noCache struct { | ||
datasource GridDataSource | ||
} | ||
|
||
func NewNoCache(datasource GridDataSource) GridCache { | ||
return &noCache{datasource: datasource} | ||
} | ||
|
||
func (n *noCache) ReadGridAt(grid int, lat, lon float32) (float32, error) { | ||
return n.datasource.ReadGridAt(grid) | ||
} |
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,36 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/scorix/grib-go/pkg/grib2/cache" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockGridDataSource struct { | ||
gridValue float32 | ||
readCount int | ||
} | ||
|
||
func (m *mockGridDataSource) ReadGridAt(grid int) (float32, error) { | ||
m.readCount++ | ||
return m.gridValue, nil | ||
} | ||
|
||
func TestNoCache(t *testing.T) { | ||
ds := &mockGridDataSource{gridValue: 100} | ||
nc := cache.NewNoCache(ds) | ||
|
||
// first read should be from source | ||
v, err := nc.ReadGridAt(1, 1, 1) | ||
require.NoError(t, err) | ||
assert.Equal(t, float32(100), v) | ||
assert.Equal(t, 1, ds.readCount) | ||
|
||
// second read should not be cached | ||
v, err = nc.ReadGridAt(1, 1, 1) | ||
require.NoError(t, err) | ||
assert.Equal(t, float32(100), v) | ||
assert.Equal(t, 2, ds.readCount) | ||
} |
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