Commit dc719b5e authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent fe52c88f
......@@ -48,7 +48,7 @@ type sqliteRegistry struct {
// openRegistrySqlite opens SQLite registry located at dburi.
// XXX network name?
func openRegistrySQLite(dburi string) (_ *sqliteRegistry, err error) {
func openRegistrySQLite(ctx context.Context, dburi string) (_ *sqliteRegistry, err error) {
r := &sqliteRegistry{uri: dburi}
defer r.regerr(&err, "open")
......@@ -58,9 +58,14 @@ func openRegistrySQLite(dburi string) (_ *sqliteRegistry, err error) {
}
r.dbpool = dbpool
return r, nil
// XXX setup
err = r.setup(ctx)
if err != nil {
r.Close()
return nil, err
}
return r, nil
}
func (r *sqliteRegistry) Close() (err error) {
......@@ -69,10 +74,16 @@ func (r *sqliteRegistry) Close() (err error) {
}
func (r *sqliteRegistry) setup(ctx context.Context) (err error) {
defer r.regerr(&err, "setup") // XXX args?
// XXX get conn
var conn *sqlite.Conn
// 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.ExecScript(conn, `
CREATE TABLE IF NOT EXISTS hosts (
......@@ -98,6 +109,7 @@ func (r *sqliteRegistry) setup(ctx context.Context) (err error) {
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
......@@ -108,10 +120,8 @@ func (r *sqliteRegistry) Announce(ctx context.Context, hostname, osladdr string)
}
defer r.dbpool.Put(conn)
//sqliteutil.Exec(conn, "SELECT hostname, osladdr FROM hosts WHERE hostname = ?", func (stmt *sqlite.Stmt) error {
err = sqliteutil.Exec(conn, "INSERT INTO hosts (hostname, osladdr) VALUES (?, ?)", nil, hostname, osladdr)
// XXX -> errNoHost
switch sqlite.ErrCode(err) {
case sqlite.SQLITE_CONSTRAINT_UNIQUE: // XXX test
err = errHostDup
......@@ -123,8 +133,16 @@ func (r *sqliteRegistry) Announce(ctx context.Context, hostname, osladdr string)
func (r *sqliteRegistry) Query(ctx context.Context, hostname string) (osladdr string, err error) {
defer r.regerr(&err, "query", hostname)
// XXX get conn
var conn *sqlite.Conn
// 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, "SELECT osladdr FROM hosts WHERE hostname = ?", func (stmt *sqlite.Stmt) error {
osladdr = stmt.ColumnText(0)
......
......@@ -37,7 +37,9 @@ func TestRegistrySQLite(t *testing.T) {
dbpath := work + "/1.db"
r, err := openRegistrySQLite(dbpath)
ctx := context.Background()
r, err := openRegistrySQLite(ctx, dbpath)
X(err)
// quert checks that result of Query(hostname) is as expect
......@@ -49,7 +51,7 @@ func TestRegistrySQLite(t *testing.T) {
// XXX ^^^ -> `r registry` (needs .Network() to get network name) ?
t.Helper()
osladdr, err := r.Query(context.Background(), hostname)
osladdr, err := r.Query(ctx, hostname)
if cause, iserr := expect.(error); iserr {
// error expected
e, ok := err.(*registryError)
......@@ -76,7 +78,7 @@ func TestRegistrySQLite(t *testing.T) {
// r.Query("α") == "alpha:1234")
// r.Query("β") == ø
r2, err := openRegistrySQLite(dbpath)
r2, err := openRegistrySQLite(ctx, dbpath)
// r2.Network() == ...
// r2.Query("α") == "alpha:1234"
// r2.Query("β") == ø
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment