IDZEBRA  2.1.2
attribute.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 
25 #include <yaz/log.h>
26 #include <yaz/diagbib1.h>
27 #include <idzebra/res.h>
28 #include <idzebra/util.h>
29 #include <attrfind.h>
30 #include "index.h"
31 #include <yaz/oid_db.h>
32 
33 static data1_att *getatt(data1_attset *p, int att)
34 {
35  data1_att *a;
37 
38  /* scan local set */
39  for (a = p->atts; a; a = a->next)
40  if (a->value == att)
41  return a;
42  /* scan included sets */
43  for (c = p->children; c; c = c->next)
44  if ((a = getatt(c->child, att)))
45  return a;
46  return 0;
47 }
48 
49 static int att_getentbyatt(ZebraHandle zi, const Odr_oid *set, int att,
50  const char **name)
51 {
52  data1_att *r;
53  data1_attset *p;
54 
55  if (!(p = data1_attset_search_id (zi->reg->dh, set)))
56  {
57  zebraExplain_loadAttsets (zi->reg->dh, zi->res);
58  p = data1_attset_search_id (zi->reg->dh, set);
59  }
60  if (!p) /* set undefined */
61  return -2;
62  if (!(r = getatt(p, att)))
63  return -1;
64  *name = r->name;
65  return 0;
66 }
67 
68 
70  Z_AttributeList *attr_list,
72  const char *index_type,
73  const Odr_oid *curAttributeSet,
74  int *ord)
75 {
76  int use_value = -1;
77  const char *use_string = 0;
78  AttrType use;
79 
80  attr_init_AttrList(&use, attr_list, 1);
81  use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
82 
83  if (use_value < 0)
84  {
85  if (!use_string)
86  use_string = "any";
87  }
88  else
89  {
90  /* we have a use attribute and attribute set */
91  int r;
92 
93  r = att_getentbyatt(zh, curAttributeSet, use_value, &use_string);
94  if (r == -2)
95  {
96  zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_ATTRIBUTE_SET, 0);
97  return ZEBRA_FAIL;
98  }
99  if (r == -1)
100  {
101  zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
102  return ZEBRA_FAIL;
103  }
104  }
105  if (!use_string)
106  {
107  zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
108  return ZEBRA_FAIL;
109  }
110  *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat,
111  index_type, use_string);
112  if (*ord == -1)
113  {
114  /* attribute 14=1 does not issue a diagnostic even
115  1) the attribute is numeric but listed in .att
116  2) the use attribute is string
117  */
118  AttrType unsup;
119  int unsup_value = 0;
120  attr_init_AttrList(&unsup, attr_list, 14);
121  unsup_value = attr_find(&unsup, 0);
122 
123  if (unsup_value != 1)
124  {
125  if (use_value < 0)
126  zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_string);
127  else
128  zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
129  return ZEBRA_FAIL;
130  }
131  }
132  return ZEBRA_OK;
133 }
134 
136  Z_AttributesPlusTerm *zapt,
137  const char *index_type,
138  const char *xpath_use,
139  const Odr_oid *curAttributeSet,
140  int *ord)
141 {
142  ZEBRA_RES res = ZEBRA_OK;
143  AttrType relation;
144  int relation_value;
146 
147  attr_init_APT(&relation, zapt, 2);
148  relation_value = attr_find(&relation, NULL);
149 
150  if (relation_value == 103) /* always matches */
152 
153  if (!xpath_use)
154  {
155  res = zebra_attr_list_get_ord(zh, zapt->attributes,
156  cat, index_type,
157  curAttributeSet, ord);
158  /* use attribute not found. But it the relation is
159  always matches and the regulare index attribute is found
160  return a different diagnostic */
161  if (res != ZEBRA_OK &&
162  relation_value == 103
164  zh, zapt->attributes,
165  zinfo_index_category_index, index_type,
166  curAttributeSet, ord) == ZEBRA_OK)
167  zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 103);
168  }
169  else
170  {
171  *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, index_type,
172  xpath_use);
173  if (*ord == -1)
174  {
175  yaz_log(YLOG_LOG, "zebra_apt_get_ord FAILED xpath=%s index_type=%s",
176  xpath_use, index_type);
177  zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
178  res = ZEBRA_FAIL;
179  }
180  else
181  {
182  yaz_log(YLOG_LOG, "zebra_apt_get_ord OK xpath=%s index_type=%s",
183  xpath_use, index_type);
184 
185  }
186  }
187  return res;
188 }
189 
191  Z_SortAttributes *sortAttributes,
192  int *ord,
193  int *numerical)
194 {
195  AttrType structure;
196  int structure_value;
197 
198  attr_init_AttrList(&structure, sortAttributes->list, 4);
199 
200  *numerical = 0;
201  structure_value = attr_find(&structure, 0);
202  if (structure_value == 109)
203  *numerical = 1;
204 
206  zh, sortAttributes->list,
208  0 /* any index */, yaz_oid_attset_bib_1, ord) == ZEBRA_OK)
209  return ZEBRA_OK;
210  return ZEBRA_FAIL;
211 }
212 
213 
214 /*
215  * Local variables:
216  * c-basic-offset: 4
217  * c-file-style: "Stroustrup"
218  * indent-tabs-mode: nil
219  * End:
220  * vim: shiftwidth=4 tabstop=8 expandtab
221  */
222 
void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
Definition: zebraapi.c:2754
#define ZEBRA_OK
Definition: util.h:82
void zebraExplain_loadAttsets(data1_handle dh, Res res)
Definition: zinfo.c:1628
int value
Definition: data1.h:59
int attr_find(AttrType *src, const Odr_oid **attribute_set_oid)
Definition: attrfind.c:99
struct zebra_register * reg
Definition: index.h:174
#define ZEBRA_FAIL
Definition: util.h:81
data1_handle dh
Definition: index.h:142
ZebraHandle zh
Definition: zebrash.c:64
static int att_getentbyatt(ZebraHandle zi, const Odr_oid *set, int att, const char **name)
Definition: attribute.c:49
data1_att * next
Definition: data1.h:60
ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh, Z_AttributesPlusTerm *zapt, const char *index_type, const char *xpath_use, const Odr_oid *curAttributeSet, int *ord)
Definition: attribute.c:135
data1_att * atts
Definition: data1.h:72
int zebraExplain_lookup_attr_str(ZebraExplainInfo zei, zinfo_index_category_t cat, const char *index_type, const char *str)
lookup ordinal from string index + index type
Definition: zinfo.c:1354
void zebra_setError_zint(ZebraHandle zh, int code, zint i)
Definition: zebraapi.c:2763
int attr_find_ex(AttrType *src, const Odr_oid **attribute_set_oid, const char **string_value)
Definition: attrfind.c:45
ZebraExplainInfo zei
Definition: index.h:139
data1_attset * data1_attset_search_id(data1_handle dh, const Odr_oid *oid)
Definition: d1_absyn.c:253
void attr_init_APT(AttrType *src, Z_AttributesPlusTerm *zapt, int type)
Definition: attrfind.c:27
data1_attset * child
Definition: data1.h:64
data1_attset_child * children
Definition: data1.h:73
ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh, Z_AttributeList *attr_list, zinfo_index_category_t cat, const char *index_type, const Odr_oid *curAttributeSet, int *ord)
Definition: attribute.c:69
static data1_att * getatt(data1_attset *p, int att)
Definition: attribute.c:33
void attr_init_AttrList(AttrType *src, Z_AttributeList *list, int type)
Definition: attrfind.c:36
zinfo_index_category_t
Definition: zinfo.h:37
data1_attset_child * next
Definition: data1.h:65
ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh, Z_SortAttributes *sortAttributes, int *ord, int *numerical)
Definition: attribute.c:190
char * name
Definition: data1.h:58
short ZEBRA_RES
Common return type for Zebra API.
Definition: util.h:80