Skip to content

Commit

Permalink
check that read files exist
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-p committed Aug 22, 2016
1 parent 639d193 commit ff8a6ac
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
18 changes: 15 additions & 3 deletions include/ReadLibrary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ class ReadLibrary {
LibraryFormat& getFormat() { return fmt_; }
const LibraryFormat& getFormat() const { return fmt_; }

bool allExist_(std::vector<std::string>& filenames, std::stringstream& errorStream) {
namespace bfs = boost::filesystem;
bool allExist{true};
for (auto& fn : filenames) {
if (!bfs::exists(fn)) {
errorStream << "ERROR: file [" << fn << "] does not appear to exist!\n\n";
allExist = false;
}
}
return allExist;
}

bool checkFileExtensions_(std::vector<std::string>& filenames, std::stringstream& errorStream) {
namespace bfs = boost::filesystem;

Expand Down Expand Up @@ -210,9 +222,9 @@ class ReadLibrary {
// (i.e. named-pipes). If the user passed in a non-regular file, we should
// have some other mechanism to check if it's of an expected format and provide
// a reasonable error message otherwise.
readsOK = readsOK && checkFileExtensions_(mateOneFilenames_, errorStream);
readsOK = readsOK && checkFileExtensions_(mateTwoFilenames_, errorStream);
readsOK = readsOK && checkFileExtensions_(unmatedFilenames_, errorStream);
readsOK = readsOK && allExist_(mateOneFilenames_, errorStream) && checkFileExtensions_(mateOneFilenames_, errorStream);
readsOK = readsOK && allExist_(mateTwoFilenames_, errorStream) && checkFileExtensions_(mateTwoFilenames_, errorStream);
readsOK = readsOK && allExist_(unmatedFilenames_, errorStream) && checkFileExtensions_(unmatedFilenames_, errorStream);

if (!readsOK) {
throw std::invalid_argument(errorStream.str());
Expand Down
2 changes: 1 addition & 1 deletion include/concurrentqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ namespace details {
: static_cast<T>(-1);
};

#ifdef __GNUC__
#if (defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)))
typedef ::max_align_t max_align_t; // GCC forgot to add it to std:: for a while
#else
typedef std::max_align_t max_align_t; // Others (e.g. MSVC) insist it can *only* be accessed via std::
Expand Down
53 changes: 36 additions & 17 deletions include/cuckoohash_map.hh
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private:
static inline int get_counterid() {
// counterid stores the per-thread counter index of each thread. Each
// counter value corresponds to a core on the machine.
static thread_local int counterid = -1;
static LIBCUCKOO_THREAD_LOCAL int counterid = -1;

if (counterid < 0) {
counterid = rand() % kNumCores();
Expand Down Expand Up @@ -892,7 +892,7 @@ private:
// hashsize returns the number of buckets corresponding to a given
// hashpower.
static inline size_t hashsize(const size_t hp) {
return 1UL << hp;
return size_t(1) << hp;
}

// hashmask returns the bitmask for the buckets array corresponding to a
Expand Down Expand Up @@ -922,7 +922,8 @@ private:
// ensure tag is nonzero for the multiply.
const partial_t nonzero_tag = (partial >> 1 << 1) + 1;
// 0xc6a4a7935bd1e995 is the hash constant from 64-bit MurmurHash2
const size_t hash_of_tag = nonzero_tag * 0xc6a4a7935bd1e995;
const size_t hash_of_tag =
static_cast<size_t>(nonzero_tag * 0xc6a4a7935bd1e995);
return (index ^ hash_of_tag) & hashmask(hp);
}

Expand Down Expand Up @@ -1272,6 +1273,8 @@ private:
// the associated value if it finds it.
bool try_read_from_bucket(const partial_t partial, const key_type &key,
mapped_type &val, const Bucket& b) const {
// Silence a warning from MSVC about partial being unused if is_simple.
(void)partial;
for (size_t i = 0; i < slot_per_bucket; ++i) {
if (!b.occupied(i)) {
continue;
Expand All @@ -1291,6 +1294,8 @@ private:
// if the key is in the bucket, and false if it isn't.
bool check_in_bucket(const partial_t partial, const key_type &key,
const Bucket& b) const {
// Silence a warning from MSVC about partial being unused if is_simple.
(void)partial;
for (size_t i = 0; i < slot_per_bucket; ++i) {
if (!b.occupied(i)) {
continue;
Expand Down Expand Up @@ -1324,9 +1329,11 @@ private:
// the table (duplicate key error) and true otherwise.
bool try_find_insert_bucket(const partial_t partial, const key_type &key,
const Bucket& b, int& slot) const {
// Silence a warning from MSVC about partial being unused if is_simple.
(void)partial;
slot = -1;
bool found_empty = false;
for (size_t i = 0; i < slot_per_bucket; ++i) {
for (int i = 0; i < static_cast<int>(slot_per_bucket); ++i) {
if (b.occupied(i)) {
if (!is_simple && partial != b.partial(i)) {
continue;
Expand Down Expand Up @@ -1390,6 +1397,8 @@ private:
template <typename Updater>
bool try_update_bucket_fn(const partial_t partial, const key_type &key,
Updater fn, Bucket& b) {
// Silence a warning from MSVC about partial being unused if is_simple.
(void)partial;
for (size_t i = 0; i < slot_per_bucket; ++i) {
if (!b.occupied(i)) {
continue;
Expand Down Expand Up @@ -1855,11 +1864,13 @@ public:
class templated_iterator :
public std::iterator<std::bidirectional_iterator_tag, value_type> {

typedef typename std::conditional<
IS_CONST, const buckets_t, buckets_t>::type
maybe_const_buckets_t;

// The buckets locked and owned by the locked table being iterated
// over.
std::reference_wrapper<
typename std::conditional<
IS_CONST, const buckets_t, buckets_t>::type> buckets_;
std::reference_wrapper<maybe_const_buckets_t> buckets_;

// The shared boolean indicating whether the iterator points to a
// still-locked table or not. It should never be nullptr.
Expand Down Expand Up @@ -1903,7 +1914,8 @@ public:
//! iterator is at the end.
ENABLE_IF(, !IS_CONST, value_type&) operator*() {
check_iterator();
return buckets_.get()[index_].kvpair(slot_);
return buckets_.get()[static_cast<size_t>(index_)].
kvpair(static_cast<size_t>(slot_));
}

//! Return a pointer to the immutable key-value pair pointed to by
Expand All @@ -1930,9 +1942,11 @@ public:
// Move forward until we get to a slot that is occupied, or we
// get to the end
check_iterator();
for (; (size_t)index_ < buckets_.get().size(); ++index_) {
while ((size_t)++slot_ < SLOT_PER_BUCKET) {
if (buckets_.get()[index_].occupied(slot_)) {
for (; static_cast<size_t>(index_) < buckets_.get().size();
++index_) {
while (static_cast<size_t>(++slot_) < SLOT_PER_BUCKET) {
if (buckets_.get()[static_cast<size_t>(index_)].
occupied(static_cast<size_t>(slot_))) {
return *this;
}
}
Expand Down Expand Up @@ -1961,7 +1975,8 @@ public:
check_iterator();
for (; index_ >= 0; --index_) {
while (--slot_ >= 0) {
if (buckets_.get()[index_].occupied(slot_)) {
if (buckets_.get()[static_cast<size_t>(index_)]
.occupied(static_cast<size_t>(slot_))) {
return *this;
}
}
Expand Down Expand Up @@ -2001,12 +2016,14 @@ public:
// end of the table, or that spot is occupied, stay. Otherwise, step
// forward to the next data item, or to the end of the table.
templated_iterator(
typename decltype(buckets_)::type& buckets,
maybe_const_buckets_t& buckets,
std::shared_ptr<bool> has_table_lock, size_t index, size_t slot)
: buckets_(buckets), has_table_lock_(has_table_lock),
index_(index), slot_(slot) {
index_(static_cast<intmax_t>(index)),
slot_(static_cast<intmax_t>(slot)) {
if (std::make_pair(index_, slot_) != end_pos(buckets) &&
!buckets[index_].occupied(slot_)) {
!buckets[static_cast<size_t>(index_)]
.occupied(static_cast<size_t>(slot_))) {
operator++();
}
}
Expand Down Expand Up @@ -2049,15 +2066,17 @@ public:
check_table();
const auto end_pos = const_iterator::end_pos(buckets_.get());
return iterator(buckets_.get(), has_table_lock_,
end_pos.first, end_pos.second);
static_cast<size_t>(end_pos.first),
static_cast<size_t>(end_pos.second));
}

//! end returns a const_iterator to the end of the table
const_iterator end() const {
check_table();
const auto end_pos = const_iterator::end_pos(buckets_.get());
return const_iterator(buckets_.get(), has_table_lock_,
end_pos.first, end_pos.second);
static_cast<size_t>(end_pos.first),
static_cast<size_t>(end_pos.second));
}

//! cend returns a const_iterator to the end of the table
Expand Down
24 changes: 21 additions & 3 deletions include/cuckoohash_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#include "cuckoohash_config.hh" // for LIBCUCKOO_DEBUG

#if LIBCUCKOO_DEBUG
# define LIBCUCKOO_DBG(fmt, ...) \
fprintf(stderr, "\x1b[32m""[libcuckoo:%s:%d:%lu] " fmt"" "\x1b[0m", \
__FILE__,__LINE__, (unsigned long)std::this_thread::get_id(), __VA_ARGS__)
# define LIBCUCKOO_DBG(fmt, ...) \
fprintf(stderr, "\x1b[32m""[libcuckoo:%s:%d:%lu] " fmt"" "\x1b[0m", \
__FILE__,__LINE__, (unsigned long)std::this_thread::get_id(), \
__VA_ARGS__)
#else
# define LIBCUCKOO_DBG(fmt, ...) do {} while (0)
#endif
Expand All @@ -36,6 +37,23 @@
#define LIBCUCKOO_SQUELCH_PADDING_WARNING
#endif

/**
* thread_local requires GCC >= 4.8 and is not supported in some clang versions,
* so we use __thread if thread_local is not supported
*/
#define LIBCUCKOO_THREAD_LOCAL thread_local
#if defined(__clang__)
# if !__has_feature(cxx_thread_local)
# undef LIBCUCKOO_THREAD_LOCAL
# define LIBCUCKOO_THREAD_LOCAL __thread
# endif
#elif defined(__GNUC__)
# if __GNUC__ == 4 && __GNUC_MINOR__ < 8
# undef LIBCUCKOO_THREAD_LOCAL
# define LIBCUCKOO_THREAD_LOCAL __thread
# endif
#endif

// For enabling certain methods based on a condition. Here's an example.
// ENABLE_IF(some_cond, type, static, inline) method() {
// ...
Expand Down

0 comments on commit ff8a6ac

Please sign in to comment.