Skip to content

Commit

Permalink
kv: add IsReadOnly method for kv.Transaction.
Browse files Browse the repository at this point in the history
It can be use for query optimization.
  • Loading branch information
coocood committed Mar 16, 2016
1 parent ae387b3 commit d4f5400
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Transaction interface {
SetOption(opt Option, val interface{})
// DelOption deletes an option.
DelOption(opt Option)
// IsReadOnly checks if the transaction has only performed read operations.
IsReadOnly() bool
}

// Snapshot defines the interface for the snapshot fetched from KV store.
Expand Down
7 changes: 7 additions & 0 deletions store/hbase/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type hbaseTxn struct {
tid uint64
valid bool
version kv.Version // commit version
dirty bool
}

func newHbaseTxn(t themis.Txn, storeName string) *hbaseTxn {
Expand All @@ -57,6 +58,7 @@ func (txn *hbaseTxn) Get(k kv.Key) ([]byte, error) {

func (txn *hbaseTxn) Set(k kv.Key, v []byte) error {
log.Debugf("[kv] set %q txn:%d", k, txn.tid)
txn.dirty = true
return txn.us.Set(k, v)
}

Expand All @@ -71,6 +73,7 @@ func (txn *hbaseTxn) Seek(k kv.Key) (kv.Iterator, error) {

func (txn *hbaseTxn) Delete(k kv.Key) error {
log.Debugf("[kv] delete %q txn:%d", k, txn.tid)
txn.dirty = true
return txn.us.Delete(k)
}

Expand Down Expand Up @@ -154,3 +157,7 @@ func (txn *hbaseTxn) LockKeys(keys ...kv.Key) error {
}
return nil
}

func (txn *hbaseTxn) IsReadOnly() bool {
return !txn.dirty
}
7 changes: 7 additions & 0 deletions store/localstore/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type dbTxn struct {
valid bool
version kv.Version // commit version
lockedKeys map[string]struct{} // origin version in snapshot
dirty bool
}

func newTxn(s *dbStore, ver kv.Version) *dbTxn {
Expand All @@ -57,6 +58,7 @@ func (txn *dbTxn) Get(k kv.Key) ([]byte, error) {

func (txn *dbTxn) Set(k kv.Key, data []byte) error {
log.Debugf("[kv] set key:%q, txn:%d", k, txn.tid)
txn.dirty = true
return txn.us.Set(k, data)
}

Expand All @@ -71,6 +73,7 @@ func (txn *dbTxn) Seek(k kv.Key) (kv.Iterator, error) {

func (txn *dbTxn) Delete(k kv.Key) error {
log.Debugf("[kv] delete key:%q, txn:%d", k, txn.tid)
txn.dirty = true
return txn.us.Delete(k)
}

Expand Down Expand Up @@ -132,3 +135,7 @@ func (txn *dbTxn) LockKeys(keys ...kv.Key) error {
}
return nil
}

func (txn *dbTxn) IsReadOnly() bool {
return !txn.dirty
}

0 comments on commit d4f5400

Please sign in to comment.