-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
621 additions
and
568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ build | |
read.md | ||
images | ||
test_images | ||
bulk | ||
bulk | ||
tests/bulk_test.cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,122 @@ | ||
|
||
|
||
#include <iostream> | ||
|
||
#include <folly/SharedMutex.h> | ||
#include <iostream> | ||
|
||
struct Image { | ||
struct WriteMetadata { | ||
std::string output_path; | ||
std::string write_timestamp; | ||
bool output_written = false; | ||
}; | ||
|
||
std::string image_sha256; | ||
std::string path; | ||
std::size_t image_size; | ||
std::string content_fuzzhash; | ||
std::string text_content; | ||
std::size_t text_size; | ||
std::string time_processed; | ||
WriteMetadata write_info; | ||
|
||
mutable std::unique_ptr<folly::SharedMutex> mutex; | ||
|
||
Image(std::string path) : path(std::move(path)), mutex(nullptr) {} | ||
|
||
// Function to update write metadata | ||
void updateWriteMetadata(const std::string &output_path, | ||
const std::string &write_timestamp, | ||
bool output_written) { | ||
if (!mutex) { | ||
mutex = std::make_unique<folly::SharedMutex>(); | ||
struct WriteMetadata { | ||
std::string output_path; | ||
std::string write_timestamp; | ||
bool output_written = false; | ||
}; | ||
|
||
std::string image_sha256; | ||
std::string path; | ||
std::size_t image_size; | ||
std::string content_fuzzhash; | ||
std::string text_content; | ||
std::size_t text_size; | ||
std::string time_processed; | ||
WriteMetadata write_info; | ||
|
||
mutable std::unique_ptr<folly::SharedMutex> mutex; | ||
|
||
Image(std::string path) : path(std::move(path)), mutex(nullptr) {} | ||
|
||
// Function to update write metadata | ||
void updateWriteMetadata(const std::string &output_path, | ||
const std::string &write_timestamp, | ||
bool output_written) { | ||
if (!mutex) { | ||
mutex = std::make_unique<folly::SharedMutex>(); | ||
} | ||
std::unique_lock<folly::SharedMutex> writerLock(*mutex); | ||
write_info.output_path = output_path; | ||
write_info.write_timestamp = write_timestamp; | ||
write_info.output_written = output_written; | ||
} | ||
std::unique_lock<folly::SharedMutex> writerLock(*mutex); | ||
write_info.output_path = output_path; | ||
write_info.write_timestamp = write_timestamp; | ||
write_info.output_written = output_written; | ||
} | ||
|
||
// Function to read write metadata | ||
WriteMetadata readWriteMetadata() { | ||
if (!mutex) { | ||
return write_info; // If mutex is not initialized, no write has occurred | ||
|
||
// Function to read write metadata | ||
WriteMetadata readWriteMetadata() { | ||
if (!mutex) { | ||
return write_info; // If mutex is not initialized, no write has | ||
// occurred | ||
} | ||
std::shared_lock<folly::SharedMutex> readerLock(*mutex); | ||
return write_info; | ||
} | ||
|
||
void logAlreadyWritten() const { | ||
std::cout << "Image already written by " << write_info.output_path | ||
<< std::endl; | ||
} | ||
std::shared_lock<folly::SharedMutex> readerLock(*mutex); | ||
return write_info; | ||
} | ||
|
||
void logAlreadyWritten() const { | ||
std::cout << "Image already written by " << write_info.output_path | ||
<< std::endl; | ||
} | ||
}; | ||
#include <gtest/gtest.h> | ||
|
||
class ImageTest : public ::testing::Test { | ||
protected: | ||
Image img{"test_path"}; | ||
protected: | ||
Image img{"test_path"}; | ||
}; | ||
|
||
TEST_F(ImageTest, MutexNullIfNoWrite) { | ||
EXPECT_EQ(img.mutex, nullptr); // Mutex should be null initially | ||
EXPECT_EQ(img.mutex, nullptr); // Mutex should be null initially | ||
} | ||
|
||
TEST_F(ImageTest, MutexLazyInitialization) { | ||
EXPECT_EQ(img.mutex, nullptr); // Mutex should be null initially | ||
EXPECT_EQ(img.mutex, nullptr); // Mutex should be null initially | ||
|
||
img.updateWriteMetadata("path/to/output", "2024-01-20", true); | ||
img.updateWriteMetadata("path/to/output", "2024-01-20", true); | ||
|
||
EXPECT_NE(img.mutex, nullptr); // now mutex exists - not nullptr | ||
EXPECT_NE(img.mutex, nullptr); // now mutex exists - not nullptr | ||
} | ||
|
||
// Test for updating write metadata | ||
TEST_F(ImageTest, UpdateWriteMetadata) { | ||
std::string new_output_path = "new_path"; | ||
std::string new_write_timestamp = "2024-01-20"; | ||
bool new_output_written = true; | ||
std::string new_output_path = "new_path"; | ||
std::string new_write_timestamp = "2024-01-20"; | ||
bool new_output_written = true; | ||
|
||
img.updateWriteMetadata(new_output_path, new_write_timestamp, | ||
new_output_written); | ||
img.updateWriteMetadata(new_output_path, new_write_timestamp, | ||
new_output_written); | ||
|
||
auto write_info = img.readWriteMetadata(); | ||
EXPECT_EQ(write_info.output_path, new_output_path); | ||
EXPECT_EQ(write_info.write_timestamp, new_write_timestamp); | ||
EXPECT_EQ(write_info.output_written, new_output_written); | ||
auto write_info = img.readWriteMetadata(); | ||
EXPECT_EQ(write_info.output_path, new_output_path); | ||
EXPECT_EQ(write_info.write_timestamp, new_write_timestamp); | ||
EXPECT_EQ(write_info.output_written, new_output_written); | ||
} | ||
|
||
// Test for reading write metadata before any write | ||
TEST_F(ImageTest, ReadWriteMetadataBeforeWrite) { | ||
auto write_info = img.readWriteMetadata(); | ||
auto write_info = img.readWriteMetadata(); | ||
|
||
// Assuming default values before any write | ||
EXPECT_EQ(write_info.output_path, ""); | ||
EXPECT_EQ(write_info.write_timestamp, ""); | ||
EXPECT_FALSE(write_info.output_written); | ||
// Assuming default values before any write | ||
EXPECT_EQ(write_info.output_path, ""); | ||
EXPECT_EQ(write_info.write_timestamp, ""); | ||
EXPECT_FALSE(write_info.output_written); | ||
} | ||
|
||
TEST(ImageConcurrentWriteTest, ConcurrentWriteAttempts) { | ||
|
||
Image img("test_path"); | ||
|
||
// Function to be run by threads | ||
auto writeAttempt = [&img](std::string path) { | ||
// Reading without a lock | ||
if (!img.write_info.output_written) { | ||
img.updateWriteMetadata(path, "2024-01-21", true); | ||
} else { | ||
img.logAlreadyWritten(); | ||
} | ||
}; | ||
|
||
std::thread writerThread1(writeAttempt, "thread 1 wrote"); | ||
std::thread writerThread2(writeAttempt, "thread 2 wrote"); | ||
std::thread writerThread3(writeAttempt, "thread 3 wrote"); | ||
std::thread writerThread4(writeAttempt, "thread 4 wrote"); | ||
|
||
// Wait for threads to complete | ||
EXPECT_NO_THROW(writerThread1.join()); | ||
EXPECT_NO_THROW(writerThread2.join()); | ||
EXPECT_NO_THROW(writerThread3.join()); | ||
EXPECT_NO_THROW(writerThread4.join()); | ||
} | ||
Image img("test_path"); | ||
|
||
// Function to be run by threads | ||
auto writeAttempt = [&img](std::string path) { | ||
// Reading without a lock | ||
if (!img.write_info.output_written) { | ||
img.updateWriteMetadata(path, "2024-01-21", true); | ||
} else { | ||
img.logAlreadyWritten(); | ||
} | ||
}; | ||
|
||
std::thread writerThread1(writeAttempt, "thread 1 wrote"); | ||
std::thread writerThread2(writeAttempt, "thread 2 wrote"); | ||
std::thread writerThread3(writeAttempt, "thread 3 wrote"); | ||
std::thread writerThread4(writeAttempt, "thread 4 wrote"); | ||
|
||
// Wait for threads to complete | ||
EXPECT_NO_THROW(writerThread1.join()); | ||
EXPECT_NO_THROW(writerThread2.join()); | ||
EXPECT_NO_THROW(writerThread3.join()); | ||
EXPECT_NO_THROW(writerThread4.join()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.