1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2015 Brazil
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "ts_util.h"
20
21#include "../grn_dat.h"
22#include "../grn_hash.h"
23#include "../grn_pat.h"
24
25#include "ts_log.h"
26
27grn_rc
28grn_ts_obj_increment_ref_count(grn_ctx *ctx, grn_obj *obj)
29{
30 grn_id id = grn_obj_id(ctx, obj);
31 grn_obj *obj_clone = grn_ctx_at(ctx, id);
32 if (!obj_clone) {
33 GRN_TS_ERR_RETURN(GRN_UNKNOWN_ERROR, "grn_ctx_at failed: %d", id);
34 }
35 if (obj_clone != obj) {
36 grn_obj_unlink(ctx, obj_clone);
37 GRN_TS_ERR_RETURN(GRN_UNKNOWN_ERROR, "wrong object: %p != %p",
38 obj, obj_clone);
39 }
40 return GRN_SUCCESS;
41}
42
43grn_ts_bool
44grn_ts_obj_is_table(grn_ctx *ctx, grn_obj *obj)
45{
46 return grn_obj_is_table(ctx, obj);
47}
48
49grn_ts_bool
50grn_ts_obj_is_column(grn_ctx *ctx, grn_obj *obj)
51{
52 switch (obj->header.type) {
53 case GRN_COLUMN_FIX_SIZE:
54 case GRN_COLUMN_VAR_SIZE: {
55 return GRN_TRUE;
56 }
57 /* GRN_COLUMN_INDEX is not supported. */
58 default: {
59 return GRN_FALSE;
60 }
61 }
62}
63
64grn_rc
65grn_ts_ja_get_value(grn_ctx *ctx, grn_obj *ja, grn_ts_id id,
66 grn_ts_buf *buf, size_t *value_size)
67{
68 grn_rc rc;
69 uint32_t size;
70 grn_io_win iw;
71 char *ptr = (char *)grn_ja_ref(ctx, (grn_ja *)ja, id, &iw, &size);
72 if (!ptr) {
73 if (value_size) {
74 *value_size = 0;
75 }
76 return GRN_SUCCESS;
77 }
78 rc = grn_ts_buf_write(ctx, buf, ptr, size);
79 grn_ja_unref(ctx, &iw);
80 if (rc != GRN_SUCCESS) {
81 return rc;
82 }
83 if (value_size) {
84 *value_size = size;
85 }
86 return GRN_SUCCESS;
87}
88
89grn_ts_bool
90grn_ts_table_has_key(grn_ctx *ctx, grn_obj *table)
91{
92 switch (table->header.type) {
93 case GRN_TABLE_HASH_KEY:
94 case GRN_TABLE_PAT_KEY:
95 case GRN_TABLE_DAT_KEY: {
96 return GRN_TRUE;
97 }
98 default: {
99 return GRN_FALSE;
100 }
101 }
102}
103
104grn_ts_bool
105grn_ts_table_has_value(grn_ctx *ctx, grn_obj *table)
106{
107 return DB_OBJ(table)->range != GRN_DB_VOID;
108}
109
110const void *
111grn_ts_table_get_value(grn_ctx *ctx, grn_obj *table, grn_ts_id id)
112{
113 switch (table->header.type) {
114 case GRN_TABLE_HASH_KEY: {
115 return grn_hash_get_value_(ctx, (grn_hash *)table, id, NULL);
116 }
117 case GRN_TABLE_PAT_KEY: {
118 uint32_t size;
119 return grn_pat_get_value_(ctx, (grn_pat *)table, id, &size);
120 }
121 /* GRN_TABLE_DAT_KEY does not support _value. */
122 case GRN_TABLE_NO_KEY: {
123 return _grn_array_get_value(ctx, (grn_array *)table, id);
124 }
125 default: {
126 return NULL;
127 }
128 }
129}
130