| 20 | namespace codec { |
| 21 | |
| 22 | bool DecodeRpcRow(const butil::IOBuf& buf, size_t offset, size_t size, size_t slice_num, hybridse::codec::Row* row) { |
| 23 | if (row == nullptr) { |
| 24 | return false; |
| 25 | } |
| 26 | if (slice_num == 0 || size == 0) { |
| 27 | *row = hybridse::codec::Row(); |
| 28 | return true; |
| 29 | } |
| 30 | size_t cur_offset = offset; |
| 31 | if (cur_offset >= buf.size()) { |
| 32 | LOG(WARNING) << "Offset " << cur_offset << " out of bound, buf size=" << buf.size(); |
| 33 | return false; |
| 34 | } |
| 35 | for (size_t i = 0; i < slice_num; ++i) { |
| 36 | uint32_t slice_size; |
| 37 | buf.copy_to(&slice_size, sizeof(uint32_t), cur_offset + 2); |
| 38 | size_t next_offset; |
| 39 | if (slice_size == 0) { |
| 40 | next_offset = cur_offset + 2 + sizeof(uint32_t); |
| 41 | } else { |
| 42 | next_offset = cur_offset + slice_size; |
| 43 | } |
| 44 | if (next_offset > buf.size()) { |
| 45 | LOG(WARNING) << "Size " << slice_size << " for " << i |
| 46 | << "th row slice out of bound, buf size=" << buf.size() << " cur offset=" << cur_offset; |
| 47 | return false; |
| 48 | } |
| 49 | if (slice_size == 0) { |
| 50 | if (i == 0) { |
| 51 | *row = hybridse::codec::Row(); |
| 52 | } else { |
| 53 | row->Append(hybridse::base::RefCountedSlice()); |
| 54 | } |
| 55 | } else { |
| 56 | int8_t* slice_buf = reinterpret_cast<int8_t*>(malloc(slice_size)); |
| 57 | buf.copy_to(slice_buf, slice_size, cur_offset); |
| 58 | if (i == 0) { |
| 59 | *row = hybridse::codec::Row(hybridse::base::RefCountedSlice::CreateManaged(slice_buf, slice_size)); |
| 60 | } else { |
| 61 | row->Append(hybridse::base::RefCountedSlice::CreateManaged(slice_buf, slice_size)); |
| 62 | } |
| 63 | } |
| 64 | cur_offset = next_offset; |
| 65 | } |
| 66 | if (offset + size != cur_offset) { |
| 67 | LOG(WARNING) << "Illegal total row size " << (cur_offset - offset) << ", expect size=" << size; |
| 68 | return false; |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool EncodeRpcRow(const hybridse::codec::Row& row, butil::IOBuf* buf, size_t* total_size) { |
| 74 | if (buf == nullptr) { |