good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
6c2dd6ef
Commit
6c2dd6ef
authored
Aug 24, 2016
by
José Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring BatchInserter a bit.
parent
376c00ab
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
internal/sqladapter/testing/adapter.go.tpl
+3
-3
3 additions, 3 deletions
internal/sqladapter/testing/adapter.go.tpl
lib/sqlbuilder/batch.go
+28
-5
28 additions, 5 deletions
lib/sqlbuilder/batch.go
with
31 additions
and
8 deletions
internal/sqladapter/testing/adapter.go.tpl
+
3
−
3
View file @
6c2dd6ef
...
@@ -1099,7 +1099,7 @@ func TestBatchInsert(t *testing.T) {
...
@@ -1099,7 +1099,7 @@ func TestBatchInsert(t *testing.T) {
err =
batch.Wait()
err =
batch.Wait()
assert.NoError
(
t
,
err
)
assert.NoError
(
t
,
err
)
assert.NoError
(
t
,
batch.Err
or
())
assert.NoError
(
t
,
batch.Err
())
c
,
err
:=
sess.Collection("artist").Find().Count()
c
,
err
:=
sess.Collection("artist").Find().Count()
assert.NoError
(
t
,
err
)
assert.NoError
(
t
,
err
)
...
@@ -1144,7 +1144,7 @@ func TestBatchInsertReturningKeys(t *testing.T) {
...
@@ -1144,7 +1144,7 @@ func TestBatchInsertReturningKeys(t *testing.T) {
assert.True(t, len(keyMap)
<
=
batchSize
)
assert.True(t, len(keyMap)
<
=
batchSize
)
//
Find
the
elements
we
'
ve
just
inserted
//
Find
the
elements
we
'
ve
just
inserted
keys
:=
make([]int,
len
(
keyMap
))
keys
:=
make([]int,
0,
len
(
keyMap
))
for
i
:=
range
keyMap
{
for
i
:=
range
keyMap
{
keys =
append(keys,
keyMap
[
i
].
ID
)
keys =
append(keys,
keyMap
[
i
].
ID
)
}
}
...
@@ -1154,7 +1154,7 @@ func TestBatchInsertReturningKeys(t *testing.T) {
...
@@ -1154,7 +1154,7 @@ func TestBatchInsertReturningKeys(t *testing.T) {
assert.NoError
(
t
,
err
)
assert.NoError
(
t
,
err
)
assert.Equal
(
t
,
uint64
(
len
(
keyMap
)),
c
)
assert.Equal
(
t
,
uint64
(
len
(
keyMap
)),
c
)
}
}
assert.NoError
(
t
,
batch.Err
or
())
assert.NoError
(
t
,
batch.Err
())
//
Count
all
new
elements
//
Count
all
new
elements
c
,
err
:=
sess.Collection("artist").Find().Count()
c
,
err
:=
sess.Collection("artist").Find().Count()
...
...
This diff is collapsed.
Click to expand it.
lib/sqlbuilder/batch.go
+
28
−
5
View file @
6c2dd6ef
package
sqlbuilder
package
sqlbuilder
// BatchInserter provides a helper that can be used to do massive insertions in
// batches.
type
BatchInserter
struct
{
type
BatchInserter
struct
{
inserter
*
inserter
inserter
*
inserter
size
int
size
int
...
@@ -25,7 +27,7 @@ func (b *BatchInserter) Values(values ...interface{}) *BatchInserter {
...
@@ -25,7 +27,7 @@ func (b *BatchInserter) Values(values ...interface{}) *BatchInserter {
return
b
return
b
}
}
func
(
b
*
BatchInserter
)
N
ext
Result
(
dst
in
t
er
face
{})
bool
{
func
(
b
*
BatchInserter
)
n
ext
Query
()
*
in
s
er
ter
{
clone
:=
b
.
inserter
.
clone
()
clone
:=
b
.
inserter
.
clone
()
i
:=
0
i
:=
0
for
values
:=
range
b
.
values
{
for
values
:=
range
b
.
values
{
...
@@ -36,23 +38,44 @@ func (b *BatchInserter) NextResult(dst interface{}) bool {
...
@@ -36,23 +38,44 @@ func (b *BatchInserter) NextResult(dst interface{}) bool {
}
}
}
}
if
i
==
0
{
if
i
==
0
{
return
nil
}
return
clone
}
// NextResult is useful when using PostgreSQL and Returning(), it dumps the
// next slice of results to dst, which can mean having the IDs of all inserted
// elements in the batch.
func
(
b
*
BatchInserter
)
NextResult
(
dst
interface
{})
bool
{
clone
:=
b
.
nextQuery
()
if
clone
==
nil
{
return
false
return
false
}
}
b
.
err
=
clone
.
Iterator
()
.
All
(
dst
)
b
.
err
=
clone
.
Iterator
()
.
All
(
dst
)
return
(
b
.
err
==
nil
)
return
(
b
.
err
==
nil
)
}
}
// Done means that no more elements are going to be added.
func
(
b
*
BatchInserter
)
Done
()
{
func
(
b
*
BatchInserter
)
Done
()
{
close
(
b
.
values
)
close
(
b
.
values
)
}
}
// Wait blocks until the whole batch is executed.
func
(
b
*
BatchInserter
)
Wait
()
error
{
func
(
b
*
BatchInserter
)
Wait
()
error
{
var
nop
[]
struct
{}
for
{
for
b
.
NextResult
(
&
nop
)
{
q
:=
b
.
nextQuery
()
if
q
==
nil
{
break
}
}
return
b
.
err
if
_
,
err
:=
q
.
Exec
();
err
!=
nil
{
b
.
err
=
err
break
}
}
return
b
.
Err
()
}
}
func
(
b
*
BatchInserter
)
Error
()
error
{
// Err returns any error while executing the batch.
func
(
b
*
BatchInserter
)
Err
()
error
{
return
b
.
err
return
b
.
err
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment