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 
40 #define AS_AUTHENTICATION_MAX_SIZE 158
41 
42 #define AS_ASYNC_CONNECTION_COMPLETE 0
43 #define AS_ASYNC_CONNECTION_PENDING 1
44 #define AS_ASYNC_CONNECTION_ERROR 2
45 
46 typedef struct as_async_write_command {
49  uint8_t space[];
51 
52 typedef struct as_async_record_command {
55  uint8_t space[];
57 
58 typedef struct as_async_value_command {
61  uint8_t space[];
63 
64 /******************************************************************************
65  * FUNCTIONS
66  *****************************************************************************/
67 
68 static inline as_event_command*
70  as_cluster* cluster, const as_policy_base* policy, as_policy_replica replica, void* partition,
71  uint8_t flags, as_async_write_listener listener, void* udata, as_event_loop* event_loop,
72  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
73  )
74 {
75  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
76  // Then, round up memory size in 1KB increments.
77  size_t s = (sizeof(as_async_write_command) + size + AS_AUTHENTICATION_MAX_SIZE + 1023) & ~1023;
78  as_event_command* cmd = (as_event_command*)cf_malloc(s);
80  cmd->total_deadline = policy->total_timeout;
81  cmd->socket_timeout = policy->socket_timeout;
82  cmd->max_retries = policy->max_retries;
83  cmd->iteration = 0;
84  cmd->replica = replica;
85  cmd->event_loop = as_event_assign(event_loop);
86  cmd->cluster = cluster;
87  cmd->node = NULL;
88  cmd->partition = partition;
89  cmd->udata = udata;
90  cmd->parse_results = parse_results;
91  cmd->pipe_listener = pipe_listener;
92  cmd->buf = wcmd->space;
93  cmd->read_capacity = (uint32_t)(s - size - sizeof(as_async_write_command));
96  cmd->flags = flags;
97  cmd->deserialize = false;
98  wcmd->listener = listener;
99  return cmd;
100 }
101 
102 static inline as_event_command*
104  as_cluster* cluster, const as_policy_base* policy, as_policy_replica replica, void* partition,
105  bool deserialize, uint8_t flags, as_async_record_listener listener, void* udata,
106  as_event_loop* event_loop, as_pipe_listener pipe_listener, size_t size,
107  as_event_parse_results_fn parse_results
108  )
109 {
110  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
111  // Then, round up memory size in 4KB increments to reduce fragmentation and to allow socket
112  // read to reuse buffer for small socket write sizes.
113  size_t s = (sizeof(as_async_record_command) + size + AS_AUTHENTICATION_MAX_SIZE + 4095) & ~4095;
114  as_event_command* cmd = (as_event_command*)cf_malloc(s);
116  cmd->total_deadline = policy->total_timeout;
117  cmd->socket_timeout = policy->socket_timeout;
118  cmd->max_retries = policy->max_retries;
119  cmd->iteration = 0;
120  cmd->replica = replica;
121  cmd->event_loop = as_event_assign(event_loop);
122  cmd->cluster = cluster;
123  cmd->node = NULL;
124  cmd->partition = partition;
125  cmd->udata = udata;
126  cmd->parse_results = parse_results;
127  cmd->pipe_listener = pipe_listener;
128  cmd->buf = rcmd->space;
129  cmd->read_capacity = (uint32_t)(s - size - sizeof(as_async_record_command));
130  cmd->type = AS_ASYNC_TYPE_RECORD;
132  cmd->flags = flags;
133  cmd->deserialize = deserialize;
134  rcmd->listener = listener;
135  return cmd;
136 }
137 
138 static inline as_event_command*
140  as_cluster* cluster, const as_policy_base* policy, as_policy_replica replica, void* partition,
141  uint8_t flags, as_async_value_listener listener, void* udata, as_event_loop* event_loop,
142  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
143  )
144 {
145  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
146  // Then, round up memory size in 4KB increments to reduce fragmentation and to allow socket
147  // read to reuse buffer for small socket write sizes.
148  size_t s = (sizeof(as_async_value_command) + size + AS_AUTHENTICATION_MAX_SIZE + 4095) & ~4095;
149  as_event_command* cmd = (as_event_command*)cf_malloc(s);
151  cmd->total_deadline = policy->total_timeout;
152  cmd->socket_timeout = policy->socket_timeout;
153  cmd->max_retries = policy->max_retries;
154  cmd->iteration = 0;
155  cmd->replica = replica;
156  cmd->event_loop = as_event_assign(event_loop);
157  cmd->cluster = cluster;
158  cmd->node = NULL;
159  cmd->partition = partition;
160  cmd->udata = udata;
161  cmd->parse_results = parse_results;
162  cmd->pipe_listener = pipe_listener;
163  cmd->buf = vcmd->space;
164  cmd->read_capacity = (uint32_t)(s - size - sizeof(as_async_value_command));
165  cmd->type = AS_ASYNC_TYPE_VALUE;
167  cmd->flags = flags;
168  cmd->deserialize = false;
169  vcmd->listener = listener;
170  return cmd;
171 }
172 
173 #ifdef __cplusplus
174 } // end extern "C"
175 #endif