1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "index/id_map.h"
18#include <gtest/gtest.h>
19using namespace proxima::be;
20using namespace proxima::be::index;
21
22
23class IDMapTest : public testing::Test {
24 protected:
25 void SetUp() {
26 char cmd_buf[100];
27 snprintf(cmd_buf, 100, "rm -rf ./data.id");
28 system(cmd_buf);
29 }
30
31 void TearDown() {}
32};
33
34TEST_F(IDMapTest, TestGeneral) {
35 auto id_map = IDMap::Create("collection_test", "./");
36 ASSERT_NE(id_map, nullptr);
37
38 ReadOptions read_options;
39 read_options.use_mmap = true;
40 read_options.create_new = true;
41 int ret = id_map->open(read_options);
42 ASSERT_EQ(ret, 0);
43
44 for (size_t i = 0; i < 20000; i++) {
45 ret = id_map->insert(i, i);
46 ASSERT_EQ(ret, 0);
47 }
48
49 for (size_t i = 0; i < 20000; i++) {
50 ASSERT_EQ(id_map->has(i), true);
51 idx_t doc_id = id_map->get_mapping_id(i);
52 ASSERT_EQ(doc_id, i);
53 }
54
55 ret = id_map->close();
56 ASSERT_EQ(ret, 0);
57
58 ret = id_map->open(read_options);
59 ASSERT_EQ(ret, 0);
60
61 for (size_t i = 0; i < 20000; i++) {
62 ASSERT_EQ(id_map->has(i), true);
63 idx_t doc_id = id_map->get_mapping_id(i);
64 ASSERT_EQ(doc_id, i);
65 }
66
67 for (size_t i = 0; i < 10000; i++) {
68 id_map->remove(i);
69 }
70
71 for (size_t i = 0; i < 10000; i++) {
72 ASSERT_EQ(id_map->has(i), false);
73 }
74
75 ret = id_map->close();
76 ASSERT_EQ(ret, 0);
77
78 ret = id_map->open(read_options);
79 ASSERT_EQ(ret, 0);
80
81 for (size_t i = 0; i < 10000; i++) {
82 ASSERT_EQ(id_map->has(i), false);
83 }
84
85 for (size_t i = 10000; i < 20000; i++) {
86 ASSERT_EQ(id_map->has(i), true);
87 idx_t doc_id = id_map->get_mapping_id(i);
88 ASSERT_EQ(doc_id, i);
89 }
90}
91