github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/pehrest/hash-transaction-get-values.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package pehrest
     4  
     5  import (
     6  	"../authorization"
     7  	"../convert"
     8  	"../ganjine"
     9  	"../protocol"
    10  	"../srpc"
    11  	"../syllab"
    12  )
    13  
    14  // HashTransactionGetValuesService store details about HashTransactionGetValues service
    15  var HashTransactionGetValuesService = service.Service{
    16  	URN:                "urn:giti:index.protocol:service:hash-transaction-get-values",
    17  	Domain:             DomainName,
    18  	ID:                 6289164385419840057,
    19  	IssueDate:          1587282740,
    20  	ExpiryDate:         0,
    21  	ExpireInFavorOfURN: "",
    22  	ExpireInFavorOfID:  0,
    23  	Status:             protocol.Software_PreAlpha,
    24  
    25  	Authorization: authorization.Service{
    26  		CRUD:     authorization.CRUDRead,
    27  		UserType: protocol.UserType_App,
    28  	},
    29  
    30  	Detail: map[protocol.LanguageID]service.ServiceDetail{
    31  		protocol.LanguageEnglish: {
    32  			Name: "Index Hash - Transaction Get Values",
    33  			Description: `Find records by indexes that store before in consistently!
    34  It will get index from transaction managers not indexes nodes!`,
    35  			TAGS: []string{},
    36  		},
    37  	},
    38  
    39  	SRPCHandler: HashTransactionGetValuesSRPC,
    40  }
    41  
    42  // HashTransactionGetValues find records by indexes that store before in consistently!
    43  // It will get index from transaction managers not indexes nodes!
    44  func HashTransactionGetValues(req *HashTransactionGetValuesReq) (res *HashTransactionGetValuesRes, err protocol.Error) {
    45  	var node protocol.ApplicationNode
    46  	node, err = protocol.App.GetNodeByStorage(req.MediaTypeID, req.IndexKey)
    47  	if err != nil {
    48  		return
    49  	}
    50  
    51  	if node.Node.State == protocol.ApplicationState_LocalNode {
    52  		return HashTransactionGetValues(req)
    53  	}
    54  
    55  	var st protocol.Stream
    56  	st, err = node.Conn.MakeOutcomeStream(0)
    57  	if err != nil {
    58  		return
    59  	}
    60  
    61  	st.Service = &HashTransactionGetValuesService
    62  	st.OutcomePayload = req.ToSyllab()
    63  
    64  	err = node.Conn.Send(st)
    65  	if err != nil {
    66  		return
    67  	}
    68  
    69  	res = &HashTransactionGetValuesRes{}
    70  	res.FromSyllab(srpc.GetPayload(st.IncomePayload))
    71  	return
    72  }
    73  
    74  // HashTransactionGetValuesSRPC is sRPC handler of HashTransactionGetValues service.
    75  func HashTransactionGetValuesSRPC(st protocol.Stream) {
    76  	if st.Connection.UserID != protocol.OS.AppManifest().AppUUID() {
    77  		// TODO::: Attack??
    78  		err = authorization.ErrUserNotAllow
    79  		return
    80  	}
    81  
    82  	var req = &HashTransactionGetValuesReq{}
    83  	req.FromSyllab(srpc.GetPayload(st.IncomePayload))
    84  
    85  	var res *HashTransactionGetValuesRes
    86  	res, err = HashTransactionGetValues(req)
    87  	if err != nil {
    88  		return
    89  	}
    90  
    91  	st.OutcomePayload = res.ToSyllab()
    92  }
    93  
    94  // HashTransactionGetValuesReq is request structure of HashTransactionGetValues()
    95  type HashTransactionGetValuesReq struct {
    96  	IndexKey [32]byte
    97  }
    98  
    99  // HashTransactionGetValuesRes is response structure of HashTransactionGetValues()
   100  type HashTransactionGetValuesRes struct {
   101  	IndexValues [][32]byte // Max 128 record return
   102  }
   103  
   104  // HashTransactionGetValues find records by indexes that store before in consistently!
   105  func HashTransactionGetValues(req *HashTransactionGetValuesReq) (res *HashTransactionGetValuesRes, err protocol.Error) {
   106  	res = &HashTransactionGetValuesRes{
   107  		// get index from transaction managers not indexes nodes
   108  		IndexValues: ganjine.Cluster.TransactionManager.GetIndexRecords(req.IndexKey),
   109  	}
   110  	return
   111  }
   112  
   113  /*
   114  	-- Syllab Encoder & Decoder --
   115  */
   116  
   117  // FromSyllab decode from buf to req
   118  // Due to this service just use internally, It skip check buf size syllab rule! Panic occur if bad request received!
   119  func (req *HashTransactionGetValuesReq) FromSyllab(payload []byte, stackIndex uint32) {
   120  	copy(req.IndexKey[:], buf[:])
   121  	return
   122  }
   123  
   124  // ToSyllab encode req to buf
   125  func (req *HashTransactionGetValuesReq) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   126  	buf = make([]byte, req.LenAsSyllab()+4) // +4 for sRPC ID instead get offset argument
   127  	copy(buf[4:], req.IndexKey[:])
   128  	return
   129  }
   130  
   131  func (req *HashTransactionGetValuesReq) LenOfSyllabStack() uint32 {
   132  	return 32
   133  }
   134  
   135  func (req *HashTransactionGetValuesReq) LenOfSyllabHeap() (ln uint32) {
   136  	return
   137  }
   138  
   139  func (req *HashTransactionGetValuesReq) LenAsSyllab() uint64 {
   140  	return uint64(req.LenOfSyllabStack() + req.LenOfSyllabHeap())
   141  }
   142  
   143  // FromSyllab decode from buf to req
   144  // Due to this service just use internally, It skip check buf size syllab rule! Panic occur if bad request received!
   145  func (res *HashTransactionGetValuesRes) FromSyllab(payload []byte, stackIndex uint32) {
   146  	buf = buf[8:]
   147  	res.IndexValues = convert.UnsafeByteSliceTo32ByteArraySlice(buf)
   148  	return
   149  }
   150  
   151  // ToSyllab encode req to buf
   152  func (res *HashTransactionGetValuesRes) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   153  	// Due to just have one field in res structure we skip set address of res.IndexValues in buf
   154  	// syllab.SetUInt32(buf, 4, res.LenOfSyllabStack())
   155  	syllab.SetUInt32(buf, 8, uint32(len(res.IndexValues)))
   156  	copy(buf[res.LenOfSyllabStack():], convert.Unsafe32ByteArraySliceToByteSlice(res.IndexValues))
   157  	return
   158  }
   159  
   160  func (res *HashTransactionGetValuesRes) LenOfSyllabStack() uint32 {
   161  	return 8
   162  }
   163  
   164  func (res *HashTransactionGetValuesRes) LenOfSyllabHeap() (ln uint32) {
   165  	return
   166  }
   167  
   168  func (res *HashTransactionGetValuesRes) LenAsSyllab() uint64 {
   169  	return uint64(res.LenOfSyllabStack() + res.LenOfSyllabHeap())
   170  }