diff options
author | Tim Barron <tjbarron@google.com> | 2023-05-19 02:45:25 +0000 |
---|---|---|
committer | Tim Barron <tjbarron@google.com> | 2023-05-19 02:46:17 +0000 |
commit | 863a42f6f9027b19b079f5a245013c1260500519 (patch) | |
tree | 1a2cbe83bb8760021e67992471e469c438e34386 /icing/file | |
parent | ed8f00e32ce543f464893527a9eb08c4bb40b664 (diff) | |
download | icing-863a42f6f9027b19b079f5a245013c1260500519.tar.gz |
Update Icing from upstream.
Descriptions:
========================================================================
Add flag to guard PersistentHashMapKeyMapper and DynamicTrieKeyMapper
========================================================================
Add flag to guard FileBackedVector premapping
========================================================================
Check tokenizer type when assigning section id
========================================================================
Bug:193919210
Change-Id: I682e22d4ba647220b7222fcc63c41bd0e01bbeb9
Diffstat (limited to 'icing/file')
-rw-r--r-- | icing/file/persistent-hash-map.cc | 30 | ||||
-rw-r--r-- | icing/file/persistent-hash-map.h | 10 | ||||
-rw-r--r-- | icing/file/persistent-hash-map_test.cc | 209 |
3 files changed, 150 insertions, 99 deletions
diff --git a/icing/file/persistent-hash-map.cc b/icing/file/persistent-hash-map.cc index ce8310b..729b09a 100644 --- a/icing/file/persistent-hash-map.cc +++ b/icing/file/persistent-hash-map.cc @@ -351,17 +351,18 @@ PersistentHashMap::InitializeNewFiles(const Filesystem& filesystem, FileBackedVector<Bucket>::Create( filesystem, GetBucketStorageFilePath(working_path), MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, max_file_size, - pre_mapping_mmap_size)); + options.pre_mapping_fbv ? pre_mapping_mmap_size : 0)); // Initialize entry_storage pre_mapping_mmap_size = sizeof(Entry) * options.max_num_entries; max_file_size = pre_mapping_mmap_size + FileBackedVector<Entry>::Header::kHeaderSize; - ICING_ASSIGN_OR_RETURN(std::unique_ptr<FileBackedVector<Entry>> entry_storage, - FileBackedVector<Entry>::Create( - filesystem, GetEntryStorageFilePath(working_path), - MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, - max_file_size, pre_mapping_mmap_size)); + ICING_ASSIGN_OR_RETURN( + std::unique_ptr<FileBackedVector<Entry>> entry_storage, + FileBackedVector<Entry>::Create( + filesystem, GetEntryStorageFilePath(working_path), + MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, max_file_size, + options.pre_mapping_fbv ? pre_mapping_mmap_size : 0)); // Initialize kv_storage pre_mapping_mmap_size = @@ -373,7 +374,7 @@ PersistentHashMap::InitializeNewFiles(const Filesystem& filesystem, FileBackedVector<char>::Create( filesystem, GetKeyValueStorageFilePath(working_path), MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, max_file_size, - pre_mapping_mmap_size)); + options.pre_mapping_fbv ? pre_mapping_mmap_size : 0)); // Initialize buckets. ICING_RETURN_IF_ERROR(bucket_storage->Set( @@ -441,17 +442,18 @@ PersistentHashMap::InitializeExistingFiles(const Filesystem& filesystem, FileBackedVector<Bucket>::Create( filesystem, GetBucketStorageFilePath(working_path), MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, max_file_size, - pre_mapping_mmap_size)); + options.pre_mapping_fbv ? pre_mapping_mmap_size : 0)); // Initialize entry_storage pre_mapping_mmap_size = sizeof(Entry) * options.max_num_entries; max_file_size = pre_mapping_mmap_size + FileBackedVector<Entry>::Header::kHeaderSize; - ICING_ASSIGN_OR_RETURN(std::unique_ptr<FileBackedVector<Entry>> entry_storage, - FileBackedVector<Entry>::Create( - filesystem, GetEntryStorageFilePath(working_path), - MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, - max_file_size, pre_mapping_mmap_size)); + ICING_ASSIGN_OR_RETURN( + std::unique_ptr<FileBackedVector<Entry>> entry_storage, + FileBackedVector<Entry>::Create( + filesystem, GetEntryStorageFilePath(working_path), + MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, max_file_size, + options.pre_mapping_fbv ? pre_mapping_mmap_size : 0)); // Initialize kv_storage pre_mapping_mmap_size = @@ -463,7 +465,7 @@ PersistentHashMap::InitializeExistingFiles(const Filesystem& filesystem, FileBackedVector<char>::Create( filesystem, GetKeyValueStorageFilePath(working_path), MemoryMappedFile::Strategy::READ_WRITE_AUTO_SYNC, max_file_size, - pre_mapping_mmap_size)); + options.pre_mapping_fbv ? pre_mapping_mmap_size : 0)); // Create instance. auto persistent_hash_map = diff --git a/icing/file/persistent-hash-map.h b/icing/file/persistent-hash-map.h index a6d14bb..845b22a 100644 --- a/icing/file/persistent-hash-map.h +++ b/icing/file/persistent-hash-map.h @@ -211,12 +211,14 @@ class PersistentHashMap : public PersistentStorage { int32_t max_num_entries_in = Entry::kMaxNumEntries, int32_t max_load_factor_percent_in = kDefaultMaxLoadFactorPercent, int32_t average_kv_byte_size_in = kDefaultAverageKVByteSize, - int32_t init_num_buckets_in = kDefaultInitNumBuckets) + int32_t init_num_buckets_in = kDefaultInitNumBuckets, + bool pre_mapping_fbv_in = false) : value_type_size(value_type_size_in), max_num_entries(max_num_entries_in), max_load_factor_percent(max_load_factor_percent_in), average_kv_byte_size(average_kv_byte_size_in), - init_num_buckets(init_num_buckets_in) {} + init_num_buckets(init_num_buckets_in), + pre_mapping_fbv(pre_mapping_fbv_in) {} bool IsValid() const; @@ -242,6 +244,10 @@ class PersistentHashMap : public PersistentStorage { // It is used when creating new persistent hash map and ignored when // creating the instance from existing files. int32_t init_num_buckets; + + // Flag indicating whether memory map max possible file size for underlying + // FileBackedVector before growing the actual file size. + bool pre_mapping_fbv; }; static constexpr WorkingPathType kWorkingPathType = diff --git a/icing/file/persistent-hash-map_test.cc b/icing/file/persistent-hash-map_test.cc index 6e9a41b..5535629 100644 --- a/icing/file/persistent-hash-map_test.cc +++ b/icing/file/persistent-hash-map_test.cc @@ -39,6 +39,7 @@ using ::testing::IsEmpty; using ::testing::IsTrue; using ::testing::Key; using ::testing::Lt; +using ::testing::Ne; using ::testing::Not; using ::testing::Pair; using ::testing::Pointee; @@ -59,7 +60,7 @@ using Options = PersistentHashMap::Options; static constexpr int32_t kCorruptedValueOffset = 3; static constexpr int32_t kTestInitNumBuckets = 1; -class PersistentHashMapTest : public ::testing::Test { +class PersistentHashMapTest : public ::testing::TestWithParam<bool> { protected: void SetUp() override { base_dir_ = GetTestTempDir() + "/icing"; @@ -103,7 +104,7 @@ class PersistentHashMapTest : public ::testing::Test { std::string working_path_; }; -TEST_F(PersistentHashMapTest, OptionsInvalidValueTypeSize) { +TEST_P(PersistentHashMapTest, OptionsInvalidValueTypeSize) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -117,7 +118,7 @@ TEST_F(PersistentHashMapTest, OptionsInvalidValueTypeSize) { EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, OptionsInvalidMaxNumEntries) { +TEST_P(PersistentHashMapTest, OptionsInvalidMaxNumEntries) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -131,7 +132,7 @@ TEST_F(PersistentHashMapTest, OptionsInvalidMaxNumEntries) { EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, OptionsInvalidMaxLoadFactorPercent) { +TEST_P(PersistentHashMapTest, OptionsInvalidMaxLoadFactorPercent) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -142,7 +143,7 @@ TEST_F(PersistentHashMapTest, OptionsInvalidMaxLoadFactorPercent) { EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, OptionsInvalidAverageKVByteSize) { +TEST_P(PersistentHashMapTest, OptionsInvalidAverageKVByteSize) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -153,7 +154,7 @@ TEST_F(PersistentHashMapTest, OptionsInvalidAverageKVByteSize) { EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, OptionsInvalidInitNumBuckets) { +TEST_P(PersistentHashMapTest, OptionsInvalidInitNumBuckets) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -171,7 +172,7 @@ TEST_F(PersistentHashMapTest, OptionsInvalidInitNumBuckets) { EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, OptionsNumBucketsRequiredExceedsMaxNumBuckets) { +TEST_P(PersistentHashMapTest, OptionsNumBucketsRequiredExceedsMaxNumBuckets) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -180,7 +181,7 @@ TEST_F(PersistentHashMapTest, OptionsNumBucketsRequiredExceedsMaxNumBuckets) { EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, OptionsEstimatedNumKeyValuePairExceedsStorageMaxSize) { Options options(/*value_type_size_in=*/sizeof(int)); ASSERT_TRUE(options.IsValid()); @@ -193,15 +194,16 @@ TEST_F(PersistentHashMapTest, EXPECT_FALSE(options.IsValid()); } -TEST_F(PersistentHashMapTest, InvalidWorkingPath) { +TEST_P(PersistentHashMapTest, InvalidWorkingPath) { EXPECT_THAT(PersistentHashMap::Create( filesystem_, "/dev/null/persistent_hash_map_test", Options(/*value_type_size_in=*/sizeof(int))), StatusIs(libtextclassifier3::StatusCode::INTERNAL)); } -TEST_F(PersistentHashMapTest, CreateWithInvalidOptionsShouldFail) { +TEST_P(PersistentHashMapTest, CreateWithInvalidOptionsShouldFail) { Options invalid_options(/*value_type_size_in=*/-1); + invalid_options.pre_mapping_fbv = GetParam(); ASSERT_FALSE(invalid_options.IsValid()); EXPECT_THAT( @@ -209,13 +211,16 @@ TEST_F(PersistentHashMapTest, CreateWithInvalidOptionsShouldFail) { StatusIs(libtextclassifier3::StatusCode::INVALID_ARGUMENT)); } -TEST_F(PersistentHashMapTest, InitializeNewFiles) { +TEST_P(PersistentHashMapTest, InitializeNewFiles) { { ASSERT_FALSE(filesystem_.DirectoryExists(working_path_.c_str())); + + Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, PersistentHashMap::Create(filesystem_, working_path_, - Options(/*value_type_size_in=*/sizeof(int)))); + std::move(options))); EXPECT_THAT(persistent_hash_map, Pointee(IsEmpty())); ICING_ASSERT_OK(persistent_hash_map->PersistToDisk()); @@ -245,7 +250,7 @@ TEST_F(PersistentHashMapTest, InitializeNewFiles) { PersistentHashMap::kCrcsMetadataFileOffset)); // # of elements in bucket_storage should be 1, so it should have non-zero // all storages crc value. - EXPECT_THAT(crcs.component_crcs.storages_crc, Not(Eq(0))); + EXPECT_THAT(crcs.component_crcs.storages_crc, Ne(0)); EXPECT_THAT(crcs.component_crcs.info_crc, Eq(Crc32(std::string_view(reinterpret_cast<const char*>(&info), sizeof(Info))) @@ -257,10 +262,9 @@ TEST_F(PersistentHashMapTest, InitializeNewFiles) { .Get())); } -TEST_F(PersistentHashMapTest, InitializeNewFilesWithCustomInitNumBuckets) { +TEST_P(PersistentHashMapTest, InitializeNewFilesWithCustomInitNumBuckets) { int custom_init_num_buckets = 128; - // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, PersistentHashMap::Create( @@ -271,11 +275,12 @@ TEST_F(PersistentHashMapTest, InitializeNewFilesWithCustomInitNumBuckets) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/custom_init_num_buckets))); + /*init_num_buckets_in=*/custom_init_num_buckets, + /*pre_mapping_fbv=*/GetParam()))); EXPECT_THAT(persistent_hash_map->num_buckets(), Eq(custom_init_num_buckets)); } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeNewFilesWithInitNumBucketsSmallerThanNumBucketsRequired) { int init_num_buckets = 65536; @@ -290,12 +295,14 @@ TEST_F(PersistentHashMapTest, /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/init_num_buckets))); + /*init_num_buckets_in=*/init_num_buckets, + /*pre_mapping_fbv=*/GetParam()))); EXPECT_THAT(persistent_hash_map->num_buckets(), Eq(init_num_buckets)); } -TEST_F(PersistentHashMapTest, InitNumBucketsShouldNotAffectExistingFiles) { +TEST_P(PersistentHashMapTest, InitNumBucketsShouldNotAffectExistingFiles) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); int original_init_num_buckets = 4; { @@ -324,9 +331,10 @@ TEST_F(PersistentHashMapTest, InitNumBucketsShouldNotAffectExistingFiles) { Eq(original_init_num_buckets)); } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializationShouldFailWithoutPersistToDiskOrDestruction) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( @@ -351,8 +359,9 @@ TEST_F(PersistentHashMapTest, StatusIs(libtextclassifier3::StatusCode::FAILED_PRECONDITION)); } -TEST_F(PersistentHashMapTest, InitializationShouldSucceedWithPersistToDisk) { +TEST_P(PersistentHashMapTest, InitializationShouldSucceedWithPersistToDisk) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( @@ -384,8 +393,9 @@ TEST_F(PersistentHashMapTest, InitializationShouldSucceedWithPersistToDisk) { EXPECT_THAT(GetValueByKey(persistent_hash_map2.get(), "b"), IsOkAndHolds(2)); } -TEST_F(PersistentHashMapTest, InitializationShouldSucceedAfterDestruction) { +TEST_P(PersistentHashMapTest, InitializationShouldSucceedAfterDestruction) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -418,9 +428,10 @@ TEST_F(PersistentHashMapTest, InitializationShouldSucceedAfterDestruction) { } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithDifferentMagicShouldFail) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -472,14 +483,15 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithDifferentValueTypeSizeShouldFail) { { // Create new persistent hash map + Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, - PersistentHashMap::Create(filesystem_, working_path_, - Options(/*value_type_size_in=*/sizeof(int)))); + PersistentHashMap::Create(filesystem_, working_path_, options)); ICING_ASSERT_OK(persistent_hash_map->Put("a", Serialize(1).data())); ICING_ASSERT_OK(persistent_hash_map->PersistToDisk()); @@ -488,11 +500,13 @@ TEST_F(PersistentHashMapTest, { // Attempt to create the persistent hash map with different value type size. // This should fail. - ASSERT_THAT(sizeof(char), Not(Eq(sizeof(int)))); + ASSERT_THAT(sizeof(char), Ne(sizeof(int))); + + Options options(/*value_type_size_in=*/sizeof(char)); + options.pre_mapping_fbv = GetParam(); libtextclassifier3::StatusOr<std::unique_ptr<PersistentHashMap>> - persistent_hash_map_or = PersistentHashMap::Create( - filesystem_, working_path_, - Options(/*value_type_size_in=*/sizeof(char))); + persistent_hash_map_or = + PersistentHashMap::Create(filesystem_, working_path_, options); EXPECT_THAT(persistent_hash_map_or, StatusIs(libtextclassifier3::StatusCode::FAILED_PRECONDITION)); EXPECT_THAT(persistent_hash_map_or.status().error_message(), @@ -500,9 +514,10 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithMaxNumEntriesSmallerThanSizeShouldFail) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( @@ -541,8 +556,9 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, InitializeExistingFilesWithWrongAllCrc) { +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithWrongAllCrc) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -583,9 +599,10 @@ TEST_F(PersistentHashMapTest, InitializeExistingFilesWithWrongAllCrc) { } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithCorruptedInfoShouldFail) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -625,9 +642,10 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithCorruptedBucketStorage) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -670,9 +688,10 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithCorruptedEntryStorage) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -714,9 +733,10 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithCorruptedKeyValueStorage) { Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); { // Create new persistent hash map @@ -757,14 +777,15 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesAllowDifferentMaxLoadFactorPercent) { Options options( /*value_type_size_in=*/sizeof(int), /*max_num_entries_in=*/Entry::kMaxNumEntries, /*max_load_factor_percent_in=*/Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv=*/GetParam()); { // Create new persistent hash map @@ -786,7 +807,7 @@ TEST_F(PersistentHashMapTest, options.max_load_factor_percent = 200; ASSERT_TRUE(options.IsValid()); ASSERT_THAT(options.max_load_factor_percent, - Not(Eq(Options::kDefaultMaxLoadFactorPercent))); + Ne(Options::kDefaultMaxLoadFactorPercent)); // Attempt to create the persistent hash map with different max load factor // percent. This should succeed and metadata should be modified correctly. @@ -824,14 +845,15 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, +TEST_P(PersistentHashMapTest, InitializeExistingFilesWithDifferentMaxLoadFactorPercentShouldRehash) { Options options( /*value_type_size_in=*/sizeof(int), /*max_num_entries_in=*/Entry::kMaxNumEntries, /*max_load_factor_percent_in=*/Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv=*/GetParam()); double prev_loading_percent; int prev_num_buckets; @@ -894,7 +916,7 @@ TEST_F(PersistentHashMapTest, EXPECT_THAT(persistent_hash_map->size() * 100.0 / persistent_hash_map->num_buckets(), Not(Gt(options.max_load_factor_percent))); - EXPECT_THAT(persistent_hash_map->num_buckets(), Not(Eq(prev_num_buckets))); + EXPECT_THAT(persistent_hash_map->num_buckets(), Ne(prev_num_buckets)); EXPECT_THAT(GetValueByKey(persistent_hash_map.get(), "a"), IsOkAndHolds(1)); EXPECT_THAT(GetValueByKey(persistent_hash_map.get(), "b"), IsOkAndHolds(2)); @@ -904,7 +926,7 @@ TEST_F(PersistentHashMapTest, } } -TEST_F(PersistentHashMapTest, PutAndGet) { +TEST_P(PersistentHashMapTest, PutAndGet) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -916,7 +938,8 @@ TEST_F(PersistentHashMapTest, PutAndGet) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv=*/GetParam()))); EXPECT_THAT(persistent_hash_map, Pointee(IsEmpty())); EXPECT_THAT(GetValueByKey(persistent_hash_map.get(), "default-google.com"), @@ -940,7 +963,7 @@ TEST_F(PersistentHashMapTest, PutAndGet) { ICING_ASSERT_OK(persistent_hash_map->PersistToDisk()); } -TEST_F(PersistentHashMapTest, PutShouldOverwriteValueIfKeyExists) { +TEST_P(PersistentHashMapTest, PutShouldOverwriteValueIfKeyExists) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -952,7 +975,8 @@ TEST_F(PersistentHashMapTest, PutShouldOverwriteValueIfKeyExists) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com", Serialize(100).data())); @@ -973,7 +997,7 @@ TEST_F(PersistentHashMapTest, PutShouldOverwriteValueIfKeyExists) { IsOkAndHolds(300)); } -TEST_F(PersistentHashMapTest, ShouldRehash) { +TEST_P(PersistentHashMapTest, ShouldRehash) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -985,7 +1009,8 @@ TEST_F(PersistentHashMapTest, ShouldRehash) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv=*/GetParam()))); int original_num_buckets = persistent_hash_map->num_buckets(); // Insert 100 key value pairs. There should be rehashing so the loading of @@ -999,8 +1024,7 @@ TEST_F(PersistentHashMapTest, ShouldRehash) { persistent_hash_map->num_buckets(), Not(Gt(Options::kDefaultMaxLoadFactorPercent))); } - EXPECT_THAT(persistent_hash_map->num_buckets(), - Not(Eq(original_num_buckets))); + EXPECT_THAT(persistent_hash_map->num_buckets(), Ne(original_num_buckets)); // After rehashing, we should still be able to get all inserted entries. for (int i = 0; i < 100; ++i) { @@ -1009,7 +1033,7 @@ TEST_F(PersistentHashMapTest, ShouldRehash) { } } -TEST_F(PersistentHashMapTest, GetOrPutShouldPutIfKeyDoesNotExist) { +TEST_P(PersistentHashMapTest, GetOrPutShouldPutIfKeyDoesNotExist) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1021,7 +1045,8 @@ TEST_F(PersistentHashMapTest, GetOrPutShouldPutIfKeyDoesNotExist) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv=*/GetParam()))); ASSERT_THAT(GetValueByKey(persistent_hash_map.get(), "default-google.com"), StatusIs(libtextclassifier3::StatusCode::NOT_FOUND)); @@ -1035,7 +1060,7 @@ TEST_F(PersistentHashMapTest, GetOrPutShouldPutIfKeyDoesNotExist) { IsOkAndHolds(1)); } -TEST_F(PersistentHashMapTest, GetOrPutShouldGetIfKeyExists) { +TEST_P(PersistentHashMapTest, GetOrPutShouldGetIfKeyExists) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1047,7 +1072,8 @@ TEST_F(PersistentHashMapTest, GetOrPutShouldGetIfKeyExists) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ASSERT_THAT( persistent_hash_map->Put("default-google.com", Serialize(1).data()), @@ -1064,7 +1090,7 @@ TEST_F(PersistentHashMapTest, GetOrPutShouldGetIfKeyExists) { IsOkAndHolds(1)); } -TEST_F(PersistentHashMapTest, Delete) { +TEST_P(PersistentHashMapTest, Delete) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1076,7 +1102,8 @@ TEST_F(PersistentHashMapTest, Delete) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); // Delete a non-existing key should get NOT_FOUND error EXPECT_THAT(persistent_hash_map->Delete("default-google.com"), @@ -1115,7 +1142,7 @@ TEST_F(PersistentHashMapTest, Delete) { IsOkAndHolds(50)); } -TEST_F(PersistentHashMapTest, DeleteMultiple) { +TEST_P(PersistentHashMapTest, DeleteMultiple) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1127,7 +1154,8 @@ TEST_F(PersistentHashMapTest, DeleteMultiple) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); std::unordered_map<std::string, int> existing_keys; std::unordered_set<std::string> deleted_keys; @@ -1168,7 +1196,7 @@ TEST_F(PersistentHashMapTest, DeleteMultiple) { Eq(existing_keys)); } -TEST_F(PersistentHashMapTest, DeleteBucketHeadElement) { +TEST_P(PersistentHashMapTest, DeleteBucketHeadElement) { // Create new persistent hash map // Set max_load_factor_percent as 1000. Load factor percent is calculated as // 100 * num_keys / num_buckets. Therefore, with 1 bucket (the initial # of @@ -1184,7 +1212,8 @@ TEST_F(PersistentHashMapTest, DeleteBucketHeadElement) { /*max_num_entries_in=*/Entry::kMaxNumEntries, /*max_load_factor_percent_in=*/1000, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1206,7 +1235,7 @@ TEST_F(PersistentHashMapTest, DeleteBucketHeadElement) { StatusIs(libtextclassifier3::StatusCode::NOT_FOUND)); } -TEST_F(PersistentHashMapTest, DeleteBucketIntermediateElement) { +TEST_P(PersistentHashMapTest, DeleteBucketIntermediateElement) { // Create new persistent hash map // Set max_load_factor_percent as 1000. Load factor percent is calculated as // 100 * num_keys / num_buckets. Therefore, with 1 bucket (the initial # of @@ -1222,7 +1251,8 @@ TEST_F(PersistentHashMapTest, DeleteBucketIntermediateElement) { /*max_num_entries_in=*/Entry::kMaxNumEntries, /*max_load_factor_percent_in=*/1000, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1243,7 +1273,7 @@ TEST_F(PersistentHashMapTest, DeleteBucketIntermediateElement) { IsOkAndHolds(2)); } -TEST_F(PersistentHashMapTest, DeleteBucketTailElement) { +TEST_P(PersistentHashMapTest, DeleteBucketTailElement) { // Create new persistent hash map // Set max_load_factor_percent as 1000. Load factor percent is calculated as // 100 * num_keys / num_buckets. Therefore, with 1 bucket (the initial # of @@ -1259,7 +1289,8 @@ TEST_F(PersistentHashMapTest, DeleteBucketTailElement) { /*max_num_entries_in=*/Entry::kMaxNumEntries, /*max_load_factor_percent_in=*/1000, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1281,7 +1312,7 @@ TEST_F(PersistentHashMapTest, DeleteBucketTailElement) { IsOkAndHolds(2)); } -TEST_F(PersistentHashMapTest, DeleteBucketOnlySingleElement) { +TEST_P(PersistentHashMapTest, DeleteBucketOnlySingleElement) { // Create new persistent hash map // Set max_load_factor_percent as 1000. Load factor percent is calculated as // 100 * num_keys / num_buckets. Therefore, with 1 bucket (the initial # of @@ -1297,7 +1328,8 @@ TEST_F(PersistentHashMapTest, DeleteBucketOnlySingleElement) { /*max_num_entries_in=*/Entry::kMaxNumEntries, /*max_load_factor_percent_in=*/1000, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com", Serialize(100).data())); @@ -1310,7 +1342,7 @@ TEST_F(PersistentHashMapTest, DeleteBucketOnlySingleElement) { StatusIs(libtextclassifier3::StatusCode::NOT_FOUND)); } -TEST_F(PersistentHashMapTest, OperationsWhenReachingMaxNumEntries) { +TEST_P(PersistentHashMapTest, OperationsWhenReachingMaxNumEntries) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1322,7 +1354,8 @@ TEST_F(PersistentHashMapTest, OperationsWhenReachingMaxNumEntries) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/1))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com", Serialize(100).data())); @@ -1346,12 +1379,13 @@ TEST_F(PersistentHashMapTest, OperationsWhenReachingMaxNumEntries) { StatusIs(libtextclassifier3::StatusCode::RESOURCE_EXHAUSTED)); } -TEST_F(PersistentHashMapTest, ShouldFailIfKeyContainsTerminationCharacter) { +TEST_P(PersistentHashMapTest, ShouldFailIfKeyContainsTerminationCharacter) { // Create new persistent hash map + Options options(/*value_type_size_in=*/sizeof(int)); + options.pre_mapping_fbv = GetParam(); ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, - PersistentHashMap::Create(filesystem_, working_path_, - Options(/*value_type_size_in=*/sizeof(int)))); + PersistentHashMap::Create(filesystem_, working_path_, options)); const char invalid_key[] = "a\0bc"; std::string_view invalid_key_view(invalid_key, 4); @@ -1367,7 +1401,7 @@ TEST_F(PersistentHashMapTest, ShouldFailIfKeyContainsTerminationCharacter) { StatusIs(libtextclassifier3::StatusCode::INVALID_ARGUMENT)); } -TEST_F(PersistentHashMapTest, EmptyHashMapIterator) { +TEST_P(PersistentHashMapTest, EmptyHashMapIterator) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1379,12 +1413,13 @@ TEST_F(PersistentHashMapTest, EmptyHashMapIterator) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); EXPECT_FALSE(persistent_hash_map->GetIterator().Advance()); } -TEST_F(PersistentHashMapTest, Iterator) { +TEST_P(PersistentHashMapTest, Iterator) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1396,7 +1431,8 @@ TEST_F(PersistentHashMapTest, Iterator) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); std::unordered_map<std::string, int> kvps; // Insert 100 key value pairs @@ -1411,7 +1447,7 @@ TEST_F(PersistentHashMapTest, Iterator) { Eq(kvps)); } -TEST_F(PersistentHashMapTest, IteratorAfterDeletingFirstKeyValuePair) { +TEST_P(PersistentHashMapTest, IteratorAfterDeletingFirstKeyValuePair) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1423,7 +1459,8 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingFirstKeyValuePair) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1440,7 +1477,7 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingFirstKeyValuePair) { Pair("default-google.com-2", 2))); } -TEST_F(PersistentHashMapTest, IteratorAfterDeletingIntermediateKeyValuePair) { +TEST_P(PersistentHashMapTest, IteratorAfterDeletingIntermediateKeyValuePair) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1452,7 +1489,8 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingIntermediateKeyValuePair) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1469,7 +1507,7 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingIntermediateKeyValuePair) { Pair("default-google.com-2", 2))); } -TEST_F(PersistentHashMapTest, IteratorAfterDeletingLastKeyValuePair) { +TEST_P(PersistentHashMapTest, IteratorAfterDeletingLastKeyValuePair) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1481,7 +1519,8 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingLastKeyValuePair) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1498,7 +1537,7 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingLastKeyValuePair) { Pair("default-google.com-1", 1))); } -TEST_F(PersistentHashMapTest, IteratorAfterDeletingAllKeyValuePairs) { +TEST_P(PersistentHashMapTest, IteratorAfterDeletingAllKeyValuePairs) { // Create new persistent hash map ICING_ASSERT_OK_AND_ASSIGN( std::unique_ptr<PersistentHashMap> persistent_hash_map, @@ -1510,7 +1549,8 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingAllKeyValuePairs) { /*max_load_factor_percent_in=*/ Options::kDefaultMaxLoadFactorPercent, /*average_kv_byte_size_in=*/Options::kDefaultAverageKVByteSize, - /*init_num_buckets_in=*/kTestInitNumBuckets))); + /*init_num_buckets_in=*/kTestInitNumBuckets, + /*pre_mapping_fbv_in=*/GetParam()))); ICING_ASSERT_OK( persistent_hash_map->Put("default-google.com-0", Serialize(0).data())); @@ -1528,6 +1568,9 @@ TEST_F(PersistentHashMapTest, IteratorAfterDeletingAllKeyValuePairs) { EXPECT_FALSE(persistent_hash_map->GetIterator().Advance()); } +INSTANTIATE_TEST_SUITE_P(PersistentHashMapTest, PersistentHashMapTest, + testing::Values(true, false)); + } // namespace } // namespace lib |