Пример #1
0
int QueryRequest::internal_encode(int version, RequestCallback* callback, BufferVec* bufs) const {
  int length = 0;
  uint8_t flags = this->flags();

    // <query> [long string] + <consistency> [short] + <flags> [byte]
  size_t query_buf_size = sizeof(int32_t) + query_.size() +
                          sizeof(uint16_t) + sizeof(uint8_t);
  size_t paging_buf_size = 0;

  if (elements_count() > 0) { // <values> = <n><value_1>...<value_n>
    query_buf_size += sizeof(uint16_t); // <n> [short]
    flags |= CASS_QUERY_FLAG_VALUES;
  }

  if (page_size() > 0) {
    paging_buf_size += sizeof(int32_t); // [int]
    flags |= CASS_QUERY_FLAG_PAGE_SIZE;
  }

  if (!paging_state().empty()) {
    paging_buf_size += sizeof(int32_t) + paging_state().size(); // [bytes]
    flags |= CASS_QUERY_FLAG_PAGING_STATE;
  }

  if (serial_consistency() != 0) {
    paging_buf_size += sizeof(uint16_t); // [short]
    flags |= CASS_QUERY_FLAG_SERIAL_CONSISTENCY;
  }

  if (version >= 3 && callback->timestamp() != CASS_INT64_MIN) {
    paging_buf_size += sizeof(int64_t); // [long]
    flags |= CASS_QUERY_FLAG_DEFAULT_TIMESTAMP;
  }

  {
    bufs->push_back(Buffer(query_buf_size));
    length += query_buf_size;

    Buffer& buf = bufs->back();
    size_t pos = buf.encode_long_string(0, query_.data(), query_.size());
    pos = buf.encode_uint16(pos, callback->consistency());
    pos = buf.encode_byte(pos, flags);

    if (has_names_for_values()) {
      if (version < 3) {
        LOG_ERROR("Protocol version %d does not support named values", version);
        return REQUEST_ERROR_UNSUPPORTED_PROTOCOL;
      }
      buf.encode_uint16(pos, value_names_.size());
      length += copy_buffers_with_names(version, bufs, callback->encoding_cache());
    } else if (elements_count() > 0) {
      buf.encode_uint16(pos, elements_count());
      int32_t result = copy_buffers(version, bufs, callback);
      if (result < 0) return result;
      length += result;
    }
  }

  if (paging_buf_size > 0) {
    bufs->push_back(Buffer(paging_buf_size));
    length += paging_buf_size;

    Buffer& buf = bufs->back();
    size_t pos = 0;

    if (page_size() >= 0) {
      pos = buf.encode_int32(pos, page_size());
    }

    if (!paging_state().empty()) {
      pos = buf.encode_bytes(pos, paging_state().data(), paging_state().size());
    }

    if (serial_consistency() != 0) {
      pos = buf.encode_uint16(pos, serial_consistency());
    }

    if (version >= 3 && callback->timestamp() != CASS_INT64_MIN) {
      pos = buf.encode_int64(pos, callback->timestamp());
    }
  }

  return length;
}
Пример #2
0
int BatchRequest::encode(int version, RequestCallback* callback, BufferVec* bufs) const {
    int length = 0;
    uint8_t flags = 0;

    if (version == 1) {
        return REQUEST_ERROR_UNSUPPORTED_PROTOCOL;
    }

    {
        // <type> [byte] + <n> [short]
        size_t buf_size = sizeof(uint8_t) + sizeof(uint16_t);

        Buffer buf(buf_size);

        size_t pos = buf.encode_byte(0, type_);
        buf.encode_uint16(pos, statements().size());

        bufs->push_back(buf);
        length += buf_size;
    }

    for (BatchRequest::StatementList::const_iterator i = statements_.begin(),
            end = statements_.end(); i != end; ++i) {
        const Statement::Ptr& statement(*i);
        if (statement->has_names_for_values()) {
            callback->on_error(CASS_ERROR_LIB_BAD_PARAMS,
                               "Batches cannot contain queries with named values");
            return REQUEST_ERROR_BATCH_WITH_NAMED_VALUES;
        }
        int32_t result = (*i)->encode_batch(version, bufs, callback);
        if (result < 0) {
            return result;
        }
        length += result;
    }

    {
        // <consistency> [short]
        size_t buf_size = sizeof(uint16_t);
        if (version >= 3) {
            // <flags>[<serial_consistency><timestamp>]
            buf_size += sizeof(uint8_t); // [byte]

            if (serial_consistency() != 0) {
                buf_size += sizeof(uint16_t); // [short]
                flags |= CASS_QUERY_FLAG_SERIAL_CONSISTENCY;
            }

            if (callback->timestamp() != CASS_INT64_MIN) {
                buf_size += sizeof(int64_t); // [long]
                flags |= CASS_QUERY_FLAG_DEFAULT_TIMESTAMP;
            }
        }

        Buffer buf(buf_size);

        size_t pos = buf.encode_uint16(0, callback->consistency());
        if (version >= 3) {
            pos = buf.encode_byte(pos, flags);

            if (serial_consistency() != 0) {
                pos = buf.encode_uint16(pos, serial_consistency());
            }

            if (callback->timestamp() != CASS_INT64_MIN) {
                pos = buf.encode_int64(pos, callback->timestamp());
            }
        }

        bufs->push_back(buf);
        length += buf_size;
    }

    return length;
}
Пример #3
0
int ExecuteRequest::internal_encode(int version, Handler* handler, BufferVec* bufs) const {
  int length = 0;
  uint8_t flags = this->flags();

  const std::string& prepared_id = prepared_->id();

    // <id> [short bytes] + <consistency> [short] + <flags> [byte]
  size_t prepared_buf_size = sizeof(uint16_t) + prepared_id.size() +
                          sizeof(uint16_t) + sizeof(uint8_t);
  size_t paging_buf_size = 0;

  if (elements_count() > 0) { // <values> = <n><value_1>...<value_n>
    prepared_buf_size += sizeof(uint16_t); // <n> [short]
    flags |= CASS_QUERY_FLAG_VALUES;
  }

  if (page_size() >= 0) {
    paging_buf_size += sizeof(int32_t); // [int]
    flags |= CASS_QUERY_FLAG_PAGE_SIZE;
  }

  if (!paging_state().empty()) {
    paging_buf_size += sizeof(int32_t) + paging_state().size(); // [bytes]
    flags |= CASS_QUERY_FLAG_PAGING_STATE;
  }

  if (serial_consistency() != 0) {
    paging_buf_size += sizeof(uint16_t); // [short]
    flags |= CASS_QUERY_FLAG_SERIAL_CONSISTENCY;
  }

  if (version >= 3 && handler->timestamp() != CASS_INT64_MIN) {
    paging_buf_size += sizeof(int64_t); // [long]
    flags |= CASS_QUERY_FLAG_DEFAULT_TIMESTAMP;
  }

  {
    bufs->push_back(Buffer(prepared_buf_size));
    length += prepared_buf_size;

    Buffer& buf = bufs->back();
    size_t pos = buf.encode_string(0,
                                 prepared_id.data(),
                                 prepared_id.size());
    pos = buf.encode_uint16(pos, handler->consistency());
    pos = buf.encode_byte(pos, flags);

    if (elements_count() > 0) {
      buf.encode_uint16(pos, elements_count());
      int32_t result = copy_buffers(version, bufs, handler);
      if (result < 0) return result;
      length += result;
    }
  }

  if (paging_buf_size > 0) {
    bufs->push_back(Buffer(paging_buf_size));
    length += paging_buf_size;

    Buffer& buf = bufs->back();
    size_t pos = 0;

    if (page_size() >= 0) {
      pos = buf.encode_int32(pos, page_size());
    }

    if (!paging_state().empty()) {
      pos = buf.encode_bytes(pos, paging_state().data(), paging_state().size());
    }

    if (serial_consistency() != 0) {
      pos = buf.encode_uint16(pos, serial_consistency());
    }

    if (version >= 3 && handler->timestamp() != CASS_INT64_MIN) {
      pos = buf.encode_int64(pos, handler->timestamp());
    }
  }

  return length;
}