pgsql: Move keyvalue to keyvalue module

This commit is contained in:
Sida Chen 2019-03-06 16:31:42 -05:00
parent ba50d7c626
commit 98e81ff5f1
3 changed files with 24 additions and 20 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package pgsql
package keyvalue
import (
"database/sql"
@ -20,6 +20,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/coreos/clair/database/pgsql/monitoring"
"github.com/coreos/clair/database/pgsql/util"
"github.com/coreos/clair/pkg/commonerr"
)
@ -32,24 +34,24 @@ const (
DO UPDATE SET key=$1, value=$2`
)
func (tx *pgSession) UpdateKeyValue(key, value string) (err error) {
func UpdateKeyValue(tx *sql.Tx, key, value string) (err error) {
if key == "" || value == "" {
log.Warning("could not insert a flag which has an empty name or value")
return commonerr.NewBadRequestError("could not insert a flag which has an empty name or value")
}
defer observeQueryTime("PersistKeyValue", "all", time.Now())
defer monitoring.ObserveQueryTime("PersistKeyValue", "all", time.Now())
_, err = tx.Exec(upsertKeyValue, key, value)
if err != nil {
return handleError("insertKeyValue", err)
return util.HandleError("insertKeyValue", err)
}
return nil
}
func (tx *pgSession) FindKeyValue(key string) (string, bool, error) {
defer observeQueryTime("FindKeyValue", "all", time.Now())
func FindKeyValue(tx *sql.Tx, key string) (string, bool, error) {
defer monitoring.ObserveQueryTime("FindKeyValue", "all", time.Now())
var value string
err := tx.QueryRow(searchKeyValue, key).Scan(&value)
@ -59,7 +61,7 @@ func (tx *pgSession) FindKeyValue(key string) (string, bool, error) {
}
if err != nil {
return "", false, handleError("searchKeyValue", err)
return "", false, util.HandleError("searchKeyValue", err)
}
return value, true, nil

View File

@ -12,38 +12,39 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package pgsql
package keyvalue
import (
"testing"
"github.com/coreos/clair/database/pgsql/testutil"
"github.com/stretchr/testify/assert"
)
func TestKeyValue(t *testing.T) {
datastore, tx := openSessionForTest(t, "KeyValue", true)
defer closeTest(t, datastore, tx)
tx, cleanup := testutil.CreateTestTxWithFixtures(t, "KeyValue")
defer cleanup()
// Get non-existing key/value
f, ok, err := tx.FindKeyValue("test")
f, ok, err := FindKeyValue(tx, "test")
assert.Nil(t, err)
assert.False(t, ok)
// Try to insert invalid key/value.
assert.Error(t, tx.UpdateKeyValue("test", ""))
assert.Error(t, tx.UpdateKeyValue("", "test"))
assert.Error(t, tx.UpdateKeyValue("", ""))
assert.Error(t, UpdateKeyValue(tx, "test", ""))
assert.Error(t, UpdateKeyValue(tx, "", "test"))
assert.Error(t, UpdateKeyValue(tx, "", ""))
// Insert and verify.
assert.Nil(t, tx.UpdateKeyValue("test", "test1"))
f, ok, err = tx.FindKeyValue("test")
assert.Nil(t, UpdateKeyValue(tx, "test", "test1"))
f, ok, err = FindKeyValue(tx, "test")
assert.Nil(t, err)
assert.True(t, ok)
assert.Equal(t, "test1", f)
// Update and verify.
assert.Nil(t, tx.UpdateKeyValue("test", "test2"))
f, ok, err = tx.FindKeyValue("test")
assert.Nil(t, UpdateKeyValue(tx, "test", "test2"))
f, ok, err = FindKeyValue(tx, "test")
assert.Nil(t, err)
assert.True(t, ok)
assert.Equal(t, "test2", f)

View File

@ -18,6 +18,7 @@ import (
"database/sql"
"time"
"github.com/coreos/clair/database/pgsql/keyvalue"
"github.com/coreos/clair/database/pgsql/vulnerability"
"github.com/coreos/clair/database"
@ -152,12 +153,12 @@ func (tx *pgSession) DeleteNotification(name string) error {
// UpdateKeyValue stores or updates a simple key/value pair.
func (tx *pgSession) UpdateKeyValue(key, value string) error {
return lock.UpdateKeyValue(tx.Tx, key, value)
return keyvalue.UpdateKeyValue(tx.Tx, key, value)
}
// FindKeyValue retrieves a value from the given key.
func (tx *pgSession) FindKeyValue(key string) (value string, found bool, err error) {
return lock.FindKeyValue(tx.Tx, key)
return keyvalue.FindKeyValue(tx.Tx, key)
}
// AcquireLock acquires a brand new lock in the database with a given name