-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbag_valuetable.cpp
More file actions
577 lines (470 loc) · 14.3 KB
/
bag_valuetable.cpp
File metadata and controls
577 lines (470 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "bag_compounddatatype.h"
#include "bag_georefmetadatalayerdescriptor.h"
#include "bag_dataset.h"
#include "bag_exceptions.h"
#include "bag_hdfhelper.h"
#include "bag_private.h"
#include "bag_valuetable.h"
#include <algorithm>
#include <array>
#include <H5Cpp.h>
namespace BAG {
namespace {
//! Convert a chunk of memory and a definition into a Record.
/*!
\param buffer
A chunk of memory representing a Record for a georeferenced metadata layer.
\param definition
The definition of the Record for a georeferenced metadata layer.
\return
A georeferenced metadata layer record using the buffer and definition.
*/
Record convertMemoryToRecord(
const uint8_t* const buffer,
const RecordDefinition& definition)
{
if (!buffer)
return {};
Record record;
record.reserve(definition.size());
size_t fieldOffset = 0;
for (const auto& field : definition)
{
const auto fieldType = static_cast<DataType>(field.type);
CompoundDataType value;
const char* str = nullptr;
switch(fieldType)
{
case DT_BOOLEAN:
value = *reinterpret_cast<const bool*>(buffer + fieldOffset);
break;
case DT_FLOAT32:
value = *reinterpret_cast<const float*>(buffer + fieldOffset);
break;
case DT_UINT32:
value = *reinterpret_cast<const uint32_t*>(buffer + fieldOffset);
break;
case DT_STRING:
{
const auto address =
*reinterpret_cast<const std::uintptr_t*>(buffer + fieldOffset);
str = reinterpret_cast<const char*>(address);
value = std::string{str ? str : ""};
// Clean up the char* allocated by HDF reading.
free(const_cast<char*>(str));
break;
}
case DT_UINT8: //[[fallthrough]]
case DT_UINT16: //[[fallthrough]]
case DT_UINT64: //[[fallthrough]]
case DT_COMPOUND: //[[fallthrough]]
case DT_UNKNOWN_DATA_TYPE: //[[fallthrough]]
default:
throw UnsupportedDataType{};
}
record.emplace_back(value);
fieldOffset += Layer::getElementSize(fieldType);
}
return record;
}
//! Convert a Record into a chunk of memory.
/*!
\param record
The record to convert into a chunk of memory.
\param buffer
The memory to store the record in.
*/
void convertRecordToMemory(
const Record& record,
uint8_t* buffer)
{
size_t fieldOffset = 0; // offset into buffer to the field
for (const auto& field : record)
{
const auto fieldType = field.getType();
switch(fieldType)
{
case DT_BOOLEAN:
*reinterpret_cast<bool*>(buffer + fieldOffset) = field.asBool();
break;
case DT_FLOAT32:
*reinterpret_cast<float*>(buffer + fieldOffset) = field.asFloat();
break;
case DT_UINT32:
*reinterpret_cast<uint32_t*>(buffer + fieldOffset) = field.asUInt32();
break;
case DT_STRING:
{
*reinterpret_cast<char**>(buffer + fieldOffset) =
const_cast<char*>(field.asString().data());
break;
}
case DT_UINT8: //[[fallthrough]]
case DT_UINT16: //[[fallthrough]]
case DT_UINT64: //[[fallthrough]]
case DT_COMPOUND: //[[fallthrough]]
case DT_UNKNOWN_DATA_TYPE: //[[fallthrough]]
default:
throw UnsupportedDataType{};
}
fieldOffset += Layer::getElementSize(fieldType);
}
}
} // namespace
//! Constructor.
/*!
\param layer
The layer the value table holds records/values for.
*/
ValueTable::ValueTable(
const GeorefMetadataLayer& layer)
: m_layer(layer)
{
// Read the Records DataSet.
const auto& h5valueDataSet = m_layer.getValueDataSet();
const auto fileDataSpace = h5valueDataSet.getSpace();
hsize_t numRecords = 0;
const auto numDims = fileDataSpace.getSimpleExtentDims(&numRecords);
if (numDims != 1)
throw InvalidValueSize{};
m_records.resize(numRecords);
if (numRecords == 1) // No user defined records.
return;
constexpr hsize_t startIndex = 0;
fileDataSpace.selectHyperslab(H5S_SELECT_SET, &numRecords, &startIndex);
auto pDescriptor = std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor());
const auto& definition = pDescriptor->getDefinition();
const size_t recordSize = getRecordSize(definition);
std::vector<uint8_t> buffer(recordSize * numRecords, 0);
const auto memDataType = createH5memoryCompType(definition);
const ::H5::DataSpace memDataSpace;
memDataSpace.setExtentSimple(1, &numRecords);
h5valueDataSet.read(buffer.data(), memDataType, memDataSpace, fileDataSpace);
// Convert the raw memory into Records.
size_t rawIndex = 0;
for (auto& record : m_records)
{
record = convertMemoryToRecord(buffer.data() + (rawIndex * recordSize),
definition);
++rawIndex;
}
}
//! Add a record/value to the end of the list.
/*!
\param record
The record/value.
\return
The index of the added record.
*/
size_t ValueTable::addRecord(
const Record& record)
{
if (!this->validateRecord(record))
throw InvalidValue{};
const auto newKey = m_records.size();
this->writeRecord(newKey, record);
m_records.emplace_back(record);
return newKey;
}
//! Add multiple records/values to the end of the list.
/*!
\param records
The records/values.
*/
void ValueTable::addRecords(
const Records& records)
{
if (records.empty())
return;
const bool allValid = std::all_of(cbegin(records), cend(records),
[this](const auto& record) {
return this->validateRecord(record);
});
if (!allValid)
throw InvalidValue{};
this->writeRecords(records);
m_records.insert(end(m_records), cbegin(records), cend(records));
}
//! Convert a record/value to a chunk of memory.
/*!
\param record
The record/value to convert.
\return
A copy of the record/value as a chunk of memory.
*/
std::vector<uint8_t> ValueTable::convertRecordToRaw(
const Record& record) const
{
auto pDescriptor =
std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor());
std::vector<uint8_t> buffer(getRecordSize(pDescriptor->getDefinition()), 0);
convertRecordToMemory(record, buffer.data());
return buffer;
}
//! Convert multiple records/values to a chunk of memory.
/*!
\param records
The records/values to convert.
\return
A copy of the records/values as a chunk of memory.
*/
std::vector<uint8_t> ValueTable::convertRecordsToRaw(
const std::vector<Record>& records) const
{
if (records.empty())
return {};
auto pDescriptor =
std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor());
const auto recordSize = getRecordSize(pDescriptor->getDefinition());
std::vector<uint8_t> buffer(recordSize * records.size(), 0);
// Write values into memory.
size_t offset = 0;
for (const auto& record : records)
{
convertRecordToMemory(record, buffer.data() + offset);
offset += recordSize;
}
return buffer;
}
//! Retrieve the record/value definition.
/*!
\return
The list of fields that define the record/value.
*/
const RecordDefinition& ValueTable::getDefinition() const & noexcept
{
return std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor())->getDefinition();
}
//! Retrieve the value of a specific field in a specific record.
/*!
\param key
The key of the record/value.
Must be greater than 0.
\param name
The name of the field.
\return
The value specified.
An exception is thrown if the key or field name is invalid.
*/
const CompoundDataType& ValueTable::getValue(
size_t key,
const std::string& name) const &
{
if (key == 0 || key >= m_records.size())
throw ValueNotFound{};
const size_t fieldIndex = this->getFieldIndex(name);
return this->getValue(key, fieldIndex);
}
//! Retrieve the value of a specific field in a specific record/value.
/*!
\param key
The key of the record/value.
Must be greater than 0.
\param fieldIndex
The index of the field.
\return
The value specified.
An exception is thrown if the record index or field index are invalid.
*/
const CompoundDataType& ValueTable::getValue(
size_t key,
size_t fieldIndex) const &
{
if (key == 0 || key >= m_records.size())
throw ValueNotFound{};
const auto& definition = this->getDefinition();
if (fieldIndex >= definition.size())
throw FieldNotFound{};
const auto& record = m_records[key];
return record[fieldIndex];
}
//! Retrieve the field index of the named field.
/*!
\param name
The name of the field.
\return
The field index of the named field.
*/
size_t ValueTable::getFieldIndex(
const std::string& name) const
{
const auto& definition = this->getDefinition();
size_t fieldIndex = 0;
for (const auto& field : definition)
{
if (name == field.name)
return fieldIndex;
++fieldIndex;
}
throw FieldNotFound{};
}
//! Retrieve the field name of the indexed field.
/*!
\param index
The index of the field.
\return
The field name of the indexed field.
*/
const char* ValueTable::getFieldName(
size_t index) const &
{
const auto& definition = this->getDefinition();
if (index >= definition.size())
throw FieldNotFound{};
return definition[index].name;
}
//! Retrieve all the records/values.
/*!
\return
All the records/values.
NOTE! This includes the no data value record at index 0.
*/
const Records& ValueTable::getRecords() const & noexcept
{
return m_records;
}
//! Set a value in a specific field in a specific record.
/*!
\param key
The record/value key.
Must be greater than 0.
\param name
The name of the field.
\param value
The value to put into the field.
*/
void ValueTable::setValue(
size_t key,
const std::string& name,
const CompoundDataType& value)
{
if (key == 0 || key >= m_records.size())
throw ValueNotFound{};
const size_t fieldIndex = this->getFieldIndex(name);
this->setValue(key, fieldIndex, value);
}
//! Set a value in a specific field in a specific record.
/*!
\param key
The record key.
Must be greater than 0.
\param fieldIndex
The index of the field.
\param value
The value to put into the field.
*/
void ValueTable::setValue(
size_t key,
size_t fieldIndex,
const CompoundDataType& value)
{
if (key == 0 || key >= m_records.size())
throw ValueNotFound{};
auto& record = m_records[key];
record[fieldIndex] = value;
this->writeRecord(key, record);
}
//! Determine if the specified record/value matches the definition used by the value table.
/*!
\param record
The record/value.
\return
\e true if the record/value matches the definition.
\e false otherwise
*/
bool ValueTable::validateRecord(
const Record& record) const
{
auto pDescriptor =
std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor());
if (!pDescriptor)
throw InvalidDescriptor{};
const auto& definition = pDescriptor->getDefinition();
if (record.size() != definition.size())
return false;
size_t defIndex = 0;
for (const auto& field : record)
{
const auto defType = static_cast<DataType>(definition[defIndex++].type);
if (defType == DT_UNKNOWN_DATA_TYPE
|| field.getType() == DT_UNKNOWN_DATA_TYPE)
return false;
const auto fieldType = field.getType();
if (fieldType != defType)
return false;
}
return true;
}
//! Write a record/value to the HDF5 DataSet at the specified key.
/*!
\param key
The record key.
Must be greater than 0.
\param record
The record/value to write.
*/
void ValueTable::writeRecord(
size_t key,
const Record& record)
{
if (key == 0 || key > m_records.size())
throw InvalidValueKey{};
const hsize_t fileRecordIndex = key;
// Prepare the memory details.
const auto rawMemory = this->convertRecordToRaw(record);
auto pDescriptor =
std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor());
const auto memDataType = createH5memoryCompType(pDescriptor->getDefinition());
constexpr hsize_t one = 1;
::H5::DataSpace memDataSpace(1, &one, &one);
// Prepare the file details.
const auto& h5valueDataSet = m_layer.getValueDataSet();
if (key == m_records.size())
{
// Make room for a new record.
const hsize_t newNumRecords = fileRecordIndex + 1;
h5valueDataSet.extend(&newNumRecords);
}
const auto fileDataSpace = h5valueDataSet.getSpace();
// select the record to write
fileDataSpace.selectElements(H5S_SELECT_SET, 1, &fileRecordIndex);
h5valueDataSet.write(rawMemory.data(), memDataType, memDataSpace,
fileDataSpace);
}
//! Write multiple records/values at the end of the list to the HDF5 DataSet.
/*!
\param records
The records/values.
*/
void ValueTable::writeRecords(
const std::vector<Record>& records)
{
if (records.empty())
return;
// Prepare the memory details.
const auto rawMemory = this->convertRecordsToRaw(records);
auto pDescriptor =
std::dynamic_pointer_cast<const GeorefMetadataLayerDescriptor>(
m_layer.getDescriptor());
const auto memDataType = createH5memoryCompType(pDescriptor->getDefinition());
const hsize_t numRecords = records.size();
::H5::DataSpace memDataSpace(1, &numRecords, &numRecords);
// Prepare the file details.
const auto& h5valueDataSet = m_layer.getValueDataSet();
// Make room for the new records.
const hsize_t newNumRecords = m_records.size() + numRecords;
h5valueDataSet.extend(&newNumRecords);
const auto fileDataSpace = h5valueDataSet.getSpace();
// Specify the key to begin writing to.
const hsize_t keyToModify = m_records.size();
fileDataSpace.selectHyperslab(H5S_SELECT_SET, &numRecords, &keyToModify);
h5valueDataSet.write(rawMemory.data(), memDataType, memDataSpace,
fileDataSpace);
}
} // namespace BAG