Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
52adb309
Commit
52adb309
authored
May 08, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
306294ae
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
65 deletions
+64
-65
go/xcommon/xnet/lonet/registry_sqlite.go
go/xcommon/xnet/lonet/registry_sqlite.go
+56
-61
go/xcommon/xnet/lonet/registry_sqlite_test.go
go/xcommon/xnet/lonet/registry_sqlite_test.go
+8
-4
No files found.
go/xcommon/xnet/lonet/registry_sqlite.go
View file @
52adb309
...
...
@@ -74,8 +74,10 @@ func (r *sqliteRegistry) Close() (err error) {
return
r
.
dbpool
.
Close
()
}
func
(
r
*
sqliteRegistry
)
setup
(
ctx
context
.
Context
)
(
err
error
)
{
// XXX dup
// withConn runs f on a dbpool connection.
//
// connection is first allocated from dbpool and put back after call to f.
func
(
r
*
sqliteRegistry
)
withConn
(
ctx
context
.
Context
,
f
func
(
*
sqlite
.
Conn
)
error
)
error
{
conn
:=
r
.
dbpool
.
Get
(
ctx
.
Done
())
if
conn
==
nil
{
// either ctx cancel or dbpool close
...
...
@@ -85,8 +87,12 @@ func (r *sqliteRegistry) setup(ctx context.Context) (err error) {
return
errRegistryDown
// db closed
}
defer
r
.
dbpool
.
Put
(
conn
)
return
f
(
conn
)
}
err
=
sqliteutil
.
ExecScript
(
conn
,
`
func
(
r
*
sqliteRegistry
)
setup
(
ctx
context
.
Context
)
(
err
error
)
{
return
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
err
:=
sqliteutil
.
ExecScript
(
conn
,
`
CREATE TABLE IF NOT EXISTS hosts (
hostname TEXT NON NULL PRIMARY KEY,
osladdr TEXT NON NULL
...
...
@@ -105,23 +111,16 @@ func (r *sqliteRegistry) setup(ctx context.Context) (err error) {
// XXX check network name
return
nil
})
}
func
(
r
*
sqliteRegistry
)
Announce
(
ctx
context
.
Context
,
hostname
,
osladdr
string
)
(
err
error
)
{
defer
r
.
regerr
(
&
err
,
"announce"
,
hostname
,
osladdr
)
// XXX dup
conn
:=
r
.
dbpool
.
Get
(
ctx
.
Done
())
if
conn
==
nil
{
// either ctx cancel or dbpool close
if
ctx
.
Err
()
!=
nil
{
return
ctx
.
Err
()
}
return
errRegistryDown
// db closed
}
defer
r
.
dbpool
.
Put
(
conn
)
err
=
sqliteutil
.
Exec
(
conn
,
"INSERT INTO hosts (hostname, osladdr) VALUES (?, ?)"
,
nil
,
hostname
,
osladdr
)
return
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
err
:=
sqliteutil
.
Exec
(
conn
,
"INSERT INTO hosts (hostname, osladdr) VALUES (?, ?)"
,
nil
,
hostname
,
osladdr
)
switch
sqlite
.
ErrCode
(
err
)
{
case
sqlite
.
SQLITE_CONSTRAINT_UNIQUE
:
// XXX test
...
...
@@ -129,6 +128,7 @@ func (r *sqliteRegistry) Announce(ctx context.Context, hostname, osladdr string)
}
return
err
})
}
var
errRegDup
=
errors
.
New
(
"registry broken: duplicate host entries"
)
...
...
@@ -136,35 +136,30 @@ var errRegDup = errors.New("registry broken: duplicate host entries")
func
(
r
*
sqliteRegistry
)
Query
(
ctx
context
.
Context
,
hostname
string
)
(
osladdr
string
,
err
error
)
{
defer
r
.
regerr
(
&
err
,
"query"
,
hostname
)
// XXX dup
conn
:=
r
.
dbpool
.
Get
(
ctx
.
Done
())
if
conn
==
nil
{
// either ctx cancel or dbpool close
if
ctx
.
Err
()
!=
nil
{
return
""
,
ctx
.
Err
()
}
return
""
,
errRegistryDown
// db closed
}
defer
r
.
dbpool
.
Put
(
conn
)
err
=
r
.
withConn
(
ctx
,
func
(
conn
*
sqlite
.
Conn
)
error
{
nrow
:=
0
err
=
sqliteutil
.
Exec
(
conn
,
"SELECT osladdr FROM hosts WHERE hostname = ?"
,
func
(
stmt
*
sqlite
.
Stmt
)
error
{
err
:=
sqliteutil
.
Exec
(
conn
,
"SELECT osladdr FROM hosts WHERE hostname = ?"
,
func
(
stmt
*
sqlite
.
Stmt
)
error
{
osladdr
=
stmt
.
ColumnText
(
0
)
nrow
++
return
nil
},
hostname
)
if
err
!=
nil
{
return
""
,
err
return
err
}
if
nrow
==
0
{
return
""
,
errNoHost
return
errNoHost
}
else
if
nrow
>
1
{
// hostname is PK - we should not get several results
return
""
,
errRegDup
osladdr
=
""
return
errRegDup
}
return
nil
})
return
osladdr
,
err
}
...
...
go/xcommon/xnet/lonet/registry_sqlite_test.go
View file @
52adb309
...
...
@@ -42,7 +42,7 @@ func TestRegistrySQLite(t *testing.T) {
r
,
err
:=
openRegistrySQLite
(
ctx
,
dbpath
)
X
(
err
)
// quer
t checks that result of Query(hostname) is as expect
// quer
y checks that result of Query(hostname) is as expected.
//
// if expect is error - it checks that Query returns error with cause == expect.
// otherwise expect must be string and it will check that Query
...
...
@@ -54,7 +54,7 @@ func TestRegistrySQLite(t *testing.T) {
osladdr
,
err
:=
r
.
Query
(
ctx
,
hostname
)
if
ewant
,
iserr
:=
expect
.
(
error
);
iserr
{
// error expected
// XXX construct full registry error around ewant + reflect.DeepCompare
// XXX construct full registry error around ewant + reflect.DeepCompare
?
e
,
ok
:=
err
.
(
*
registryError
)
if
!
(
ok
&&
e
.
Err
==
ewant
&&
osladdr
==
""
)
{
t
.
Fatalf
(
"%s: query %q:
\n
want:
\"\"
, %v
\n
have: %q, %v"
,
...
...
@@ -70,7 +70,7 @@ func TestRegistrySQLite(t *testing.T) {
}
}
// announce checks that result of Announce(hostname, osladdr) is as expected
// announce checks that result of Announce(hostname, osladdr) is as expected
.
//
// if len(errv) == 1 - it checks that Announce returns error with cause == errv[0].
// otherwise it will check that Announce succeeds and returns nil error.
...
...
@@ -86,7 +86,7 @@ func TestRegistrySQLite(t *testing.T) {
}
if
ewant
!=
nil
{
// error expected
// XXX construct full registry error around ewant + reflect.DeepCompare
// XXX construct full registry error around ewant + reflect.DeepCompare
?
e
,
ok
:=
err
.
(
*
registryError
)
if
(
!
ok
&&
e
.
Err
==
ewant
)
{
t
.
Fatalf
(
"%s: announce %q %q:
\n
want %v
\n
have: %v"
,
...
...
@@ -128,5 +128,9 @@ func TestRegistrySQLite(t *testing.T) {
announce
(
r
,
"γ"
,
"gamma:qqq"
,
errRegistryDown
)
query
(
r
,
"γ"
,
errRegistryDown
)
query
(
r2
,
"α"
,
"alpha:1234"
)
X
(
r2
.
Close
())
query
(
r2
,
"α"
,
errRegistryDown
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment