コード例 #1
0
ファイル: result.c プロジェクト: 469306621/Languages
memcached_result_st *memcached_result_create(const memcached_st *memc,
                                             memcached_result_st *ptr)
{
  WATCHPOINT_ASSERT(memc);

  /* Saving malloc calls :) */
  if (ptr)
  {
    ptr->options.is_allocated= false;
  }
  else
  {
    ptr= libmemcached_malloc(memc, sizeof(memcached_result_st));

    if (ptr == NULL)
      return NULL;

    ptr->options.is_allocated= true;
  }

  ptr->options.is_initialized= true;

  _result_init(ptr, memc);

  ptr->root= memc;
  WATCHPOINT_SET(ptr->value.options.is_initialized= false);
  memcached_string_create(memc, &ptr->value, 0);
  WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
  WATCHPOINT_ASSERT(ptr->value.string == NULL);

  return ptr;
}
コード例 #2
0
ファイル: storage.c プロジェクト: bcui6611/libmemcached
static inline memcached_return_t memcached_send(memcached_st *ptr,
                                                const char *master_key, size_t master_key_length,
                                                const char *key, size_t key_length,
                                                const char *value, size_t value_length,
                                                time_t expiration,
                                                uint32_t flags,
                                                uint64_t cas,
                                                uint16_t vbucketid,
                                                memcached_storage_action_t verb)
{
  bool to_write;
  size_t write_length;
  memcached_return_t rc;
  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
  uint32_t server_key;
  memcached_server_write_instance_st instance;

  WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));

  rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
  unlikely (rc != MEMCACHED_SUCCESS)
    return rc;

  unlikely (memcached_server_count(ptr) == 0)
    return MEMCACHED_NO_SERVERS;

  if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
    return MEMCACHED_BAD_KEY_PROVIDED;

  server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
  instance= memcached_server_instance_fetch(ptr, server_key);
  instance->read_buffer_length = 0;

  WATCHPOINT_SET(instance->io_wait_count.read= 0);
  WATCHPOINT_SET(instance->io_wait_count.write= 0);

  if (ptr->flags.binary_protocol)
  {
    rc= memcached_send_binary(ptr, instance, server_key,
                              key, key_length,
                              value, value_length, expiration,
                              flags, cas, vbucketid, verb);
    WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.read > 2, "read IO_WAIT", instance->io_wait_count.read);
    WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.write > 2, "write_IO_WAIT", instance->io_wait_count.write);
  }
  else
  {

    if (cas)
    {
      int check_length;
      check_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
                                    "%s %.*s%.*s %u %llu %lu %llu%s\r\n",
                                    storage_op_string(verb),
                                    (int)ptr->prefix_key_length,
                                    ptr->prefix_key,
                                    (int)key_length, key, flags,
                                    (unsigned long long)expiration, (unsigned long)value_length,
                                    (unsigned long long)cas,
                                    (ptr->flags.no_reply) ? " noreply" : "");
      if (check_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || check_length < 0)
      {
        rc= MEMCACHED_WRITE_FAILURE;
        memcached_io_reset(instance);

        return rc;
      }
      write_length= check_length;
    }
    else
    {
      char *buffer_ptr= buffer;
      const char *command= storage_op_string(verb);

      /* Copy in the command, no space needed, we handle that in the command function*/
      memcpy(buffer_ptr, command, strlen(command));

      /* Copy in the key prefix, switch to the buffer_ptr */
      buffer_ptr= memcpy((buffer_ptr + strlen(command)), ptr->prefix_key, ptr->prefix_key_length);

      /* Copy in the key, adjust point if a key prefix was used. */
      buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key_length ? ptr->prefix_key_length : 0),
                         key, key_length);
      buffer_ptr+= key_length;
      buffer_ptr[0]=  ' ';
      buffer_ptr++;

      write_length= (size_t)(buffer_ptr - buffer);
      int check_length;
      check_length= snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE -(size_t)(buffer_ptr - buffer),
                                    "%u %llu %lu%s\r\n",
                                    flags,
                                    (unsigned long long)expiration, (unsigned long)value_length,
                                    ptr->flags.no_reply ? " noreply" : "");
      if ((size_t)check_length >= MEMCACHED_DEFAULT_COMMAND_SIZE -(size_t)(buffer_ptr - buffer) || check_length < 0)
      {
        rc= MEMCACHED_WRITE_FAILURE;
        memcached_io_reset(instance);

        return rc;
      }

      write_length+= (size_t)check_length;
      WATCHPOINT_ASSERT(write_length < MEMCACHED_DEFAULT_COMMAND_SIZE);
    }

    if (ptr->flags.use_udp && ptr->flags.buffer_requests)
    {
      size_t cmd_size= write_length + value_length + 2;
      if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
        return MEMCACHED_WRITE_FAILURE;
      if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
        memcached_io_write(instance, NULL, 0, true);
    }

    if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
    {
      rc= MEMCACHED_WRITE_FAILURE;
    }
    else
    {
      struct libmemcached_io_vector_st vector[]=
      {
        { .length= write_length, .buffer= buffer },
        { .length= value_length, .buffer= value },
        { .length= 2, .buffer= "\r\n" }
      };