Skip to content

Commit

Permalink
inspectkv: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Dec 24, 2015
1 parent f8bf6ad commit c09f413
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 51 deletions.
52 changes: 18 additions & 34 deletions inspectkv/inspectkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func GetIndexRecordsCount(txn kv.Transaction, kvIndex kv.Index, startVals []inte

// ScanIndexData scans the index handles and values in a limited number, according to the index information.
// It returns data and the next startVals until it doesn't have data, then returns data is nil and
// the next startVals is the values which can't get data.
// If limit = -1, it returns the index data of the whole.
// the next startVals is the values which can't get data. If startVals = nil and limit = -1,
// it returns the index data of the whole.
func ScanIndexData(txn kv.Transaction, kvIndex kv.Index, startVals []interface{}, limit int64) (
[]*RecordData, []interface{}, error) {
it, _, err := kvIndex.Seek(txn, startVals)
Expand Down Expand Up @@ -136,33 +136,19 @@ func ScanIndexData(txn kv.Transaction, kvIndex kv.Index, startVals []interface{}
return idxRows, nextVals, nil
}

// ScanIndexColData scans the index handles and values in a limited number, according to the corresponding column.
// It returns data and the next startHandle until it doesn't have data, then returns data is nil and
// the next startHandle is the handle which can't get data.
// If limit = -1, it returns the index data of the whole.
func ScanIndexColData(txn kv.Transaction, t table.Table, idx *column.IndexedCol, startHandle, limit int64) (
[]*RecordData, int64, error) {
cols := make([]*column.Col, len(idx.Columns))
for i, col := range idx.Columns {
cols[i] = t.Cols()[col.Offset]
}

return scanTableData(txn, t, cols, startHandle, limit)
}

// CompareIndexData compares index data one by one.
// It returns nil if the data from the index is equal to the data from the table columns,
// otherwise it returns an error with a different set of records.
func CompareIndexData(txn kv.Transaction, t table.Table, idx *column.IndexedCol) error {
err := checkIndexAndCols(txn, t, idx)
err := checkIndexAndRecord(txn, t, idx)
if err != nil {
return errors.Trace(err)
}

return checkColsAndIndex(txn, t, idx)
return checkRecordAndIndex(txn, t, idx)
}

func checkIndexAndCols(txn kv.Transaction, t table.Table, idx *column.IndexedCol) error {
func checkIndexAndRecord(txn kv.Transaction, t table.Table, idx *column.IndexedCol) error {
kvIndex := kv.NewKVIndex(t.IndexPrefix(), idx.Name.L, idx.ID, idx.Unique)
it, err := kvIndex.SeekFirst(txn)
if err != nil {
Expand Down Expand Up @@ -201,7 +187,7 @@ func checkIndexAndCols(txn kv.Transaction, t table.Table, idx *column.IndexedCol
return nil
}

func checkColsAndIndex(txn kv.Transaction, t table.Table, idx *column.IndexedCol) error {
func checkRecordAndIndex(txn kv.Transaction, t table.Table, idx *column.IndexedCol) error {
cols := make([]*column.Col, len(idx.Columns))
for i, col := range idx.Columns {
cols[i] = t.Cols()[col.Offset]
Expand All @@ -227,7 +213,6 @@ func checkColsAndIndex(txn kv.Transaction, t table.Table, idx *column.IndexedCol
return true, nil
}
err := t.IterRecords(txn, string(startKey), cols, filterFunc)

if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -267,37 +252,36 @@ func scanTableData(retriever kv.Retriever, t table.Table, cols []*column.Col, st
return records, nextHandle, nil
}

// ScanTableData scans table row handles and column values in a limited number.
// ScanTableRecord scans table row handles and column values in a limited number.
// It returns data and the next startHandle until it doesn't have data, then returns data is nil and
// the next startHandle is the handle which can't get data.
// If limit = -1, it returns the table data of the whole.
func ScanTableData(retriever kv.Retriever, t table.Table, startHandle, limit int64) (
// the next startHandle is the handle which can't get data. If startHandle = 0 and limit = -1,
// it returns the table data of the whole.
func ScanTableRecord(retriever kv.Retriever, t table.Table, startHandle, limit int64) (
[]*RecordData, int64, error) {
return scanTableData(retriever, t, t.Cols(), startHandle, limit)
}

// ScanSnapshotTableData scans the ver version of the table data in a limited number.
// ScanSnapshotTableRecord scans the ver version of the table data in a limited number.
// It returns data and the next startHandle until it doesn't have data, then returns data is nil and
// the next startHandle is the handle which can't get data.
// If limit = -1, it returns the table data of the whole.
func ScanSnapshotTableData(store kv.Storage, ver kv.Version, t table.Table, startHandle, limit int64) (
// the next startHandle is the handle which can't get data. If startHandle = 0 and limit = -1,
// it returns the table data of the whole.
func ScanSnapshotTableRecord(store kv.Storage, ver kv.Version, t table.Table, startHandle, limit int64) (
[]*RecordData, int64, error) {
snap, err := store.GetSnapshot(ver)
if err != nil {
return nil, 0, errors.Trace(err)
}
defer snap.Release()

records, nextHandle, err := ScanTableData(snap, t, startHandle, limit)
records, nextHandle, err := ScanTableRecord(snap, t, startHandle, limit)

return records, nextHandle, errors.Trace(err)
}

// CompareTableData compares data and the corresponding table data one by one.
// CompareTableRecord compares data and the corresponding table data one by one.
// It returns nil if data is equal to the data that scans from table, otherwise
// it returns an error with a different set of records. If exact is false, only
// compares handle.
func CompareTableData(txn kv.Transaction, t table.Table, data []*RecordData, exact bool) error {
// it returns an error with a different set of records. If exact is false, only compares handle.
func CompareTableRecord(txn kv.Transaction, t table.Table, data []*RecordData, exact bool) error {
var err error
var vals []interface{}
m := make(map[int64]struct{}, len(data))
Expand Down
29 changes: 12 additions & 17 deletions inspectkv/inspectkv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (s *testSuite) TestScan(c *C) {
record2 := &RecordData{Handle: int64(2), Values: []interface{}{int64(20), int64(21)}}
ver, err := s.store.CurrentVersion()
c.Assert(err, IsNil)
records, _, err := ScanSnapshotTableData(s.store, ver, tb, int64(1), 1)
records, _, err := ScanSnapshotTableRecord(s.store, ver, tb, int64(1), 1)
c.Assert(err, IsNil)
c.Assert(records, DeepEquals, []*RecordData{record1})

Expand All @@ -175,25 +175,20 @@ func (s *testSuite) TestScan(c *C) {
txn, err := s.store.Begin()
c.Assert(err, IsNil)

records, nextHandle, err := ScanTableData(txn, tb, int64(1), 1)
records, nextHandle, err := ScanTableRecord(txn, tb, int64(1), 1)
c.Assert(err, IsNil)
c.Assert(records, DeepEquals, []*RecordData{record1})
records, nextHandle, err = ScanTableData(txn, tb, nextHandle, 1)
records, nextHandle, err = ScanTableRecord(txn, tb, nextHandle, 1)
c.Assert(err, IsNil)
c.Assert(records, DeepEquals, []*RecordData{record2})
startHandle := nextHandle
records, nextHandle, err = ScanTableData(txn, tb, startHandle, 1)
records, nextHandle, err = ScanTableRecord(txn, tb, startHandle, 1)
c.Assert(err, IsNil)
c.Assert(records, IsNil)
c.Assert(nextHandle, Equals, startHandle)

idxRow1 := &RecordData{Handle: int64(1), Values: []interface{}{int64(10)}}
idxRow2 := &RecordData{Handle: int64(2), Values: []interface{}{int64(20)}}
idxRows, nextHandle, err := ScanIndexColData(txn, tb, indices[0], 0, 2)
c.Assert(err, IsNil)
c.Assert(idxRows, DeepEquals, []*RecordData{idxRow1, idxRow2})
c.Assert(nextHandle, Equals, startHandle)

kvIndex := kv.NewKVIndex(tb.IndexPrefix(), indices[0].Name.L, indices[0].ID, indices[0].Unique)
idxRows, nextVals, err := ScanIndexData(txn, kvIndex, idxRow1.Values, 2)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -227,7 +222,7 @@ func (s *testSuite) testTableData(c *C, tb table.Table, rs []*RecordData) {
txn, err := s.store.Begin()
c.Assert(err, IsNil)

err = CompareTableData(txn, tb, rs, true)
err = CompareTableRecord(txn, tb, rs, true)
c.Assert(err, IsNil)

cnt, err := GetTableRecordsCount(txn, tb, 0)
Expand All @@ -238,32 +233,32 @@ func (s *testSuite) testTableData(c *C, tb table.Table, rs []*RecordData) {
{Handle: rs[0].Handle},
{Handle: rs[1].Handle},
}
err = CompareTableData(txn, tb, records, false)
err = CompareTableRecord(txn, tb, records, false)
c.Assert(err, IsNil)

record := &RecordData{Handle: rs[1].Handle, Values: []interface{}{int64(30)}}
err = CompareTableData(txn, tb, []*RecordData{rs[0], record}, true)
err = CompareTableRecord(txn, tb, []*RecordData{rs[0], record}, true)
c.Assert(err, NotNil)
diffMsg := newDiffRetError("data", record, rs[1])
c.Assert(err.Error(), DeepEquals, diffMsg)

record.Handle = 3
err = CompareTableData(txn, tb, []*RecordData{rs[0], record, rs[1]}, true)
err = CompareTableRecord(txn, tb, []*RecordData{rs[0], record, rs[1]}, true)
c.Assert(err, NotNil)
diffMsg = newDiffRetError("data", record, nil)
c.Assert(err.Error(), DeepEquals, diffMsg)

err = CompareTableData(txn, tb, []*RecordData{rs[0], rs[1], record}, true)
err = CompareTableRecord(txn, tb, []*RecordData{rs[0], rs[1], record}, true)
c.Assert(err, NotNil)
diffMsg = newDiffRetError("data", record, nil)
c.Assert(err.Error(), DeepEquals, diffMsg)

err = CompareTableData(txn, tb, []*RecordData{rs[0]}, true)
err = CompareTableRecord(txn, tb, []*RecordData{rs[0]}, true)
c.Assert(err, NotNil)
diffMsg = newDiffRetError("data", nil, rs[1])
c.Assert(err.Error(), DeepEquals, diffMsg)

err = CompareTableData(txn, tb, nil, true)
err = CompareTableRecord(txn, tb, nil, true)
c.Assert(err, NotNil)
diffMsg = newDiffRetError("data", nil, rs[0])
c.Assert(err.Error(), DeepEquals, diffMsg)
Expand Down Expand Up @@ -332,7 +327,7 @@ func (s *testSuite) testIndex(c *C, tb table.Table, idx *column.IndexedCol) {

txn, err = s.store.Begin()
c.Assert(err, IsNil)
err = checkColsAndIndex(txn, tb, idx)
err = checkRecordAndIndex(txn, tb, idx)
c.Assert(err, NotNil)
record2 = &RecordData{Handle: int64(5), Values: []interface{}{int64(30)}}
diffMsg = newDiffRetError("index", record1, record2)
Expand Down

0 comments on commit c09f413

Please sign in to comment.