Skip to content

Support lastInsertID/affectedRows in kayak #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions cleanupDB.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

PROJECT_DIR=$(cd $(dirname $0)/; pwd)

cd ${PROJECT_DIR} && find . -name '*.db' -exec rm -f {} \;
cd ${PROJECT_DIR} && find . -name '*.db-shm' -exec rm -f {} \;
cd ${PROJECT_DIR} && find . -name '*.db-wal' -exec rm -f {} \;
cd ${PROJECT_DIR} && find . -name 'db.meta' -exec rm -f {} \;
cd ${PROJECT_DIR} && find . -name 'public.keystore' -exec rm -f {} \;
cd ${PROJECT_DIR} && find . -name '*.public.keystore' -exec rm -f {} \;
cd ${PROJECT_DIR} && find . -type d -name '*.ldb' -prune -exec rm -rf {} \;
cd ${PROJECT_DIR} && find . -name '*.db' -exec rm -vf {} \;
cd ${PROJECT_DIR} && find . -name '*.db-shm' -exec rm -vf {} \;
cd ${PROJECT_DIR} && find . -name '*.db-wal' -exec rm -vf {} \;
cd ${PROJECT_DIR} && find . -name 'db.meta' -exec rm -vf {} \;
cd ${PROJECT_DIR} && find . -name 'public.keystore' -exec rm -vf {} \;
cd ${PROJECT_DIR} && find . -name '*.public.keystore' -exec rm -vf {} \;
cd ${PROJECT_DIR} && find . -type d -name '*.ldb' -prune -exec rm -vrf {} \;
24 changes: 18 additions & 6 deletions client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,16 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name

// TODO(xq262144): make use of the ctx argument
sq := convertQuery(query, args)
if _, err = c.addQuery(wt.WriteQuery, sq); err != nil {

var affectedRows, lastInsertID int64
if affectedRows, lastInsertID, _, err = c.addQuery(wt.WriteQuery, sq); err != nil {
return
}

result = driver.ResultNoRows
result = &execResult{
affectedRows: affectedRows,
lastInsertID: lastInsertID,
}

return
}
Expand All @@ -220,7 +225,9 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam

// TODO(xq262144): make use of the ctx argument
sq := convertQuery(query, args)
return c.addQuery(wt.ReadQuery, sq)
_, _, rows, err = c.addQuery(wt.ReadQuery, sq)

return
}

