github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/object/service-save-sdk.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package object 4 5 import ( 6 "crypto/sha512" 7 8 etime "../earth-time" 9 "../protocol" 10 "../service" 11 "../srpc" 12 "../syllab" 13 ) 14 15 type saveService struct { 16 service.Service 17 } 18 19 var SaveService = saveService{ 20 Service: service.New("urn:giti:object.protocol:service:save", "", protocol.ServiceStatePreAlpha, 1587282740). 21 SetDetail(protocol.LanguageEnglish, "Save", 22 `Write the object to storage engine. To replace old one you need to delete old`, 23 []string{}). 24 SetAuthorization(protocol.CRUDCreate, protocol.UserTypeApp).Expired(0, ""), 25 } 26 27 func (ser *saveService) DoSRPC(data protocol.Codec) (metadata protocol.ObjectMetadata, err protocol.Error) { 28 var object = make([]byte, MetadataLength, int(MetadataLength)+data.Len()) 29 var objectMetadata = Metadata(object) 30 metadata = objectMetadata 31 objectMetadata.setWriteTime(etime.Now()) 32 objectMetadata.setMediaTypeID(data.MediaType().URN().ID()) 33 objectMetadata.setCompressTypeID(data.CompressType().URN().ID()) 34 objectMetadata.setDataLength(data.Len()) 35 36 object = data.MarshalTo(object[MetadataLength:]) 37 var objectID = sha512.Sum512_256(object[32:]) 38 objectMetadata.setID(objectID) 39 40 var node protocol.ApplicationNode 41 node, err = protocol.App.GetNodeByObjectID(objectID) 42 if err != nil { 43 return 44 } 45 46 var req = SaveRequest{ 47 requestType: RequestTypeBroadcast, 48 object: object, 49 } 50 if node.Status() == protocol.ApplicationStateLocalNode { 51 err = save(req) 52 return 53 } 54 55 _, err = srpc.HandleOutcomeRequest(node.Conn(), ser, &req) 56 return 57 } 58 59 /* 60 Service request and response shape 61 */ 62 63 type saveRequest interface { 64 RequestType() RequestType 65 Object() []byte 66 } 67 68 /* 69 Service Request 70 */ 71 72 // SaveRequest is request structure of Save() 73 type SaveRequest struct { 74 requestType RequestType 75 object []byte 76 } 77 78 // methods to implements saveRequest interface 79 func (req *SaveRequest) RequestType() RequestType { return req.requestType } 80 func (req *SaveRequest) Object() (objectID []byte) { return req.object } 81 func (req *SaveRequest) SetRequestType(rt RequestType) { req.requestType = rt } 82 83 // methods to implements protocol.Syllab interface 84 func (req *SaveRequest) CheckSyllab(payload []byte) (err protocol.Error) { 85 if len(payload) < int(req.LenOfSyllabStack()) { 86 err = syllab.ErrShortArrayDecode 87 } 88 return 89 } 90 func (req *SaveRequest) FromSyllab(payload []byte, stackIndex uint32) { 91 req.requestType = RequestType(syllab.GetUInt8(payload, 0)) 92 req.object = syllab.GetByteArray(payload, 1) 93 } 94 func (req *SaveRequest) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) { 95 syllab.SetUInt8(payload, 4, uint8(req.requestType)) 96 freeHeapIndex = syllab.SetByteArray(payload, req.object, stackIndex, heapIndex) 97 return 98 } 99 func (req *SaveRequest) LenAsSyllab() uint64 { 100 return uint64(req.LenOfSyllabStack() + req.LenOfSyllabHeap()) 101 } 102 func (req *SaveRequest) LenOfSyllabStack() uint32 { return 9 } 103 func (req *SaveRequest) LenOfSyllabHeap() (ln uint32) { return uint32(len(req.object)) } 104 105 type saveRequestSyllab []byte 106 107 // methods to implements saveRequest interface 108 func (req saveRequestSyllab) RequestType() RequestType { return RequestType(syllab.GetUInt8(req, 0)) } 109 func (req saveRequestSyllab) Object() (objectID []byte) { return syllab.GetByteArray(req, 1) } 110 func (req saveRequestSyllab) SetRequestType(rt RequestType) { syllab.SetUInt8(req, 0, uint8(rt)) } 111 112 // methods to implements protocol.Syllab interface 113 func (req saveRequestSyllab) CheckSyllab(payload []byte) (err protocol.Error) { 114 if len(req) < int(req.LenOfSyllabStack()) { 115 err = syllab.ErrShortArrayDecode 116 } 117 return 118 } 119 func (req saveRequestSyllab) FromSyllab(payload []byte, stackIndex uint32) { 120 // err = ErrSourceNotChangeable 121 } 122 func (req saveRequestSyllab) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) { 123 copy(payload[stackIndex:], req) 124 return heapIndex 125 } 126 func (req saveRequestSyllab) LenAsSyllab() uint64 { return 9 } 127 func (req saveRequestSyllab) LenOfSyllabStack() uint32 { return 9 } 128 func (req saveRequestSyllab) LenOfSyllabHeap() (ln uint32) { return uint32(len(req) - 9) }