kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
blocking_getkeyrange.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_getkeyrange(KineticSession * const session) {
35  for (int i = 0; i < 5; 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  const size_t max_key_count = 5;
72  const size_t max_key_length = 64;
73  uint8_t first_key[max_key_length];
74  uint8_t last_key[max_key_length];
75 
76  KineticKeyRange range = {
77  .startKey = ByteBuffer_CreateAndAppendCString(first_key, sizeof(first_key), "key"),
78  .endKey = ByteBuffer_CreateAndAppendCString(last_key, sizeof(last_key), "key\xFF"),
79  .startKeyInclusive = true,
80  .endKeyInclusive = true,
81  .maxReturned = max_key_count,
82  };
83 
84  uint8_t key_mem[max_key_count][max_key_length];
85  memset(key_mem, 0, sizeof(key_mem));
86 
87  ByteBuffer key_buffers[max_key_count];
88  for (size_t i = 0; i < max_key_count; i++) {
89  key_buffers[i] = ByteBuffer_Create(&key_buffers[i], max_key_length, 0);
90  }
91  ByteBufferArray keys = {
92  .buffers = key_buffers,
93  .count = max_key_count,
94  };
95 
96  /* Request the key range as specified in &range, populating the keys in &keys. */
97  KineticStatus status = KineticClient_GetKeyRange(session, &range, &keys, NULL);
98  printf("GetKeyRange status: %s\n", Kinetic_GetStatusDescription(status));
99 
100  if (status == KINETIC_STATUS_SUCCESS) {
101  for (size_t i = 0; i < max_key_count; i++) {
102  printf("%zd: %s\n", i, key_buffers[i].array.data);
103  }
104  }
105 
106  /* No cleanup necessary */
107 }
108 
109 int main(int argc, char** argv)
110 {
111  (void)argc;
112  (void)argv;
113 
114  // Initialize kinetic-c and configure sessions
115  KineticSession* session;
116  KineticClientConfig clientConfig = {
117  .logFile = "stdout",
118  .logLevel = 1,
119  };
120  KineticClient * client = KineticClient_Init(&clientConfig);
121  if (client == NULL) { return 1; }
122  const char HmacKeyString[] = "asdfasdf";
123  KineticSessionConfig sessionConfig = {
124  .host = "localhost",
125  .port = KINETIC_PORT,
126  .clusterVersion = 0,
127  .identity = 1,
128  .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
129  };
130  KineticStatus status = KineticClient_CreateSession(&sessionConfig, client, &session);
131  if (status != KINETIC_STATUS_SUCCESS) {
132  fprintf(stderr, "Failed connecting to the Kinetic device w/status: %s\n",
134  exit(1);
135  }
136 
137  do_put_and_getkeyrange(session);
138 
139  // Shutdown client connection and cleanup
141  KineticClient_Shutdown(client);
142  return 0;
143 }
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.
ByteBuffer * buffers
Definition: byte_array.h:59
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.
int main(int argc, char **argv)
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
Kinetic Key Range request structure.
KineticStatus KineticClient_GetKeyRange(KineticSession *const session, KineticKeyRange *range, ByteBufferArray *keys, KineticCompletionClosure *closure)
Executes a GETKEYRANGE operation to retrieve a set of keys in the range specified range from the Kine...
#define KINETIC_PORT
Default kinetic port.
Definition: kinetic_types.h:40
ByteBuffer startKey
Required bytes, the beginning of the requested range.
static void do_put_and_getkeyrange(KineticSession *const 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
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.
size_t bytesUsed
Reflects the number of bytes used from the array
Definition: byte_array.h:55
void ByteBuffer_Free(ByteBuffer buffer)
Definition: byte_array.c:272
ByteArray ByteArray_CreateWithCString(const char *str)
Definition: byte_array.c:38
ByteBuffer ByteBuffer_CreateAndAppendCString(void *data, size_t max_len, const char *value)
Definition: byte_array.c:97