diff --git a/kv/index_iter.go b/kv/index_iter.go index 795028b70e..1a06c41eae 100644 --- a/kv/index_iter.go +++ b/kv/index_iter.go @@ -245,26 +245,33 @@ func (c *kvIndex) SeekFirst(txn Transaction) (iter IndexIterator, err error) { return &indexIter{it: it, idx: c, prefix: c.prefix}, nil } -func (c *kvIndex) Exist(txn Transaction, indexedValues []interface{}, h int64) (int64, bool, error) { +func (c *kvIndex) Exist(txn Transaction, indexedValues []interface{}, h int64) (bool, int64, error) { key, err := c.genIndexKey(indexedValues, h) if err != nil { - return 0, false, errors.Trace(err) + return false, 0, errors.Trace(err) } value, err := txn.Get(key) + if IsErrNotFound(err) { + return false, 0, nil + } if err != nil { - return 0, false, errors.Trace(err) + return false, 0, errors.Trace(err) } // For uniq index, the value of key is handle. if c.unique { handle, err := decodeHandle(value) if err != nil { - return 0, false, errors.Trace(err) + return false, 0, errors.Trace(err) } - return handle, handle == h, nil + if handle != h { + return true, handle, errors.Trace(ErrKeyExists) + } + + return true, handle, nil } - return h, true, nil + return true, h, nil } diff --git a/kv/kv.go b/kv/kv.go index fce314f089..963821e0dd 100644 --- a/kv/kv.go +++ b/kv/kv.go @@ -193,7 +193,7 @@ type Index interface { Create(txn Transaction, indexedValues []interface{}, h int64) error // supports insert into statement Delete(txn Transaction, indexedValues []interface{}, h int64) error // supports delete from statement Drop(txn Transaction) error // supports drop table, drop index statements - Exist(txn Transaction, indexedValues []interface{}, h int64) (int64, bool, error) // supports check index exist + Exist(txn Transaction, indexedValues []interface{}, h int64) (bool, int64, error) // supports check index exist Seek(txn Transaction, indexedValues []interface{}) (iter IndexIterator, hit bool, err error) // supports where clause SeekFirst(txn Transaction) (iter IndexIterator, err error) // supports aggregate min / ascending order by }