~jbingham-gmail/libmemcached/embedded-ct

« back to all changes in this revision

Viewing changes to libmemcached/memcached_delete.c

  • Committer: Trond Norbye
  • Date: 2009-12-04 13:08:17 UTC
  • Revision ID: tn202803@tor01-20091204130817-8oku2f80uq4apr4g
Recreate embedded branch from scratch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
1
2
#include "common.h"
2
 
#include "memcached/protocol_binary.h"
3
3
 
4
4
memcached_return memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
5
5
                                  time_t expiration)
8
8
                                 key, key_length, expiration);
9
9
}
10
10
 
11
 
static inline memcached_return binary_delete(memcached_st *ptr,
12
 
                                             unsigned int server_key,
13
 
                                             const char *key,
14
 
                                             size_t key_length,
15
 
                                             uint8_t flush);
16
 
 
17
11
memcached_return memcached_delete_by_key(memcached_st *ptr,
18
 
                                         const char *master_key, size_t master_key_length,
19
 
                                         const char *key, size_t key_length,
 
12
                                         const char *master_key,
 
13
                                         size_t master_key_length,
 
14
                                         const char *key,
 
15
                                         size_t key_length,
20
16
                                         time_t expiration)
21
17
{
22
 
  uint8_t to_write;
23
 
  size_t send_length;
 
18
  unlikely (expiration)
 
19
    return MEMCACHED_INVALID_ARGUMENTS;
 
20
 
24
21
  memcached_return rc;
25
 
  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
26
22
  unsigned int server_key;
27
23
 
28
24
  LIBMEMCACHED_MEMCACHED_DELETE_START();
29
25
 
30
 
  rc= memcached_validate_key_length(key_length,
31
 
                                    ptr->flags & MEM_BINARY_PROTOCOL);
32
 
  unlikely (rc != MEMCACHED_SUCCESS)
33
 
    return rc;
34
 
 
35
26
  unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
36
27
    return MEMCACHED_NO_SERVERS;
37
28
 
38
29
  server_key= memcached_generate_hash(ptr, master_key, master_key_length);
39
 
  to_write= (uint8_t)((ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1);
40
 
  bool no_reply= (ptr->flags & MEM_NOREPLY);
41
 
 
42
 
  if (ptr->flags & MEM_BINARY_PROTOCOL)
43
 
  {
44
 
    likely (!expiration)
45
 
      rc= binary_delete(ptr, server_key, key, key_length, to_write);
46
 
    else
47
 
      rc= MEMCACHED_INVALID_ARGUMENTS;
48
 
  }
49
 
  else
50
 
  {
51
 
    unlikely (expiration)
52
 
    {
53
 
       if ((ptr->hosts[server_key].major_version == 1 &&
54
 
            ptr->hosts[server_key].minor_version > 2) ||
55
 
           ptr->hosts[server_key].major_version > 1)
56
 
       {
57
 
         rc= MEMCACHED_INVALID_ARGUMENTS;
58
 
         goto error;
59
 
       }
60
 
       else
61
 
       {
62
 
          if (ptr->hosts[server_key].minor_version == 0)
63
 
          {
64
 
             if (no_reply || !to_write)
65
 
             {
66
 
                /* We might get out of sync with the server if we
67
 
                 * send this command to a server newer than 1.2.x..
68
 
                 * disable no_reply and buffered mode.
69
 
                 */
70
 
                to_write= 1;
71
 
                if (no_reply)
72
 
                   memcached_server_response_increment(&ptr->hosts[server_key]);
73
 
                no_reply= false;
74
 
             }
75
 
          }
76
 
          send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
77
 
                                         "delete %s%.*s %u%s\r\n",
78
 
                                         ptr->prefix_key,
79
 
                                         (int) key_length, key,
80
 
                                         (uint32_t)expiration,
81
 
                                         no_reply ? " noreply" :"" );
82
 
       }
83
 
    }
84
 
    else
85
 
       send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
86
 
                                      "delete %s%.*s%s\r\n",
87
 
                                      ptr->prefix_key,
88
 
                                      (int)key_length, key, no_reply ? " noreply" :"");
89
 
 
90
 
    if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
91
 
    {
92
 
      rc= MEMCACHED_WRITE_FAILURE;
93
 
      goto error;
94
 
    }
95
 
 
96
 
    if (ptr->flags & MEM_USE_UDP && !to_write)
97
 
    {
98
 
      if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
99
 
        return MEMCACHED_WRITE_FAILURE;
100
 
      if (send_length + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
101
 
        memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
102
 
    }
103
 
 
104
 
    rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
105
 
  }
106
 
 
107
 
  if (rc != MEMCACHED_SUCCESS)
108
 
    goto error;
109
 
 
110
 
  if (!to_write)
111
 
    rc= MEMCACHED_BUFFERED;
112
 
  else if (!no_reply)
113
 
  {
114
 
    rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
115
 
    if (rc == MEMCACHED_DELETED)
116
 
      rc= MEMCACHED_SUCCESS;
117
 
  }
118
 
 
 
30
  rc= ptr->protocol_hooks->delete(ptr, server_key, key, key_length);
119
31
  if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
120
32
    ptr->delete_trigger(ptr, key, key_length);
121
33
 
122
 
error:
123
34
  LIBMEMCACHED_MEMCACHED_DELETE_END();
124
35
  return rc;
125
36
}
127
38
static inline memcached_return binary_delete(memcached_st *ptr,
128
39
                                             unsigned int server_key,
129
40
                                             const char *key,
130
 
                                             size_t key_length,
131
 
                                             uint8_t flush)
 
