/** * Recursive node serialization: * - length of GhtHash * - GhtHash (no null terminator) * - number of GhtAttributes * - GhtAttribute[] * - number of child GhtNodes * - GhtNode[] */ GhtErr ght_node_write(const GhtNode *node, GhtWriter *writer) { uint8_t attrcount = 0; uint8_t childcount = 0; GhtAttribute *attr = node->attributes; /* Write the hash */ GHT_TRY(ght_hash_write(node->hash, writer)); /* Write the attributes */ GHT_TRY(ght_node_count_attributes(node, &attrcount)); ght_write(writer, &attrcount, 1); while( attr ) { ght_attribute_write(attr, writer); attr = attr->next; } /* Write the children */ if ( node->children ) childcount = node->children->num_nodes; ght_write(writer, &childcount, 1); if ( childcount ) { int i; for ( i = 0; i < node->children->num_nodes; i++ ) { ght_node_write(node->children->nodes[i], writer); } } return GHT_OK; }
static void test_ght_node_file_serialization(void) { GhtCoordinate coord; GhtNode *node, *root, *noderead; GhtErr err; GhtWriter *writer; GhtReader *reader; stringbuffer_t *sb1; GhtAttribute *attr; const char* testfile = "test.ght"; if ( fexists(testfile) ) remove(testfile); coord.x = -127.4123; coord.y = 49.23141; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); CU_ASSERT_EQUAL(err, GHT_OK); root = node; coord.x = -127.4122; coord.y = 49.23142; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); err = ght_attribute_new_from_double(schema->dims[2], 88.88, &attr); err = ght_node_add_attribute(node, attr); err = ght_node_insert_node(root, node, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); coord.x = -127.4122001; coord.y = 49.23142001; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); err = ght_attribute_new_from_double(schema->dims[2], 15.23, &attr); err = ght_node_add_attribute(node, attr); err = ght_node_insert_node(root, node, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); coord.x = -127.4122002; coord.y = 49.23142002; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node); err = ght_attribute_new_from_double(schema->dims[2], 19.23, &attr); err = ght_node_add_attribute(node, attr); err = ght_node_insert_node(root, node, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); // sb1 = stringbuffer_create(); // err = ght_node_to_string(root, sb1, 0); // printf("\n%s\n", stringbuffer_getstring(sb1)); // stringbuffer_destroy(sb1); err = ght_writer_new_file(testfile, &writer); CU_ASSERT_EQUAL(err, GHT_OK); err = ght_node_write(root, writer); CU_ASSERT_EQUAL(err, GHT_OK); ght_writer_free(writer); err = ght_reader_new_file(testfile, schema, &reader); CU_ASSERT_EQUAL(err, GHT_OK); err = ght_node_read(reader, &noderead); CU_ASSERT_EQUAL(err, GHT_OK); ght_reader_free(reader); remove(testfile); ght_node_free(root); ght_node_free(noderead); }
static void test_ght_node_serialization(void) { GhtCoordinate coord; int x, y; GhtNode *node1, *node2, *node3; GhtErr err; GhtWriter *writer; GhtReader *reader; const uint8_t *bytes; size_t bytes_size; stringbuffer_t *sb1, *sb2; GhtAttribute *attr; char *hex; /* ght_node_new_from_coordinate(const GhtCoordinate *coord, unsigned int resolution, GhtNode **node); */ coord.x = -127.4123; coord.y = 49.23141; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node1); CU_ASSERT_STRING_EQUAL(node1->hash, "c0v2hdm1wpzpy4vtv4"); CU_ASSERT_EQUAL(err, GHT_OK); err = ght_writer_new_mem(&writer); err = ght_node_write(node1, writer); bytes = bytebuffer_getbytes(writer->bytebuffer); bytes_size = bytebuffer_getsize(writer->bytebuffer); err = ght_reader_new_mem(bytes, bytes_size, schema, &reader); err = ght_node_read(reader, &node2); CU_ASSERT_STRING_EQUAL(node1->hash, node2->hash); ght_node_free(node2); /* add a child */ coord.x = -127.4125; coord.y = 49.23144; err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node3); err = ght_attribute_new_from_double(schema->dims[3], 88.88, &attr); err = ght_node_add_attribute(node3, attr); err = ght_node_insert_node(node1, node3, GHT_DUPES_YES); CU_ASSERT_EQUAL(err, GHT_OK); /* add another (dupe) child */ err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node3); err = ght_node_insert_node(node1, node3, GHT_DUPES_YES); /* add another (dupe) child with an attribute */ err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node3); err = ght_attribute_new_from_double(schema->dims[2], 99.99, &attr); err = ght_node_add_attribute(node3, attr); err = ght_node_insert_node(node1, node3, GHT_DUPES_YES); sb1 = stringbuffer_create(); err = ght_node_to_string(node1, sb1, 0); // printf("ORIGINAL\n%s\n", stringbuffer_getstring(sb1)); err = ght_writer_new_mem(&writer); err = ght_node_write(node1, writer); bytes = bytebuffer_getbytes(writer->bytebuffer); bytes_size = bytebuffer_getsize(writer->bytebuffer); err = hexbytes_from_bytes(bytes, bytes_size, &hex); CU_ASSERT_STRING_EQUAL("086330763268646D3100020A77707A7079347674763400000A6374643463637839796201035800020000000001020F27000000", hex); // printf("\n\n%s\n", hex); err = ght_reader_new_mem(bytes, bytes_size, schema, &reader); err = ght_node_read(reader, &node2); sb2 = stringbuffer_create(); err = ght_node_to_string(node2, sb2, 0); // printf("COPY\n%s\n", stringbuffer_getstring(sb2)); CU_ASSERT_STRING_EQUAL(stringbuffer_getstring(sb1), stringbuffer_getstring(sb2)); stringbuffer_destroy(sb2); stringbuffer_destroy(sb1); ght_node_free(node1); ght_node_free(node2); ght_writer_free(writer); ght_reader_free(reader); }