]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
database: Map network nodes section when opening the database
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Jan 2018 20:09:44 +0000 (20:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Jan 2018 20:09:44 +0000 (20:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/loc/format.h

index 07cb66c5f68a937b63a976bdc1fb9bf274024dca..b1eead619bcd09af5bd85c520960b2e504707afc 100644 (file)
@@ -46,6 +46,10 @@ struct loc_database {
        struct loc_database_as_v0* as_v0;
        size_t as_count;
 
+       // Network tree
+       struct loc_database_network_node_v0* network_nodes_v0;
+       size_t network_nodes_count;
+
        struct loc_stringpool* pool;
 };
 
@@ -98,6 +102,26 @@ static int loc_database_read_as_section_v0(struct loc_database* db,
        return 0;
 }
 
+static int loc_database_read_network_nodes_section_v0(struct loc_database* db,
+               FILE* f, off_t network_nodes_offset, size_t network_nodes_length) {
+       DEBUG(db->ctx, "Reading network nodes section from %jd (%zu bytes)\n",
+               network_nodes_offset, network_nodes_length);
+
+       if (network_nodes_length > 0) {
+               db->network_nodes_v0 = mmap(NULL, network_nodes_length, PROT_READ,
+                       MAP_SHARED, fileno(f), network_nodes_offset);
+
+               if (db->network_nodes_v0 == MAP_FAILED)
+                       return -errno;
+       }
+
+       db->network_nodes_count = network_nodes_length / sizeof(*db->network_nodes_v0);
+
+       INFO(db->ctx, "Read %zu network nodes from the database\n", db->network_nodes_count);
+
+       return 0;
+}
+
 static int loc_database_read_header_v0(struct loc_database* db, FILE* f) {
        struct loc_database_header_v0 header;
 
@@ -131,6 +155,15 @@ static int loc_database_read_header_v0(struct loc_database* db, FILE* f) {
        if (r)
                return r;
 
+       // Network Nodes
+       off_t network_nodes_offset  = be32toh(header.network_tree_offset);
+       size_t network_nodes_length = be32toh(header.network_tree_length);
+
+       r = loc_database_read_network_nodes_section_v0(db, f,
+               network_nodes_offset, network_nodes_length);
+       if (r)
+               return r;
+
        return 0;
 }
 
index a1c0ebd55a68ca28187761239cac2d9af356e1ce..2c7496b1325312c10cfd021955b37e8d9a003fa5 100644 (file)
@@ -49,7 +49,7 @@ struct loc_database_header_v0 {
        uint32_t network_data_offset;
        uint32_t network_data_length;
 
-       // Tells us where the network tree starts
+       // Tells us where the network nodes start
        uint32_t network_tree_offset;
        uint32_t network_tree_length;