-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_iter.cpp
229 lines (167 loc) · 6.76 KB
/
block_iter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// Created by bokket on 2024/2/20.
//
#include <memory>
#include "block_iter.h"
using namespace bokket;
Block::Block(std::string_view content)
:data_(content)
,size_(content.size())
{
size_t restarts_len_offset=size_- sizeof(int32_t);
LOG_INFO("{}",std::string_view {data_.data()+restarts_len_offset,4});
num_restart_=DecodeFixed32(data_.data()+restarts_len_offset);
assert(size_>=4*(num_restart_+1));
data_end_=data_.data()+size_-4*(num_restart_+1);
//data_end_=std::string_view { data_.data(),size_-4*(num_restart_+1)}.data();
// ,std::string_view { data_end_,static_cast<std::string_view::size_type>(size_-4*(num_restart_+1))}
LOG_INFO("block_iter size_:{},num_restart_:{},data_:{}",size_,num_restart_,std::string_view { data_.data(),size_-4*(num_restart_+1)});
}
Block::Iter Block::find(std::string_view key) {
auto it= lower_bound(key);
return Compare(it.key(),key)!=0 ? end() : it;
}
Block::Iter Block::begin() const {
LOG_INFO("{}",data_.data());
return Iter(this,data_.data(),0);
}
Block::Iter Block::end() const {
return Iter(this,data_end_,0);
}
int32_t Block::restartPoint(int id) const {
assert(id<=num_restart_);
int32_t restart_len=size_-4*(num_restart_-id+1);
int32_t r= DecodeFixed32(data_.data()+restart_len);
LOG_INFO("r:{}",r);
assert(r<=data_end_-data_.data());
return r;
}
std::string_view Block::keyAtRestartPoint(int id) const {
int32_t shared,unshared,value_len;
int32_t pos= restartPoint(id);
LOG_INFO("keyAt:{} id:{}",pos,id);
std::string_view buf{data_.data()+pos,static_cast<std::string_view::size_type>(data_end_-data_.data()-pos) };
shared=DecodeFixed32(buf.data());
assert(shared==0);
unshared= DecodeFixed32(buf.data()+sizeof(int32_t));
value_len= DecodeFixed32(buf.data()+sizeof(int32_t)*2);
LOG_INFO("buf:{} shared:{} unshared:{} value_len:{} | {}",buf,shared,unshared,value_len,std::string_view {buf.data()+sizeof(int32_t)*3,static_cast<std::string_view::size_type>(unshared)});
return std::string_view {buf.data()+sizeof(int32_t)*3,static_cast<std::string_view::size_type>(unshared)};
}
Block::Iter Block::lower_bound(std::string_view key) const {
int left=0,mid=left;
int right=num_restart_;
while(left+1<right) {
mid=(left+right)>>1;
LOG_INFO("mid key:{}",keyAtRestartPoint(mid));
LOG_INFO("left:{} right:{} mid:{} compare:{}",left,right,mid,Compare(keyAtRestartPoint(mid),key));
if(Compare(keyAtRestartPoint(mid),key)>=0) {
right=mid;
} else {
left=mid;
}
LOG_INFO("left:{} right:{} mid:{}",left,right,mid);
}
if(Compare(keyAtRestartPoint(left),key)>0) {
assert(left==0 && right==left+1);
return end();
}
int32_t pos= restartPoint(left);
LOG_INFO("left:{} pos:{}",left,pos);
auto it=Iter(this,data_.data()+pos,pos);
for(;it!=end();it++) {
// LOG_INFO("it.key:{} | key:{}",it.key(),key);
auto res=Compare(it.key(),key);
LOG_INFO("res:{} it.key():{} it.value:{}",res,it.key(),it.value());
if(res>=0)
break;
}
return it;
}
BlockConstIter::BlockConstIter(const Block *container, const char *p, int32_t restart)
:last_key_{nullptr}
,container_{container}
,restarts_block_idx_(restart)
{
init(p);
}
void BlockConstIter::increment() {
// const char* last_key=cur_key_.data();
//last_key_=Buf(cur_key_).data();
std::string tmp=cur_key_.data();
last_key_=std::move(tmp.data());
LOG_INFO("increment last_key_:{}",last_key_);
std::string_view buf{buf_.data()+ unshared_key_len_+value_len_,buf_len_-unshared_key_len_-value_len_-shared_key_len_};
LOG_INFO("increment:{} | {} | cur_len:{}",buf_,shared_key_len_,buf_len_-unshared_key_len_-value_len_);
//buf_=buf;
cur_key_.clear();
init(buf.data());
}
bool BlockConstIter::equal(const BlockConstIter &other) const {
return buf_==other.buf_ && buf_len_==other.buf_len_;
}
void BlockConstIter::init(const char *p) {
auto len=container_->data_end_-p;
// LOG_INFO("BlockConstIter::init len:{}",len);
assert(len>=0);
if(len>0) {
shared_key_len_=DecodeFixed32(p);
unshared_key_len_= DecodeFixed32(p+sizeof(int32_t));
value_len_= DecodeFixed32(p+sizeof(int32_t)*2);
//static_cast<std::string_view::size_type>(len-sizeof(int32_t)*3)
buf_=std::string_view {p+sizeof(int32_t)*3,static_cast<std::string_view::size_type>(len-sizeof(int32_t)*3)};
buf_len_=len-sizeof(int32_t)*3;
LOG_INFO("cur_entry:{}",cur_entry_);
cur_entry_=std::string_view {p+sizeof(int32_t)*3,static_cast<std::string_view::size_type>(unshared_key_len_+value_len_)};;
//buf_=buf_.data()+shared_key_len_+unshared_key_len_+value_len_;
// LOG_INFO("buf_:{} | buf_len_:{} |",buf_.data(),buf_len_);
LOG_INFO("len:{} | shared_key_len_:{} | unshared_key_len_:{} | value_len_:{} |",len,shared_key_len_,unshared_key_len_,value_len_);
//cur_key_
if(last_key_!= nullptr) {
LOG_INFO("last_key:{}| cur_entry:{} | cur_key:{}",last_key_,cur_entry_,cur_key_);
}
assert(cur_key_.empty());
cur_key_.reserve(shared_key_len_+unshared_key_len_);
cur_key_.append(last_key_,shared_key_len_);
cur_key_.append(cur_entry_.data(),unshared_key_len_);
// LOG_INFO("cur_key_",cur_key_);
if(unshared_key_len_==0) {
restarts_block_idx_=static_cast<int32_t>(p-container_->data_.data());
LOG_INFO("restart_index:{}",restarts_block_idx_);
}
} else {
buf_=p;
buf_len_=0;
}
}
std::string_view BlockConstIter::key() const {
assert(buf_.data()!=container_->data_end_);
if(!cur_key_.empty())
return cur_key_;
// if(last_key_!= nullptr) {
// auto it=BlockConstIter(container_,container_->data_.data()+restarts_block_idx_,restarts_block_idx_);
// while(it.buf_<buf_) {
// it++;
//
//
// LOG_INFO("buf_:{}",it.buf_);
// }
//
// assert(it.buf_==buf_);
// }
//
// //cur_key_
// if(last_key_!= nullptr) {
// LOG_INFO("last_key:{}",last_key_);
// }
// assert(cur_key_.empty());
// cur_key_.reserve(shared_key_len_+unshared_key_len_);
// cur_key_.append(last_key_,shared_key_len_);
// cur_key_.append(buf_.data(),unshared_key_len_);
// cur_key_.append(cur_entry_.data(),unshared_key_len_);
return cur_key_;
}
std::string_view BlockConstIter::value() const {
// return {buf_.data()+unshared_key_len_,static_cast<std::string_view::size_type>(value_len_)};
return {cur_entry_.data()+unshared_key_len_,static_cast<std::string_view::size_type>(value_len_)};
}