-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathArcMap.pas
283 lines (242 loc) · 6.16 KB
/
ArcMap.pas
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
unit ArcMap;
interface
uses
typex, sharedobject, queuestream, helpers_stream, sysutils, classes, debug, virtualdiskconstants, betterobject;
type
TArcMapEntry = packed record
private
_logid: int64;
check: int64;
procedure SetLogID(const Value: int64);
function GetLogID: int64;
public
property logid:int64 read GetLogID write SetLogID;
end;
TlocalfileStream = TUnbufferedFileStream;
TArcMap = class(TSharedObject)
private
FFileName: string;
procedure SetFileName(value: string);
function GetEntryAddress(idx: int64): int64;
//arcvat is a list of the fileids and final blocks associated with each ARC zone.
protected
FCachedNextID: int64;
fs: TUnbufferedFileStream;
public
shutdownban: boolean;
procedure Init;override;//
procedure Detach;override;
function GetEntry(idx: int64): TArcMapEntry; //
procedure PutEntry(idx: int64; ent: TArcMApEntry; bIKnowWhatImDoing: boolean);
property FileName: string read FFileName write SetFileName;
function CalculateChecksum: int64;
function CalculateZoneChecksum(zonebase: int64; zonesize: int64): int64;
function CalculateZoneChecksum2(zonebase: int64; zonesize: int64): int64;
procedure Reset;
procedure ResetZone(zonebase, zonelength: int64; bProvisioned: boolean);//
procedure IncrementZoneRev(revidx: int64);
procedure IncrementZoneRevForStartingBlock(block: int64);
procedure SetPerfNodeParentId(id: ni);
end;
implementation
{ TArcMap }
function TArcMap.CalculateChecksum: int64;
var
l: int64;
begin
lock;
try
result := Stream_CalculateChecksum(fs);
finally
unlock;
end;
end;
function TArcMap.CalculateZoneChecksum(zonebase, zonesize: int64): int64;
var
l: int64;
begin
lock;
try
result := Stream_CalculateChecksum(fs, zonebase*sizeof(TArcMapEntry), zonesize*sizeof(TArcMapEntry));
debug.log('zone checksum '+inttohex(zonebase,8)+'='+result.tohexstring);
finally
unlock;
end;
end;
function TArcMap.CalculateZoneChecksum2(zonebase, zonesize: int64): int64;
var
r: TArcMapEntry;
begin
Lock;
try
result := 0;
while zonesize > 0 do begin
r := GetEntry(zonebase);
result := result + r.logid;
dec(zonesize);
inc(zonebase);
end;
finally
Unlock;
end;
end;
procedure TArcMap.Detach;
begin
fs.Free;
fs := nil;
inherited;
end;
function TArcMap.GetEntry(idx: int64): TArcMapEntry;
var
addr: int64;
bCreate: boolean;
r: TArcMapEntry;
begin
Lock;
try
addr := GetentryAddress(idx);
bCreate := fs.Size < (addr+sizeof(result));
if bCreate then begin
stream_Grow(fs, (((addr+sizeof(result)) div MEGA)+1)*(MEGA));
end;
fs.Seek(addr, soBeginning);
stream_GuaranteeRead(fs, pbyte(@r), sizeof(r));
// if r.logid < -1 then begin
// Debug.Log('Logid < -1 was stored in arcmap! forcing refresh');
// bCreate := true;
// end;
result := r;
if bCreate then begin
result.logid := 0;
PutEntry(idx, result, true);
end;
finally
Unlock;
end;
end;
function TArcMap.GetEntryAddress(idx: int64): int64;
begin
result := sizeof(TArcMapEntry) * idx;
end;
procedure TArcMap.IncrementZoneRev(revidx: int64);
var
ent: TArcMapEntry;
begin
lock;
try
if fs = nil then begin
Debug.Log('Arc Map filestream is nil, halting system in 30 seconds.');
sleep(30000);
halt;
end;
ent := GetEntry(revidx);
ent.logid := ent.logid + 1;
PutEntry(revidx, ent, true);
finally
unlock;
end;
end;
procedure TArcMap.IncrementZoneRevForStartingBlock(block: int64);
begin
IncrementZoneRev(block shr ARC_ZONE_BLOCK_SHIFT);
end;
procedure TArcMap.Init;
begin
inherited;
FCachedNextID := -1;
end;
procedure TArcMap.PutEntry(idx: int64; ent: TArcMApEntry; bIKnowWhatImDoing: boolean);
var
addr: int64;
oldent: TArcMapEntry;
begin
if shutdownban then
Debug.Log('shutdown! putEntry is banned!');
if (idx >= MAX_ZONES) or (idx < 0) then
raise ECritical.create('zone index not valid!'+idx.tostring+' '+idx.tohexstring);
Lock;
try
oldEnt := GetEntry(idx);
addr := GetEntryAddress(idx);
stream_Grow(fs, addr);
fs.Seek(addr, soBeginning);
if (oldEnt.logid > ent.logid) and (ent.logid >=0) then
Debug.Log('Cannot decrease log id!');
if (ent.logid > oldent.logid) or (ent.logid < 0) then begin
//Debug.Log('Idx '+idx.tohexstring+' changes '+oldent.logid.tostring+'->'+ent.logid.tostring);
stream_GuaranteeWrite(fs, pbyte(@ent), sizeof(ent));
end;
//fs.CheckFlush;
finally
unlock;
end;
end;
procedure TArcMap.Reset;
var
sFile: string;
begin
Lock;
try
sFile := fs.FileName;
fs.free;
fs := nil;
DeleteFile(sFile);
fs := TUnbufferedFileStream.Create(sFile, fmCreate, fmShareExclusive);
finally
Unlock;
end;
end;
procedure TArcMap.ResetZone(zonebase: int64; zonelength: int64; bProvisioned: boolean);
var
sFile: string;
arcmap: TArcMapEntry;
begin
Lock;
try
sFile := fs.FileName;
fs.GrowFile(zonebase*SizeOf(TArcMapEntry));
if bProvisioned then
arcmap.SetLogID(1)
else
arcmap.SetLogID(0);
fs.seek(zonebase*sizeof(TArcmapEntry), soBeginning);
Stream_GuaranteeWrite(fs, @arcmap, sizeof(arcmap));
finally
Unlock;
end;
end;
procedure TArcMap.SetFileName(value: string);
begin
fFileName := value;
if (value <> FFileName) or (fs=nil) then begin
fs.free;
fs := nil;
if not fileexists(fFileName) then begin
forcedirectories(extractfilepath(FFileName));
fs := TLocalFileStream.create(FFileName, fmCreate,0,0);
end else begin
fs := TLocalFileStream.create(FFileName, fmOpenReadWRite+fmShareExclusive,0,0);
if fs.size > sizeof(TArcMapEntry)*MAX_ZONES then
fs.Size := sizeof(TArcMapEntry)*MAX_ZONES;
end;
end;
end;
procedure TArcMap.SetPerfNodeParentId(id: ni);
begin
if fs <> nil then
fs.phIO.desc.left := id;
end;
{ TArcMapEntry }
function TArcMapEntry.GetLogID: int64;
begin
if check = (int64(_logid) xor int64($FFFFFFFFFFFFFFFF)) then
result := _logid
else
result := 0;
end;
procedure TArcMapEntry.SetLogID(const Value: int64);
begin
_logid := value;
check := value xor int64($FFFFFFFFFFFFFFFF);
end;
end.