Skip to content

Commit ad02ac7

Browse files
Add TSK_DUMP_FORCE_OFFSET64 flag and test
This adds an intermediate step towards using 64 bit sizes internally.
1 parent 9a53e9d commit ad02ac7

File tree

2 files changed

+76
-35
lines changed

2 files changed

+76
-35
lines changed

c/tests/test_file_format.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ test_force_offset_64(void)
480480
int ret;
481481
tsk_treeseq_t *ts = caterpillar_tree(5, 3, 3);
482482
tsk_table_collection_t t1;
483+
tsk_table_collection_t t2;
483484
kastore_t store;
484485
kaitem_t *item;
485486
const char *suffix;
@@ -509,7 +510,15 @@ test_force_offset_64(void)
509510

510511
ret = tsk_table_collection_load(&t1, _tmp_file_name, 0);
511512
CU_ASSERT_EQUAL_FATAL(ret, 0);
513+
514+
ret = tsk_treeseq_copy_tables(ts, &t2, 0);
515+
CU_ASSERT_EQUAL_FATAL(ret, 0);
516+
CU_ASSERT_TRUE(tsk_table_collection_equals(&t1, &t2, 0));
517+
512518
tsk_table_collection_free(&t1);
519+
tsk_table_collection_free(&t2);
520+
tsk_treeseq_free(ts);
521+
free(ts);
513522
}
514523

515524
static void

c/tskit/tables.c

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,24 @@ read_table_cols(kastore_t *store, tsk_size_t *num_rows, read_table_col_t *cols,
146146
}
147147

