diff options
Diffstat (limited to 'icing/index/numeric/integer-index-storage_test.cc')
-rw-r--r-- | icing/index/numeric/integer-index-storage_test.cc | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/icing/index/numeric/integer-index-storage_test.cc b/icing/index/numeric/integer-index-storage_test.cc index 8675172..a632bc8 100644 --- a/icing/index/numeric/integer-index-storage_test.cc +++ b/icing/index/numeric/integer-index-storage_test.cc @@ -57,6 +57,7 @@ using ::testing::IsFalse; using ::testing::IsTrue; using ::testing::Key; using ::testing::Le; +using ::testing::Lt; using ::testing::Ne; using ::testing::Not; @@ -1428,6 +1429,130 @@ TEST_P(IntegerIndexStorageTest, EqualsDocHitInfo(kDefaultDocumentId, expected_sections)))); } +TEST_P(IntegerIndexStorageTest, IteratorCallStatsMultipleBuckets) { + // We use predefined custom buckets to initialize new integer index storage + // and create some test keys accordingly. + std::vector<Bucket> custom_init_sorted_buckets = { + Bucket(-1000, -100), Bucket(0, 100), Bucket(150, 199), Bucket(200, 300), + Bucket(301, 999)}; + std::vector<Bucket> custom_init_unsorted_buckets = { + Bucket(1000, std::numeric_limits<int64_t>::max()), Bucket(-99, -1), + Bucket(101, 149), Bucket(std::numeric_limits<int64_t>::min(), -1001)}; + ICING_ASSERT_OK_AND_ASSIGN( + std::unique_ptr<IntegerIndexStorage> storage, + IntegerIndexStorage::Create( + filesystem_, working_path_, + Options(std::move(custom_init_sorted_buckets), + std::move(custom_init_unsorted_buckets), + IntegerIndexStorage::kDefaultNumDataThresholdForBucketSplit, + /*pre_mapping_fbv_in=*/GetParam()), + serializer_.get())); + + // Add some keys into sorted buckets [(-1000,-100), (200,300)]. + ICING_ASSERT_OK(storage->AddKeys(/*document_id=*/0, kDefaultSectionId, + /*new_keys=*/{-500})); + ICING_ASSERT_OK(storage->AddKeys(/*document_id=*/1, kDefaultSectionId, + /*new_keys=*/{208})); + ICING_ASSERT_OK(storage->AddKeys(/*document_id=*/2, kDefaultSectionId, + /*new_keys=*/{-200})); + ICING_ASSERT_OK(storage->AddKeys(/*document_id=*/3, kDefaultSectionId, + /*new_keys=*/{-1000})); + ICING_ASSERT_OK(storage->AddKeys(/*document_id=*/4, kDefaultSectionId, + /*new_keys=*/{300})); + ASSERT_THAT(storage->num_data(), Eq(5)); + + // GetIterator for range [INT_MIN, INT_MAX] and Advance all. Those 5 keys are + // in 2 buckets, so we will be inspecting 2 posting lists in 2 blocks. + ICING_ASSERT_OK_AND_ASSIGN( + std::unique_ptr<DocHitInfoIterator> iter1, + storage->GetIterator(/*key_lower=*/std::numeric_limits<int64_t>::min(), + /*key_upper=*/std::numeric_limits<int64_t>::max())); + while (iter1->Advance().ok()) { + // Advance all hits. + } + EXPECT_THAT( + iter1->GetCallStats(), + EqualsDocHitInfoIteratorCallStats( + /*num_leaf_advance_calls_lite_index=*/0, + /*num_leaf_advance_calls_main_index=*/0, + /*num_leaf_advance_calls_integer_index=*/5, + /*num_leaf_advance_calls_no_index=*/0, /*num_blocks_inspected=*/2)); + + // GetIterator for range [-1000, -100] and Advance all. Since we only have to + // read bucket (-1000,-100), there will be 3 advance calls and 1 block + // inspected. + ICING_ASSERT_OK_AND_ASSIGN( + std::unique_ptr<DocHitInfoIterator> iter2, + storage->GetIterator(/*key_lower=*/-1000, /*key_upper=*/-100)); + while (iter2->Advance().ok()) { + // Advance all hits. + } + EXPECT_THAT( + iter2->GetCallStats(), + EqualsDocHitInfoIteratorCallStats( + /*num_leaf_advance_calls_lite_index=*/0, + /*num_leaf_advance_calls_main_index=*/0, + /*num_leaf_advance_calls_integer_index=*/3, + /*num_leaf_advance_calls_no_index=*/0, /*num_blocks_inspected=*/1)); +} + +TEST_P(IntegerIndexStorageTest, IteratorCallStatsSingleBucketChainedBlocks) { + // We use predefined custom buckets to initialize new integer index storage + // and create some test keys accordingly. + ICING_ASSERT_OK_AND_ASSIGN( + std::unique_ptr<IntegerIndexStorage> storage, + IntegerIndexStorage::Create( + filesystem_, working_path_, + Options(IntegerIndexStorage::kDefaultNumDataThresholdForBucketSplit, + /*pre_mapping_fbv_in=*/GetParam()), + serializer_.get())); + + int32_t num_keys_to_add = 800; + ASSERT_THAT(num_keys_to_add, + Lt(IntegerIndexStorage::kDefaultNumDataThresholdForBucketSplit)); + for (int i = 0; i < num_keys_to_add; ++i) { + ICING_ASSERT_OK(storage->AddKeys(/*document_id=*/i, kDefaultSectionId, + /*new_keys=*/{i})); + } + + // Those 800 keys are in 1 single bucket with 3 chained posting lists, so we + // will be inspecting 3 blocks. + int32_t expected_num_blocks_inspected = 3; + + // GetIterator for range [INT_MIN, INT_MAX] and Advance all. + ICING_ASSERT_OK_AND_ASSIGN( + std::unique_ptr<DocHitInfoIterator> iter1, + storage->GetIterator(/*key_lower=*/std::numeric_limits<int64_t>::min(), + /*key_upper=*/std::numeric_limits<int64_t>::max())); + while (iter1->Advance().ok()) { + // Advance all hits. + } + EXPECT_THAT(iter1->GetCallStats(), + EqualsDocHitInfoIteratorCallStats( + /*num_leaf_advance_calls_lite_index=*/0, + /*num_leaf_advance_calls_main_index=*/0, + /*num_leaf_advance_calls_integer_index=*/num_keys_to_add, + /*num_leaf_advance_calls_no_index=*/0, + expected_num_blocks_inspected)); + + // GetIterator for range [1, 1] and Advance all. Although there is only 1 + // relevant data, we still have to inspect the entire bucket and its posting + // lists chain (which contain 3 blocks and 800 data). + ICING_ASSERT_OK_AND_ASSIGN( + std::unique_ptr<DocHitInfoIterator> iter2, + storage->GetIterator(/*key_lower=*/1, /*key_upper=*/1)); + while (iter2->Advance().ok()) { + // Advance all hits. + } + EXPECT_THAT(iter2->GetCallStats(), + EqualsDocHitInfoIteratorCallStats( + /*num_leaf_advance_calls_lite_index=*/0, + /*num_leaf_advance_calls_main_index=*/0, + /*num_leaf_advance_calls_integer_index=*/num_keys_to_add, + /*num_leaf_advance_calls_no_index=*/0, + expected_num_blocks_inspected)); +} + TEST_P(IntegerIndexStorageTest, SplitBuckets) { int32_t custom_num_data_threshold_for_bucket_split = 300; |