Skip to content

Commit

Permalink
lib: make htable into typesafe htable<Key,T>
Browse files Browse the repository at this point in the history
  • Loading branch information
arogge committed Feb 24, 2022
1 parent 74b5a67 commit 4b29164
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 70 deletions.
4 changes: 2 additions & 2 deletions core/src/cats/bvfs.cc
Expand Up @@ -44,13 +44,13 @@ class pathid_cache {
int nb_node;
int max_node;
alist<hlink*>* table_node;
htable* cache_ppathid;
htable<char*, hlink>* cache_ppathid;

public:
pathid_cache()
{
hlink link;
cache_ppathid = new htable(&link, &link, NITEMS);
cache_ppathid = new htable<char*, hlink>(&link, &link, NITEMS);
max_node = NITEMS;
nodes = (hlink*)malloc(max_node * sizeof(hlink));
nb_node = 0;
Expand Down
18 changes: 11 additions & 7 deletions core/src/dird/ndmp_fhdb_mem.cc
Expand Up @@ -111,11 +111,14 @@ struct ooo_metadata {
};
typedef struct ooo_metadata OOO_MD;

using MetadataTable = htable<uint64_t, OOO_MD>;

struct fhdb_state_mem {
N_TREE_ROOT* fhdb_root;
htable* out_of_order_metadata;
MetadataTable* out_of_order_metadata;
};


/**
* Lightweight version of Bareos tree functions for holding the NDMP
* filehandle index database. See lib/tree.[ch] for the full version.
Expand Down Expand Up @@ -413,7 +416,7 @@ static inline void add_out_of_order_metadata(NIS* nis,
{
N_TREE_NODE* nt_node;
OOO_MD* md_entry = NULL;
htable* meta_data
MetadataTable* meta_data
= ((struct fhdb_state_mem*)nis->fhdb_state)->out_of_order_metadata;

nt_node = ndmp_fhdb_new_tree_node(fhdb_root);
Expand All @@ -435,7 +438,8 @@ static inline void add_out_of_order_metadata(NIS* nis,
item_size = sizeof(OOO_MD);
nr_items = (nr_pages * B_PAGE_SIZE) / item_size;

meta_data = new htable(md_entry, &md_entry->link, nr_items, nr_pages);
meta_data
= new MetadataTable(md_entry, &md_entry->link, nr_items, nr_pages);
((struct fhdb_state_mem*)nis->fhdb_state)->out_of_order_metadata
= meta_data;
}
Expand All @@ -445,7 +449,7 @@ static inline void add_out_of_order_metadata(NIS* nis,
md_entry->dir_node = dir_node;
md_entry->nt_node = nt_node;

meta_data->insert((uint64_t)node, (void*)md_entry);
meta_data->insert((uint64_t)node, md_entry);

Dmsg2(100,
"bndmp_fhdb_mem_add_dir: Added out of order metadata entry for node "
Expand Down Expand Up @@ -499,7 +503,7 @@ extern "C" int bndmp_fhdb_mem_add_dir(struct ndmlog* ixlog,
}

// This tries recursivly to add the missing parents to the tree.
static N_TREE_NODE* insert_metadata_parent_node(htable* meta_data,
static N_TREE_NODE* insert_metadata_parent_node(MetadataTable* meta_data,
N_TREE_ROOT* fhdb_root,
uint64_t dir_node)
{
Expand Down Expand Up @@ -553,7 +557,7 @@ static N_TREE_NODE* insert_metadata_parent_node(htable* meta_data,
* tree. Only used for NDMP DMAs which are sending their metadata fully at
* random.
*/
static inline bool ProcessOutOfOrderMetadata(htable* meta_data,
static inline bool ProcessOutOfOrderMetadata(MetadataTable* meta_data,
N_TREE_ROOT* fhdb_root)
{
OOO_MD* md_entry;
Expand Down Expand Up @@ -612,7 +616,7 @@ extern "C" int bndmp_fhdb_mem_add_node(struct ndmlog* ixlog,
N_TREE_ROOT* fhdb_root;
N_TREE_NODE* wanted_node;
PoolMem attribs(PM_FNAME);
htable* meta_data
MetadataTable* meta_data
= ((struct fhdb_state_mem*)nis->fhdb_state)->out_of_order_metadata;

Dmsg1(100, "bndmp_fhdb_mem_add_node: New node [%llu]\n", node);
Expand Down
6 changes: 4 additions & 2 deletions core/src/filed/accurate.h
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2013-2014 Planets Communications B.V.
Copyright (C) 2013-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -113,8 +113,10 @@ struct CurFile {
#endif

class BareosAccurateFilelistHtable : public BareosAccurateFilelist {
using FileList = htable<char*, CurFile>;

protected:
htable* file_list_;
FileList* file_list_;
void destroy();

public:
Expand Down
2 changes: 1 addition & 1 deletion core/src/filed/accurate_htable.cc
Expand Up @@ -44,7 +44,7 @@ BareosAccurateFilelistHtable::BareosAccurateFilelistHtable(
number_of_previous_files_ = number_of_files;

CurFile* elt = NULL;
file_list_ = new htable(elt, &elt->link, number_of_previous_files_);
file_list_ = new FileList(elt, &elt->link, number_of_previous_files_);
seen_bitmap_ = (char*)malloc(NbytesForBits(number_of_previous_files_));
ClearAllBits(number_of_previous_files_, seen_bitmap_);
}
Expand Down
8 changes: 6 additions & 2 deletions core/src/findlib/find.h
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2001-2010 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -61,6 +61,7 @@
int Readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result);
# endif
#endif

// For options FO_xxx values see src/fileopts.h
enum
{
Expand Down Expand Up @@ -185,6 +186,9 @@ struct CurLink {
char name[1]; /**< The name */
};

using LinkHash = htable<htable_binary_key, CurLink>;


/**
* Definition of the FindFiles packet passed as the
* first argument to the FindFiles callback subroutine.
Expand Down Expand Up @@ -250,7 +254,7 @@ struct FindFilesPacket {
alist<const char*> drivetypes; /**< Allowed drive types */

// List of all hard linked files found
htable* linkhash{nullptr}; /**< Hard linked files */
LinkHash* linkhash{nullptr}; /**< Hard linked files */
struct CurLink* linked{nullptr}; /**< Set if this file is hard linked */

/*
Expand Down
2 changes: 1 addition & 1 deletion core/src/findlib/hardlink.cc
Expand Up @@ -63,7 +63,7 @@ CurLink* new_hardlink(JobControlRecord* jcr,
uint8_t* new_key;

if (!ff_pkt->linkhash) {
ff_pkt->linkhash = new htable(hl, &hl->link, 10000, 480);
ff_pkt->linkhash = new LinkHash(hl, &hl->link, 10000, 480);
}

len = strlen(fname) + 1;
Expand Down
6 changes: 3 additions & 3 deletions core/src/include/jcr.h
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -44,14 +44,14 @@
#include "lib/tls_conf.h"
#include "lib/breg.h"
#include "lib/dlink.h"
#include "lib/path_list.h"
#include <unordered_set>


struct job_callback_item;
class BareosDb;
class BareosSocket;
template <typename T> class dlist;
class htable;
class JobControlRecord;

struct AttributesDbRecord;
Expand Down Expand Up @@ -230,7 +230,7 @@ class JobControlRecord {
PluginContext* plugin_ctx{}; /**< Current plugin context */
POOLMEM* comment{}; /**< Comment for this Job */
int64_t max_bandwidth{}; /**< Bandwidth limit for this Job */
htable* path_list{}; /**< Directory list (used by findlib) */
PathList* path_list{}; /**< Directory list (used by findlib) */
bool is_passive_client_connection_probing{}; /**< Set if director probes a passive client connection */

JobControlRecordPrivate* impl{nullptr}; /* Pointer to implementation of each daemon */
Expand Down
33 changes: 16 additions & 17 deletions core/src/lib/htable.h
Expand Up @@ -135,12 +135,12 @@ struct htable_binary_key {
uint32_t len;
};

class htable {
template <typename Key, typename T> class htable {
std::unique_ptr<htableImpl> pimpl;

public:
htable() { pimpl = std::make_unique<htableImpl>(); }
htable(void* item,
htable(T* item,
void* link,
int tsize = 31,
int nr_pages = 0,
Expand All @@ -149,27 +149,26 @@ class htable {
pimpl
= std::make_unique<htableImpl>(item, link, tsize, nr_pages, nr_entries);
}
void* lookup(char* key) { return pimpl->lookup(key); }
bool insert(char* key, void* item) { return pimpl->insert(key, item); }

void* lookup(uint32_t key) { return pimpl->lookup(key); }
bool insert(uint32_t key, void* item) { return pimpl->insert(key, item); }

void* lookup(uint64_t key) { return pimpl->lookup(key); }
bool insert(uint64_t key, void* item) { return pimpl->insert(key, item); }

void* lookup(htable_binary_key key)
T* lookup(Key key)
{
return pimpl->lookup(key.ptr, key.len);
if constexpr (std::is_same<Key, htable_binary_key>::value) {
return (T*)pimpl->lookup(key.ptr, key.len);
} else {
return (T*)pimpl->lookup(key);
}
}
bool insert(htable_binary_key key, void* item)
bool insert(Key key, T* item)
{
return pimpl->insert(key.ptr, key.len, item);
if constexpr (std::is_same<Key, htable_binary_key>::value) {
return pimpl->insert(key.ptr, key.len, item);
} else {
return pimpl->insert(key, item);
}
}

char* hash_malloc(int size) { return pimpl->hash_malloc(size); }
void* first() { return pimpl->first(); }
void* next() { return pimpl->next(); }
T* first() { return (T*)pimpl->first(); }
T* next() { return (T*)pimpl->next(); }
uint32_t size() { return pimpl->size(); }
};

Expand Down
15 changes: 5 additions & 10 deletions core/src/lib/path_list.cc
Expand Up @@ -28,23 +28,18 @@

#define debuglevel 50

typedef struct PrivateCurDir {
hlink link;
char fname[1];
} CurDir;

// Initialize the path hash table
htable* path_list_init()
PathList* path_list_init()
{
CurDir* elt = NULL;
return new htable(elt, &elt->link, 10000);
return new PathList(elt, &elt->link, 10000);
}

/*
* Add a path to the hash when we create a directory with the replace=NEVER
* option
*/
bool PathListAdd(htable* path_list, uint32_t len, const char* fname)
bool PathListAdd(PathList* path_list, uint32_t len, const char* fname)
{
CurDir* item;

Expand All @@ -63,7 +58,7 @@ bool PathListAdd(htable* path_list, uint32_t len, const char* fname)
return true;
}

bool PathListLookup(htable* path_list, const char* fname)
bool PathListLookup(PathList* path_list, const char* fname)
{
int len;
bool found = false;
Expand Down Expand Up @@ -94,4 +89,4 @@ bool PathListLookup(htable* path_list, const char* fname)
return found;
}

void FreePathList(htable* path_list) { delete path_list; }
void FreePathList(PathList* path_list) { delete path_list; }
19 changes: 13 additions & 6 deletions core/src/lib/path_list.h
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2019 Bareos GmbH & Co. KG
Copyright (C) 2018-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -21,11 +21,18 @@
#ifndef BAREOS_LIB_PATH_LIST_H_
#define BAREOS_LIB_PATH_LIST_H_

class htable;
#include "htable.h"

htable* path_list_init();
bool PathListLookup(htable* path_list, const char* fname);
bool PathListAdd(htable* path_list, uint32_t len, const char* fname);
void FreePathList(htable* path_list);
typedef struct PrivateCurDir {
hlink link;
char fname[1];
} CurDir;

using PathList = htable<char*, CurDir>;

PathList* path_list_init();
bool PathListLookup(PathList* path_list, const char* fname);
bool PathListAdd(PathList* path_list, uint32_t len, const char* fname);
void FreePathList(PathList* path_list);

#endif // BAREOS_LIB_PATH_LIST_H_
2 changes: 1 addition & 1 deletion core/src/lib/tree.cc
Expand Up @@ -92,7 +92,7 @@ TREE_ROOT* new_tree(int count)
root->type = TN_ROOT;
root->fname = "";
HL_ENTRY* entry = NULL;
new (&root->hardlinks) htable(entry, &entry->link, 0, 1);
new (&root->hardlinks) HardlinkTable(entry, &entry->link, 0, 1);
return root;
}

Expand Down
22 changes: 12 additions & 10 deletions core/src/lib/tree.h
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2002-2009 Free Software Foundation Europe e.V.
Copyright (C) 2016-2021 Bareos GmbH & Co. KG
Copyright (C) 2016-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -100,6 +100,16 @@ struct s_tree_node {
};
typedef struct s_tree_node TREE_NODE;

/* hardlink hashtable entry */
struct s_hl_entry {
uint64_t key;
hlink link;
TREE_NODE* node;
};
typedef struct s_hl_entry HL_ENTRY;

using HardlinkTable = htable<uint64_t, HL_ENTRY>;

struct s_tree_root {
s_tree_root()
: type{false}
Expand Down Expand Up @@ -138,18 +148,10 @@ struct s_tree_root {
int cached_path_len{}; /* length of cached path */
char* cached_path{}; /* cached current path */
TREE_NODE* cached_parent{}; /* cached parent for above path */
htable hardlinks; /* references to first occurence of hardlinks */
HardlinkTable hardlinks; /* references to first occurence of hardlinks */
};
typedef struct s_tree_root TREE_ROOT;

/* hardlink hashtable entry */
struct s_hl_entry {
uint64_t key;
hlink link;
TREE_NODE* node;
};
typedef struct s_hl_entry HL_ENTRY;

#ifdef HAVE_HPUX_OS
# pragma pack(pop)
#endif
Expand Down

0 comments on commit 4b29164

Please sign in to comment.