-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cpp
More file actions
73 lines (73 loc) · 2.05 KB
/
Copy pathBlock.cpp
File metadata and controls
73 lines (73 loc) · 2.05 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
#include "Block.h"
Block :: Block():filename(""),file(NULL),dirty(false),\
pin(false),offsetNum(-1),UsingSize(0),\
time(0),next(NULL),pre(NULL),end(false),data(new char[BLOCK_SIZE]()){};
Block :: ~Block(){
if ( this->dirty && this->offsetNum != -1 ) WriteBack();
delete data;
}
void Block :: clear(){
memset(data, 0, BLOCK_SIZE);
dirty = false;
file = NULL;
next = NULL;
offsetNum = -1;
pre = NULL;
time = 0;
UsingSize = 0;
}
void Block :: SetPin(){
pin = true;
}
void Block :: ClearPin(){
pin = false;
}
void Block :: SetDirty(){
dirty = true;
}
void Block :: ClearDirty(){
dirty = false;
}
void Block :: SetClock(){
time = clock()*1.0 / 1000 ;
}
void Block :: ReadIn(){
string filename = this->filename;
string filetype = ( this->file->type?"index" : "record");
string fpath = "../data/"+filetype+"/"+filename+".db";
fstream file(fpath, ios::in | ios::out | ios :: binary);
int offset = this->offsetNum * BLOCK_SIZE;
file.seekp(offset, ios::beg);
if( this->data == NULL )
this->data = new char[BLOCK_SIZE]();
file.read( this->data, BLOCK_SIZE );
if( file.eof() ) this->end = true;
UsingSize = file.gcount();
file.close();
}
void Block :: WriteBack(){
string filename = this->filename;
string filetype = ( this->file->type?"index" : "record" );
string fpath = "../data/"+filetype+"/"+filename+".db";
fstream file(fpath, ios::in | ios::out | ios :: binary);
int offset = this->offsetNum * BLOCK_SIZE;
file.seekp(offset, ios::beg);
file.write( this->data, this->UsingSize );
file.close();
}
void Block :: SetUsingSize( int size ){
UsingSize += size;
}
void Block :: write(int offset, const char * data, int length){
memcpy( this->data+offset , data, length);
SetDirty();
}
char * Block :: FetchRecord( int index , int size ){
char * ret = new char[size];
int offset = index * ( size + 1 );
memcpy(ret , data + offset , size);
return ret;
}
char * Block :: GetContent(){
return this->data;
}