4dc2366e330d266437ec6d70b932bbc9b6c2531f
[pazpar2-moved-to-github.git] / src / record.c
1 /* $Id: record.c,v 1.10 2007-06-13 21:29:04 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /* $Id: record.c,v 1.10 2007-06-13 21:29:04 adam Exp $ */
23
24
25 #include <string.h>
26
27 #include <yaz/yaz-util.h>
28 #include <yaz/nmem.h>
29
30 #if HAVE_CONFIG_H
31 #include <cconfig.h>
32 #endif
33
34 //#define CONFIG_NOEXTERNS
35 #include "config.h"
36 #include "record.h"
37
38
39
40 union data_types * data_types_assign(NMEM nmem, 
41                                      union data_types ** data1, 
42                                      union data_types data2)
43 {
44     // assert(nmem);
45
46     if (!data1)
47         return 0;
48
49     if (!*data1){
50         if (!nmem)
51             return 0;
52         else
53             *data1  = nmem_malloc(nmem, sizeof(union data_types));
54     }
55     
56     **data1 = data2;
57     return *data1;
58 }
59
60
61 struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys,
62                               struct client *client, int position)
63 {
64     struct record * record = 0;
65     int i = 0;
66     
67     // assert(nmem);
68
69     record = nmem_malloc(nmem, sizeof(struct record));
70
71     record->next = 0;
72     record->client = client;
73
74     record->metadata 
75         = nmem_malloc(nmem, 
76                       sizeof(struct record_metadata*) * num_metadata);
77     for (i = 0; i < num_metadata; i++)
78         record->metadata[i] = 0;
79     
80     record->sortkeys  
81         = nmem_malloc(nmem, 
82                       sizeof(union data_types*) * num_sortkeys);
83     for (i = 0; i < num_sortkeys; i++)
84         record->sortkeys[i] = 0;
85
86     record->position = position;
87     
88     return record;
89 }
90
91 struct record_metadata * record_metadata_create(NMEM nmem)
92 {
93     struct record_metadata * rec_md 
94         = nmem_malloc(nmem, sizeof(struct record_metadata));
95     rec_md->next = 0;
96     return rec_md;
97 }
98
99
100 struct record_metadata * record_metadata_insert(NMEM nmem, 
101                                                 struct record_metadata ** rmd,
102                                                 union data_types data)
103 {
104     struct record_metadata * tmp_rmd = 0;
105     // assert(nmem);
106
107     if(!rmd)
108         return 0;
109
110     // construct new record_metadata
111     tmp_rmd  = nmem_malloc(nmem, sizeof(struct record_metadata));
112     tmp_rmd->data = data;
113
114
115     // insert in *rmd's place, moving *rmd one down the list
116     tmp_rmd->next = *rmd;
117     *rmd = tmp_rmd;
118
119     return *rmd;
120 }
121
122 struct record_metadata * record_add_metadata_field_id(NMEM nmem, 
123                                                      struct record * record,
124                                                      int field_id, 
125                                                      union data_types data)
126 {
127     if (field_id < 0 || !record || !record->metadata)
128         return 0;
129
130     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
131 }
132
133
134 struct record_metadata * record_add_metadata(NMEM nmem, 
135                                              struct record * record,
136                                              struct conf_service * service,
137                                              const char * name,
138                                              union data_types data)
139 {
140     int field_id = 0;
141
142     if (!record || !record->metadata || !service || !name)  
143         return 0;
144     
145     field_id = conf_service_metadata_field_id(service, name);
146
147     if (-1 == field_id)
148         return 0;
149     
150     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
151 }
152
153
154
155
156
157
158 union data_types * record_assign_sortkey_field_id(NMEM nmem, 
159                                                struct record * record,
160                                                int field_id, 
161                                                union data_types data)
162 {
163     if (field_id < 0 || !record || !record->sortkeys)
164         return 0;
165
166     return data_types_assign(nmem, &(record->sortkeys[field_id]), data);
167 }
168
169
170
171 union data_types * record_assign_sortkey(NMEM nmem, 
172                                       struct record * record,
173                                       struct conf_service * service,
174                                       const char * name,
175                                       union data_types data)
176 {
177     int field_id = 0;
178
179     if (!record || !service || !name)  
180         return 0;
181     
182     field_id = conf_service_sortkey_field_id(service, name);
183
184     if (!(-1 < field_id) || !(field_id < service->num_sortkeys))
185         return 0;
186
187     return record_assign_sortkey_field_id(nmem, record, field_id, data);
188 }
189
190
191
192 /*
193  * Local variables:
194  * c-basic-offset: 4
195  * indent-tabs-mode: nil
196  * End:
197  * vim: shiftwidth=4 tabstop=8 expandtab
198  */