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