Skip to content

Commit 233e730

Browse files
committed
CR: compared the results of unsafe iterations with hardcoded values in the FileSafeVsUnsafeIterators test.
1 parent 0835a7e commit 233e730

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

Blocks/Persistence/file.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,7 @@ class FilePersister {
270270
: file_persister_impl_(file_persister_impl, [this]() { valid_ = false; }), i_(i) {
271271
if (!filename.empty()) {
272272
fi_ = std::make_unique<std::ifstream>(filename);
273-
// This `if` condition is only here to test performance with vs. without the `seekg`.
274-
// The high performance version jumps to the desired entry right away,
275-
// The poor performance one scans the file from the very beginning for each new iterator created.
276-
if (true) {
277-
cit_ = std::make_unique<IteratorOverFileOfPersistedEntries<ENTRY>>(*fi_, offset, index_at_offset);
278-
} else {
279-
// Inefficient, scan the file from the very beginning.
280-
cit_ = std::make_unique<IteratorOverFileOfPersistedEntries<ENTRY>>(*fi_, 0, 0);
281-
}
273+
cit_ = std::make_unique<IteratorOverFileOfPersistedEntries<ENTRY>>(*fi_, offset, index_at_offset);
282274
}
283275
}
284276

Blocks/Persistence/test.cc

+40-20
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,15 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
697697
struct_schema.AddType<StorableString>();
698698
const std::string signature =
699699
"#signature " + JSON(current::ss::StreamSignature(namespace_name, struct_schema.GetSchemaInfo())) + '\n';
700-
std::string lines[] = {"{\"index\":0,\"us\":100}\t{\"s\":\"foo\"}\n",
701-
"{\"index\":1,\"us\":200}\t{\"s\":\"bar\"}\n",
702-
"#head 00000000000000000300\n",
703-
"{\"index\":2,\"us\":500}\t{\"s\":\"meh\"}\n",
704-
"#head 00000000000000000600\n"};
700+
std::vector<std::string> lines = {"{\"index\":0,\"us\":100}\t{\"s\":\"foo\"}",
701+
"{\"index\":1,\"us\":200}\t{\"s\":\"bar\"}",
702+
"#head 00000000000000000300",
703+
"{\"index\":2,\"us\":500}\t{\"s\":\"meh\"}",
704+
"#head 00000000000000000600"};
705705
const auto CombineFileContents = [&]() -> std::string {
706706
std::string contents = signature;
707707
for (const auto& line : lines) {
708-
contents += line;
708+
contents += line + '\n';
709709
}
710710
return contents;
711711
};
@@ -715,12 +715,12 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
715715
std::mutex mutex;
716716
IMPL impl(mutex, namespace_name, persistence_file_name);
717717

718-
const auto CheckUnsafeIteration = [&]() {
719-
std::vector<std::string> all_entries_unsafe;
718+
const auto GetUnsafeIterationResult = [&]() -> std::string {
719+
std::string combined_result;
720720
for (const auto& e : impl.Iterate<current::ss::IterationMode::Unsafe>()) {
721-
all_entries_unsafe.push_back(e);
721+
combined_result += e + '\n';
722722
}
723-
EXPECT_EQ(lines[0] + lines[1] + lines[3], Join(all_entries_unsafe, "\n") + '\n');
723+
return combined_result;
724724
};
725725

726726
{
@@ -730,7 +730,11 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
730730
"%s %d %d", e.entry.s.c_str(), static_cast<int>(e.idx_ts.index), static_cast<int>(e.idx_ts.us.count())));
731731
}
732732
EXPECT_EQ("foo 0 100,bar 1 200,meh 2 500", Join(all_entries, ","));
733-
CheckUnsafeIteration();
733+
EXPECT_EQ(
734+
"{\"index\":0,\"us\":100}\t{\"s\":\"foo\"}\n"
735+
"{\"index\":1,\"us\":200}\t{\"s\":\"bar\"}\n"
736+
"{\"index\":2,\"us\":500}\t{\"s\":\"meh\"}\n",
737+
GetUnsafeIterationResult());
734738
}
735739

