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/persist_hash_map.h"
18#include <ailego/utility/time_helper.h>
19#include <gtest/gtest.h>
20#include "index/snapshot.h"
21using namespace proxima::be;
22using namespace proxima::be::index;
23
24
25class PersistHashMapTest : public testing::Test {
26 protected:
27 void SetUp() {
28 char cmd_buf[100];
29 snprintf(cmd_buf, 100, "rm -rf ./idmap");
30 system(cmd_buf);
31 }
32
33 void TearDown() {}
34};
35
36TEST_F(PersistHashMapTest, TestGeneral) {
37 ReadOptions read_options;
38 read_options.use_mmap = true;
39 read_options.create_new = true;
40
41 SnapshotPtr snapshot;
42 int ret = Snapshot::CreateAndOpen("./idmap", FileID::ID_FILE, read_options,
43 &snapshot);
44 ASSERT_EQ(ret, 0);
45
46 PersistHashMap<uint64_t, idx_t> id_map;
47
48 ret = id_map.mount(snapshot->data());
49 ASSERT_EQ(ret, 0);
50
51 ASSERT_EQ(0, id_map.reserve(50000));
52
53 for (size_t i = 0; i < 20000; i++) {
54 ret = id_map.emplace(i, i);
55 ASSERT_EQ(ret, 0);
56 }
57
58 size_t size = id_map.size();
59 ASSERT_EQ(size, 20000);
60
61 for (size_t i = 0; i < 20000; i++) {
62 ASSERT_EQ(id_map.has(i), true);
63 idx_t doc_id;
64 ASSERT_EQ(0, id_map.get(i, &doc_id));
65 ASSERT_EQ(doc_id, i);
66 }
67
68 id_map.unmount();
69
70 ret = id_map.mount(snapshot->data());
71 ASSERT_EQ(ret, 0);
72
73 size = id_map.size();
74 ASSERT_EQ(size, 20000);
75
76 for (size_t i = 0; i < 20000; i++) {
77 ASSERT_EQ(id_map.has(i), true);
78 idx_t doc_id;
79 ASSERT_EQ(0, id_map.get(i, &doc_id));
80 ASSERT_EQ(doc_id, i);
81 }
82
83 for (size_t i = 0; i < 10000; i++) {
84 ret = id_map.erase(i);
85 ASSERT_EQ(ret, 0);
86 }
87
88 for (size_t i = 0; i < 10000; i++) {
89 ASSERT_EQ(id_map.has(i), false);
90 }
91
92 for (size_t i = 20000; i < 50000; i++) {
93 ret = id_map.emplace(i, i);
94 ASSERT_EQ(ret, 0);
95 }
96
97 id_map.unmount();
98
99 ret = id_map.mount(snapshot->data());
100 ASSERT_EQ(ret, 0);
101
102 size = id_map.size();
103 ASSERT_EQ(size, 40000);
104
105 for (size_t i = 0; i < 10000; i++) {
106 ASSERT_EQ(id_map.has(i), false);
107 }
108
109 for (size_t i = 10000; i < 50000; i++) {
110 ASSERT_EQ(id_map.has(i), true);
111 idx_t doc_id;
112 ASSERT_EQ(0, id_map.get(i, &doc_id));
113 ASSERT_EQ(doc_id, i);
114 }
115
116 for (size_t i = 40000; i < 50000; i++) {
117 int ret = id_map.emplace_or_assign(i, i + 1);
118 ASSERT_EQ(ret, 0);
119 }
120
121 for (size_t i = 40000; i < 50000; i++) {
122 idx_t doc_id;
123 ASSERT_EQ(0, id_map.get(i, &doc_id));
124 ASSERT_EQ(doc_id, i + 1);
125 }
126}
127