From 2209d2e36aea43ee17610489a2644af2212a4bc3 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 24 Aug 2019 16:27:54 -0500 Subject: [PATCH] Rename mode constants --- pscache/lrucache.go | 8 ++++---- pscache/lrucache_test.go | 12 ++++++------ pscache/pscache.go | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pscache/lrucache.go b/pscache/lrucache.go index d5d6062f..cdcec63c 100644 --- a/pscache/lrucache.go +++ b/pscache/lrucache.go @@ -22,7 +22,7 @@ type LRUCache struct { psNamePrefix string } -// NewLRUCache creates a new LRUCache. mode is either PrepareMode or DescribeMode. cap is the maximum size of the cache. +// NewLRUCache creates a new LRUCache. mode is either ModePrepare or ModeDescribe. cap is the maximum size of the cache. func NewLRUCache(conn *pgconn.PgConn, mode int, cap int) *LRUCache { mustBeValidMode(mode) mustBeValidCap(cap) @@ -86,14 +86,14 @@ func (c *LRUCache) Cap() int { return c.cap } -// Mode returns the mode of the cache (PrepareMode or DescribeMode) +// Mode returns the mode of the cache (ModePrepare or ModeDescribe) func (c *LRUCache) Mode() int { return c.mode } func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) { var name string - if c.mode == PrepareMode { + if c.mode == ModePrepare { name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount) c.prepareCount += 1 } @@ -104,7 +104,7 @@ func (c *LRUCache) prepare(ctx context.Context, sql string) (*pgconn.PreparedSta func (c *LRUCache) removeOldest(ctx context.Context) error { oldest := c.l.Back() c.l.Remove(oldest) - if c.mode == PrepareMode { + if c.mode == ModePrepare { return c.conn.Exec(ctx, fmt.Sprintf("deallocate %s", oldest.Value.(*pgconn.PreparedStatementDescription).Name)).Close() } return nil diff --git a/pscache/lrucache_test.go b/pscache/lrucache_test.go index bf2fcbe0..a5d413e3 100644 --- a/pscache/lrucache_test.go +++ b/pscache/lrucache_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestLRUCachePrepareMode(t *testing.T) { +func TestLRUCacheModePrepare(t *testing.T) { t.Parallel() ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -22,10 +22,10 @@ func TestLRUCachePrepareMode(t *testing.T) { require.NoError(t, err) defer conn.Close(ctx) - cache := pscache.NewLRUCache(conn, pscache.PrepareMode, 2) + cache := pscache.NewLRUCache(conn, pscache.ModePrepare, 2) require.EqualValues(t, 0, cache.Len()) require.EqualValues(t, 2, cache.Cap()) - require.EqualValues(t, pscache.PrepareMode, cache.Mode()) + require.EqualValues(t, pscache.ModePrepare, cache.Mode()) psd, err := cache.Get(ctx, "select 1") require.NoError(t, err) @@ -57,7 +57,7 @@ func TestLRUCachePrepareMode(t *testing.T) { require.Empty(t, fetchServerStatements(t, ctx, conn)) } -func TestLRUCacheDescribeMode(t *testing.T) { +func TestLRUCacheModeDescribe(t *testing.T) { t.Parallel() ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -67,10 +67,10 @@ func TestLRUCacheDescribeMode(t *testing.T) { require.NoError(t, err) defer conn.Close(ctx) - cache := pscache.NewLRUCache(conn, pscache.DescribeMode, 2) + cache := pscache.NewLRUCache(conn, pscache.ModeDescribe, 2) require.EqualValues(t, 0, cache.Len()) require.EqualValues(t, 2, cache.Cap()) - require.EqualValues(t, pscache.DescribeMode, cache.Mode()) + require.EqualValues(t, pscache.ModeDescribe, cache.Mode()) psd, err := cache.Get(ctx, "select 1") require.NoError(t, err) diff --git a/pscache/pscache.go b/pscache/pscache.go index bfd51e81..4f8cf723 100644 --- a/pscache/pscache.go +++ b/pscache/pscache.go @@ -8,8 +8,8 @@ import ( ) const ( - PrepareMode = iota // Cache should prepare named statements. - DescribeMode // Cache should prepare the anonymous prepared statement to only fetch the description of the statement. + ModePrepare = iota // Cache should prepare named statements. + ModeDescribe // Cache should prepare the anonymous prepared statement to only fetch the description of the statement. ) // Cache prepares and caches prepared statement descriptions. @@ -26,11 +26,11 @@ type Cache interface { // Cap returns the maximum number of cached prepared statement descriptions. Cap() int - // Mode returns the mode of the cache (PrepareMode or DescribeMode) + // Mode returns the mode of the cache (ModePrepare or ModeDescribe) Mode() int } -// New returns the preferred cache implementation for mode and cap. mode is either PrepareMode or DescribeMode. cap is +// New returns the preferred cache implementation for mode and cap. mode is either ModePrepare or ModeDescribe. cap is // the maximum size of the cache. func New(conn *pgconn.PgConn, mode int, cap int) Cache { mustBeValidMode(mode) @@ -40,8 +40,8 @@ func New(conn *pgconn.PgConn, mode int, cap int) Cache { } func mustBeValidMode(mode int) { - if mode != PrepareMode && mode != DescribeMode { - panic("mode must be PrepareMode or DescribeMode") + if mode != ModePrepare && mode != ModeDescribe { + panic("mode must be ModePrepare or ModeDescribe") } }