All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_async.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2017 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
20 #include <aerospike/as_cluster.h>
22 #include <aerospike/as_listener.h>
23 #include <citrusleaf/alloc.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /******************************************************************************
30  * TYPES
31  *****************************************************************************/
32 
33 #define AS_ASYNC_TYPE_WRITE 0
34 #define AS_ASYNC_TYPE_RECORD 1
35 #define AS_ASYNC_TYPE_VALUE 2
36 #define AS_ASYNC_TYPE_BATCH 3
37 #define AS_ASYNC_TYPE_SCAN 4
38 #define AS_ASYNC_TYPE_QUERY 5
39 #define AS_ASYNC_TYPE_MASK 7
40 #define AS_ASYNC_TYPE_REGISTERED 128
41 
42 #define AS_AUTHENTICATION_MAX_SIZE 158
43 
44 #define AS_ASYNC_CONNECTION_COMPLETE 0
45 #define AS_ASYNC_CONNECTION_PENDING 1
46 #define AS_ASYNC_CONNECTION_ERROR 2
47 
48 typedef struct as_async_write_command {
51  uint8_t space[];
53 
54 typedef struct as_async_record_command {
57  uint8_t space[];
59 
60 typedef struct as_async_value_command {
63  uint8_t space[];
65 
66 /******************************************************************************
67  * FUNCTIONS
68  *****************************************************************************/
69 
70 static inline as_event_command*
72  as_cluster* cluster, as_node* node, uint32_t socket_timeout, uint32_t total_timeout,
73  bool deserialize, as_async_write_listener listener, void* udata, as_event_loop* event_loop,
74  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
75  )
76 {
77  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
78  // Then, round up memory size in 1KB increments.
79  size_t s = (sizeof(as_async_write_command) + size + AS_AUTHENTICATION_MAX_SIZE + 1023) & ~1023;
80  as_event_command* cmd = (as_event_command*)cf_malloc(s);
82  cmd->event_loop = as_event_assign(event_loop);
83  cmd->conn = 0;
84  cmd->cluster = cluster;
85  cmd->node = node;
86  cmd->udata = udata;
87  cmd->parse_results = parse_results;
88  cmd->pipe_listener = pipe_listener;
89  cmd->buf = wcmd->space;
90  cmd->total_deadline = total_timeout;
91  cmd->socket_timeout = socket_timeout;
92  cmd->capacity = (uint32_t)(s - sizeof(as_async_write_command));
93  cmd->len = 0;
94  cmd->pos = 0;
95  cmd->auth_len = 0;
98  cmd->flags = 0;
99  cmd->deserialize = deserialize;
100  wcmd->listener = listener;
101  return cmd;
102 }
103 
104 static inline as_event_command*
106  as_cluster* cluster, as_node* node, uint32_t socket_timeout, uint32_t total_timeout,
107  bool deserialize, as_async_record_listener listener, void* udata, as_event_loop* event_loop,
108  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
109  )
110 {
111  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
112  // Then, round up memory size in 1KB increments to reduce fragmentation and to allow socket
113  // read to reuse buffer for small socket write sizes.
114  size_t s = (sizeof(as_async_record_command) + size + AS_AUTHENTICATION_MAX_SIZE + 1023) & ~1023;
115  as_event_command* cmd = (as_event_command*)cf_malloc(s);
117  cmd->event_loop = as_event_assign(event_loop);
118  cmd->conn = 0;
119  cmd->cluster = cluster;
120  cmd->node = node;
121  cmd->udata = udata;
122  cmd->parse_results = parse_results;
123  cmd->pipe_listener = pipe_listener;
124  cmd->buf = rcmd->space;
125  cmd->total_deadline = total_timeout;
126  cmd->socket_timeout = socket_timeout;
127  cmd->capacity = (uint32_t)(s - sizeof(as_async_record_command));
128  cmd->len = 0;
129  cmd->pos = 0;
130  cmd->auth_len = 0;
131  cmd->type = AS_ASYNC_TYPE_RECORD;
133  cmd->flags = 0;
134  cmd->deserialize = deserialize;
135  rcmd->listener = listener;
136  return cmd;
137 }
138 
139 static inline as_event_command*
141  as_cluster* cluster, as_node* node, uint32_t socket_timeout, uint32_t total_timeout,
142  bool deserialize, as_async_value_listener listener, void* udata, as_event_loop* event_loop,
143  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
144  )
145 {
146  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
147  // Then, round up memory size in 1KB increments to reduce fragmentation and to allow socket
148  // read to reuse buffer for small socket write sizes.
149  size_t s = (sizeof(as_async_value_command) + size + AS_AUTHENTICATION_MAX_SIZE + 1023) & ~1023;
150  as_event_command* cmd = (as_event_command*)cf_malloc(s);
152  cmd->event_loop = as_event_assign(event_loop);
153  cmd->conn = 0;
154  cmd->cluster = cluster;
155  cmd->node = node;
156  cmd->udata = udata;
157  cmd->parse_results = parse_results;
158  cmd->pipe_listener = pipe_listener;
159  cmd->buf = vcmd->space;
160  cmd->total_deadline = total_timeout;
161  cmd->socket_timeout = socket_timeout;
162  cmd->capacity = (uint32_t)(s - sizeof(as_async_value_command));
163  cmd->len = 0;
164  cmd->pos = 0;
165  cmd->auth_len = 0;
166  cmd->type = AS_ASYNC_TYPE_VALUE;
168  cmd->flags = 0;
169  cmd->deserialize = deserialize;
170  vcmd->listener = listener;
171  return cmd;
172 }
173 
174 #ifdef __cplusplus
175 } // end extern "C"
176 #endif
as_event_loop * event_loop
as_event_command command
Definition: as_async.h:55
void(* as_async_value_listener)(as_error *err, as_val *val, void *udata, as_event_loop *event_loop)
Definition: as_listener.h:62
as_event_parse_results_fn parse_results
#define AS_ASYNC_TYPE_RECORD
Definition: as_async.h:34
static as_event_command * as_async_write_command_create(as_cluster *cluster, as_node *node, uint32_t socket_timeout, uint32_t total_timeout, bool deserialize, as_async_write_listener listener, void *udata, as_event_loop *event_loop, as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results)
Definition: as_async.h:71
bool(* as_event_parse_results_fn)(struct as_event_command *cmd)
#define AS_ASYNC_TYPE_VALUE
Definition: as_async.h:35
void(* as_async_record_listener)(as_error *err, as_record *record, void *udata, as_event_loop *event_loop)
Definition: as_listener.h:49
as_event_command command
Definition: as_async.h:49
as_cluster * cluster
#define AS_AUTHENTICATION_MAX_SIZE
Definition: as_async.h:42
static as_event_command * as_async_record_command_create(as_cluster *cluster, as_node *node, uint32_t socket_timeout, uint32_t total_timeout, bool deserialize, as_async_record_listener listener, void *udata, as_event_loop *event_loop, as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results)
Definition: as_async.h:105
as_async_record_listener listener
Definition: as_async.h:56
void(* as_async_write_listener)(as_error *err, void *udata, as_event_loop *event_loop)
Definition: as_listener.h:36
void(* as_pipe_listener)(void *udata, as_event_loop *event_loop)
Definition: as_listener.h:73
as_async_value_listener listener
Definition: as_async.h:62
#define AS_ASYNC_STATE_UNREGISTERED
as_event_connection * conn
as_async_write_listener listener
Definition: as_async.h:50
as_pipe_listener pipe_listener
as_event_command command
Definition: as_async.h:61
static as_event_loop * as_event_assign(as_event_loop *event_loop)
#define AS_ASYNC_TYPE_WRITE
Definition: as_async.h:33
static as_event_command * as_async_value_command_create(as_cluster *cluster, as_node *node, uint32_t socket_timeout, uint32_t total_timeout, bool deserialize, as_async_value_listener listener, void *udata, as_event_loop *event_loop, as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results)
Definition: as_async.h:140