Preserve pz:metadata attributes
[pazpar2-moved-to-github.git] / src / record.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
4 Pazpar2 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 Pazpar2 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 #include <string.h>
21
22 #include <yaz/yaz-util.h>
23 #include <yaz/nmem.h>
24
25 #if HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include "pazpar2_config.h"
30 #include "record.h"
31
32 union data_types * data_types_assign(NMEM nmem, 
33                                      union data_types ** data1, 
34                                      union data_types data2)
35 {
36     // assert(nmem);
37
38     if (!data1)
39         return 0;
40
41     if (!*data1){
42         if (!nmem)
43             return 0;
44         else
45             *data1  = nmem_malloc(nmem, sizeof(union data_types));
46     }
47     
48     **data1 = data2;
49     return *data1;
50 }
51
52
53 struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys,
54                               struct client *client, int position)
55 {
56     struct record * record = 0;
57     int i = 0;
58     
59     // assert(nmem);
60
61     record = nmem_malloc(nmem, sizeof(struct record));
62
63     record->next = 0;
64     record->client = client;
65
66     record->metadata 
67         = nmem_malloc(nmem, 
68                       sizeof(struct record_metadata*) * num_metadata);
69     for (i = 0; i < num_metadata; i++)
70         record->metadata[i] = 0;
71     
72     record->sortkeys  
73         = nmem_malloc(nmem, 
74                       sizeof(union data_types*) * num_sortkeys);
75     for (i = 0; i < num_sortkeys; i++)
76         record->sortkeys[i] = 0;
77
78     record->position = position;
79     
80     return record;
81 }
82
83 struct record_metadata * record_metadata_create(NMEM nmem)
84 {
85     struct record_metadata * rec_md 
86         = nmem_malloc(nmem, sizeof(struct record_metadata));
87     rec_md->next = 0;
88     rec_md->attributes = 0;
89     return rec_md;
90 }
91
92
93 struct record_metadata * record_metadata_insert(NMEM nmem, 
94                                                 struct record_metadata ** rmd,
95                                                 union data_types data)
96 {
97     struct record_metadata * tmp_rmd = 0;
98     // assert(nmem);
99
100     if(!rmd)
101         return 0;
102
103     // construct new record_metadata
104     tmp_rmd  = nmem_malloc(nmem, sizeof(struct record_metadata));
105     tmp_rmd->data = data;
106
107
108     // insert in *rmd's place, moving *rmd one down the list
109     tmp_rmd->next = *rmd;
110     *rmd = tmp_rmd;
111
112     return *rmd;
113 }
114
115 struct record_metadata * record_add_metadata_field_id(NMEM nmem, 
116                                                      struct record * record,
117                                                      int field_id, 
118                                                      union data_types data)
119 {
120     if (field_id < 0 || !record || !record->metadata)
121         return 0;
122
123     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
124 }
125
126
127 struct record_metadata * record_add_metadata(NMEM nmem, 
128                                              struct record * record,
129                                              struct conf_service * service,
130                                              const char * name,
131                                              union data_types data)
132 {
133     int field_id = 0;
134
135     if (!record || !record->metadata || !service || !name)  
136         return 0;
137     
138     field_id = conf_service_metadata_field_id(service, name);
139
140     if (-1 == field_id)
141         return 0;
142     
143     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
144 }
145
146
147
148
149
150
151 union data_types * record_assign_sortkey_field_id(NMEM nmem, 
152                                                struct record * record,
153                                                int field_id, 
154                                                union data_types data)
155 {
156     if (field_id < 0 || !record || !record->sortkeys)
157         return 0;
158
159     return data_types_assign(nmem, &(record->sortkeys[field_id]), data);
160 }
161
162
163
164 union data_types * record_assign_sortkey(NMEM nmem, 
165                                       struct record * record,
166                                       struct conf_service * service,
167                                       const char * name,
168                                       union data_types data)
169 {
170     int field_id = 0;
171
172     if (!record || !service || !name)  
173         return 0;
174     
175     field_id = conf_service_sortkey_field_id(service, name);
176
177     if (!(-1 < field_id) || !(field_id < service->num_sortkeys))
178         return 0;
179
180     return record_assign_sortkey_field_id(nmem, record, field_id, data);
181 }
182
183
184
185 /*
186  * Local variables:
187  * c-basic-offset: 4
188  * c-file-style: "Stroustrup"
189  * indent-tabs-mode: nil
190  * End:
191  * vim: shiftwidth=4 tabstop=8 expandtab
192  */
193