// Commit implements the driver.Tx.Commit method.
Expand All @@ -240,7 +247,7 @@ func (c *conn) Commit() (err error) {

if len(c.queries) > 0 {
// send query
if _, err = c.sendQuery(wt.WriteQuery, c.queries); err != nil {
if _, _, _, err = c.sendQuery(wt.WriteQuery, c.queries); err != nil {
return
}
}
Expand Down Expand Up @@ -270,7 +277,7 @@ func (c *conn) Rollback() error {
return nil
}

func (c *conn) addQuery(queryType wt.QueryType, query *wt.Query) (rows driver.Rows, err error) {
func (c *conn) addQuery(queryType wt.QueryType, query *wt.Query) (affectedRows int64, lastInsertID int64, rows driver.Rows, err error) {
if c.inTransaction {
// check query type, enqueue query
if queryType == wt.ReadQuery {
Expand Down Expand Up @@ -298,7 +305,7 @@ func (c *conn) addQuery(queryType wt.QueryType, query *wt.Query) (rows driver.Ro
return c.sendQuery(queryType, []wt.Query{*query})
}

func (c *conn) sendQuery(queryType wt.QueryType, queries []wt.Query) (rows driver.Rows, err error) {
func (c *conn) sendQuery(queryType wt.QueryType, queries []wt.Query) (affectedRows int64, lastInsertID int64, rows driver.Rows, err error) {
var peers *kayak.Peers
if peers, err = cacheGetPeers(c.dbID, c.privKey); err != nil {
return
Expand Down Expand Up @@ -352,6 +359,11 @@ func (c *conn) sendQuery(queryType wt.QueryType, queries []wt.Query) (rows drive
}
rows = newRows(&response)

if queryType == wt.WriteQuery {
affectedRows = response.Header.AffectedRows
lastInsertID = response.Header.LastInsertID
}

// build ack
c.ackCh <- &wt.Ack{
Header: wt.SignedAckHeader{
Expand Down
10 changes: 9 additions & 1 deletion client/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,18 @@ func TestTransaction(t *testing.T) {
So(db, ShouldNotBeNil)
So(err, ShouldBeNil)

var execResult sql.Result
var lastInsertID, affectedRows int64

_, err = db.Exec("create table test (test int)")
So(err, ShouldBeNil)
_, err = db.Exec("insert into test values (1)")
execResult, err = db.Exec("insert into test values (1)")
So(err, ShouldBeNil)
lastInsertID, err = execResult.LastInsertId()
So(err, ShouldBeNil)
So(lastInsertID, ShouldEqual, 1)
affectedRows, err = execResult.RowsAffected()
So(affectedRows, ShouldEqual, 1)

// test start transaction
var tx *sql.Tx
Expand Down
32 changes: 32 additions & 0 deletions client/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package client

type execResult struct {
affectedRows int64
lastInsertID int64
}

// LastInsertId teturn last inserted ID.
func (r *execResult) LastInsertId() (int64, error) {
return r.lastInsertID, nil
}

// RowsAffected return how many rows affected.
func (r *execResult) RowsAffected() (int64, error) {
return r.affectedRows, nil
}
9 changes: 7 additions & 2 deletions cmd/cql-adapter/api/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,15 @@ func (a *queryAPI) Write(rw http.ResponseWriter, r *http.Request) {
log.WithField("db", dbID).WithField("query", query).Info("got exec")

var err error
if err = config.GetConfig().StorageInstance.Exec(dbID, query); err != nil {
var affectedRows int64
var lastInsertID int64
if affectedRows, lastInsertID, err = config.GetConfig().StorageInstance.Exec(dbID, query); err != nil {
sendResponse(http.StatusInternalServerError, false, err, nil, rw)
return
}

sendResponse(http.StatusOK, true, nil, nil, rw)
sendResponse(http.StatusOK, true, nil, map[string]interface{}{
"last_insert_id": lastInsertID,
"affected_rows": affectedRows,
}, rw)
}
8 changes: 6 additions & 2 deletions cmd/cql-adapter/storage/covenantsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@ func (s *CovenantSQLStorage) Query(dbID string, query string) (columns []string,
}

// Exec implements the Storage abstraction interface.
func (s *CovenantSQLStorage) Exec(dbID string, query string) (err error) {
func (s *CovenantSQLStorage) Exec(dbID string, query string) (affectedRows int64, lastInsertID int64, err error) {
var conn *sql.DB
if conn, err = s.getConn(dbID); err != nil {
return
}
defer conn.Close()

_, err = conn.Exec(query)
var result sql.Result
result, err = conn.Exec(query)

affectedRows, _ = result.RowsAffected()
lastInsertID, _ = result.LastInsertId()

return
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/cql-adapter/storage/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,18 @@ func (s *SQLite3Storage) Query(dbID string, query string) (columns []string, typ
}

// Exec implements the Storage abstraction interface.
func (s *SQLite3Storage) Exec(dbID string, query string) (err error) {
func (s *SQLite3Storage) Exec(dbID string, query string) (affectedRows int64, lastInsertID int64, err error) {
var conn *sql.DB
if conn, err = s.getConn(dbID, false); err != nil {
return
}
defer conn.Close()

_, err = conn.Exec(query)
var result sql.Result
result, err = conn.Exec(query)

affectedRows, _ = result.RowsAffected()
lastInsertID, _ = result.LastInsertId()

return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cql-adapter/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Storage interface {
// Query for result.
Query(dbID string, query string) (columns []string, types []string, rows [][]interface{}, err error)
// Exec for update.
Exec(dbID string, query string) (err error)
Exec(dbID string, query string) (affectedRows int64, lastInsertID int64, err error)
}

// golang does trick convert, use rowScanner to return the original result type in sqlite3 driver
Expand Down
14 changes: 8 additions & 6 deletions cmd/cqld/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ func (s *LocalStorage) Prepare(ctx context.Context, wb twopc.WriteBatch) (err er
}

// Commit implements twopc Worker.Commit
func (s *LocalStorage) Commit(ctx context.Context, wb twopc.WriteBatch) (err error) {
func (s *LocalStorage) Commit(ctx context.Context, wb twopc.WriteBatch) (_ interface{}, err error) {
payload, err := s.decodeLog(wb)
if err != nil {
log.WithError(err).Error("decode log failed")
return
}
return s.commit(ctx, payload)
err = s.commit(ctx, payload)
return
}

func (s *LocalStorage) commit(ctx context.Context, payload *KayakPayload) (err error) {
Expand Down Expand Up @@ -132,7 +133,8 @@ func (s *LocalStorage) commit(ctx context.Context, payload *KayakPayload) (err e
s.consistent.AddCache(nodeToSet)
}

return s.Storage.Commit(ctx, execLog)
_, err = s.Storage.Commit(ctx, execLog)
return
}

// Rollback implements twopc Worker.Rollback
Expand Down Expand Up @@ -300,7 +302,7 @@ func (s *KayakKVServer) SetNode(node *proto.Node) (err error) {
return err
}

_, err = s.Runtime.Apply(writeData.Bytes())
_, _, err = s.Runtime.Apply(writeData.Bytes())
if err != nil {
log.Errorf("Apply set node failed: %#v\nPayload:\n %#v", err, writeData)
}
Expand Down Expand Up @@ -371,7 +373,7 @@ func (s *KayakKVServer) SetDatabase(meta wt.ServiceInstance) (err error) {
return err
}

_, err = s.Runtime.Apply(writeData.Bytes())
_, _, err = s.Runtime.Apply(writeData.Bytes())
if err != nil {
log.Errorf("Apply set database failed: %#v\nPayload:\n %#v", err, writeData)
}
Expand Down Expand Up @@ -400,7 +402,7 @@ func (s *KayakKVServer) DeleteDatabase(dbID proto.DatabaseID) (err error) {
return err
}

_, err = s.Runtime.Apply(writeData.Bytes())
_, _, err = s.Runtime.Apply(writeData.Bytes())
if err != nil {
log.Errorf("Apply set database failed: %#v\nPayload:\n %#v", err, writeData)
}
Expand Down
Loading