148148
static int
149-
cast_offset_array(read_table_ragged_col_t *col, tsk_size_t num_rows)
149+
cast_offset_array(read_table_ragged_col_t *col, uint64_t *source, tsk_size_t num_rows)
150150
{
151151
int ret = 0;
152152
tsk_size_t len = num_rows + 1;
153153
tsk_size_t j;
154-
uint64_t *source = *col->offset_array_dest;
155154
uint32_t *dest = malloc(len * sizeof(*dest));
156155

157156
if (dest == NULL) {
158157
ret = TSK_ERR_NO_MEMORY;
159158
goto out;
160159
}
161160
col->offset_array_mem = dest;
161+
*col->offset_array_dest = dest;
162162
for (j = 0; j < len; j++) {
163-
dest[j] = source[j]
163+
/* We don't bother catching errors here because we'll be switching this
164+
* to casting up to 64 bit soon; current version is temporary. */
165+
dest[j] = (uint32_t) source[j];
164166
}
165-
col->offset_col_present
166167
out:
167168
return ret;
168169
}
@@ -177,6 +178,7 @@ read_table_ragged_cols(kastore_t *store, tsk_size_t *num_rows,
177178
read_table_ragged_col_t *col;
178179
char offset_col_name[TSK_MAX_COL_NAME_LEN];
179180
bool data_col_present, offset_col_present;
181+
void *store_offset_array;
180182
tsk_size_t *offset_array;
181183

182184
for (col = cols; col->name != NULL; col++) {
@@ -220,8 +222,8 @@ read_table_ragged_cols(kastore_t *store, tsk_size_t *num_rows,
220222
goto out;
221223
}
222224
if (offset_col_present) {
223-
ret = kastore_gets(store, offset_col_name, (void **) col->offset_array_dest,
224-
&offset_len, &type);
225+
ret = kastore_gets(
226+
store, offset_col_name, &store_offset_array, &offset_len, &type);
225227
if (ret != 0) {
226228
ret = tsk_set_kas_error(ret);
227229
goto out;
@@ -241,12 +243,14 @@ read_table_ragged_cols(kastore_t *store, tsk_size_t *num_rows,
241243
goto out;
242244
}
243245
}
244-
if (type == KAS_UINT64) {
245-
ret = cast_offset_array(col, *num_rows);
246+
if (type == KAS_UINT32) {
247+
*col->offset_array_dest = (uint32_t *) store_offset_array;
248+
} else if (type == KAS_UINT64) {
249+
ret = cast_offset_array(col, (uint64_t *) store_offset_array, *num_rows);
246250
if (ret != 0) {
247251
goto out;
248252
}
249-
} else if (type == KAS_UINT32) {
253+
} else {
250254
ret = TSK_ERR_BAD_COLUMN_TYPE;
251255
goto out;
252256
}
@@ -328,8 +332,22 @@ read_table(kastore_t *store, tsk_size_t *num_rows, read_table_col_t *cols,
328332
return ret;
329333
}
330334

335+
static void
336+
free_read_table_mem(read_table_col_t *TSK_UNUSED(cols),
337+
read_table_ragged_col_t *ragged_cols, read_table_property_t *TSK_UNUSED(properties))
338+
{
339+
read_table_ragged_col_t *ragged_col;
340+
341+
if (ragged_cols != NULL) {
342+
for (ragged_col = ragged_cols; ragged_col->name != NULL; ragged_col++) {
343+
tsk_safe_free(ragged_col->offset_array_mem);
344+
}
345+
}
346+
}
347+
331348
static int
332-
write_offset_col(kastore_t *store, const write_table_ragged_col_t *col, tsk_flags_t options)
349+
write_offset_col(
350+
kastore_t *store, const write_table_ragged_col_t *col, tsk_flags_t options)
333351
{
334352
int ret = 0;
335353
char offset_col_name[TSK_MAX_COL_NAME_LEN];
@@ -350,7 +368,6 @@ write_offset_col(kastore_t *store, const write_table_ragged_col_t *col, tsk_flag
350368
* to force huge arrays to test all the code paths.
351369
*/
352370
if (options & TSK_DUMP_FORCE_OFFSET_64) {
353-
printf("write 64\n");
354371
offset64 = malloc(len * sizeof(*offset64));
355372
if (offset64 == NULL) {
356373
ret = TSK_ERR_NO_MEMORY;
@@ -376,8 +393,8 @@ write_offset_col(kastore_t *store, const write_table_ragged_col_t *col, tsk_flag
376393
}
377394

378395
static int
379-
write_table_ragged_cols(kastore_t *store, const write_table_ragged_col_t *write_cols,
380-
tsk_flags_t options)
396+
write_table_ragged_cols(
397+
kastore_t *store, const write_table_ragged_col_t *write_cols, tsk_flags_t options)
381398
{
382399
int ret = 0;
383400
const write_table_ragged_col_t *col;
@@ -1234,7 +1251,8 @@ tsk_individual_table_equals(const tsk_individual_table_t *self,
12341251
}
12351252

12361253
static int
1237-
tsk_individual_table_dump(const tsk_individual_table_t *self, kastore_t *store, tsk_flags_t options)
1254+
tsk_individual_table_dump(
1255+
const tsk_individual_table_t *self, kastore_t *store, tsk_flags_t options)
12381256
{
12391257
const write_table_col_t write_cols[] = {
12401258
{ "individuals/flags", (void *) self->flags, self->num_rows,
@@ -1277,11 +1295,11 @@ tsk_individual_table_load(tsk_individual_table_t *self, kastore_t *store)
12771295
};
12781296
read_table_ragged_col_t ragged_cols[] = {
12791297
{ "individuals/location", (void **) &location, &location_length, KAS_FLOAT64,
1280-
&location_offset, 0, NULL},
1298+
&location_offset, 0, NULL },
12811299
{ "individuals/parents", (void **) &parents, &parents_length,
1282-
TSK_ID_STORAGE_TYPE, &parents_offset, TSK_COL_OPTIONAL, NULL},
1300+
TSK_ID_STORAGE_TYPE, &parents_offset, TSK_COL_OPTIONAL, NULL },
12831301
{ "individuals/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
1284-
&metadata_offset, 0, NULL},
1302+
&metadata_offset, 0, NULL },
12851303
{ .name = NULL },
12861304
};
12871305
read_table_property_t properties[] = {
@@ -1307,6 +1325,7 @@ tsk_individual_table_load(tsk_individual_table_t *self, kastore_t *store)
13071325
}
13081326
}
13091327
out:
1328+
free_read_table_mem(cols, ragged_cols, properties);
13101329
return ret;
13111330
}
13121331

@@ -1873,7 +1892,7 @@ tsk_node_table_load(tsk_node_table_t *self, kastore_t *store)
18731892
};
18741893
read_table_ragged_col_t ragged_cols[] = {
18751894
{ "nodes/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
1876-
&metadata_offset, 0, NULL},
1895+
&metadata_offset, 0, NULL },
18771896
{ .name = NULL },
18781897
};
18791898
read_table_property_t properties[] = {
@@ -1899,6 +1918,7 @@ tsk_node_table_load(tsk_node_table_t *self, kastore_t *store)
18991918
}
19001919
}
19011920
out:
1921+
free_read_table_mem(cols, ragged_cols, properties);
19021922
return ret;
19031923
}
19041924

@@ -2509,7 +2529,7 @@ tsk_edge_table_load(tsk_edge_table_t *self, kastore_t *store)
25092529
};
25102530
read_table_ragged_col_t ragged_cols[] = {
25112531
{ "edges/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
2512-
&metadata_offset, TSK_COL_OPTIONAL, NULL},
2532+
&metadata_offset, TSK_COL_OPTIONAL, NULL },
25132533
{ .name = NULL },
25142534
};
25152535
read_table_property_t properties[] = {
@@ -2535,6 +2555,7 @@ tsk_edge_table_load(tsk_edge_table_t *self, kastore_t *store)
25352555
}
25362556
}
25372557
out:
2558+
free_read_table_mem(cols, ragged_cols, properties);
25382559
return ret;
25392560
}
25402561

