-
Notifications
You must be signed in to change notification settings - Fork 5
/
hash-delete-value.go
135 lines (113 loc) · 3.71 KB
/
hash-delete-value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* For license and copyright information please see LEGAL file in repository */
package pehrest
import (
"../achaemenid"
"../authorization"
er "../error"
"../ganjine"
gs "../ganjine-services"
lang "../language"
"../srpc"
"../syllab"
)
// HashDeleteValueService store details about HashDeleteValue service
var HashDeleteValueService = achaemenid.Service{
ID: 733788081,
IssueDate: 1587282740,
ExpiryDate: 0,
ExpireInFavorOf: "", // English name of favor service just to show off!
ExpireInFavorOfID: 0,
Status: achaemenid.ServiceStatePreAlpha,
Authorization: authorization.Service{
CRUD: authorization.CRUDDelete,
UserType: authorization.UserTypeApp,
},
Name: map[lang.Language]string{
lang.LanguageEnglish: "Index Hash - Delete Value",
},
Description: map[lang.Language]string{
lang.LanguageEnglish: "Delete the value from exiting index hash key",
},
TAGS: []string{
"",
},
SRPCHandler: HashDeleteValueSRPC,
}
// HashDeleteValueSRPC is sRPC handler of HashDeleteValue service.
func HashDeleteValueSRPC(st *achaemenid.Stream) {
if st.Connection.UserID != achaemenid.Server.AppID {
// TODO::: Attack??
st.Err = ganjine.ErrNotAuthorizeRequest
return
}
var req = &HashDeleteValueReq{}
req.SyllabDecoder(srpc.GetPayload(st.IncomePayload))
st.Err = HashDeleteValue(req)
st.OutcomePayload = make([]byte, 4)
}
// HashDeleteValueReq is request structure of HashDeleteValue()
type HashDeleteValueReq struct {
Type gs.RequestType
IndexKey [32]byte
IndexValue [32]byte
}
// HashDeleteValue delete the value from exiting index key
func HashDeleteValue(req *HashDeleteValueReq) (err *er.Error) {
if req.Type == gs.RequestTypeBroadcast {
// tell other node that this node handle request and don't send this request to other nodes!
req.Type = gs.RequestTypeStandalone
var reqEncoded = req.SyllabEncoder()
// send request to other related nodes
for i := 1; i < len(ganjine.Cluster.Replications.Zones); i++ {
var st *achaemenid.Stream
st, err = ganjine.Cluster.Replications.Zones[i].Nodes[ganjine.Cluster.Node.ID].Conn.MakeOutcomeStream(0)
if err != nil {
// TODO::: Can we easily return error if two nodes did their job and not have enough resource to send request to final node??
return
}
st.Service = &achaemenid.Service{ID: 733788081}
st.OutcomePayload = reqEncoded
err = achaemenid.SrpcOutcomeRequestHandler( st)
if err != nil {
// TODO::: Can we easily return error if two nodes do their job and just one node connection lost??
return
}
// TODO::: Can we easily return response error without handle some known situations??
err = st.Err
}
}
// Do for i=0 as local node
var hashIndex = IndexHash{
RecordID: req.IndexKey,
}
err = hashIndex.Delete(req.IndexValue)
return
}
/*
-- Syllab Encoder & Decoder --
*/
// SyllabDecoder decode from buf to req
// Due to this service just use internally, It skip check buf size syllab rule! Panic occur if bad request received!
func (req *HashDeleteValueReq) SyllabDecoder(buf []byte) {
req.Type = gs.RequestType(syllab.GetUInt8(buf, 0))
copy(req.IndexKey[:], buf[1:])
copy(req.IndexValue[:], buf[33:])
return
}
// SyllabEncoder encode req to buf
func (req *HashDeleteValueReq) SyllabEncoder() (buf []byte) {
buf = make([]byte, req.syllabLen()+4) // +4 for sRPC ID instead get offset argument
syllab.SetUInt8(buf, 4, uint8(req.Type))
copy(buf[5:], req.IndexKey[:])
copy(buf[37:], req.IndexValue[:])
return
}
func (req *HashDeleteValueReq) syllabStackLen() (ln uint32) {
return 65
}
func (req *HashDeleteValueReq) syllabHeapLen() (ln uint32) {
return
}
func (req *HashDeleteValueReq) syllabLen() (ln uint64) {
return uint64(req.syllabStackLen() + req.syllabHeapLen())
}