1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include "request.hpp"
18 
19 #include "external.hpp"
20 
21 using namespace datastax::internal::core;
22 
23 extern "C" {
24 
cass_custom_payload_new()25 CassCustomPayload* cass_custom_payload_new() {
26   CustomPayload* payload = new CustomPayload();
27   payload->inc_ref();
28   return CassCustomPayload::to(payload);
29 }
30 
cass_custom_payload_set(CassCustomPayload * payload,const char * name,const cass_byte_t * value,size_t value_size)31 void cass_custom_payload_set(CassCustomPayload* payload, const char* name, const cass_byte_t* value,
32                              size_t value_size) {
33   payload->set(name, SAFE_STRLEN(name), value, value_size);
34 }
35 
cass_custom_payload_set_n(CassCustomPayload * payload,const char * name,size_t name_length,const cass_byte_t * value,size_t value_size)36 void cass_custom_payload_set_n(CassCustomPayload* payload, const char* name, size_t name_length,
37                                const cass_byte_t* value, size_t value_size) {
38   payload->set(name, name_length, value, value_size);
39 }
40 
cass_custom_payload_remove(CassCustomPayload * payload,const char * name)41 void cass_custom_payload_remove(CassCustomPayload* payload, const char* name) {
42   payload->remove(name, SAFE_STRLEN(name));
43 }
44 
cass_custom_payload_remove_n(CassCustomPayload * payload,const char * name,size_t name_length)45 void cass_custom_payload_remove_n(CassCustomPayload* payload, const char* name,
46                                   size_t name_length) {
47   payload->remove(name, name_length);
48 }
49 
cass_custom_payload_free(CassCustomPayload * payload)50 void cass_custom_payload_free(CassCustomPayload* payload) { payload->dec_ref(); }
51 
52 } // extern "C"
53 
set(const char * name,size_t name_length,const uint8_t * value,size_t value_size)54 void CustomPayload::set(const char* name, size_t name_length, const uint8_t* value,
55                         size_t value_size) {
56   Buffer buf(sizeof(uint16_t) + name_length + sizeof(int32_t) + value_size);
57   size_t pos = buf.encode_string(0, name, static_cast<uint16_t>(name_length));
58   buf.encode_bytes(pos, reinterpret_cast<const char*>(value), value_size);
59   items_[String(name, name_length)] = buf;
60 }
61 
encode(BufferVec * bufs) const62 int32_t CustomPayload::encode(BufferVec* bufs) const {
63   int32_t length = 0;
64   for (ItemMap::const_iterator i = items_.begin(), end = items_.end(); i != end; ++i) {
65     const Buffer& buf = i->second;
66     length += buf.size();
67     bufs->push_back(buf);
68   }
69   return length;
70 }
71