Skip to content

Commit c4218c2

Browse files
authored
cacheの設計の見直し (#15)
速度を少し犠牲にし、安全性を手に入れた
1 parent 3e8f974 commit c4218c2

File tree

5 files changed

+63
-44
lines changed

5 files changed

+63
-44
lines changed

template/cache_test.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package template
33
import (
44
"context"
55
"database/sql/driver"
6-
"errors"
7-
"fmt"
86
"os"
97
"testing"
108

@@ -33,7 +31,7 @@ func TestCacheRows(t *testing.T) {
3331
assert.NoError(t, err)
3432
defer conn.Close()
3533

36-
var cacheRows driver.Rows
34+
var cacheRows *CacheRows
3735
err = conn.Raw(func(driverConn any) error {
3836
conn := driverConn.(driver.Conn)
3937
stmt, err := conn.Prepare("SELECT id, name FROM users")
@@ -46,31 +44,10 @@ func TestCacheRows(t *testing.T) {
4644
if err != nil {
4745
return err
4846
}
49-
cacheRows = NewCachedRows(rows)
47+
cacheRows = NewCacheRows(rows)
5048
defer cacheRows.Close()
5149

52-
dest := make([]driver.Value, 2)
53-
if err := cacheRows.Next(dest); err != nil {
54-
return err
55-
}
56-
57-
id, ok := dest[0].(int64)
58-
if !ok {
59-
return fmt.Errorf("dest[0] is not int, got=%T", dest[0])
60-
}
61-
if id != 1 {
62-
return errors.New("id != 1")
63-
}
64-
65-
name, ok := dest[1].([]byte)
66-
if !ok {
67-
return fmt.Errorf("dest[1] is not string, got=%T", dest[1])
68-
}
69-
if string(name) != "Alice" {
70-
return errors.New("name != Alice")
71-
}
72-
73-
return nil
50+
return cacheRows.createCache()
7451
})
7552
if err != nil {
7653
t.Error(err)

template/driver.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (r *CacheRows) Clone() *CacheRows {
153153
}
154154
}
155155

156-
func NewCachedRows(inner driver.Rows) *CacheRows {
156+
func NewCacheRows(inner driver.Rows) *CacheRows {
157157
return &CacheRows{inner: inner}
158158
}
159159

@@ -208,7 +208,6 @@ func (r *CacheRows) Close() error {
208208
r.rows.reset()
209209
return nil
210210
}
211-
r.cached = true
212211
return r.inner.Close()
213212
}
214213

@@ -244,3 +243,19 @@ func (r *CacheRows) Next(dest []driver.Value) error {
244243

245244
return nil
246245
}
246+
247+
func (r *CacheRows) createCache() error {
248+
columns := r.Columns()
249+
dest := make([]driver.Value, len(columns))
250+
for {
251+
err := r.Next(dest)
252+
if err == io.EOF {
253+
break
254+
}
255+
if err != nil {
256+
return err
257+
}
258+
}
259+
r.Close()
260+
return nil
261+
}

template/driver.tmpl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (r *CacheRows) Clone() *CacheRows {
152152
}
153153
}
154154

155-
func NewCachedRows(inner driver.Rows) *CacheRows {
155+
func NewCacheRows(inner driver.Rows) *CacheRows {
156156
return &CacheRows{inner: inner}
157157
}
158158

@@ -207,7 +207,6 @@ func (r *CacheRows) Close() error {
207207
r.rows.reset()
208208
return nil
209209
}
210-
r.cached = true
211210
return r.inner.Close()
212211
}
213212

@@ -243,3 +242,19 @@ func (r *CacheRows) Next(dest []driver.Value) error {
243242

244243
return nil
245244
}
245+
246+
func (r *CacheRows) createCache() error {
247+
columns := r.Columns()
248+
dest := make([]driver.Value, len(columns))
249+
for {
250+
err := r.Next(dest)
251+
if err == io.EOF {
252+
break
253+
}
254+
if err != nil {
255+
return err
256+
}
257+
}
258+
r.Close()
259+
return nil
260+
}

template/stmt.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ func cacheKey(args []driver.Value) string {
175175
}
176176

177177
func replaceFn(ctx context.Context, key string) (*CacheRows, error) {
178+
var res *CacheRows
179+
178180
queryerCtx, ok := ctx.Value(queryerCtxKey{}).(driver.QueryerContext)
179181
if ok {
180182
query := ctx.Value(queryKey{}).(string)
@@ -183,16 +185,20 @@ func replaceFn(ctx context.Context, key string) (*CacheRows, error) {
183185
if err != nil {
184186
return nil, err
185187
}
186-
return NewCachedRows(rows), nil
188+
res = NewCacheRows(rows)
189+
} else {
190+
stmt := ctx.Value(stmtKey{}).(*CustomCacheStatement)
191+
args := ctx.Value(argsKey{}).([]driver.Value)
192+
rows, err := stmt.inner.Query(args)
193+
if err != nil {
194+
return nil, err
195+
}
196+
res = NewCacheRows(rows)
187197
}
188198

189-
stmt := ctx.Value(stmtKey{}).(*CustomCacheStatement)
190-
191-
args := ctx.Value(argsKey{}).([]driver.Value)
192-
rows, err := stmt.inner.Query(args)
193-
if err != nil {
199+
if err := res.createCache(); err != nil {
194200
return nil, err
195201
}
196202

197-
return NewCachedRows(rows), nil
203+
return res, nil
198204
}

template/stmt.tmpl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ func cacheKey(args []driver.Value) string {
175175
}
176176

177177
func replaceFn(ctx context.Context, key string) (*CacheRows, error) {
178+
var res *CacheRows
179+
178180
queryerCtx, ok := ctx.Value(queryerCtxKey{}).(driver.QueryerContext)
179181
if ok {
180182
query := ctx.Value(queryKey{}).(string)
@@ -183,16 +185,20 @@ func replaceFn(ctx context.Context, key string) (*CacheRows, error) {
183185
if err != nil {
184186
return nil, err
185187
}
186-
return NewCachedRows(rows), nil
188+
res = NewCacheRows(rows)
189+
} else {
190+
stmt := ctx.Value(stmtKey{}).(*CustomCacheStatement)
191+
args := ctx.Value(argsKey{}).([]driver.Value)
192+
rows, err := stmt.inner.Query(args)
193+
if err != nil {
194+
return nil, err
195+
}
196+
res = NewCacheRows(rows)
187197
}
188198

189-
stmt := ctx.Value(stmtKey{}).(*CustomCacheStatement)
190-
191-
args := ctx.Value(argsKey{}).([]driver.Value)
192-
rows, err := stmt.inner.Query(args)
193-
if err != nil {
199+
if err := res.createCache(); err != nil {
194200
return nil, err
195201
}
196202

197-
return NewCachedRows(rows), nil
203+
return res, nil
198204
}

0 commit comments

Comments
 (0)