summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/ts/ts_util.c
blob: 18e660602ea5c5e86d77b878095f4389c779c92a (plain)
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
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  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-1335  USA
*/

#include "ts_util.h"

#include "../grn_dat.h"
#include "../grn_hash.h"
#include "../grn_pat.h"

#include "ts_log.h"

grn_rc
grn_ts_obj_increment_ref_count(grn_ctx *ctx, grn_obj *obj)
{
  grn_id id = grn_obj_id(ctx, obj);
  grn_obj *obj_clone = grn_ctx_at(ctx, id);
  if (!obj_clone) {
    GRN_TS_ERR_RETURN(GRN_UNKNOWN_ERROR, "grn_ctx_at failed: %d", id);
  }
  if (obj_clone != obj) {
    grn_obj_unlink(ctx, obj_clone);
    GRN_TS_ERR_RETURN(GRN_UNKNOWN_ERROR, "wrong object: %p != %p",
                      obj, obj_clone);
  }
  return GRN_SUCCESS;
}

grn_ts_bool
grn_ts_obj_is_table(grn_ctx *ctx, grn_obj *obj)
{
  return grn_obj_is_table(ctx, obj);
}

grn_ts_bool
grn_ts_obj_is_column(grn_ctx *ctx, grn_obj *obj)
{
  switch (obj->header.type) {
    case GRN_COLUMN_FIX_SIZE:
    case GRN_COLUMN_VAR_SIZE: {
      return GRN_TRUE;
    }
    /* GRN_COLUMN_INDEX is not supported. */
    default: {
      return GRN_FALSE;
    }
  }
}

grn_rc
grn_ts_ja_get_value(grn_ctx *ctx, grn_obj *ja, grn_ts_id id,
                    grn_ts_buf *buf, size_t *value_size)
{
  grn_rc rc;
  uint32_t size;
  grn_io_win iw;
  char *ptr = (char *)grn_ja_ref(ctx, (grn_ja *)ja, id, &iw, &size);
  if (!ptr) {
    if (value_size) {
      *value_size = 0;
    }
    return GRN_SUCCESS;
  }
  rc = grn_ts_buf_write(ctx, buf, ptr, size);
  grn_ja_unref(ctx, &iw);
  if (rc != GRN_SUCCESS) {
    return rc;
  }
  if (value_size) {
    *value_size = size;
  }
  return GRN_SUCCESS;
}

grn_ts_bool
grn_ts_table_has_key(grn_ctx *ctx, grn_obj *table)
{
  switch (table->header.type) {
    case GRN_TABLE_HASH_KEY:
    case GRN_TABLE_PAT_KEY:
    case GRN_TABLE_DAT_KEY: {
      return GRN_TRUE;
    }
    default: {
      return GRN_FALSE;
    }
  }
}

grn_ts_bool
grn_ts_table_has_value(grn_ctx *ctx, grn_obj *table)
{
  return DB_OBJ(table)->range != GRN_DB_VOID;
}

const void *
grn_ts_table_get_value(grn_ctx *ctx, grn_obj *table, grn_ts_id id)
{
  switch (table->header.type) {
    case GRN_TABLE_HASH_KEY: {
      return grn_hash_get_value_(ctx, (grn_hash *)table, id, NULL);
    }
    case GRN_TABLE_PAT_KEY: {
      uint32_t size;
      return grn_pat_get_value_(ctx, (grn_pat *)table, id, &size);
    }
    /* GRN_TABLE_DAT_KEY does not support _value. */
    case GRN_TABLE_NO_KEY: {
      return _grn_array_get_value(ctx, (grn_array *)table, id);
    }
    default: {
      return NULL;
    }
  }
}