-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.cpp
154 lines (145 loc) · 5.24 KB
/
blockchain.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
//
// Created by lenovo on 2021/1/19.
//
#include "blockchain.h"
#include <cstring>
element::element(const int &offset_, const string &key_) {
offset=offset_;
strcpy(key,key_.c_str());
}
bool element::operator<(const element &a) const {
return strcmp(key,a.key)<0;
//return key<a.key;
}
element & element::operator=(const element &right) {
if (this==&right) { return *this; }
offset=right.offset;
string temp=right.key;
strcpy(key,temp.c_str());
return *this;
}
//block::block(int pre_, int nxt_, int numofelement_) {
// pre=pre_,nxt=nxt_,numofelment=numofelement_;
//
//}
block::block() {
pre=-1;
nxt=-1;
numofelment=0;
}
block & block::operator=(const block &right) {
if (this==&right) return *this;
pre=right.pre;
nxt=right.nxt;
numofelment=right.numofelment;
for (int i = 0; i <right.numofelment ; ++i) {
array[i]=right.array[i];
}
for (int i = right.numofelment; i < MAXN_NUM_OF_BLOCK; ++i) {
array[i]=element();
}
return *this;
}
UnrolledLinkedList::UnrolledLinkedList(const string &name): filename(name){
//cout<<"constructer in blocklist"<<endl;
f1.open(filename,ios_base::binary|ios::in | ios::out);
if (!f1){
//cout<<"8888"<<endl;
f1.open(filename,ios::out|ios::binary);
// f1.seekg(0,ios::end);
//cout<<"constructer tellg end"<<" "<<f1.tellg()<<endl;//输出为-1????
}
f2.open(filename,ios_base::binary|ios::in | ios::out);
if (!f2){
f2.open(filename,ios::out|ios::binary);
}
f3.open(filename,ios_base::binary|ios::in | ios::out);
if (!f3){
f3.open(filename,ios::out|ios::binary);
}
f4.open(filename,ios_base::binary|ios::in | ios::out);
if (!f4){
f4.open(filename,ios::out|ios::binary);
}
f5.open(filename,ios_base::binary|ios::in | ios::out);
if (!f5){f5.open(filename,ios::out|ios::binary);}
f6.open(filename,ios_base::binary|ios::in | ios::out);
if (!f6){f6.open(filename,ios::out|ios::binary);}
// cout<<f1.fail()<<" "<<f2.fail()<<"in blocklist constructer"<<endl;
f1.close(),f2.close(),f3.close(),f4.close(),f5.close(),f6.close();
}
UnrolledLinkedList::~UnrolledLinkedList() {
f1.close(),f2.close(),f3.close();
f4.close(),f5.close(),f6.close();
}
void UnrolledLinkedList::splitblock(const int offset)//this offset means the location in the file(倒排文档的绝对位置)
{f1.clear(),f2.clear(),f3.clear(),f4.clear(),f5.clear(),f6.clear();
block tempblock;
f1.open(filename,ios_base::binary|ios::in | ios::out);
if (!f1){
f1.open(filename,ios::out|ios::binary);
}
f2.open(filename,ios_base::binary|ios::in | ios::out);
if (!f2){f2.open(filename,ios::out|ios::binary);}
f3.open(filename,ios_base::binary|ios::in | ios::out);
if (!f3){f3.open(filename,ios::out|ios::binary);}
f4.open(filename,ios_base::binary|ios::in | ios::out);
if (!f4){f4.open(filename,ios::out|ios::binary);}
f5.open(filename,ios_base::binary|ios::in | ios::out);
if (!f5){f5.open(filename,ios::out|ios::binary);}
f6.open(filename,ios_base::binary|ios::in | ios::out);
if (!f6){f6.open(filename,ios::out|ios::binary);}
//cout<<f1.fail()<<" "<<f2.fail()<<" "<<f3.fail()<<" "<<f4.fail()<<" "<<f5.fail()<<" "<<f6.fail()<<endl;
f1.seekg(offset);
f1.read(reinterpret_cast<char*>(&tempblock),sizeof (block));
// tempblock
// tempblock.setnum()
tempblock.setpre(offset);
// tempblock.nxt=-1;
f1.close();
f2.seekg(offset+8);
int temp;
f2.read(reinterpret_cast<char*>(&temp),4);
f2.close();
f3.seekg(0,ios::end);
// cout<<f3.fail()<<" debug in split"<<endl;
int endlocation=f3.tellg();
f3.close();
for (int i = 0; i <temp-SPLIT_NUM_LEFT; ++i) {
tempblock.array[i]=tempblock.array[i+SPLIT_NUM_LEFT];
}
for (int i = temp-SPLIT_NUM_LEFT; i <tempblock.numofelment ; ++i) {
tempblock.array[i]=element();
}
tempblock.numofelment-=SPLIT_NUM_LEFT;//SPLIT_NUM意思是剩下的元素数量
f6.seekg(offset);
f6.write(reinterpret_cast<char*>(&endlocation),4);
f6.close();
f5.seekg(offset+8);
int num=SPLIT_NUM_LEFT;
f5.write(reinterpret_cast<char*>(&num),4);
f5.seekg(endlocation);
f5.write(reinterpret_cast<char*>(&tempblock),sizeof (block));
f1.close(),f2.close(),f3.close();
f4.close(),f5.close(),f6.close();
}
void UnrolledLinkedList::mergeblock(int offset1, int offset2)
{
f1.open(filename,ios::in | ios::out|ios::binary);
f2.open(filename,ios::in | ios::out|ios::binary);
f3.open(filename,ios::in | ios::out|ios::binary);
f4.open(filename,ios::in | ios::out|ios::binary);
block tempblock1,tempblock2;
f1.seekg(offset1);
f1.read(reinterpret_cast<char*>(&tempblock1),sizeof (block));
f1.seekg(offset2);
f1.read(reinterpret_cast<char*>(&tempblock2),sizeof (block));
tempblock1.nxt=tempblock2.nxt;
tempblock1.numofelment+=tempblock2.numofelment;
for (int i = tempblock1.numofelment-tempblock2.numofelment; i <tempblock1.numofelment ; ++i) {
tempblock1.array[i]=tempblock2.array[i-(tempblock1.numofelment-tempblock2.numofelment)];
}
f3.seekg(offset1);
f3.write(reinterpret_cast<char*>(&tempblock1),sizeof (block));
f1.close(),f2.close(),f3.close(),f4.close();
}