Skip to content

Commit

Permalink
*: warp kv RunInNewTxn and support get global/table id.
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Oct 21, 2015
1 parent ba475f8 commit 835b53a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
44 changes: 16 additions & 28 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,36 +162,14 @@ func (m *Meta) Begin() (*TMeta, error) {
}

// RunInNewTxn runs fn in a new transaction.
func (m *Meta) RunInNewTxn(retryable bool, fn func(t *TMeta) error) error {
const maxRetryNum = 10
for i := 0; i < maxRetryNum; i++ {
txn, err := m.Begin()
if err != nil {
log.Errorf("RunInNewTxn error - %v", err)
return errors.Trace(err)
}

err = fn(txn)
if retryable && kv.IsRetryableError(err) {
log.Warnf("Retry txn %v", txn)
txn.Rollback()
continue
}
if err != nil {
return errors.Trace(err)
}

err = txn.Commit()
if retryable && kv.IsRetryableError(err) {
log.Warnf("Retry txn %v", txn)
txn.Rollback()
continue
}

return errors.Trace(err)
func (m *Meta) RunInNewTxn(retryable bool, f func(t *TMeta) error) error {
fn := func(txn *structure.TStructure) error {
t := &TMeta{txn: txn}
return errors.Trace(f(t))
}

return errors.Errorf("retry too many times")
err := m.store.RunInNewTxn(retryable, fn)
return errors.Trace(err)
}

// GenGlobalID generates next id globally.
Expand All @@ -202,11 +180,21 @@ func (m *TMeta) GenGlobalID() (int64, error) {
return m.txn.Inc(globalIDKey, 1)
}

// GetGlobalID gets current global id.
func (m *TMeta) GetGlobalID() (int64, error) {
return m.txn.GetInt64(globalIDKey)
}

// GenAutoTableID adds step to the auto id of the table and returns the sum.
func (m *TMeta) GenAutoTableID(tableID int64, step int64) (int64, error) {
return m.txn.Inc([]byte(AutoIDKey(tableID)), step)
}

// GetAutoTableID gets current auto id with table id.
func (m *TMeta) GetAutoTableID(tableID int64) (int64, error) {
return m.txn.GetInt64([]byte(AutoIDKey(tableID)))
}

// GetSchemaVersion gets current global schema version.
func (m *TMeta) GetSchemaVersion() (int64, error) {
return m.txn.GetInt64(SchemaMetaVersionKey)
Expand Down
21 changes: 21 additions & 0 deletions meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ func (s *testSuite) TestMeta(c *C) {
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(1))

n, err = t.GetGlobalID()
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(1))

n, err = t.GenAutoTableID(1, 10)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(10))

n, err = t.GetAutoTableID(1)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(10))

n, err = t.GetSchemaVersion()
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(0))
Expand Down Expand Up @@ -163,4 +171,17 @@ func (s *testSuite) TestMeta(c *C) {

err = t.Commit()
c.Assert(err, IsNil)

fn := func(txn *meta.TMeta) error {
n, err = txn.GenSchemaVersion()
c.Assert(err, IsNil)

n1, err := txn.GetSchemaVersion()
c.Assert(err, IsNil)
c.Assert(n, Equals, n1)
return nil
}

err = m.RunInNewTxn(false, fn)
c.Assert(err, IsNil)
}
10 changes: 10 additions & 0 deletions structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func (s *TStore) Begin() (*TStructure, error) {
return t, nil
}

// RunInNewTxn runs f in a new transaction
func (s *TStore) RunInNewTxn(retryable bool, f func(t *TStructure) error) error {
fn := func(txn kv.Transaction) error {
t := &TStructure{done: false, prefix: s.prefix, txn: txn}
return errors.Trace(f(t))
}
err := kv.RunInNewTxn(s.store, retryable, fn)
return errors.Trace(err)
}

// TStructure supports some simple data structure like string, hash, list, etc... and
// you can use these in a transaction.
type TStructure struct {
Expand Down
12 changes: 12 additions & 0 deletions structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,16 @@ func (s *testStructureSuite) TestHash(c *C) {

err = tx.Commit()
c.Assert(err, IsNil)

fn := func(t *TStructure) error {
err = t.Set(key, []byte("abc"))
c.Assert(err, IsNil)

value, err = t.Get(key)
c.Assert(err, IsNil)
c.Assert(value, DeepEquals, []byte("abc"))
return nil
}
err = s.s.RunInNewTxn(false, fn)
c.Assert(err, IsNil)
}

0 comments on commit 835b53a

Please sign in to comment.