Skip to content

Commit 0a39d68

Browse files
committed
Updated blob so that a copy is made
1 parent 64d6d82 commit 0a39d68

File tree

6 files changed

+26
-37
lines changed

6 files changed

+26
-37
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ plugins: $(PLUGIN_DIR)
3737
npm: $(NPM_DIR)
3838

3939
cmd: dependencies mkdir $(CMD_DIR)
40-
40+
4141
$(NPM_DIR): FORCE
4242
@echo Build npm $(notdir $@)
4343
@cd $@ && ${NPM} run build

pkg/sqlite3/cache.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ func (cache *ConnCache) Prepare(conn *sqlite3.ConnEx, q string) (*Results, error
4242
}
4343
st := cache.load(q)
4444
if st == nil {
45-
// Prepare a statement and store in cache
46-
var err error
4745
cache.Mutex.Lock()
4846
defer cache.Mutex.Unlock()
47+
// Prepare a statement and store in cache
48+
var err error
4949
if st, err = conn.Prepare(q); err != nil {
5050
return nil, err
5151
} else {
@@ -57,6 +57,9 @@ func (cache *ConnCache) Prepare(conn *sqlite3.ConnEx, q string) (*Results, error
5757

5858
// Close all conn cache prepared statements
5959
func (cache *ConnCache) Close() error {
60+
cache.Mutex.Lock()
61+
defer cache.Mutex.Unlock()
62+
6063
var result error
6164
cache.Map.Range(func(key, value interface{}) bool {
6265
if err := value.(*sqlite3.StatementEx).Close(); err != nil {

pkg/sqlite3/cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Test_Cache_001(t *testing.T) {
2929
// SELECT n between 0-9 over 100 executions in parallel should
3030
// return the same result, with a perfect cache hit rate of 9 in 10?
3131
var wg sync.WaitGroup
32-
for i := 0; i < 20; i++ {
32+
for i := 0; i < 1000; i++ {
3333
wg.Add(1)
3434
go func() {
3535
defer wg.Done()

pkg/sqlite3/pool_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func Test_Pool_002(t *testing.T) {
3333
} else {
3434
t.Log(pool)
3535
}
36-
pool.SetMax(1000)
36+
pool.SetMax(5000)
3737

3838
// Get/put connections
3939
var wg sync.WaitGroup
40-
for i := 0; i < 1000; i++ {
40+
for i := 0; i < 5000; i++ {
4141
wg.Add(1)
4242
go func(i int) {
4343
defer wg.Done()
44-
<-time.After(randomDuration(100 * time.Millisecond))
44+
<-time.After(randomDuration(10 * time.Millisecond))
4545
conn := pool.Get()
4646
if conn != nil {
4747
t.Log("conn [", i, "] => ", conn, " cur=", pool.Cur())
@@ -50,7 +50,7 @@ func Test_Pool_002(t *testing.T) {
5050
return err
5151
})
5252
// Wait for a random amount of time before we open the next connection
53-
<-time.After(randomDuration(100 * time.Millisecond))
53+
<-time.After(randomDuration(10 * time.Millisecond))
5454
// Return connection
5555
pool.Put(conn)
5656
t.Log(" returned conn [", i, "] => cur=", pool.Cur())

sys/sqlite3/column.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,13 @@ func (s *Statement) ColumnText(index int) string {
8686
}
8787
}
8888

89-
// Return blob
90-
func (s *Statement) ColumnBlob(index int) []byte {
91-
// TODO: This might make many copies of the data? Look into this
92-
93-
// Allocate a blob
89+
// Return blob, copy data if clone is true
90+
func (s *Statement) ColumnBlob(index int, clone bool) []byte {
91+
// Got blob contents
9492
p := C.sqlite3_column_blob((*C.sqlite3_stmt)(s), C.int(index))
9593
if p == nil {
9694
return nil
9795
}
98-
9996
// Get length of blob
10097
len := s.ColumnBytes(index)
10198
if len == 0 {
@@ -109,7 +106,13 @@ func (s *Statement) ColumnBlob(index int) []byte {
109106
data.Cap = len
110107

111108
// Return slice
112-
return *(*[]byte)(unsafe.Pointer(&data))
109+
if clone {
110+
dst := make([]byte, len)
111+
copy(dst, *(*[]byte)(unsafe.Pointer(&data)))
112+
return dst
113+
} else {
114+
return *(*[]byte)(unsafe.Pointer(&data))
115+
}
113116
}
114117

115118
// Return column as interface
@@ -125,7 +128,7 @@ func (s *Statement) ColumnInterface(index int) interface{} {
125128
case SQLITE_NULL:
126129
return nil
127130
case SQLITE_BLOB:
128-
return s.ColumnBlob(index)
131+
return s.ColumnBlob(index, true)
129132
}
130133
panic(fmt.Sprint("Bad type returned for column:", t))
131134
}

sys/sqlite3/results.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ func (r *Results) Next(t ...reflect.Type) ([]interface{}, error) {
100100
r.cols[i] = v
101101
}
102102
} else {
103-
if v, err := r.value(i); err != nil {
104-
result = multierror.Append(result, err)
105-
} else {
106-
r.cols[i] = v
107-
}
103+
r.cols[i] = r.value(i)
108104
}
109105
}
110106

@@ -188,21 +184,8 @@ func (r *Results) ColumnOriginName(i int) string {
188184
///////////////////////////////////////////////////////////////////////////////
189185
// PRIVATE METHODS
190186

191-
func (r *Results) value(index int) (interface{}, error) {
192-
switch r.st.ColumnType(index) {
193-
case SQLITE_INTEGER:
194-
return r.st.ColumnInt64(index), nil
195-
case SQLITE_FLOAT:
196-
return r.st.ColumnDouble(index), nil
197-
case SQLITE_TEXT:
198-
return r.st.ColumnText(index), nil
199-
case SQLITE_BLOB:
200-
return r.st.ColumnBlob(index), nil
201-
case SQLITE_NULL:
202-
return nil, nil
203-
default:
204-
return nil, fmt.Errorf("Unsupported column type %d", r.st.ColumnType(index))
205-
}
187+
func (r *Results) value(index int) interface{} {
188+
return r.st.ColumnInterface(index)
206189
}
207190

208191
func (r *Results) castvalue(index int, t reflect.Type) (interface{}, error) {
@@ -251,7 +234,7 @@ func (r *Results) castvalue(index int, t reflect.Type) (interface{}, error) {
251234
}
252235
case typeBlob:
253236
if st == SQLITE_BLOB {
254-
return r.st.ColumnBlob(index), nil
237+
return r.st.ColumnBlob(index, true), nil
255238
} else if st == SQLITE_TEXT {
256239
return []byte(r.st.ColumnText(index)), nil
257240
}

0 commit comments

Comments
 (0)