@@ -3180,9 +3201,9 @@ tsk_site_table_load(tsk_site_table_t *self, kastore_t *store)
31803201
};
31813202
read_table_ragged_col_t ragged_cols[] = {
31823203
{ "sites/ancestral_state", (void **) &ancestral_state, &ancestral_state_length,
3183-
KAS_UINT8, &ancestral_state_offset, 0, NULL},
3204+
KAS_UINT8, &ancestral_state_offset, 0, NULL },
31843205
{ "sites/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
3185-
&metadata_offset, 0, NULL},
3206+
&metadata_offset, 0, NULL },
31863207
{ .name = NULL },
31873208
};
31883209
read_table_property_t properties[] = {
@@ -3208,6 +3229,7 @@ tsk_site_table_load(tsk_site_table_t *self, kastore_t *store)
32083229
}
32093230
}
32103231
out:
3232+
free_read_table_mem(cols, ragged_cols, properties);
32113233
return ret;
32123234
}
32133235

@@ -3817,7 +3839,8 @@ tsk_mutation_table_dump_text(const tsk_mutation_table_t *self, FILE *out)
38173839
}
38183840

38193841
static int
3820-
tsk_mutation_table_dump(const tsk_mutation_table_t *self, kastore_t *store, tsk_flags_t options)
3842+
tsk_mutation_table_dump(
3843+
const tsk_mutation_table_t *self, kastore_t *store, tsk_flags_t options)
38213844
{
38223845
const write_table_col_t cols[] = {
38233846
{ "mutations/site", (void *) self->site, self->num_rows, TSK_ID_STORAGE_TYPE },
@@ -3865,9 +3888,9 @@ tsk_mutation_table_load(tsk_mutation_table_t *self, kastore_t *store)
38653888
};
38663889
read_table_ragged_col_t ragged_cols[] = {
38673890
{ "mutations/derived_state", (void **) &derived_state, &derived_state_length,
3868-
KAS_UINT8, &derived_state_offset, 0, NULL},
3891+
KAS_UINT8, &derived_state_offset, 0, NULL },
38693892
{ "mutations/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
3870-
&metadata_offset, 0, NULL},
3893+
&metadata_offset, 0, NULL },
38713894
{ .name = NULL },
38723895
};
38733896
read_table_property_t properties[] = {
@@ -3893,6 +3916,7 @@ tsk_mutation_table_load(tsk_mutation_table_t *self, kastore_t *store)
38933916
}
38943917
}
38953918
out:
3919+
free_read_table_mem(cols, ragged_cols, properties);
38963920
return ret;
38973921
}
38983922

@@ -4413,7 +4437,8 @@ tsk_migration_table_equals(const tsk_migration_table_t *self,
44134437
}
44144438

44154439
static int
4416-
tsk_migration_table_dump(const tsk_migration_table_t *self, kastore_t *store, tsk_flags_t options)
4440+
tsk_migration_table_dump(
4441+
const tsk_migration_table_t *self, kastore_t *store, tsk_flags_t options)
44174442
{
44184443
const write_table_col_t cols[] = {
44194444
{ "migrations/left", (void *) self->left, self->num_rows, KAS_FLOAT64 },
@@ -4462,7 +4487,7 @@ tsk_migration_table_load(tsk_migration_table_t *self, kastore_t *store)
44624487
};
44634488
read_table_ragged_col_t ragged_cols[] = {
44644489
{ "migrations/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
4465-
&metadata_offset, TSK_COL_OPTIONAL, NULL},
4490+
&metadata_offset, TSK_COL_OPTIONAL, NULL },
44664491
{ .name = NULL },
44674492
};
44684493
read_table_property_t properties[] = {
@@ -4488,6 +4513,7 @@ tsk_migration_table_load(tsk_migration_table_t *self, kastore_t *store)
44884513
}
44894514
}
44904515
out:
4516+
free_read_table_mem(cols, ragged_cols, properties);
44914517
return ret;
44924518
}
44934519

@@ -4942,7 +4968,8 @@ tsk_population_table_equals(const tsk_population_table_t *self,
49424968
}
49434969