736740
{
@@ -740,7 +744,7 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
740744
EXPECT_NO_THROW(*(++impl.Iterate().begin()));
741745

742746
// swap the first two entries to provoke the InconsistentIndexException exceptions.
743-
lines[0].swap(lines[1]);
747+
std::swap(lines[0], lines[1]);
744748
current::FileSystem::WriteStringToFile(CombineFileContents(), persistence_file_name.c_str());
745749

746750
// Check that "safe" iterators throw the InconsistentIndexException while "unsafe" ones don't.
@@ -752,21 +756,29 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
752756
EXPECT_NO_THROW(*impl.Iterate<current::ss::IterationMode::Unsafe>(1, 2).begin());
753757
EXPECT_NO_THROW(*impl.Iterate<current::ss::IterationMode::Unsafe>().begin());
754758
EXPECT_NO_THROW(*(++impl.Iterate<current::ss::IterationMode::Unsafe>().begin()));
755-
CheckUnsafeIteration();
756-
lines[0].swap(lines[1]);
759+
EXPECT_EQ(
760+
"{\"index\":1,\"us\":200}\t{\"s\":\"bar\"}\n"
761+
"{\"index\":0,\"us\":100}\t{\"s\":\"foo\"}\n"
762+
"{\"index\":2,\"us\":500}\t{\"s\":\"meh\"}\n",
763+
GetUnsafeIterationResult());
764+
std::swap(lines[0], lines[1]);
757765
}
758766

759767
{
760-
// Produce wrong JSON instead of an entry: replace all symbols, except last \n one.
761-
lines[1].replace(0, lines[1].length() - 1, lines[1].length() - 1, 'A');
768+
// Produce wrong JSON instead of an entire entry - replace both index and value with garbage.
769+
lines[1].replace(0, lines[1].length(), lines[1].length(), 'A');
762770
current::FileSystem::WriteStringToFile(CombineFileContents(), persistence_file_name.c_str());
763771

764772
// Check that "safe" iterators throw the MalformedEntryException while "unsafe" ones don't.
765773
EXPECT_THROW(*(++(impl.Iterate().begin())), current::persistence::MalformedEntryException);
766774
EXPECT_THROW(*impl.Iterate(1, 2).begin(), current::persistence::MalformedEntryException);
767775
EXPECT_NO_THROW(*impl.Iterate<current::ss::IterationMode::Unsafe>(1, 2).begin());
768776
EXPECT_NO_THROW(*(++impl.Iterate<current::ss::IterationMode::Unsafe>().begin()));
769-
CheckUnsafeIteration();
777+
EXPECT_EQ(
778+
"{\"index\":0,\"us\":100}\t{\"s\":\"foo\"}\n"
779+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
780+
"{\"index\":2,\"us\":500}\t{\"s\":\"meh\"}\n",
781+
GetUnsafeIterationResult());
770782
}
771783

772784
{
@@ -783,7 +795,11 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
783795
EXPECT_THROW(*impl.Iterate(0, 1).begin(), current::serialization::json::InvalidJSONException);
784796
EXPECT_THROW(*impl.Iterate().begin(), current::serialization::json::InvalidJSONException);
785797
EXPECT_NO_THROW(*impl.Iterate<current::ss::IterationMode::Unsafe>(0, 1).begin());
786-
CheckUnsafeIteration();
798+
EXPECT_EQ(
799+
"BBBBBBBBBBBBBBBBBBBB\t{\"s\":\"foo\"}\n"
800+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
801+
"{\"index\":2,\"us\":500}\t{\"s\":\"meh\"}\n",
802+
GetUnsafeIterationResult());
787803
}
788804

789805
{
@@ -792,14 +808,18 @@ TEST(PersistenceLayer, FileSafeVsUnsafeIterators) {
792808
// Produce wrong JSON instead of the entry's value only and leave index and timestamp section valid.
793809
const auto tab_pos = lines[3].find('\t');
794810
ASSERT_FALSE(std::string::npos == tab_pos);
795-
const auto length_to_replace = (lines[3].length() - 1) - (tab_pos + 1);
811+
const auto length_to_replace = lines[3].length() - (tab_pos + 1);
796812
lines[3].replace(tab_pos + 1, length_to_replace, length_to_replace, 'C');
797813
current::FileSystem::WriteStringToFile(CombineFileContents(), persistence_file_name.c_str());
798814

799815
// Check that "safe" iterators throw the InvalidJSONException while "unsafe" ones don't.
800816
EXPECT_THROW(*impl.Iterate(2, 3).begin(), current::serialization::json::InvalidJSONException);
801817
EXPECT_NO_THROW(*impl.Iterate<current::ss::IterationMode::Unsafe>(2, 3).begin());
802-
CheckUnsafeIteration();
818+
EXPECT_EQ(
819+
"BBBBBBBBBBBBBBBBBBBB\t{\"s\":\"foo\"}\n"
820+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
821+
"{\"index\":2,\"us\":500}\tCCCCCCCCCCC\n",
822+
GetUnsafeIterationResult());
803823
}
804824
}
805825

0 commit comments

Comments
 (0)