41
                                             size_t key_length,
 
42
                                             uint8_t flush)
132
43
{
133
 
  protocol_binary_request_delete request= {.bytes= {0}};
134
 
 
135
 
  request.message.header.request.magic= PROTOCOL_BINARY_REQ;
136
 
  if (ptr->flags & MEM_NOREPLY)
137
 
    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
138
 
  else
139
 
    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
140
 
  request.message.header.request.keylen= htons((uint16_t)key_length);
141
 
  request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
142
 
  request.message.header.request.bodylen= htonl((uint32_t) key_length);
 
44
  protocol_binary_request_delete request= {
 
45
     .message.header.request= {
 
46
        .magic= PROTOCOL_BINARY_REQ,
 
47
        .opcode= (ptr->flags & MEM_NOREPLY) ? PROTOCOL_BINARY_CMD_DELETEQ :
 
48
                                              PROTOCOL_BINARY_CMD_DELETE,
 
49
        .keylen= htons((uint16_t)key_length),
 
50
        .datatype= PROTOCOL_BINARY_RAW_BYTES,
 
51
        .bodylen= htonl((uint32_t) key_length)
 
52
     }
 
53
  };
143
54
 
144
55
  if (ptr->flags & MEM_USE_UDP && !flush)
145
56
  {
183
94
 
184
95
  return rc;
185
96
}
 
97
 
 
98
memcached_return memcached_binary_delete(memcached_st *ptr,
 
99
                                         unsigned int server_key,
 
100
                                         const char *key,
 
101
                                         size_t key_length)
 
102
{
 
103
  memcached_return rc= memcached_validate_key_length(key_length, true);
 
104
  unlikely (rc != MEMCACHED_SUCCESS)
 
105
    return rc;
 
106
  uint8_t to_write= (uint8_t)((ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1);
 
107
  bool no_reply= (ptr->flags & MEM_NOREPLY);
 
108
 
 
109
  rc= binary_delete(ptr, server_key, key, key_length, to_write);
 
110
  if (rc == MEMCACHED_SUCCESS)
 
111
  {
 
112
    if ((ptr->flags & MEM_BUFFER_REQUESTS))
 
113
      rc= MEMCACHED_BUFFERED;
 
114
    else if (!no_reply)
 
115
    {
 
116
      char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
 
117
      rc= memcached_response(&ptr->hosts[server_key], buffer,
 
118
                             sizeof(buffer), NULL);
 
119
      if (rc == MEMCACHED_DELETED)
 
120
        rc= MEMCACHED_SUCCESS;
 
121
    }
 
122
  }
 
123
 
 
124
  return rc;
 
125
}
 
126
 
 
127
memcached_return memcached_internal_delete(memcached_st *ptr,
 
128
                                           unsigned int server_key,
 
129
                                           const char *key,
 
130
                                           size_t key_length)
 
131
{
 
132
  (void)server_key;
 
133
  memcached_return rc= memcached_validate_key_length(key_length, false);
 
134
  unlikely (rc != MEMCACHED_SUCCESS)
 
135
    return rc;
 
136
 
 
137
  return internal_delete(ptr, key, key_length);
 
138
}
 
139
 
 
140
 
 
141
memcached_return memcached_ascii_delete(memcached_st *ptr,
 
142
                                        unsigned int server_key,
 
143
                                        const char *key,
 
144
                                        size_t key_length)
 
145
{
 
146
  memcached_return rc= memcached_validate_key_length(key_length, false);
 
147
  unlikely (rc != MEMCACHED_SUCCESS)
 
148
    return rc;
 
149
 
 
150
  uint8_t to_write= (uint8_t)((ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1);
 
151
  bool no_reply= (ptr->flags & MEM_NOREPLY);
 
152
  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
 
153
  size_t send_length= (size_t) snprintf(buffer, sizeof(buffer),
 
154
                                        "delete %s%.*s%s\r\n",
 
155
                                        ptr->prefix_key,
 
156
                                        (int)key_length, key,
 
157
                                        no_reply ? " noreply" :"");
 
158
 
 
159
  if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
 
160
  {
 
161
    rc= MEMCACHED_WRITE_FAILURE;
 
162
    goto error;
 
163
  }
 
164
 
 
165
  if (ptr->flags & MEM_USE_UDP && !to_write)
 
166
  {
 
167
    if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
 
168
    {
 
169
      return MEMCACHED_WRITE_FAILURE;
 
170
    }
 
171
 
 
172
    if (send_length + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
 
173
    {
 
174
      memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
 
175
    }
 
176
  }
 
177
 
 
178
  rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
 
179
 
 
180
  if (rc != MEMCACHED_SUCCESS)
 
181
    goto error;
 
182
 
 
183
  if (!to_write)
 
184
    rc= MEMCACHED_BUFFERED;
 
185
  else if (!no_reply)
 
186
  {
 
187
    rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
 
188
    if (rc == MEMCACHED_DELETED)
 
189
      rc= MEMCACHED_SUCCESS;
 
190
  }
 
191
 
 
192
  if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
 
193
    ptr->delete_trigger(ptr, key, key_length);
 
194
 
 
195
error:
 
196
  LIBMEMCACHED_MEMCACHED_DELETE_END();
 
197
  return rc;
 
198
}