-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_read_test.cpp
More file actions
77 lines (63 loc) · 2.04 KB
/
Copy pathblock_read_test.cpp
File metadata and controls
77 lines (63 loc) · 2.04 KB
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
#include "file_op.h"
#include "index_handle.h"
#include <sstream>
using namespace std;
using namespace qiniu;
const static largefile::MMapOption mmap_option = { 1024000, 4096, 4096 };
const static uint32_t main_block_size = 1024 * 1024 * 64;//主块文件的大小,64MB
const static uint32_t bucket_size = 1000;//哈希桶的大小
int main(int argc, char** argv) {
uint32_t block_id;
string main_block_path;
string index_path;
int32_t ret = largefile::TFS_SUCCESS;
cout << "Type your block id: " << endl;
cin >> block_id;
if (block_id < 1) {
cerr << "无效的block id" << endl;
exit(-1);
}
//1.加载索引文件
largefile::IndexHandler* index_handler = new largefile::IndexHandler(".", block_id);
if (debug) printf("Load index ..\n");
ret = index_handler->load(block_id, bucket_size, mmap_option);
if (ret != largefile::TFS_SUCCESS) {
fprintf(stderr, "Load index: %d failed\n", block_id);
delete index_handler;
exit(-1);
}
//2. 读取文件meta info
uint64_t file_id = 0;
cout << "Type your file ID: " << endl;
cin >> file_id;
if (file_id < 1) {
cerr << "Invalid file id" << endl;
exit(-2);
}
largefile::MetaInfo meta;
ret = index_handler->read_segment_meta(file_id, meta);
if (ret != largefile::TFS_SUCCESS) {
fprintf(stderr, "read_segement_meta error, file_id: %lu, ret: %d\n", file_id, ret);
exit(-3);
}
//3.根据meta info读取文件
stringstream tmp_stream;
tmp_stream << "." << largefile::MAINBLOCK_DIR_PREFIX << block_id;
tmp_stream >> main_block_path;
largefile::FileOperation* main_block = new largefile::FileOperation(main_block_path, O_RDWR);
char *buffer = new char[meta.get_size()+1];
ret = main_block->pRead_file(buffer, meta.get_size(), meta.get_offset());
if (ret != largefile::TFS_SUCCESS) {
fprintf(stderr, "Read from main block %s failed. Reason: %s\n", main_block_path.c_str(), strerror(errno));
main_block->close_file();
delete index_handler;
delete main_block;
exit(-3);
}
buffer[meta.get_size()] = '\0';
printf("read: %s\n", buffer);
main_block->close_file();
delete main_block;
delete index_handler;
return 0;
}