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-2016 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, as_node* node, uint32_t timeout_ms, bool deserialize,
71  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 = cf_malloc(s);
80  cmd->event_loop = as_event_assign(event_loop);
81  cmd->conn = 0;
82  cmd->cluster = cluster;
83  cmd->node = node;
84  cmd->udata = udata;
85  cmd->parse_results = parse_results;
86  cmd->buf = wcmd->space;
87  cmd->capacity = (uint32_t)(s - sizeof(as_async_write_command));
88  cmd->len = 0;
89  cmd->pos = 0;
90  cmd->auth_len = 0;
91  cmd->timeout_ms = timeout_ms;
94  cmd->pipe_listener = pipe_listener;
95  cmd->deserialize = deserialize;
96  cmd->free_buf = false;
97  wcmd->listener = listener;
98  return cmd;
99 }
100 
101 static inline as_event_command*
103  as_cluster* cluster, as_node* node, uint32_t timeout_ms, bool deserialize,
104  as_async_record_listener listener, void* udata, as_event_loop* event_loop,
105  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
106  )
107 {
108  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
109  // Then, round up memory size in 1KB increments to reduce fragmentation and to allow socket
110  // read to reuse buffer for small socket write sizes.
111  size_t s = (sizeof(as_async_record_command) + size + AS_AUTHENTICATION_MAX_SIZE + 1023) & ~1023;
112  as_event_command* cmd = cf_malloc(s);
114  cmd->event_loop = as_event_assign(event_loop);
115  cmd->conn = 0;
116  cmd->cluster = cluster;
117  cmd->node = node;
118  cmd->udata = udata;
119  cmd->parse_results = parse_results;
120  cmd->buf = rcmd->space;
121  cmd->capacity = (uint32_t)(s - sizeof(as_async_record_command));
122  cmd->len = 0;
123  cmd->pos = 0;
124  cmd->auth_len = 0;
125  cmd->timeout_ms = timeout_ms;
126  cmd->type = AS_ASYNC_TYPE_RECORD;
128  cmd->pipe_listener = pipe_listener;
129  cmd->deserialize = deserialize;
130  cmd->free_buf = false;
131  rcmd->listener = listener;
132  return cmd;
133 }
134 
135 static inline as_event_command*
137  as_cluster* cluster, as_node* node, uint32_t timeout_ms, bool deserialize,
138  as_async_value_listener listener, void* udata, as_event_loop* event_loop,
139  as_pipe_listener pipe_listener, size_t size, as_event_parse_results_fn parse_results
140  )
141 {
142  // Allocate enough memory to cover: struct size + write buffer size + auth max buffer size
143  // Then, round up memory size in 1KB increments to reduce fragmentation and to allow socket
144  // read to reuse buffer for small socket write sizes.
145  size_t s = (sizeof(as_async_value_command) + size + AS_AUTHENTICATION_MAX_SIZE + 1023) & ~1023;
146  as_event_command* cmd = cf_malloc(s);
148  cmd->event_loop = as_event_assign(event_loop);
149  cmd->conn = 0;
150  cmd->cluster = cluster;
151  cmd->node = node;
152  cmd->udata = udata;
153  cmd->parse_results = parse_results;
154  cmd->buf = vcmd->space;
155  cmd->capacity = (uint32_t)(s - sizeof(as_async_value_command));
156  cmd->len = 0;
157  cmd->pos = 0;
158  cmd->auth_len = 0;
159  cmd->timeout_ms = timeout_ms;
160  cmd->type = AS_ASYNC_TYPE_VALUE;
162  cmd->pipe_listener = pipe_listener;
163  cmd->deserialize = deserialize;
164  cmd->free_buf = false;
165  vcmd->listener = listener;
166  return cmd;
167 }
168 
169 #ifdef __cplusplus
170 } // end extern "C"
171 #endif
as_event_loop * event_loop
as_event_command command
Definition: as_async.h:53
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
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:47
static as_event_command * as_async_record_command_create(as_cluster *cluster, as_node *node, uint32_t timeout_ms, 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:102
as_cluster * cluster
#define AS_AUTHENTICATION_MAX_SIZE
Definition: as_async.h:40
static as_event_command * as_async_write_command_create(as_cluster *cluster, as_node *node, uint32_t timeout_ms, 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:69
as_async_record_listener listener
Definition: as_async.h:54
void(* as_async_write_listener)(as_error *err, void *udata, as_event_loop *event_loop)
Definition: as_listener.h:36
static as_event_command * as_async_value_command_create(as_cluster *cluster, as_node *node, uint32_t timeout_ms, 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:136
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:60
#define AS_ASYNC_STATE_UNREGISTERED
as_event_connection * conn
as_async_write_listener listener
Definition: as_async.h:48
as_pipe_listener pipe_listener
as_event_command command
Definition: as_async.h:59
static as_event_loop * as_event_assign(as_event_loop *event_loop)
#define AS_ASYNC_TYPE_WRITE
Definition: as_async.h:33