Add new mutex create where mutex attribute can be set
[yaz-moved-to-github.git] / src / facet.c
1
2
3 #include <yaz/facet.h>
4 #include <yaz/diagbib1.h>
5 #include <yaz/oid_db.h>
6 #include <yaz/oid_std.h>
7 #include <yaz/otherinfo.h>
8 #include <assert.h>
9
10 /* Little helper to extract a string attribute */
11 /* Gets the first string, there is usually only one */
12 /* in case of errors, returns null */
13
14 void facet_struct_init(struct attrvalues *attr_values) {
15     attr_values->errcode   = 0;
16     attr_values->errstring = 0;
17     attr_values->relation  = 0;
18     attr_values->useattr   = 0;
19     attr_values->useattrbuff[0] = 0;
20     attr_values->limit     = 0;
21 }
22
23 const char *stringattr( Z_ComplexAttribute *c ) {
24     int i;
25      Z_StringOrNumeric *son;
26     for ( i = 0; i < c->num_list; i++ ) {
27         son = c->list[i];
28         if ( son->which == Z_StringOrNumeric_string )
29             return son->u.string;
30     }
31     return 0;
32 }
33
34 /* Use attribute, @attr1, can be numeric or string */
35 void useattr ( Z_AttributeElement *ae,
36                        struct attrvalues *av )
37 {
38     const char *s;
39     if ( ae->which == Z_AttributeValue_complex ) {
40         s = stringattr( ae->value.complex );
41         yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr);
42         if (s) {
43             if (!av->useattr)
44                 av->useattr = s;
45             else { /* already seen one, can't have duplicates */
46                 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
47                 av->errstring = "multiple use attributes";
48             }
49         } else { /* complex that did not return a string */
50             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
51             av->errstring = "non-string complex attribute";
52         }
53     } else { /* numeric - could translate 4 to 'title' etc */
54         sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric );
55         av->useattr = av->useattrbuff;
56     }
57 } /* useattr */
58
59
60 /* TODO rename to sortorder attr */
61 void relationattr ( Z_AttributeElement *ae,
62                            struct attrvalues *av )
63 {
64     if ( ae->which == Z_AttributeValue_numeric ) {
65         if ( *ae->value.numeric == 0 )
66             av->relation = "desc";
67         else if ( *ae->value.numeric == 1 )
68                 av->relation = "asc";
69             else
70         if ( *ae->value.numeric == 3 ) {
71             av->relation = "unknown/unordered";
72         } else {
73             av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
74             sprintf(av->useattrbuff, ODR_INT_PRINTF,
75                         *ae-> attributeType);
76             av->errstring = av->useattrbuff;
77         }
78     } else {
79         av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
80         av->errstring = "non-numeric relation attribute";
81     }
82 } /* relationattr */
83
84 void limitattr ( Z_AttributeElement *ae,
85                         struct attrvalues *av )
86 {
87   /* TODO - check numeric first, then value! */
88     if ( ae->which == Z_AttributeValue_numeric ) {
89         av->limit = *ae->value.numeric;
90         yaz_log(YLOG_DEBUG, "limitattr %d ", av->limit);
91     } else {
92         av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
93         av->errstring = "non-numeric limit attribute";
94     }
95 } /* relationattr */
96
97 /* Get the index to be searched from the attributes.
98    @attr 1
99      can be either "string", or some well-known value like
100      4 for title
101    Returns a null and sets errors in rr,
102    emtpy string if no attr found,
103    or the string itself - always a pointer to the Z-structs,
104    so no need to free that string!
105 */
106
107 void facetattrs( Z_AttributeList *attributes,
108                           struct attrvalues *av )
109 {
110     int i;
111     Z_AttributeElement *ae;
112     yaz_log(YLOG_DEBUG, "Attribute num attributes: %d", attributes->num_attributes);
113     for ( i=0; i < attributes->num_attributes; i++ ) {
114         ae = attributes->attributes[i];
115         /* ignoring the attributeSet here */
116         yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType);
117         if ( *ae->attributeType == 1 ) { /* use attribute */
118             useattr(ae, av);
119         } else if ( *ae->attributeType == 2 ) { /* sortorder */
120             relationattr(ae, av);
121         } else if ( *ae->attributeType == 3 ) { /* limit */
122             limitattr(ae, av);
123         } else { /* unknown attribute */
124             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
125             sprintf(av->useattrbuff, ODR_INT_PRINTF,
126                         *ae-> attributeType);
127             av->errstring = av->useattrbuff;
128             yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff);
129             /* would like to give a better message, but the standard */
130             /* tells me to return the attribute type */
131         }
132         if ( av->errcode )
133             return; /* no need to dig deeper, return on first error */
134     }
135     return;
136 } /* facetattrs */
137
138
139 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) {
140     Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(&search_input, odr, yaz_oid_userinfo_facet_1, 1, 0);
141     return facet_list;
142 }
143
144 Z_Term *term_create(ODR odr, const char *cstr) {
145     Z_Term *term = odr_malloc(odr, sizeof(*term));
146     term->which = Z_Term_characterString;
147     term->u.characterString = odr_strdup(odr, cstr);
148     return term;
149 }
150
151 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq) {
152     Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
153     facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
154     facet_term->term = term;
155     *facet_term->count = freq;
156     return facet_term;
157 }
158
159 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, int num_terms) {
160     Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
161     facet_field->attributes = attributes;
162     facet_field->num_terms = num_terms;
163     facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
164     return facet_field;
165 }
166
167 void facet_field_term_set(ODR odr, Z_FacetField *field, Z_FacetTerm *facet_term, int index) {
168     assert(0 <= index && index < field->num_terms);
169     field->terms[index] = facet_term;
170 }
171
172 Z_FacetList* facet_list_create(ODR odr, int num_facets) {
173     Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
174     facet_list->num = num_facets;
175     facet_list->elements = odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
176     return facet_list;
177 }
178
179 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, int index) {
180     assert(0 <= index && index < list->num);
181     list->elements[index] = field;
182 }
183