github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/go-hbase/result.go (about)

     1  package hbase
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/insionng/yougam/libraries/pingcap/go-hbase/proto"
     7  )
     8  
     9  type Kv struct {
    10  	Row   []byte
    11  	Ts    uint64
    12  	Value []byte
    13  	// history results
    14  	Values map[uint64][]byte
    15  	Column
    16  }
    17  
    18  func (kv *Kv) String() string {
    19  	if kv == nil {
    20  		return "<nil>"
    21  	}
    22  	return fmt.Sprintf("Kv(%+v)", *kv)
    23  }
    24  
    25  type ResultRow struct {
    26  	Row           []byte
    27  	Columns       map[string]*Kv
    28  	SortedColumns []*Kv
    29  }
    30  
    31  func (r *ResultRow) String() string {
    32  	if r == nil {
    33  		return "<nil>"
    34  	}
    35  	return fmt.Sprintf("ResultRow(%+v)", *r)
    36  }
    37  
    38  func NewResultRow(result *proto.Result) *ResultRow {
    39  	// empty response
    40  	if len(result.GetCell()) == 0 {
    41  		return nil
    42  	}
    43  	res := &ResultRow{}
    44  	res.Columns = make(map[string]*Kv)
    45  	res.SortedColumns = make([]*Kv, 0)
    46  
    47  	for _, cell := range result.GetCell() {
    48  		res.Row = cell.GetRow()
    49  
    50  		col := &Kv{
    51  			Row: res.Row,
    52  			Column: Column{
    53  				Family: cell.GetFamily(),
    54  				Qual:   cell.GetQualifier(),
    55  			},
    56  			Value: cell.GetValue(),
    57  			Ts:    cell.GetTimestamp(),
    58  		}
    59  
    60  		colName := string(col.Column.Family) + ":" + string(col.Column.Qual)
    61  
    62  		if v, exists := res.Columns[colName]; exists {
    63  			// renew the same cf result
    64  			if col.Ts > v.Ts {
    65  				v.Value = col.Value
    66  				v.Ts = col.Ts
    67  			}
    68  			v.Values[col.Ts] = col.Value
    69  		} else {
    70  			col.Values = map[uint64][]byte{col.Ts: col.Value}
    71  			res.Columns[colName] = col
    72  			res.SortedColumns = append(res.SortedColumns, col)
    73  		}
    74  	}
    75  	return res
    76  }