github.com/iotexproject/iotex-core@v1.14.1-rc1/db/batch/writeinfo.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package batch 7 8 const ( 9 // Put indicate the type of write operation to be Put 10 Put WriteType = iota 11 // Delete indicate the type of write operation to be Delete 12 Delete 13 ) 14 15 type ( 16 // WriteType is the type of write 17 WriteType uint8 18 19 // WriteInfo is the struct to store Put/Delete operation info 20 WriteInfo struct { 21 writeType WriteType 22 namespace string 23 key []byte 24 value []byte 25 errorMessage string 26 } 27 28 // WriteInfoFilter filters a write 29 WriteInfoFilter func(wi *WriteInfo) bool 30 31 // WriteInfoSerialize serializes a write to bytes 32 WriteInfoSerialize func(wi *WriteInfo) []byte 33 34 // WriteInfoTranslate translates a write info 35 WriteInfoTranslate func(wi *WriteInfo) *WriteInfo 36 ) 37 38 // NewWriteInfo creates a new write info 39 func NewWriteInfo( 40 writeType WriteType, 41 namespace string, 42 key, 43 value []byte, 44 errorMessage string, 45 ) *WriteInfo { 46 return &WriteInfo{ 47 writeType: writeType, 48 namespace: namespace, 49 key: key, 50 value: value, 51 errorMessage: errorMessage, 52 } 53 } 54 55 // Namespace returns the namespace of a write info 56 func (wi *WriteInfo) Namespace() string { 57 return wi.namespace 58 } 59 60 // WriteType returns the type of a write info 61 func (wi *WriteInfo) WriteType() WriteType { 62 return wi.writeType 63 } 64 65 // Key returns a copy of key 66 func (wi *WriteInfo) Key() []byte { 67 key := make([]byte, len(wi.key)) 68 copy(key, wi.key) 69 70 return key 71 } 72 73 // Value returns a copy of value 74 func (wi *WriteInfo) Value() []byte { 75 value := make([]byte, len(wi.value)) 76 copy(value, wi.value) 77 78 return value 79 } 80 81 // Error returns the error Message 82 func (wi *WriteInfo) Error() string { 83 return wi.errorMessage 84 } 85 86 // Serialize serializes the write info 87 func (wi *WriteInfo) Serialize() []byte { 88 lenNamespace, lenKey, lenValue := len(wi.namespace), len(wi.key), len(wi.value) 89 bytes := make([]byte, 1+lenNamespace+lenKey+lenValue) 90 bytes[0] = byte(wi.writeType) 91 copy(bytes[1:], []byte(wi.namespace)) 92 copy(bytes[1+lenNamespace:], wi.key) 93 copy(bytes[1+lenNamespace+lenKey:], wi.value) 94 return bytes 95 } 96 97 // SerializeWithoutWriteType serializes the write info without write type 98 func (wi *WriteInfo) SerializeWithoutWriteType() []byte { 99 lenNamespace, lenKey, lenValue := len(wi.namespace), len(wi.key), len(wi.value) 100 bytes := make([]byte, lenNamespace+lenKey+lenValue) 101 copy(bytes[0:], []byte(wi.namespace)) 102 copy(bytes[lenNamespace:], wi.key) 103 copy(bytes[lenNamespace+lenKey:], wi.value) 104 return bytes 105 }