kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
get_key_range.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 
31 static bool create_entries(KineticSession * const session, const int count);
32 
33 int main(int argc, char** argv)
34 {
35  (void)argc;
36  (void)argv;
37 
38  // Initialize kinetic-c and configure sessions
39  KineticSession* session;
40  KineticClientConfig clientConfig = {
41  .logFile = "stdout",
42  .logLevel = 1,
43  };
44  KineticClient * client = KineticClient_Init(&clientConfig);
45  if (client == NULL) { return 1; }
46  const char HmacKeyString[] = "asdfasdf";
47  KineticSessionConfig sessionConfig = {
48  .host = "localhost",
49  .port = KINETIC_PORT,
50  .clusterVersion = 0,
51  .identity = 1,
52  .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
53  };
54  KineticStatus status = KineticClient_CreateSession(&sessionConfig, client, &session);
55  if (status != KINETIC_STATUS_SUCCESS) {
56  fprintf(stderr, "Failed connecting to the Kinetic device w/status: %s\n",
58  exit(1);
59  }
60 
61  // Create some entries so that we can query the keys
62  printf("Storing some entries on the device...\n");
63  const size_t numKeys = 3;
64  if (!create_entries(session, numKeys)) {
65  return 2;
66  }
67 
68  // Query a range of keys
69  const size_t keyLen = 64;
70  uint8_t startKeyData[keyLen], endKeyData[keyLen];
71  KineticKeyRange range = {
72  .startKey = ByteBuffer_CreateAndAppendCString(startKeyData, sizeof(startKeyData), "key_prefix_00"),
73  .endKey = ByteBuffer_CreateAndAppendCString(endKeyData, sizeof(endKeyData), "key_prefix_01"),
74  .startKeyInclusive = true,
75  .endKeyInclusive = true,
76  .maxReturned = 3,
77  };
78  uint8_t keysData[numKeys][keyLen];
79  ByteBuffer keyBuff[] = {
80  ByteBuffer_Create(&keysData[0], keyLen, 0),
81  ByteBuffer_Create(&keysData[1], keyLen, 0),
82  ByteBuffer_Create(&keysData[2], keyLen, 0),
83  };
84  ByteBufferArray keys = {.buffers = &keyBuff[0], .count = numKeys};
85 
86  status = KineticClient_GetKeyRange(session, &range, &keys, NULL);
87  if (status != KINETIC_STATUS_SUCCESS) {
88  fprintf(stderr, "FAILURE: Failed retrieving key range from device!\n");
89  return 3;
90  }
91 
92  if (keys.used != 2) {
93  fprintf(stderr, "FAILURE: Unexpected number of keys in returned range!\n");
94  return 4;
95  };
96  if (keyBuff[0].bytesUsed != strlen("key_prefix_00")) {
97  fprintf(stderr, "FAILURE: Key 0 length check failed!\n");
98  return 4;
99  }
100  if (keyBuff[1].bytesUsed != strlen("key_prefix_01")) {
101  fprintf(stderr, "FAILURE: Key 1 length check failed!\n");
102  return 4;
103  }
104  if (keyBuff[2].bytesUsed != 0) {
105  fprintf(stderr, "FAILURE: Key 2 was not empty as expected!\n");
106  return 4;
107  }
108 
109  // Shutdown client connection and cleanup
111  KineticClient_Shutdown(client);
112  printf("Key range retrieved successfully!\n");
113 
114  return 0;
115 }
116 
117 static bool create_entries(KineticSession * const session, const int count)
118 {
119  static const ssize_t sz = 20;
120  char key_buf[sz];
121  char value_buf[sz];
122 
123  for (int i = 0; i < count; i++) {
124 
125  ByteBuffer KeyBuffer = ByteBuffer_CreateAndAppendFormattedCString(key_buf, sz, "key_prefix_%02d", i);
126  ByteBuffer ValueBuffer = ByteBuffer_CreateAndAppendFormattedCString(value_buf, sz, "val_%02d", i);
127 
128  KineticEntry entry = {
129  .key = KeyBuffer,
130  .value = ValueBuffer,
131  .algorithm = KINETIC_ALGORITHM_SHA1,
132  .force = true,
133  };
134 
135  KineticStatus status = KineticClient_Put(session, &entry, NULL);
136  if (KINETIC_STATUS_SUCCESS != status) { return false; }
137  }
138 
139  return true;
140 }
Structure for an embedded ByteArray as a buffer.
Definition: byte_array.h:53
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.
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
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.
int main(int argc, char **argv)
Definition: get_key_range.c:33
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.
ByteBuffer ByteBuffer_CreateAndAppendFormattedCString(void *data, size_t max_len, const char *format,...)
Definition: byte_array.c:221
KineticStatus
Kinetic status codes.
static bool create_entries(KineticSession *const session, const int count)
KineticStatus KineticClient_Put(KineticSession *const session, KineticEntry *const entry, KineticCompletionClosure *closure)
Executes a PUT operation to store/update an entry on the Kinetic Device.
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.
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