49444970
static int
4945-
tsk_population_table_dump(const tsk_population_table_t *self, kastore_t *store, tsk_flags_t options)
4971+
tsk_population_table_dump(
4972+
const tsk_population_table_t *self, kastore_t *store, tsk_flags_t options)
49464973
{
49474974
const write_table_col_t cols[] = {
49484975
{ "populations/metadata_schema", (void *) self->metadata_schema,
@@ -4969,7 +4996,7 @@ tsk_population_table_load(tsk_population_table_t *self, kastore_t *store)
49694996

49704997
read_table_ragged_col_t ragged_cols[] = {
49714998
{ "populations/metadata", (void **) &metadata, &metadata_length, KAS_UINT8,
4972-
&metadata_offset, 0, NULL},
4999+
&metadata_offset, 0, NULL },
49735000
{ .name = NULL },
49745001
};
49755002
read_table_property_t properties[] = {
@@ -4994,6 +5021,7 @@ tsk_population_table_load(tsk_population_table_t *self, kastore_t *store)
49945021
}
49955022
}
49965023
out:
5024+
free_read_table_mem(NULL, ragged_cols, properties);
49975025
return ret;
49985026
}
49995027

@@ -5510,7 +5538,8 @@ tsk_provenance_table_equals(const tsk_provenance_table_t *self,
55105538
}
55115539

55125540
static int
5513-
tsk_provenance_table_dump(const tsk_provenance_table_t *self, kastore_t *store, tsk_flags_t options)
5541+
tsk_provenance_table_dump(
5542+
const tsk_provenance_table_t *self, kastore_t *store, tsk_flags_t options)
55145543
{
55155544
write_table_ragged_col_t ragged_cols[] = {
55165545
{ "provenances/timestamp", (void *) self->timestamp, self->timestamp_length,
@@ -5535,9 +5564,9 @@ tsk_provenance_table_load(tsk_provenance_table_t *self, kastore_t *store)
55355564

55365565
read_table_ragged_col_t ragged_cols[] = {
55375566
{ "provenances/timestamp", (void **) &timestamp, &timestamp_length, KAS_UINT8,
5538-
&timestamp_offset, 0, NULL},
5567+
&timestamp_offset, 0, NULL },
55395568
{ "provenances/record", (void **) &record, &record_length, KAS_UINT8,
5540-
&record_offset, 0, NULL},
5569+
&record_offset, 0, NULL },
55415570
{ .name = NULL },
55425571
};
55435572

@@ -5551,6 +5580,7 @@ tsk_provenance_table_load(tsk_provenance_table_t *self, kastore_t *store)
55515580
goto out;
55525581
}
55535582
out:
5583+
free_read_table_mem(NULL, ragged_cols, NULL);
55545584
return ret;
55555585
}
55565586

@@ -9949,7 +9979,8 @@ tsk_table_collection_read_format_data(tsk_table_collection_t *self, kastore_t *s
99499979
}
99509980

99519981
static int TSK_WARN_UNUSED
9952-
tsk_table_collection_dump_indexes(const tsk_table_collection_t *self, kastore_t *store, tsk_flags_t TSK_UNUSED(options))
9982+
tsk_table_collection_dump_indexes(const tsk_table_collection_t *self, kastore_t *store,
9983+
tsk_flags_t TSK_UNUSED(options))
99539984
{
99549985
int ret = 0;
99559986
write_table_col_t cols[] = {
@@ -10136,8 +10167,8 @@ tsk_table_collection_load(
1013610167
}
1013710168

1013810169
static int TSK_WARN_UNUSED
10139-
tsk_table_collection_write_format_data(
10140-
const tsk_table_collection_t *self, kastore_t *store, tsk_flags_t TSK_UNUSED(options))
10170+
tsk_table_collection_write_format_data(const tsk_table_collection_t *self,
10171+
kastore_t *store, tsk_flags_t TSK_UNUSED(options))
1014110172
{
1014210173
int ret = 0;
1014310174
char format_name[TSK_FILE_FORMAT_NAME_LENGTH];
@@ -10199,7 +10230,8 @@ tsk_table_collection_dump(
1019910230
}
1020010231

1020110232
int TSK_WARN_UNUSED
10202-
tsk_table_collection_dumpf(const tsk_table_collection_t *self, FILE *file, tsk_flags_t options)
10233+
tsk_table_collection_dumpf(
10234+
const tsk_table_collection_t *self, FILE *file, tsk_flags_t options)
1020310235
{
1020410236
int ret = 0;
1020510237
kastore_t store;

0 commit comments

Comments
 (0)