From 4be782ddd683c32cd003d1323ff169db0bd9d59f Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Fri, 14 Dec 2018 21:11:21 +0100 Subject: [PATCH] =?UTF-8?q?odczyt=20z=20warto=C5=9Bciami?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.c | 13 +++++++++++++ index.h | 2 ++ readidx.c | 24 ++++++++++++++++++------ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/index.c b/index.c index 1c381f8..c9d6e14 100644 --- a/index.c +++ b/index.c @@ -650,6 +650,19 @@ btree_entry_t *btree_get_entry(btree_node_t* node, unsigned n) return (btree_entry_t*)(node->entries + NODE_ENTRY_OFFSET(n)); } +page_t btree_read_record(btree_t *tree, record_key_t key, record_t *record) +{ + btree_entry_t entry; + page_t page = btree_find(tree, key, &entry, NULL, NULL); + + if (page == PAGE_NONE) { + return PAGE_NONE; + } + + tape_read(tree->main, entry.location, record, sizeof(record_t)); + return page; +} + void btree_close(btree_t* tree) { printfv(VERBOSITY_DEBUG, "[btree] Closing index %s.\n", tree->file->filename); diff --git a/index.h b/index.h index 12665d9..e6cb95c 100644 --- a/index.h +++ b/index.h @@ -78,6 +78,8 @@ page_t btree_remove(btree_t *tree, record_key_t key); bool btree_update(btree_t *tree, record_key_t key, record_t record); page_t btree_find(btree_t *tree, record_key_t key, btree_entry_t *entry, btree_node_t *node, unsigned *index); +page_t btree_read_record(btree_t *tree, record_key_t key, record_t *record); + btree_entry_t *btree_get_entry(btree_node_t* node, unsigned n); void btree_close(btree_t* tree); diff --git a/readidx.c b/readidx.c index dfaec00..e6f8441 100644 --- a/readidx.c +++ b/readidx.c @@ -6,11 +6,13 @@ #include "common.h" #include "index.h" -typedef struct { - char* index; -} opts_t; +#define OPTION_KEYS_ONLY 1 + +struct { + char* index; + uint8_t flags; +} options; -opts_t options; btree_t tree; void init_args(int args, char* argv[]) @@ -20,6 +22,9 @@ void init_args(int args, char* argv[]) for (char opt; opt != -1; opt = optparse(&opts, "qv")) { switch (opt) { + case 'k': + options.flags |= OPTION_KEYS_ONLY; + break; case 'q': verbosity--; break; @@ -54,10 +59,17 @@ void print_page(page_t page) memcpy(&node.entries, buffer + sizeof(node.header), NODE_SIZE_MAX); btree_entry_t *entry; + record_t record; for (int i = 0; i < node.header.entries; i++) { entry = btree_get_entry(&node, i); print_page(entry->left); - printf("%u\n", entry->key); + + if (~options.flags & OPTION_KEYS_ONLY) { + btree_read_record(&tree, entry->key, &record); + printf("%u: (%lf, %lf)\n", entry->key, record.x, record.y); + } else { + printf("%u\n", entry->key); + } } print_page(entry->right); } @@ -72,7 +84,7 @@ int main(int argc, char* argv[]) init_args(argc, argv); if (btree_open(&tree, options.index) != 0) { - fprintf(stderr, "Cannot open indes %s for reading.", options.index); + fprintf(stderr, "Cannot open index %s for reading.", options.index); return 1; }