1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
Copyright (C) 2009-2016 Brazil
Copyright (C) 2020-2024 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grn_ctx_impl.h"
#include "grn_db.h"
#include "grn_type.h"
const char *
grn_type_id_to_string_builtin(grn_ctx *ctx, grn_id id)
{
switch (id) {
case GRN_DB_OBJECT:
return "Object";
case GRN_DB_BOOL:
return "Bool";
case GRN_DB_INT8:
return "Int8";
case GRN_DB_UINT8:
return "UInt8";
case GRN_DB_INT16:
return "Int16";
case GRN_DB_UINT16:
return "UInt16";
case GRN_DB_INT32:
return "Int32";
case GRN_DB_UINT32:
return "UInt32";
case GRN_DB_INT64:
return "Int64";
case GRN_DB_UINT64:
return "UInt64";
case GRN_DB_FLOAT:
return "Float";
case GRN_DB_TIME:
return "Time";
case GRN_DB_SHORT_TEXT:
return "ShortText";
case GRN_DB_TEXT:
return "Text";
case GRN_DB_LONG_TEXT:
return "LongText";
case GRN_DB_TOKYO_GEO_POINT:
return "TokyoGeoPoint";
case GRN_DB_WGS84_GEO_POINT:
return "TokyoWGS84GeoPoint";
case GRN_DB_FLOAT32:
return "Float32";
case GRN_DB_BFLOAT16:
return "BFloat16";
default:
return "not a builtin type";
}
}
bool
grn_type_id_is_builtin(grn_ctx *ctx, grn_id id)
{
return id >= GRN_DB_OBJECT && id <= GRN_DB_BFLOAT16;
}
bool
grn_type_id_is_number_family(grn_ctx *ctx, grn_id id)
{
return (GRN_DB_INT8 <= id && id <= GRN_DB_FLOAT) || (id == GRN_DB_FLOAT32) ||
(id == GRN_DB_BFLOAT16);
}
bool
grn_type_id_is_float_family(grn_ctx *ctx, grn_id id)
{
switch (id) {
case GRN_DB_BFLOAT16:
case GRN_DB_FLOAT32:
case GRN_DB_FLOAT:
return true;
default:
return false;
}
}
bool
grn_type_id_is_text_family(grn_ctx *ctx, grn_id id)
{
return GRN_DB_SHORT_TEXT <= id && id <= GRN_DB_LONG_TEXT;
}
bool
grn_type_id_is_compatible(grn_ctx *ctx, grn_id id1, grn_id id2)
{
if (id1 == id2) {
return true;
}
if (grn_type_id_is_text_family(ctx, id1) &&
grn_type_id_is_text_family(ctx, id2)) {
return true;
}
return false;
}
size_t
grn_type_id_size(grn_ctx *ctx, grn_id id)
{
switch (id) {
case GRN_DB_BOOL:
return sizeof(bool);
case GRN_DB_INT8:
return sizeof(int8_t);
case GRN_DB_UINT8:
return sizeof(uint8_t);
case GRN_DB_INT16:
return sizeof(int16_t);
case GRN_DB_UINT16:
return sizeof(uint16_t);
case GRN_DB_INT32:
return sizeof(int32_t);
case GRN_DB_UINT32:
return sizeof(uint32_t);
case GRN_DB_INT64:
return sizeof(int64_t);
case GRN_DB_UINT64:
return sizeof(uint64_t);
case GRN_DB_BFLOAT16:
#ifdef GRN_HAVE_BFLOAT16
return sizeof(grn_bfloat16);
#else
return sizeof(uint16_t);
#endif
case GRN_DB_FLOAT32:
return sizeof(float);
case GRN_DB_FLOAT:
return sizeof(double);
case GRN_DB_TIME:
return sizeof(int64_t);
case GRN_DB_SHORT_TEXT:
return GRN_TYPE_SHORT_TEXT_SIZE;
case GRN_DB_TEXT:
return GRN_TYPE_TEXT_SIZE;
case GRN_DB_LONG_TEXT:
return GRN_TYPE_LONG_TEXT_SIZE;
case GRN_DB_TOKYO_GEO_POINT:
case GRN_DB_WGS84_GEO_POINT:
return sizeof(grn_geo_point);
default:
{
GRN_API_ENTER;
size_t size = 0;
if (grn_type_id_is_builtin(ctx, id)) {
grn_obj *obj = grn_ctx_at(ctx, id);
if (grn_obj_is_type(ctx, obj)) {
size = grn_type_size(ctx, obj);
}
grn_obj_unref(ctx, obj);
} else {
/*
We need to check the ID is a ID for table:
grn_obj *obj = grn_ctx_at(ctx, id);
if (grn_obj_is_table(ctx, obj)) {
size = sizeof(grn_id);
}
grn_obj_unref(ctx, obj);
But we want to reduce grn_ctx_at() call for performance.
So we assume the ID is a ID for table here.
*/
size = sizeof(grn_id);
}
GRN_API_RETURN(size);
}
}
}
grn_obj *
grn_type_create(grn_ctx *ctx,
const char *name,
unsigned int name_size,
grn_obj_flags flags,
unsigned int size)
{
grn_obj *db;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "[type][create] DB is not initialized");
return NULL;
}
GRN_API_ENTER;
if (grn_db_check_name(ctx, name, name_size)) {
GRN_DB_CHECK_NAME_ERR("[type][create]", name, name_size);
GRN_API_RETURN(NULL);
}
grn_id id = grn_obj_register(ctx, db, name, name_size);
if (id == GRN_ID_NIL) {
ERR(GRN_INVALID_ARGUMENT,
"[type][create] failed to register type: <%.*s>",
(int)name_size,
name);
GRN_API_RETURN(NULL);
}
grn_obj *type = grn_type_create_internal(ctx, id, flags, size);
GRN_API_RETURN(type);
}
grn_obj *
grn_type_create_internal(grn_ctx *ctx,
grn_id id,
grn_obj_flags flags,
unsigned int size)
{
GRN_API_ENTER;
struct _grn_type *type = GRN_CALLOC(sizeof(grn_db_obj));
if (!type) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"[type][init] failed to allocate type: <%u>",
id);
GRN_API_RETURN(NULL);
}
GRN_DB_OBJ_SET_TYPE(type, GRN_TYPE);
type->obj.header.flags = flags;
type->obj.header.domain = GRN_ID_NIL;
GRN_TYPE_SIZE(&(type->obj)) = size;
if (grn_db_obj_init(ctx, ctx->impl->db, id, DB_OBJ(type))) {
GRN_FREE(type);
GRN_API_RETURN(NULL);
}
GRN_API_RETURN((grn_obj *)type);
}
uint32_t
grn_type_size(grn_ctx *ctx, grn_obj *type)
{
uint32_t size;
GRN_API_ENTER;
if (!type) {
ERR(GRN_INVALID_ARGUMENT, "[type][size] type is NULL");
GRN_API_RETURN(0);
}
size = GRN_TYPE_SIZE(DB_OBJ(type));
GRN_API_RETURN(size);
}
|