Skip to content

Commit

Permalink
pgsql: Move keyvalue to keyvalue module
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyboardNerd committed Mar 13, 2019
1 parent ba50d7c commit 98e81ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package pgsql
package keyvalue

import (
"database/sql"
"time"

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"
)

Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions database/pgsql/pgsession.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98e81ff

Please sign in to comment.