github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/object/service-write-sdk.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package object
     4  
     5  import (
     6  	"../protocol"
     7  	"../service"
     8  	"../srpc"
     9  	"../syllab"
    10  )
    11  
    12  var WriteService = writeService{
    13  	Service: service.New("urn:giti:object.protocol:service:write", "", protocol.ServiceStatePreAlpha, 1587282740).
    14  		SetDetail(protocol.LanguageEnglish, "Write",
    15  			`write some part of a object! Don't use this service until you force to use!
    16  Recalculate checksum do in database server that is not so efficient!`,
    17  			[]string{}).
    18  		SetAuthorization(protocol.CRUDUpdate, protocol.UserTypeApp).Expired(0, ""),
    19  }
    20  
    21  type writeService struct {
    22  	service.Service
    23  }
    24  
    25  func (ser *writeService) DoSRPC(req *WriteReq) (err protocol.Error) {
    26  	var node protocol.ApplicationNode
    27  	node, err = protocol.App.GetNodeByObjectID(req.objectID)
    28  	if err != nil {
    29  		return
    30  	}
    31  
    32  	if node.Status() == protocol.ApplicationStateLocalNode {
    33  		return write(req)
    34  	}
    35  
    36  	_, err = srpc.HandleOutcomeRequest(node.Conn(), ser, req)
    37  	return
    38  }
    39  
    40  /*
    41  	Service request and response shape
    42  */
    43  
    44  type writeRequest interface {
    45  	RequestType() RequestType
    46  	ObjectID() [32]byte
    47  	ObjectStructureID() uint64
    48  	Offset() uint64
    49  	Data() []byte
    50  }
    51  
    52  /*
    53  	Service Request
    54  */
    55  
    56  // WriteReq is request structure of Write()
    57  type WriteReq struct {
    58  	requestType       RequestType
    59  	objectID          [32]byte
    60  	objectStructureID uint64
    61  	offset            uint64 // start location of write data
    62  	data              []byte
    63  }
    64  
    65  // methods to implements writeRequest interface
    66  func (req *WriteReq) RequestType() RequestType      { return req.requestType }
    67  func (req *WriteReq) ObjectID() [32]byte            { return req.objectID }
    68  func (req *WriteReq) ObjectStructureID() uint64     { return req.objectStructureID }
    69  func (req *WriteReq) Offset() uint64                { return req.offset }
    70  func (req *WriteReq) Data() (data []byte)           { return req.data }
    71  func (req *WriteReq) SetRequestType(rt RequestType) { req.requestType = rt }
    72  
    73  // methods to implements protocol.Syllab interface
    74  func (req *WriteReq) CheckSyllab(payload []byte) (err protocol.Error) {
    75  	if len(payload) < int(req.LenOfSyllabStack()) {
    76  		err = syllab.ErrShortArrayDecode
    77  	}
    78  	return
    79  }
    80  func (req *WriteReq) FromSyllab(payload []byte, stackIndex uint32) {
    81  	req.requestType = RequestType(syllab.GetUInt8(payload, 0))
    82  	copy(req.objectID[:], payload[1:])
    83  	req.objectStructureID = syllab.GetUInt64(payload, 33)
    84  	req.offset = syllab.GetUInt64(payload, 41)
    85  	req.data = syllab.GetByteArray(payload, 49)
    86  }
    87  func (req *WriteReq) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
    88  	syllab.SetUInt8(payload, 4, uint8(req.requestType))
    89  	copy(payload[5:], req.objectID[:])
    90  	syllab.SetUInt64(payload, 33, req.objectStructureID)
    91  	syllab.SetUInt64(payload, 41, req.offset)
    92  	freeHeapIndex = syllab.SetByteArray(payload, req.data, 49, heapIndex)
    93  	return
    94  }
    95  func (req *WriteReq) LenAsSyllab() uint64 {
    96  	return uint64(req.LenOfSyllabStack() + req.LenOfSyllabHeap())
    97  }
    98  func (req *WriteReq) LenOfSyllabStack() uint32     { return 57 }
    99  func (req *WriteReq) LenOfSyllabHeap() (ln uint32) { return uint32(len(req.data)) }
   100  
   101  type writeRequestSyllab []byte
   102  
   103  // methods to implements writeRequest interface
   104  func (req writeRequestSyllab) RequestType() RequestType      { return RequestType(syllab.GetUInt8(req, 0)) }
   105  func (req writeRequestSyllab) ObjectID() (oID [32]byte)      { copy(oID[:], req[1:]); return }
   106  func (req writeRequestSyllab) ObjectStructureID() uint64     { return syllab.GetUInt64(req, 33) }
   107  func (req writeRequestSyllab) Offset() uint64                { return syllab.GetUInt64(req, 41) }
   108  func (req writeRequestSyllab) Data() (data []byte)           { return syllab.GetByteArray(req, 1) }
   109  func (req writeRequestSyllab) SetRequestType(rt RequestType) { syllab.SetUInt8(req, 0, uint8(rt)) }
   110  
   111  // methods to implements protocol.Syllab interface
   112  func (req writeRequestSyllab) CheckSyllab(payload []byte) (err protocol.Error) {
   113  	if len(req) < int(req.LenOfSyllabStack()) {
   114  		err = syllab.ErrShortArrayDecode
   115  	}
   116  	return
   117  }
   118  func (req writeRequestSyllab) FromSyllab(payload []byte, stackIndex uint32) {
   119  	// err = ErrSourceNotChangeable
   120  }
   121  func (req writeRequestSyllab) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   122  	copy(payload[stackIndex:], req)
   123  	return heapIndex
   124  }
   125  func (req writeRequestSyllab) LenAsSyllab() uint64 {
   126  	return uint64(req.LenOfSyllabStack() + req.LenOfSyllabHeap())
   127  }
   128  func (req writeRequestSyllab) LenOfSyllabStack() uint32     { return 57 }
   129  func (req writeRequestSyllab) LenOfSyllabHeap() (ln uint32) { return uint32(len(req) - 57) }