using record creator functions in production code in ingest_record()
[pazpar2-moved-to-github.git] / src / record.c
1 /* $Id: record.c,v 1.7 2007-04-26 11:41:26 marc 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.7 2007-04-26 11:41:26 marc 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 {
63     struct record * record = 0;
64     int i = 0;
65     
66     // assert(nmem);
67
68     record = nmem_malloc(nmem, sizeof(struct record));
69
70     record->next = 0;
71     // which client should I use for record->client = cl;  ??
72     record->client = 0;
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     return record;
87 }
88
89
90 struct client * record_assign_client(struct record * record,
91                                      struct client * client)
92 {
93     record->client = client;
94     return client;
95 }
96
97 struct record_metadata * record_metadata_insert(NMEM nmem, 
98                                                 struct record_metadata ** rmd,
99                                                 union data_types data)
100 {
101     struct record_metadata * tmp_rmd = 0;
102     // assert(nmem);
103
104     if(!rmd)
105         return 0;
106
107     // construct new record_metadata
108     tmp_rmd  = nmem_malloc(nmem, sizeof(struct record_metadata));
109     tmp_rmd->data = data;
110
111
112     // insert in *rmd's place, moving *rmd one down the list
113     tmp_rmd->next = *rmd;
114     *rmd = tmp_rmd;
115
116     return *rmd;
117 }
118
119 struct record_metadata * record_add_metadata_field_id(NMEM nmem, 
120                                                      struct record * record,
121                                                      int field_id, 
122                                                      union data_types data)
123 {
124     if (field_id < 0 || !record || !record->metadata)
125         return 0;
126
127     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
128 }
129
130
131 struct record_metadata * record_add_metadata(NMEM nmem, 
132                                              struct record * record,
133                                              struct conf_service * service,
134                                              const char * name,
135                                              union data_types data)
136 {
137     int field_id = 0;
138
139     if (!record || !record->metadata || !service || !name)  
140         return 0;
141     
142     field_id = conf_service_metadata_field_id(service, name);
143
144     if (-1 == field_id)
145         return 0;
146     
147     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
148 }
149
150
151
152
153
154
155 union data_types * record_assign_sortkey_field_id(NMEM nmem, 
156                                                struct record * record,
157                                                int field_id, 
158                                                union data_types data)
159 {
160     if (field_id < 0 || !record || !record->sortkeys)
161         return 0;
162
163     return data_types_assign(nmem, &(record->sortkeys[field_id]), data);
164 }
165
166
167
168 union data_types * record_assign_sortkey(NMEM nmem, 
169                                       struct record * record,
170                                       struct conf_service * service,
171                                       const char * name,
172                                       union data_types data)
173 {
174     int field_id = 0;
175
176     if (!record || !service || !name)  
177         return 0;
178     
179     field_id = conf_service_sortkey_field_id(service, name);
180
181     if (!(-1 < field_id) || !(field_id < service->num_sortkeys))
182         return 0;
183
184     return record_assign_sortkey_field_id(nmem, record, field_id, data);
185 }
186
187
188
189 /*
190  * Local variables:
191  * c-basic-offset: 4
192  * indent-tabs-mode: nil
193  * End:
194  * vim: shiftwidth=4 tabstop=8 expandtab
195  */