Hitachi

In-Memory Data Grid Hitachi Elastic Application Data Store


19.1.1 Flow of cache access and data operations

The following diagram shows the flow of cache access and data operations.

[Figure]

Organization of this subsection

(1) Example of a source program in C

The following shows an example of a source program in C (to store a key and value):

#include <stdio.h>
#include <string.h>
 
#include <eads.h>
 
int main(int argc, char **argv) {
    int ret = 0;
    int error_code = 0;
    char CONFPATH[] = "./conf/eads_sample_client.properties";
    char CACHENAME[] = "cache1";
    char KEY[] = "key1";
    char VALUE[] = "value1";
    EAD_CACHE_MANAGER* cmp = NULL;
    EAD_CACHE *cp = NULL;
    ead_value_element value_element;
 
    value_element.value = (void *)VALUE;
    value_element.value_size = strlen(VALUE) + 1;
 
    /* Initialize EADS client */
    cmp = ead_init_client(CONFPATH, &error_code);
    printf("ead_init_client() done. (error_code = %d)\n", error_code);
    if (error_code != EAD_OK) {
        return 1;
    }
 
    /* Start access to the cache */
    cp = ead_start_cache(cmp, CACHENAME, &error_code);
    printf("ead_start_cache() done. (error_code = %d)\n", error_code);
    if (error_code != EAD_OK) {
        /* Need to terminate EADS client */
        goto ERR;
    }
 
    /* Store key and value */
    ead_put(cp, KEY, &value_element, &error_code);
    printf("ead_put() done. (error_code = %d)\n", error_code);
    if (error_code != EAD_OK) {
        /* Need to terminate EADS client */
        goto ERR;
    }
 
    /* Stop access to the cache */
    ead_stop_cache(cp, &error_code);
    cp = NULL;
    printf("ead_stop_cache() done. (error_code = %d)\n", error_code);
    if (error_code != EAD_OK) {
        /* Need to terminate EADS client */
        goto ERR;
    }
 
    /* Terminate EADS client */
    ead_terminate_client(cmp, &error_code);
    cmp = NULL;
    printf("ead_terminate_client() done. (error_code = %d)\n", error_code);
    if (error_code != EAD_OK) {
        /* Need to terminate EADS client */
        goto ERR;
    }
 
    return 0;
 
ERR:
    if(cp != NULL) {
        /* Stop access to the cache */
        ead_stop_cache(cp, &error_code);
        cp = NULL;
        printf("ead_stop_cache() done. (error_code = %d)\n", error_code);
    }
 
    if(cmp != NULL) {
        /* Terminate EADS client */
        ead_terminate_client(cmp, &error_code);
        cmp = NULL;
        printf("ead_terminate_client() done. (error_code = %d)\n", error_code);
    }
 
    return 1;
}

(2) Initializing the EADS client

To initialize an EADS client, use ead_init_client() or ead_init_client_n() and obtain a handle (pointer) to the cache manager for managing the cache.

Some settings are specified in the client properties, such as the connection-target EADS server.

If you want to use multiple cache managers with different settings, such as when connection is established with multiple clusters, edit the client properties and then execute ead_init_client() or ead_init_client_n() multiple times. To terminate the EADS client when you have executed ead_init_client() or ead_init_client_n() multiple times, execute ead_terminate_client() on each of the acquired handles to the cache manager.

(3) Starting access to the cache

After initialization of the EADS client is complete, start access to the cache.

To start access to the cache, use ead_start_cache() to obtain the handle (pointer) for controlling access to the cache. Using this handle will allow you to gain access to the cache.

(4) Storing keys and values

Use ead_put() to store a key and value in the cache.

In ead_put(), specify the handle to the cache obtained from ead_start_cache(). In addition, specify the key and value information (the value and its size) to be stored in the cache. The value information is provided as an ead_value_element structure.

(5) Retrieving values

Use ead_get() to retrieve values from the cache.

In ead_get(), specify the handle to the cache obtained from ead_start_cache(). In addition, specify the key associated with the value you want to retrieve.

If the value is successfully retrieved by ead_get(), the value information associated with the key is returned as an ead_value_element structure.

