pgsql: Move keyvalue to keyvalue module
This commit is contained in:
parent
ba50d7c626
commit
98e81ff5f1
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package pgsql
|
package keyvalue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@ -20,6 +20,8 @@ import (
|
|||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
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"
|
"github.com/coreos/clair/pkg/commonerr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,24 +34,24 @@ const (
|
|||||||
DO UPDATE SET key=$1, value=$2`
|
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 == "" {
|
if key == "" || value == "" {
|
||||||
log.Warning("could not insert a flag which has an empty name or 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")
|
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)
|
_, err = tx.Exec(upsertKeyValue, key, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return handleError("insertKeyValue", err)
|
return util.HandleError("insertKeyValue", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *pgSession) FindKeyValue(key string) (string, bool, error) {
|
func FindKeyValue(tx *sql.Tx, key string) (string, bool, error) {
|
||||||
defer observeQueryTime("FindKeyValue", "all", time.Now())
|
defer monitoring.ObserveQueryTime("FindKeyValue", "all", time.Now())
|
||||||
|
|
||||||
var value string
|
var value string
|
||||||
err := tx.QueryRow(searchKeyValue, key).Scan(&value)
|
err := tx.QueryRow(searchKeyValue, key).Scan(&value)
|
||||||
@ -59,7 +61,7 @@ func (tx *pgSession) FindKeyValue(key string) (string, bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", false, handleError("searchKeyValue", err)
|
return "", false, util.HandleError("searchKeyValue", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return value, true, nil
|
return value, true, nil
|
@ -12,38 +12,39 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package pgsql
|
package keyvalue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/coreos/clair/database/pgsql/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKeyValue(t *testing.T) {
|
func TestKeyValue(t *testing.T) {
|
||||||
datastore, tx := openSessionForTest(t, "KeyValue", true)
|
tx, cleanup := testutil.CreateTestTxWithFixtures(t, "KeyValue")
|
||||||
defer closeTest(t, datastore, tx)
|
defer cleanup()
|
||||||
|
|
||||||
// Get non-existing key/value
|
// Get non-existing key/value
|
||||||
f, ok, err := tx.FindKeyValue("test")
|
f, ok, err := FindKeyValue(tx, "test")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
|
|
||||||
// Try to insert invalid key/value.
|
// Try to insert invalid key/value.
|
||||||
assert.Error(t, tx.UpdateKeyValue("test", ""))
|
assert.Error(t, UpdateKeyValue(tx, "test", ""))
|
||||||
assert.Error(t, tx.UpdateKeyValue("", "test"))
|
assert.Error(t, UpdateKeyValue(tx, "", "test"))
|
||||||
assert.Error(t, tx.UpdateKeyValue("", ""))
|
assert.Error(t, UpdateKeyValue(tx, "", ""))
|
||||||
|
|
||||||
// Insert and verify.
|
// Insert and verify.
|
||||||
assert.Nil(t, tx.UpdateKeyValue("test", "test1"))
|
assert.Nil(t, UpdateKeyValue(tx, "test", "test1"))
|
||||||
f, ok, err = tx.FindKeyValue("test")
|
f, ok, err = FindKeyValue(tx, "test")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.Equal(t, "test1", f)
|
assert.Equal(t, "test1", f)
|
||||||
|
|
||||||
// Update and verify.
|
// Update and verify.
|
||||||
assert.Nil(t, tx.UpdateKeyValue("test", "test2"))
|
assert.Nil(t, UpdateKeyValue(tx, "test", "test2"))
|
||||||
f, ok, err = tx.FindKeyValue("test")
|
f, ok, err = FindKeyValue(tx, "test")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.Equal(t, "test2", f)
|
assert.Equal(t, "test2", f)
|
@ -18,6 +18,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/clair/database/pgsql/keyvalue"
|
||||||
"github.com/coreos/clair/database/pgsql/vulnerability"
|
"github.com/coreos/clair/database/pgsql/vulnerability"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"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.
|
// UpdateKeyValue stores or updates a simple key/value pair.
|
||||||
func (tx *pgSession) UpdateKeyValue(key, value string) error {
|
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.
|
// FindKeyValue retrieves a value from the given key.
|
||||||
func (tx *pgSession) FindKeyValue(key string) (value string, found bool, err error) {
|
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
|
// AcquireLock acquires a brand new lock in the database with a given name
|
||||||
|
Loading…
Reference in New Issue
Block a user