github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/object/service-read-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 ReadService = readService{
    13  	Service: service.New("urn:giti:object.protocol:service:read", "", protocol.ServiceStatePreAlpha, 1587282740).
    14  		SetDetail(protocol.LanguageEnglish, "Read",
    15  			`use to read some part of an object! It must send to proper node otherwise get not found error!
    16  Mostly use to get metadata first to know about object size before get it to split to some shorter part!`,
    17  			[]string{}).
    18  		SetAuthorization(protocol.CRUDRead, protocol.UserTypeApp).Expired(0, ""),
    19  }
    20  
    21  type readService struct {
    22  	service.Service
    23  }
    24  
    25  func (ser *readService) DoSRPC(req ReadRequest) (res readResponse, 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 read(&req)
    34  	}
    35  
    36  	var srpcRes srpc.Response
    37  	srpcRes, err = srpc.HandleOutcomeRequest(node.Conn(), ser, &req)
    38  	if err != nil {
    39  		return
    40  	}
    41  	var srpcResponsePayload = srpcRes.Payload()
    42  	var resAsSyllab = readResponseSyllab(srpcResponsePayload)
    43  	err = resAsSyllab.CheckSyllab(srpcResponsePayload)
    44  	if err != nil {
    45  		return
    46  	}
    47  	res = resAsSyllab
    48  	return
    49  }
    50  
    51  /*
    52  	Service request and response shape
    53  */
    54  
    55  type readRequest interface {
    56  	ObjectID() [32]byte
    57  	ObjectStructureID() uint64
    58  	Offset() uint64
    59  	Limit() uint64
    60  }
    61  
    62  type readResponse interface {
    63  	Data() []byte
    64  }
    65  
    66  /*
    67  	Service Request
    68  */
    69  
    70  // ReadRequest is request structure of Read()
    71  type ReadRequest struct {
    72  	objectID          [32]byte
    73  	objectStructureID uint64
    74  	offset            uint64
    75  	limit             uint64
    76  }
    77  
    78  // methods to implements readRequest interface
    79  func (req *ReadRequest) ObjectID() [32]byte               { return req.objectID }
    80  func (req *ReadRequest) ObjectStructureID() uint64        { return req.objectStructureID }
    81  func (req *ReadRequest) Offset() uint64                   { return req.offset }
    82  func (req *ReadRequest) Limit() uint64                    { return req.limit }
    83  func (req *ReadRequest) SetObjectID(oID [32]byte)         { req.objectID = oID }
    84  func (req *ReadRequest) SetObjectStructureID(osID uint64) { req.objectStructureID = osID }
    85  func (req *ReadRequest) SetOffset(offset uint64)          { req.offset = offset }
    86  func (req *ReadRequest) SetLimit(limit uint64)            { req.limit = limit }
    87  
    88  // methods to implements protocol.Syllab interface
    89  func (req *ReadRequest) CheckSyllab(payload []byte) (err protocol.Error) {
    90  	if len(payload) < int(req.LenOfSyllabStack()) {
    91  		err = syllab.ErrShortArrayDecode
    92  	}
    93  	return
    94  }
    95  func (req *ReadRequest) FromSyllab(payload []byte, stackIndex uint32) {
    96  	copy(req.objectID[:], payload[:])
    97  	req.objectStructureID = syllab.GetUInt64(payload, 32)
    98  	req.offset = syllab.GetUInt64(payload, 40)
    99  	req.limit = syllab.GetUInt64(payload, 48)
   100  }
   101  func (req *ReadRequest) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   102  	copy(payload[0:], req.objectID[:])
   103  	syllab.SetUInt64(payload, 32, req.objectStructureID)
   104  	syllab.SetUInt64(payload, 40, req.offset)
   105  	syllab.SetUInt64(payload, 48, req.limit)
   106  	return heapIndex
   107  }
   108  func (req *ReadRequest) LenAsSyllab() uint64          { return 56 }
   109  func (req *ReadRequest) LenOfSyllabStack() uint32     { return 56 }
   110  func (req *ReadRequest) LenOfSyllabHeap() (ln uint32) { return }
   111  
   112  type readRequestSyllab []byte
   113  
   114  // methods to implements readRequest interface
   115  func (req readRequestSyllab) ObjectID() (objectID [32]byte)    { copy(objectID[:], req[0:]); return }
   116  func (req readRequestSyllab) ObjectStructureID() (osID uint64) { return syllab.GetUInt64(req, 32) }
   117  func (req readRequestSyllab) Offset() uint64                   { return syllab.GetUInt64(req, 40) }
   118  func (req readRequestSyllab) Limit() uint64                    { return syllab.GetUInt64(req, 48) }
   119  
   120  // methods to implements protocol.Syllab interface
   121  func (req readRequestSyllab) CheckSyllab(payload []byte) (err protocol.Error) {
   122  	if len(req) < int(req.LenOfSyllabStack()) {
   123  		err = syllab.ErrShortArrayDecode
   124  	}
   125  	return
   126  }
   127  func (req readRequestSyllab) FromSyllab(payload []byte, stackIndex uint32) {
   128  	// err = ErrSourceNotChangeable
   129  }
   130  func (req readRequestSyllab) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   131  	copy(payload[stackIndex:], req)
   132  	return heapIndex
   133  }
   134  func (req readRequestSyllab) LenAsSyllab() uint64          { return 56 }
   135  func (req readRequestSyllab) LenOfSyllabStack() uint32     { return 56 }
   136  func (req readRequestSyllab) LenOfSyllabHeap() (ln uint32) { return }
   137  
   138  /*
   139  	Service Response
   140  */
   141  
   142  // ReadResponse is response structure of Read Serice
   143  type ReadResponse struct {
   144  	data []byte
   145  }
   146  
   147  // methods to implements readResponse interface
   148  func (req ReadResponse) Data() []byte { return req.data }
   149  
   150  // methods to implements protocol.Syllab interface
   151  func (req ReadResponse) CheckSyllab(payload []byte) (err protocol.Error) {
   152  	if len(payload) < int(req.LenOfSyllabStack()) {
   153  		err = syllab.ErrShortArrayDecode
   154  	}
   155  	return
   156  }
   157  func (res ReadResponse) FromSyllab(payload []byte, stackIndex uint32) {
   158  	res.data = syllab.GetByteArray(payload, stackIndex)
   159  }
   160  func (res ReadResponse) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   161  	freeHeapIndex = syllab.SetByteArray(payload, res.data, stackIndex, heapIndex)
   162  	return
   163  }
   164  func (res ReadResponse) LenAsSyllab() uint64 {
   165  	return uint64(res.LenOfSyllabStack() + res.LenOfSyllabHeap())
   166  }
   167  func (res ReadResponse) LenOfSyllabStack() uint32     { return 8 }
   168  func (res ReadResponse) LenOfSyllabHeap() (ln uint32) { return uint32(len(res.data)) }
   169  
   170  type readResponseSyllab []byte
   171  
   172  // methods to implements readResponse interface
   173  func (res readResponseSyllab) Data() []byte { return syllab.GetByteArray(res, 0) }
   174  
   175  // methods to implements protocol.Syllab interface
   176  func (res readResponseSyllab) CheckSyllab(payload []byte) (err protocol.Error) {
   177  	if len(res) < int(res.LenOfSyllabStack()) {
   178  		err = syllab.ErrShortArrayDecode
   179  	}
   180  	return
   181  }
   182  func (res readResponseSyllab) FromSyllab(payload []byte, stackIndex uint32) {
   183  	// err = ErrSourceNotChangeable
   184  }
   185  func (res readResponseSyllab) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   186  	freeHeapIndex = syllab.SetByteArray(payload, res.Data(), stackIndex, heapIndex)
   187  	return heapIndex
   188  }
   189  func (res readResponseSyllab) LenAsSyllab() uint64 {
   190  	return uint64(res.LenOfSyllabStack() + res.LenOfSyllabHeap())
   191  }
   192  func (res readResponseSyllab) LenOfSyllabStack() uint32     { return 8 }
   193  func (res readResponseSyllab) LenOfSyllabHeap() (ln uint32) { return uint32(len(res) - 8) }