CUBRID Engine  latest
shard_key_func.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * shard_key_func.c -
22  *
23  */
24 
25 #ident "$Id$"
26 
27 
28 #include "shard_key_func.h"
29 #include "broker_shm.h"
30 #include "shard_metadata.h"
31 /* FOR DEBUG MSG -- remove this */
32 #include "broker_config.h"
33 
34 #if defined(CUB_PROXY)
35 #include "shard_proxy_log.h"
36 #else /* CUB_PROXY */
37 #if defined(WINDOWS)
38 #define PROXY_LOG(level, fmt, ...)
39 #else /* WINDOWS */
40 #define PROXY_LOG(level, fmt, args...)
41 #endif /* !WINDOWS */
42 #endif /* !CUB_PROXY */
43 
44 extern T_SHM_PROXY *shm_proxy_p;
45 
46 int
48 {
49  int error;
50 
51  if (shm_proxy_p->shard_key_library_name[0] != '\0' && shm_proxy_p->shard_key_function_name[0] != '\0')
52  {
53  error = load_shard_key_function (shm_proxy_p->shard_key_library_name, shm_proxy_p->shard_key_function_name);
54  if (error < 0)
55  {
57  "Failed to load " "shard hashing library. " "(library_name:[%s], function:[%s]).\n",
58  shm_proxy_p->shard_key_library_name, shm_proxy_p->shard_key_function_name);
60  return -1;
61  }
62 
64  "Loading shard hashing " "library was completed. " "(library_name:[%s], function:[%s]).\n",
65  shm_proxy_p->shard_key_library_name, shm_proxy_p->shard_key_function_name);
66  return 0;
67  }
68 
70 #if defined(SHARD_VERBOSE_DEBUG)
71  printf ("FN_GET_SHARD_KEY REGISTER DEFAULT DONE\n");
72 #endif
73 
74  return 0;
75 }
76 
77 int
78 fn_get_shard_key_default (const char *shard_key, T_SHARD_U_TYPE type, const void *value, int value_len)
79 {
80  int modular_key;
81 
82  if (value == NULL)
83  {
84  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Invalid shard key value. " "Shard key value couldn't be NUll.");
85  return ERROR_ON_ARGUMENT;
86  }
87 
88  modular_key = shm_proxy_p->shard_key_modular;
89  if (modular_key < 0)
90  {
92  "Invalid modular key. " "Shard modular key value couldn't be negative integer. " "(modular_key:%d).",
93  modular_key);
95  }
96 
97  if (type == SHARD_U_TYPE_INT || type == SHARD_U_TYPE_UINT)
98  {
99  unsigned int ival;
100  ival = (unsigned int) (*(unsigned int *) value);
101  return ival % modular_key;
102  }
103  else if (type == SHARD_U_TYPE_SHORT || type == SHARD_U_TYPE_USHORT)
104  {
105  unsigned short sval;
106  sval = (unsigned short) (*(unsigned short *) value);
107  return sval % modular_key;
108  }
109  else if (type == SHARD_U_TYPE_BIGINT || type == SHARD_U_TYPE_UBIGINT)
110  {
111  UINT64 lval;
112  lval = (UINT64) (*(UINT64 *) value);
113  return lval % modular_key;
114  }
115  else
116  {
117  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Unexpected shard key type. " "(type:%d).", type);
118  return ERROR_ON_ARGUMENT;
119  }
120 }
121 
122 int
123 proxy_find_shard_id_by_hint_value (SP_VALUE * value_p, const char *key_column)
124 {
125  int shard_key_id = -1;
126  INT64 shard_key_val_int;
127  char *shard_key_val_string;
128  int shard_key_val_len;
129 
130  if (value_p->type == VT_INTEGER)
131  {
132  shard_key_val_int = value_p->integer;
133  shard_key_id = (*fn_get_shard_key) (key_column, SHARD_U_TYPE_BIGINT, &shard_key_val_int, sizeof (INT64));
134  }
135  else if (value_p->type == VT_STRING)
136  {
137  shard_key_val_string = value_p->string.value;
138  shard_key_val_len = value_p->string.length;
139  shard_key_id = (*fn_get_shard_key) (key_column, SHARD_U_TYPE_STRING, shard_key_val_string, shard_key_val_len);
140  }
141  else
142  {
143  PROXY_LOG (PROXY_LOG_MODE_ERROR, "Invalid hint value type. (value_type:%d).", value_p->type);
144  }
145  return shard_key_id;
146 }
char shard_key_library_name[SHM_PROXY_NAME_MAX]
Definition: broker_shm.h:538
INT64 integer
Definition: shard_parser.h:114
FN_GET_SHARD_KEY fn_get_shard_key
char * value
Definition: shard_parser.h:110
#define ERROR_ON_MAKE_SHARD_KEY
Definition: shard_key.h:84
int load_shard_key_function(const char *library_name, const char *function_name)
SP_VALUE_TYPE type
Definition: shard_parser.h:106
#define ERROR_ON_ARGUMENT
Definition: shard_key.h:83
T_SHARD_U_TYPE
Definition: shard_key.h:45
#define NULL
Definition: freelistheap.h:34
int fn_get_shard_key_default(const char *shard_key, T_SHARD_U_TYPE type, const void *value, int value_len)
int proxy_find_shard_id_by_hint_value(SP_VALUE *value_p, const char *key_column)
char shard_key_function_name[SHM_PROXY_NAME_MAX]
Definition: broker_shm.h:539
static void error(const char *msg)
Definition: gencat.c:331
T_SHM_PROXY * shm_proxy_p
#define PROXY_LOG(level, fmt, args...)
struct sp_value::@40 string
int shard_key_modular
Definition: broker_shm.h:537
int register_fn_get_shard_key(void)
void close_shard_key_function(void)