kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
blocking_put_delete.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_delete(KineticSession *session) {
35  const char key[] = "key";
36  ByteBuffer put_key_buf = ByteBuffer_MallocAndAppend(key, strlen(key));
37 
38  const uint8_t value[] = "value\x01\x02\x03\x04";
39  ByteBuffer put_value_buf = ByteBuffer_MallocAndAppend(value, sizeof(value));
40 
41  /* Populate tag with SHA1 of value */
42  ByteBuffer put_tag_buf = ByteBuffer_Malloc(20);
43  uint8_t sha1[20];
44  SHA1(put_value_buf.array.data, put_value_buf.bytesUsed, &sha1[0]);
45  ByteBuffer_Append(&put_tag_buf, sha1, sizeof(sha1));
46 
47  KineticEntry put_entry = {
48  .key = put_key_buf,
49  .value = put_value_buf,
50  .tag = put_tag_buf,
51  .algorithm = KINETIC_ALGORITHM_SHA1,
52  /* Set sync to WRITETHROUGH, which will wait to complete
53  * until the drive has persistend the write. (WRITEBACK
54  * returns as soon as the drive has buffered the write.) */
55  .synchronization = KINETIC_SYNCHRONIZATION_WRITETHROUGH,
56  };
57 
58  /* Put "key" => "value\x01\x02\x03\x04".
59  * This will block, because the callback field (arg 3) is NULL. */
60  KineticStatus status = KineticClient_Put(session, &put_entry, NULL);
61  printf("Put status: %s\n", Kinetic_GetStatusDescription(status));
62 
63  /* Allocate a tag large enough for the SHA1. */
64  ByteBuffer delete_tag_buf = ByteBuffer_Malloc(put_tag_buf.bytesUsed);
65 
66  KineticEntry delete_entry = {
67  .key = put_key_buf,
68  .tag = delete_tag_buf,
69  .algorithm = KINETIC_ALGORITHM_SHA1,
70  };
71 
72  status = KineticClient_Delete(session, &delete_entry, NULL);
73  printf("Delete status: %s\n", Kinetic_GetStatusDescription(status));
74 
75  /* Cleanup */
76  ByteBuffer_Free(put_key_buf);
77  ByteBuffer_Free(put_value_buf);
78  ByteBuffer_Free(put_tag_buf);
79  ByteBuffer_Free(delete_tag_buf);
80 }
81 
82 int main(int argc, char** argv)
83 {
84  (void)argc;
85  (void)argv;
86 
87  // Initialize kinetic-c and configure sessions
88  KineticSession* session;
89  KineticClientConfig clientConfig = {
90  .logFile = "stdout",
91  .logLevel = 1,
92  };
93  KineticClient * client = KineticClient_Init(&clientConfig);
94  if (client == NULL) { return 1; }
95  const char HmacKeyString[] = "asdfasdf";
96  KineticSessionConfig sessionConfig = {
97  .host = "localhost",
98  .port = KINETIC_PORT,
99  .clusterVersion = 0,
100  .identity = 1,
101  .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
102  };
103  KineticStatus status = KineticClient_CreateSession(&sessionConfig, client, &session);
104  if (status != KINETIC_STATUS_SUCCESS) {
105  fprintf(stderr, "Failed connecting to the Kinetic device w/status: %s\n",
107  exit(1);
108  }
109 
110  do_put_and_delete(session);
111 
112  // Shutdown client connection and cleanup
114  KineticClient_Shutdown(client);
115  return 0;
116 }
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_DestroySession(KineticSession *const session)
Closes the connection to a host.
KineticStatus KineticClient_Delete(KineticSession *const session, KineticEntry *const entry, KineticCompletionClosure *closure)
Executes a DELETE operation to delete an entry from the Kinetic Device.
int main(int argc, char **argv)
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
#define KINETIC_PORT
Default kinetic port.
Definition: kinetic_types.h:40
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
static void do_put_and_delete(KineticSession *session)
void ByteBuffer_Free(ByteBuffer buffer)
Definition: byte_array.c:272
ByteArray ByteArray_CreateWithCString(const char *str)
Definition: byte_array.c:38