From 366128f0795cebca052e4948a003bce67e7ae6e8 Mon Sep 17 00:00:00 2001 From: Elim Tsiagbey Date: Tue, 28 Nov 2023 10:17:17 -0500 Subject: [PATCH] Update Storage options to support rolling buffer (#798) Add new storage options: - `WithBuffer`: WithBuffer sets the buffer size. The value must be a factorial of 4KiB. - `WithMinimumBuffer`: WithMinimumBuffer sets the threshold to enforce when the buffer expands back to its original size. --- internal/storage/options.go | 20 ++++++++++++++++++++ internal/storage/options_test.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/internal/storage/options.go b/internal/storage/options.go index a272bea079..ce40a009da 100644 --- a/internal/storage/options.go +++ b/internal/storage/options.go @@ -52,6 +52,8 @@ func getDefaultOptions() Options { WithCloseSyncMode: Asynchronous, WithFileAccessMode: ReadOnly, WithCreateFile: false, + WithBuffer: 0, + WithMinimumBuffer: 0, } } @@ -60,6 +62,8 @@ type Options struct { WithCloseSyncMode SyncMode WithFileAccessMode AccessMode WithCreateFile bool + WithBuffer uint64 + WithMinimumBuffer uint64 } // Option is a storage option. @@ -85,3 +89,19 @@ func WithFileAccessMode(m AccessMode) Option { o.WithFileAccessMode = m } } + +// WithBuffer sets the buffer size. +// The value must be a factorial of 4KiB. +func WithBuffer(b uint64) Option { + return func(o *Options) { + o.WithBuffer = b + } +} + +// WithMinimumBuffer sets the threshold to enforce when the +// buffer expands back to its original size. +func WithMinimumBuffer(m uint64) Option { + return func(o *Options) { + o.WithMinimumBuffer = m + } +} diff --git a/internal/storage/options_test.go b/internal/storage/options_test.go index a0cb5936bd..c5c12a6b5e 100644 --- a/internal/storage/options_test.go +++ b/internal/storage/options_test.go @@ -59,4 +59,24 @@ func Test_getOpts(t *testing.T) { testOpts.WithCreateFile = true assert.Equal(opts, testOpts) }) + t.Run("WithBuffer", func(t *testing.T) { + testOpts := getDefaultOptions() + opts := GetOpts(WithBuffer(0)) + assert.Equal(testOpts, opts) + + testOpts = getDefaultOptions() + opts = GetOpts(WithBuffer(4096)) + testOpts.WithBuffer = 4096 + assert.Equal(opts, testOpts) + }) + t.Run("WithMinimumBuffer", func(t *testing.T) { + testOpts := getDefaultOptions() + opts := GetOpts(WithMinimumBuffer(0)) + assert.Equal(testOpts, opts) + + testOpts = getDefaultOptions() + opts = GetOpts(WithMinimumBuffer(88)) + testOpts.WithMinimumBuffer = 88 + assert.Equal(opts, testOpts) + }) }