Note that the memory area that is returned as the return value is not automatically freed. For details about freeing this memory, see (10) Freeing a memory area returned as a return value.

The following is an example of source code for retrieving a value.

Example of source code (for retrieving a value)

{
    /* Retrieve a value */
    ead_value_element ret_value;
    char KEY[] = "key1";
    ret_value = ead_get(cp, KEY, &error_code);
    printf("ead_get() done. (error_code = %d)\n", error_code);
    if (error_code != EAD_OK) {
        /* Need to terminate EADS client */
        goto ERR;
    }
    /* Free the retrieved value when done using it */
    freeValue(&ret_value);
}

(6) Removing keys and values

Use ead_remove() to remove a specified key from the cache, as well as the value associated with that key.

In ead_remove(), specify the handle to the cache obtained from ead_start_cache(). In addition, specify the key associated with the value you want to remove.

(7) Executing user functions

(a) Execution method specifying a key or a group

Use ead_execute_function() to execute a user function by specifying a key or a group.

In ead_execute_function(), specify the handle to the cache acquired by ead_start_cache().

In addition, specify the name of the key or group to execute a user function and the name of the user function to be executed.

In addition, specify the user function arguments as an ead_object structure.

When the user function is executed by ead_execute_function(), the execution result of the user function is returned as an ead_object structure.

(b) Execution method specifying an EADS server

To execute a user function with an EADS server specified, use ead_execute_node_function().

In ead_execute_node_function(), specify the handle to the cache obtained from ead_start_cache().

Specify the EADS server that will be executing the user function as an ead_node structure and the arguments to be specified in the user function as an ead_object structure.

Also, specify the name of the user function to be executed.

When the user function is executed by ead_execute_node_function(), the user function execution results are returned as an ead_object structure.

(8) Stopping access to the cache

Use ead_stop_cache() to stop access to the cache.

In ead_stop_cache(), specify the handle to the cache obtained from ead_start_cache().

(9) Terminating the EADS client

Use ead_terminate_client() to terminate the EADS client.

Specify in ead_terminate_client() the handle to the cache manager that was acquired by ead_init_client() or ead_init_client_n().

(10) Freeing a memory area returned as a return value

The memory area that is returned as the return value of ead_get() is not freed automatically. Instead, you must define in the application program a function such as the one below to free the memory.

/* Freeing a memory area returned as a return value */
void freeValue(ead_value_element *value) {
    if (value->value != NULL) {
        free(value->value);
        value->value = NULL;
    }
    value->value_size = 0;
}

The table below lists the functions for which memory area returned as a return value must be freed by the application program. If a return value to be released is not NULL, free the memory area for the functions listed in this table when the return value is no longer needed.

Table 19‒1: Functions whose memory area is to be freed when the return value is released

No.

Function name

Return value to be released

1

ead_get_cache_name()

char-type pointer returned as a cache name

2

ead_put_all()

failure_info member of the ead_put_all_results structure

3

ead_get()

value member of the ead_value_element structure

4

ead_get_group()

The following members of the ead_get_group_results structure:

  • key_value_array member

  • key member of the ead_key_value_pair structure in the key_value_array member

  • value member of the ead_value_element structure within the ead_key_value_pair structure in the key_value_array member

5

ead_get_all()

The following members of the ead_get_all_results structure:

  • values member

  • value member of the ead_value_element structure in the values member

  • failure_info member

6

ead_remove_all()

failure_info member of the ead_remove_all_results structure

7

ead_get_group_names()

group_names member of the ead_group_names structure

8

ead_get_group_keys()

keys member of the ead_keys structure

9

ead_get_node_keys()

10

ead_get_group_first_key()

char-type pointer returned as a key

11

ead_get_node_first_key()

12

ead_get_group_next_key()

13

ead_get_node_next_key()

14

ead_execute_function()

object member of the ead_object structure that is returned as user function execution results

15

ead_execute_function_rt()

16

ead_execute_node_function()

17

ead_execute_node_function_rt()

18

ead_get_nodelist()

nodes member of the ead_nodelist structure

19

ead_get_slave_nodelist()