kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
blocking_getnext_getprevious.c
Go to the documentation of this file.
1 /*
2 * kinetic-c
3 * Copyright (C) 2015 Seagate Technology.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 
21 #include "kinetic_client.h"
22 #include "kinetic_types.h"
23 #include "byte_array.h"
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include <stdio.h>
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <sys/file.h>
30 #include <ctype.h>
31 
32 #include <openssl/sha.h>
33 
34 static void do_put_and_getprevious_getnext(KineticSession *session) {
35  for (int i = 0; i < 3; i++) {
36  char key[] = "keyX";
37  key[3] = '0' + i;
38  ByteBuffer put_key_buf = ByteBuffer_MallocAndAppend(key, strlen(key));
39 
40  uint8_t value[] = "valueX";
41  value[5] = '0' + i;
42  ByteBuffer put_value_buf = ByteBuffer_MallocAndAppend(value, sizeof(value));
43 
44  /* Populate tag with SHA1 of value */
45  ByteBuffer put_tag_buf = ByteBuffer_Malloc(20);
46  uint8_t sha1[20];
47  SHA1(put_value_buf.array.data, put_value_buf.bytesUsed, &sha1[0]);
48  ByteBuffer_Append(&put_tag_buf, sha1, sizeof(sha1));
49 
50  KineticEntry put_entry = {
51  .key = put_key_buf,
52  .value = put_value_buf,
53  .tag = put_tag_buf,
54  .algorithm = KINETIC_ALGORITHM_SHA1,
55  /* Set sync to WRITETHROUGH, which will wait to complete
56  * until the drive has persistend the write. (WRITEBACK
57  * returns as soon as the drive has buffered the write.) */
58  .synchronization = KINETIC_SYNCHRONIZATION_WRITETHROUGH,
59  };
60 
61  /* Put "keyX" => "valueX", where 'X' is 0..4.
62  * This will block, because the callback field (arg 3) is NULL. */
63  KineticStatus status = KineticClient_Put(session, &put_entry, NULL);
64  printf("Put status: %s\n", Kinetic_GetStatusDescription(status));
65 
66  ByteBuffer_Free(put_key_buf);
67  ByteBuffer_Free(put_value_buf);
68  ByteBuffer_Free(put_tag_buf);
69  }
70 
71  printf("\n\n\n");
72 
73  for (int i = 1; i < 3; i++) {
75  static const ssize_t sz = 100;
76  char key_buf[sz];
77  char tag_buf[sz];
78  char value_buf[sz];
79  ByteBuffer keyBuffer = ByteBuffer_CreateAndAppendFormattedCString(key_buf, sz, "key%d", i);
80  ByteBuffer tagBuffer = ByteBuffer_CreateAndAppendFormattedCString(tag_buf, sz, "tag%d", i);
81  ByteBuffer valueBuffer = ByteBuffer_Create(value_buf, sz, 0);
82 
83  KineticEntry entry = {
84  .key = keyBuffer,
85  .tag = tagBuffer,
86  .value = valueBuffer,
87  .algorithm = KINETIC_ALGORITHM_SHA1,
88  };
89 
90  if (i > 0) {
91  status = KineticClient_GetPrevious(session, &entry, NULL);
92  printf("GetPrevious status: %s\n", Kinetic_GetStatusDescription(status));
93  if (status == KINETIC_STATUS_SUCCESS) {
94  printf("Previous key before 'key%d': '%s', value '%s'\n",
95  i, key_buf, value_buf);
96  }
97  }
98  }
99 
100  for (int i = 0; i < 2; i++) {
102  static const ssize_t sz = 100;
103  char key_buf[sz];
104  char tag_buf[sz];
105  char value_buf[sz];
106  ByteBuffer keyBuffer = ByteBuffer_CreateAndAppendFormattedCString(key_buf, sz, "key%d", i);
107  ByteBuffer tagBuffer = ByteBuffer_CreateAndAppendFormattedCString(tag_buf, sz, "tag%d", i);
108  ByteBuffer valueBuffer = ByteBuffer_Create(value_buf, sz, 0);
109 
110  KineticEntry entry = {
111  .key = keyBuffer,
112  .tag = tagBuffer,
113  .value = valueBuffer,
114  .algorithm = KINETIC_ALGORITHM_SHA1,
115  };
116 
117  if (i < 2) {
118  status = KineticClient_GetNext(session, &entry, NULL);
119  printf("GetNext status: %s\n", Kinetic_GetStatusDescription(status));
120  if (status == KINETIC_STATUS_SUCCESS) {
121  printf("Next key after 'key%d': '%s', value '%s'\n",
122  i, key_buf, value_buf);
123  }
124  }
125  }
126 
127  /* No cleanup necessary */
128 }
129 
130 int main(int argc, char** argv)
131 {
132  (void)argc;
133  (void)argv;
134 
135  // Initialize kinetic-c and configure sessions
136  KineticSession* session;
137  KineticClientConfig clientConfig = {
138  .logFile = "stdout",
139  .logLevel = 1,
140  };
141  KineticClient * client = KineticClient_Init(&clientConfig);
142  if (client == NULL) { return 1; }
143  const char HmacKeyString[] = "asdfasdf";
144  KineticSessionConfig sessionConfig = {
145  .host = "localhost",
146  .port = KINETIC_PORT,
147  .clusterVersion = 0,
148  .identity = 1,
149  .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
150  };
151  KineticStatus status = KineticClient_CreateSession(&sessionConfig, client, &session);
152  if (status != KINETIC_STATUS_SUCCESS) {
153  fprintf(stderr, "Failed connecting to the Kinetic device w/status: %s\n",
155  exit(1);
156  }
157 
159 
160  // Shutdown client connection and cleanup
162  KineticClient_Shutdown(client);
163  return 0;
164 }
ByteBuffer * ByteBuffer_Append(ByteBuffer *buffer, const void *data, size_t len)
Definition: byte_array.c:135
Structure for an embedded ByteArray as a buffer.
Definition: byte_array.h:53
This request is made persistent before returning.
Definition: kinetic_types.h:93
ByteBuffer ByteBuffer_Malloc(size_t size)
Definition: byte_array.c:254
Operation successful.
KineticStatus KineticClient_CreateSession(KineticSessionConfig *const config, KineticClient *const client, KineticSession **session)
Creates a session with the Kinetic Device per specified configuration.
Structure used to specify the configuration for a session.
KineticStatus KineticClient_GetPrevious(KineticSession *const session, KineticEntry *const entry, KineticCompletionClosure *closure)
Executes a GETPREVIOUS operation to retrieve the next entry from the Kinetic Device.
KineticStatus KineticClient_DestroySession(KineticSession *const session)
Closes the connection to a host.
ByteArray array
ByteArray holding allocated array w/length = allocated size.
Definition: byte_array.h:54
Kinetic object instance.
char host[256]
Host name/IP address of Kinetic Device.
const char * Kinetic_GetStatusDescription(KineticStatus status)
Provides a string representation for a KineticStatus code.
Definition: kinetic_types.c:67
ByteBuffer ByteBuffer_Create(void *data, size_t max_len, size_t used)
Definition: byte_array.c:68
#define KINETIC_PORT
Default kinetic port.
Definition: kinetic_types.h:40
static void do_put_and_getprevious_getnext(KineticSession *session)
const char * logFile
Path to log file. Specify 'stdout' to log to STDOUT or NULL to disable logging.
ByteBuffer key
Key associated with the object stored on disk.
uint8_t * data
Pointer to an allocated array of data bytes.
Definition: byte_array.h:36
ByteBuffer ByteBuffer_CreateAndAppendFormattedCString(void *data, size_t max_len, const char *format,...)
Definition: byte_array.c:221
Status not available (no reponse/status available)
KineticStatus
Kinetic status codes.
KineticStatus KineticClient_Put(KineticSession *const session, KineticEntry *const entry, KineticCompletionClosure *closure)
Executes a PUT operation to store/update an entry on the Kinetic Device.
ByteBuffer ByteBuffer_MallocAndAppend(const void *data, size_t len)
Definition: byte_array.c:262
Configuration values for the KineticClient connection.
void KineticClient_Shutdown(KineticClient *const client)
Performs shutdown/cleanup of the kinetic-c client library.
KineticClient * KineticClient_Init(KineticClientConfig *config)
Initializes the Kinetic API and configures logging.
int main(int argc, char **argv)
size_t bytesUsed
Reflects the number of bytes used from the array
Definition: byte_array.h:55
KineticStatus KineticClient_GetNext(KineticSession *const session, KineticEntry *const entry, KineticCompletionClosure *closure)
Executes a GETNEXT operation to retrieve the next entry from the Kinetic Device.
void ByteBuffer_Free(ByteBuffer buffer)
Definition: byte_array.c:272
ByteArray ByteArray_CreateWithCString(const char *str)
Definition: byte_array.c:38