github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/pehrest/hash-delete-key-history.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package pehrest
     4  
     5  import (
     6  	"../authorization"
     7  	"../ganjine"
     8  	"../protocol"
     9  	"../service"
    10  	"../srpc"
    11  	"../syllab"
    12  )
    13  
    14  var HashDeleteKeyHistoryService = hashDeleteKeyHistoryService{
    15  	Service: service.New("urn:giti:index.protocol:service:hash-delete-key-history", "", protocol.Software_PreAlpha, 1587282740).
    16  		SetDetail(protocol.LanguageEnglish, "Index Hash - Delete Key History",
    17  			"Delete all records associate to given IndexKey and delete indexKey itself!",
    18  			[]string{}).
    19  		SetAuthorization(protocol.CRUDDelete, protocol.UserType_App).Expired(0, ""),
    20  }
    21  
    22  type hashDeleteKeyHistoryService struct {
    23  	service.Service
    24  }
    25  
    26  func (ser *hashDeleteKeyHistoryService) Process(req HashDeleteKeyHistoryReq) (err protocol.Error) {
    27  	var hashIndex = IndexHash{
    28  		RecordID: req.IndexKey,
    29  	}
    30  	var recordsID [][32]byte
    31  	recordsID, err = hashIndex.Get(0, 0)
    32  	var ln = len(recordsID)
    33  	for i := 0; i < ln; i++ {
    34  		err = ganjine.DeleteRecord(&ganjine.DeleteRecordReq{Type: req.Type, RecordID: recordsID[i]})
    35  		if err != nil {
    36  			// TODO::: Can we easily return error if two nodes do their job and just one node connection lost??
    37  			return
    38  		}
    39  	}
    40  
    41  	err = HashDeleteKey(&HashDeleteKeyReq{Type: req.Type, IndexKey: req.IndexKey})
    42  	if err != nil {
    43  		// TODO::: Can we easily return error if two nodes do their job and just one node connection lost??
    44  		return
    45  	}
    46  
    47  	return
    48  }
    49  
    50  func (ser *hashDeleteKeyHistoryService) ServeSRPC(st protocol.Stream) (err protocol.Error) {
    51  	if st.Connection.UserID != protocol.OS.AppManifest().AppUUID() {
    52  		// TODO::: Attack??
    53  		err = authorization.ErrUserNotAllow
    54  		return
    55  	}
    56  
    57  	var req = &HashDeleteKeyHistoryReq{}
    58  	req.FromSyllab(srpc.GetPayload(st.IncomePayload))
    59  
    60  	err = ser.Process(req)
    61  	st.OutcomePayload = make([]byte, srpc.MinLength)
    62  	return
    63  }
    64  
    65  func (ser *hashDeleteKeyHistoryService) Do(req HashDeleteKeyHistoryReq) (err protocol.Error) {
    66  	return
    67  }
    68  func (ser *hashDeleteKeyHistoryService) doSRPC(req HashDeleteKeyHistoryReq) (err protocol.Error) {
    69  	var node protocol.ApplicationNode
    70  	node, err = protocol.App.GetNodeByObjectID(req.IndexKey)
    71  	if err != nil {
    72  		return
    73  	}
    74  
    75  	if node.Status() == protocol.ApplicationState_LocalNode {
    76  		return hashDeleteKeyHistory(req)
    77  	}
    78  
    79  	_, err = srpc.SendBidirectionalRequest(node.Conn(), ser, &req)
    80  	return
    81  }
    82  
    83  // HashDeleteKeyHistoryReq is request structure of HashDeleteKeyHistory()
    84  type HashDeleteKeyHistoryReq struct {
    85  	Type     ganjine.RequestType
    86  	IndexKey [32]byte
    87  }
    88  
    89  /*
    90  	-- Syllab Encoder & Decoder --
    91  */
    92  
    93  // FromSyllab decode from buf to req
    94  // Due to this service just use internally, It skip check buf size syllab rule! Panic occur if bad request received!
    95  func (req *HashDeleteKeyHistoryReq) FromSyllab(payload []byte, stackIndex uint32) {
    96  	req.Type = ganjine.RequestType(syllab.GetUInt8(buf, 0))
    97  	copy(req.IndexKey[:], buf[1:])
    98  	return
    99  }
   100  
   101  // ToSyllab encode req to buf
   102  func (req *HashDeleteKeyHistoryReq) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   103  	buf = make([]byte, req.LenAsSyllab()+4) // +4 for sRPC ID instead get offset argument
   104  	syllab.SetUInt8(buf, 4, uint8(req.Type))
   105  	copy(buf[5:], req.IndexKey[:])
   106  	return
   107  }
   108  
   109  func (req *HashDeleteKeyHistoryReq) LenOfSyllabStack() uint32 {
   110  	return 33
   111  }
   112  
   113  func (req *HashDeleteKeyHistoryReq) LenOfSyllabHeap() (ln uint32) {
   114  	return
   115  }
   116  
   117  func (req *HashDeleteKeyHistoryReq) LenAsSyllab() uint64 {
   118  	return uint64(req.LenOfSyllabStack() + req.LenOfSyllabHeap())
   119  }