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

     1  package themis
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  
     7  	"github.com/insionng/yougam/libraries/juju/errors"
     8  	"github.com/insionng/yougam/libraries/ngaut/log"
     9  	"github.com/insionng/yougam/libraries/pingcap/go-hbase"
    10  	"github.com/insionng/yougam/libraries/pingcap/go-hbase/iohelper"
    11  )
    12  
    13  type themisSecondaryLock struct {
    14  	*themisLock
    15  	primaryCoordinate *hbase.ColumnCoordinate
    16  }
    17  
    18  func newThemisSecondaryLock() *themisSecondaryLock {
    19  	return &themisSecondaryLock{
    20  		themisLock: &themisLock{
    21  			clientAddr: "null",
    22  		},
    23  		primaryCoordinate: &hbase.ColumnCoordinate{},
    24  	}
    25  }
    26  
    27  func (l *themisSecondaryLock) Primary() Lock {
    28  	pl := newThemisPrimaryLock()
    29  	pl.coordinate = l.primaryCoordinate
    30  	pl.ts = l.ts
    31  	pl.clientAddr = l.clientAddr
    32  	pl.addSecondary(l.coordinate, l.typ)
    33  	return pl
    34  }
    35  
    36  func (l *themisSecondaryLock) Secondaries() []Lock {
    37  	return nil
    38  }
    39  
    40  func (l *themisSecondaryLock) Role() LockRole {
    41  	return RoleSecondary
    42  }
    43  
    44  func (l *themisSecondaryLock) Encode() []byte {
    45  	buf := bytes.NewBuffer(nil)
    46  	binary.Write(buf, binary.BigEndian, uint8(0))
    47  	l.themisLock.write(buf)
    48  	// TODO: handle error, now just log
    49  	if err := l.primaryCoordinate.Write(buf); err != nil {
    50  		log.Warnf("write error, primary coordinate: %s, buf: %s, err: %v", l, buf, err)
    51  	}
    52  	return buf.Bytes()
    53  }
    54  
    55  func (l *themisSecondaryLock) parse(r iohelper.ByteMultiReader) error {
    56  	l.themisLock.parse(r)
    57  	primary := &hbase.ColumnCoordinate{}
    58  	err := primary.ParseField(r)
    59  	if err != nil {
    60  		return errors.Trace(err)
    61  	}
    62  	l.primaryCoordinate = primary
    63  	return nil
    64  }