Revision 039a89e5 trunk/Pithos.Core/Agents/StatusAgent.cs
b/trunk/Pithos.Core/Agents/StatusAgent.cs | ||
---|---|---|
42 | 42 |
ActiveRecordStarter.CreateSchema(); |
43 | 43 |
} |
44 | 44 |
|
45 |
|
|
46 |
|
|
45 | 47 |
private static InPlaceConfigurationSource GetConfiguration(string pithosDbPath) |
46 | 48 |
{ |
47 | 49 |
if (String.IsNullOrWhiteSpace(pithosDbPath)) |
... | ... | |
174 | 176 |
|
175 | 177 |
private int UpdateStatusDirect(Guid id, FileStatus status) |
176 | 178 |
{ |
177 |
try
|
|
179 |
using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
|
|
178 | 180 |
{ |
179 | 181 |
|
180 |
var connectionString = GetConnectionString(); |
|
181 |
using (var connection = new SQLiteConnection(connectionString)) |
|
182 |
using (var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where Id = :id ", |
|
183 |
connection)) |
|
182 |
try |
|
184 | 183 |
{ |
185 | 184 |
|
185 |
using (var connection = GetConnection()) |
|
186 |
using ( |
|
187 |
var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where Id = :id ", |
|
188 |
connection)) |
|
189 |
{ |
|
190 |
command.Parameters.AddWithValue("fileStatus", status); |
|
191 |
|
|
192 |
command.Parameters.AddWithValue("id", id); |
|
193 |
|
|
194 |
var affected = command.ExecuteNonQuery(); |
|
195 |
|
|
196 |
return affected; |
|
197 |
} |
|
186 | 198 |
|
187 |
command.Parameters.AddWithValue("fileStatus", status); |
|
188 |
|
|
189 |
command.Parameters.AddWithValue("id", id); |
|
190 |
connection.Open(); |
|
191 |
var affected = command.ExecuteNonQuery(); |
|
192 |
return affected; |
|
199 |
} |
|
200 |
catch (Exception exc) |
|
201 |
{ |
|
202 |
Log.Error(exc.ToString()); |
|
203 |
throw; |
|
193 | 204 |
} |
194 | 205 |
} |
195 |
catch (Exception exc) |
|
196 |
{ |
|
197 |
Log.Error(exc.ToString()); |
|
198 |
throw; |
|
199 |
} |
|
200 |
|
|
201 | 206 |
} |
202 | 207 |
|
203 | 208 |
private int UpdateStatusDirect(string path, FileStatus status) |
204 | 209 |
{ |
205 |
try
|
|
210 |
using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
|
|
206 | 211 |
{ |
207 | 212 |
|
208 |
var connectionString = GetConnectionString(); |
|
209 |
using (var connection = new SQLiteConnection(connectionString)) |
|
210 |
using (var command = new SQLiteCommand("update FileState set FileStatus= :fileStatus where FilePath = :path ", |
|
211 |
connection)) |
|
213 |
try |
|
212 | 214 |
{ |
215 |
|
|
213 | 216 |
|
217 |
using (var connection = GetConnection()) |
|
218 |
using ( |
|
219 |
var command = |
|
220 |
new SQLiteCommand("update FileState set FileStatus= :fileStatus where FilePath = :path ", |
|
221 |
connection)) |
|
222 |
{ |
|
214 | 223 |
|
215 |
command.Parameters.AddWithValue("fileStatus", status); |
|
216 | 224 |
|
217 |
command.Parameters.AddWithValue("path", path.ToLower()); |
|
218 |
connection.Open(); |
|
219 |
var affected = command.ExecuteNonQuery(); |
|
220 |
return affected; |
|
225 |
command.Parameters.AddWithValue("fileStatus", status); |
|
226 |
|
|
227 |
command.Parameters.AddWithValue("path", path.ToLower()); |
|
228 |
|
|
229 |
var affected = command.ExecuteNonQuery(); |
|
230 |
return affected; |
|
231 |
} |
|
232 |
} |
|
233 |
catch (Exception exc) |
|
234 |
{ |
|
235 |
Log.Error(exc.ToString()); |
|
236 |
throw; |
|
221 | 237 |
} |
222 | 238 |
} |
223 |
catch (Exception exc) |
|
224 |
{ |
|
225 |
Log.Error(exc.ToString()); |
|
226 |
throw; |
|
227 |
} |
|
228 |
|
|
229 | 239 |
} |
230 | 240 |
|
231 | 241 |
private int UpdateStatusDirect(string absolutePath, FileStatus fileStatus, FileOverlayStatus overlayStatus) |
232 | 242 |
{ |
233 |
try
|
|
243 |
using (log4net.ThreadContext.Stacks["StatusAgent"].Push("UpdateStatusDirect"))
|
|
234 | 244 |
{ |
235 | 245 |
|
236 |
var connectionString = GetConnectionString(); |
|
237 |
using (var connection = new SQLiteConnection(connectionString)) |
|
238 |
using (var command = new SQLiteCommand("update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path ", |
|
239 |
connection)) |
|
246 |
try |
|
240 | 247 |
{ |
248 |
|
|
241 | 249 |
|
242 |
command.Parameters.AddWithValue("path", absolutePath.ToLower()); |
|
243 |
command.Parameters.AddWithValue("fileStatus", fileStatus); |
|
244 |
command.Parameters.AddWithValue("overlayStatus", overlayStatus); |
|
245 |
connection.Open(); |
|
246 |
var affected = command.ExecuteNonQuery(); |
|
247 |
return affected; |
|
250 |
using (var connection = GetConnection()) |
|
251 |
using ( |
|
252 |
var command = |
|
253 |
new SQLiteCommand( |
|
254 |
"update FileState set OverlayStatus= :overlayStatus, FileStatus= :fileStatus where FilePath = :path ", |
|
255 |
connection)) |
|
256 |
{ |
|
257 |
|
|
258 |
command.Parameters.AddWithValue("path", absolutePath.ToLower()); |
|
259 |
command.Parameters.AddWithValue("fileStatus", fileStatus); |
|
260 |
command.Parameters.AddWithValue("overlayStatus", overlayStatus); |
|
261 |
|
|
262 |
var affected = command.ExecuteNonQuery(); |
|
263 |
return affected; |
|
264 |
} |
|
265 |
} |
|
266 |
catch (Exception exc) |
|
267 |
{ |
|
268 |
Log.Error(exc.ToString()); |
|
269 |
throw; |
|
248 | 270 |
} |
249 | 271 |
} |
250 |
catch (Exception exc) |
|
251 |
{ |
|
252 |
Log.Error(exc.ToString()); |
|
253 |
throw; |
|
254 |
} |
|
255 |
|
|
256 | 272 |
} |
257 | 273 |
|
258 | 274 |
|
... | ... | |
292 | 308 |
private readonly string _pithosDataPath; |
293 | 309 |
|
294 | 310 |
|
311 |
public FileState GetStateByFilePath(string path) |
|
312 |
{ |
|
313 |
if (String.IsNullOrWhiteSpace(path)) |
|
314 |
throw new ArgumentNullException("path"); |
|
315 |
if (!Path.IsPathRooted(path)) |
|
316 |
throw new ArgumentException("The path must be rooted", "path"); |
|
317 |
Contract.EndContractBlock(); |
|
318 |
|
|
319 |
try |
|
320 |
{ |
|
321 |
|
|
322 |
using (var connection = GetConnection()) |
|
323 |
using (var command = new SQLiteCommand("select Id, FilePath, OverlayStatus,FileStatus ,Checksum ,Version ,VersionTimeStamp,IsShared ,SharedBy ,ShareWrite from FileState where FilePath=:path", connection)) |
|
324 |
{ |
|
325 |
|
|
326 |
command.Parameters.AddWithValue("path", path.ToLower()); |
|
327 |
|
|
328 |
using (var reader = command.ExecuteReader()) |
|
329 |
{ |
|
330 |
if (reader.Read()) |
|
331 |
{ |
|
332 |
//var values = new object[reader.FieldCount]; |
|
333 |
//reader.GetValues(values); |
|
334 |
var state = new FileState |
|
335 |
{ |
|
336 |
Id = reader.GetGuid(0), |
|
337 |
FilePath = reader.IsDBNull(1)?"":reader.GetString(1), |
|
338 |
OverlayStatus =reader.IsDBNull(2)?FileOverlayStatus.Unversioned: (FileOverlayStatus) reader.GetInt64(2), |
|
339 |
FileStatus = reader.IsDBNull(3)?FileStatus.Missing:(FileStatus) reader.GetInt64(3), |
|
340 |
Checksum = reader.IsDBNull(4)?"":reader.GetString(4), |
|
341 |
Version = reader.IsDBNull(5)?default(long):reader.GetInt64(5), |
|
342 |
VersionTimeStamp = reader.IsDBNull(6)?default(DateTime):reader.GetDateTime(6), |
|
343 |
IsShared = !reader.IsDBNull(7) && reader.GetBoolean(7), |
|
344 |
SharedBy = reader.IsDBNull(8)?"":reader.GetString(8), |
|
345 |
ShareWrite = !reader.IsDBNull(9) && reader.GetBoolean(9) |
|
346 |
}; |
|
347 |
/* |
|
348 |
var state = new FileState |
|
349 |
{ |
|
350 |
Id = (Guid) values[0], |
|
351 |
FilePath = (string) values[1], |
|
352 |
OverlayStatus = (FileOverlayStatus) (long)values[2], |
|
353 |
FileStatus = (FileStatus) (long)values[3], |
|
354 |
Checksum = (string) values[4], |
|
355 |
Version = (long?) values[5], |
|
356 |
VersionTimeStamp = (DateTime?) values[6], |
|
357 |
IsShared = (long)values[7] == 1, |
|
358 |
SharedBy = (string) values[8], |
|
359 |
ShareWrite = (long)values[9] == 1 |
|
360 |
}; |
|
361 |
*/ |
|
362 |
return state; |
|
363 |
} |
|
364 |
else |
|
365 |
{ |
|
366 |
return null; |
|
367 |
} |
|
368 |
|
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
catch (Exception exc) |
|
373 |
{ |
|
374 |
Log.ErrorFormat(exc.ToString()); |
|
375 |
throw; |
|
376 |
} |
|
377 |
} |
|
378 |
|
|
295 | 379 |
public FileOverlayStatus GetFileOverlayStatus(string path) |
296 | 380 |
{ |
297 | 381 |
if (String.IsNullOrWhiteSpace(path)) |
... | ... | |
302 | 386 |
|
303 | 387 |
try |
304 | 388 |
{ |
305 |
var connectionString = GetConnectionString(); |
|
306 |
using (var connection = new SQLiteConnection(connectionString))
|
|
389 |
|
|
390 |
using (var connection = GetConnection())
|
|
307 | 391 |
using (var command = new SQLiteCommand("select OverlayStatus from FileState where FilePath=:path", connection)) |
308 | 392 |
{ |
309 | 393 |
|
310 | 394 |
command.Parameters.AddWithValue("path", path.ToLower()); |
311 |
connection.Open(); |
|
395 |
|
|
312 | 396 |
var s = command.ExecuteScalar(); |
313 | 397 |
return (FileOverlayStatus) Convert.ToInt32(s); |
314 | 398 |
} |
... | ... | |
322 | 406 |
|
323 | 407 |
private string GetConnectionString() |
324 | 408 |
{ |
325 |
var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N;Pooling=True;Default Isolation Level=ReadCommited", _pithosDataPath); |
|
409 |
var connectionString = String.Format(@"Data Source={0}\pithos.db;Version=3;Enlist=N;Pooling=True;Default Isolation Level=ReadCommited;show_sql=true", _pithosDataPath);
|
|
326 | 410 |
return connectionString; |
327 | 411 |
} |
328 | 412 |
|
413 |
private SQLiteConnection GetConnection() |
|
414 |
{ |
|
415 |
var connectionString = GetConnectionString(); |
|
416 |
var connection = new SQLiteConnection(connectionString); |
|
417 |
connection.Open(); |
|
418 |
using(var cmd =connection.CreateCommand()) |
|
419 |
{ |
|
420 |
cmd.CommandText = "PRAGMA journal_mode=WAL"; |
|
421 |
cmd.ExecuteNonQuery(); |
|
422 |
} |
|
423 |
return connection; |
|
424 |
} |
|
425 |
|
|
329 | 426 |
public void SetFileOverlayStatus(string path, FileOverlayStatus overlayStatus) |
330 | 427 |
{ |
331 | 428 |
if (String.IsNullOrWhiteSpace(path)) |
... | ... | |
366 | 463 |
_persistenceAgent.Post(() => UpdateStatusDirect(path.ToLower(), fileStatus, overlayStatus)); |
367 | 464 |
} |
368 | 465 |
|
466 |
/* |
|
369 | 467 |
public void StoreInfo(string path,ObjectInfo objectInfo) |
370 | 468 |
{ |
371 | 469 |
if (String.IsNullOrWhiteSpace(path)) |
... | ... | |
404 | 502 |
}); |
405 | 503 |
|
406 | 504 |
} |
407 |
public void StoreInfoDirect(string path, ObjectInfo objectInfo) |
|
505 |
*/ |
|
506 |
|
|
507 |
public void StoreInfo(string path, ObjectInfo objectInfo) |
|
408 | 508 |
{ |
409 | 509 |
if (String.IsNullOrWhiteSpace(path)) |
410 | 510 |
throw new ArgumentNullException("path"); |
... | ... | |
414 | 514 |
throw new ArgumentNullException("objectInfo", "objectInfo can't be empty"); |
415 | 515 |
Contract.EndContractBlock(); |
416 | 516 |
|
417 |
_persistenceAgent.Post(() => |
|
418 |
{ |
|
419 |
try |
|
420 |
{ |
|
421 |
|
|
422 |
var connectionString = GetConnectionString(); |
|
423 |
using (var connection = new SQLiteConnection(connectionString)) |
|
424 |
{ |
|
425 |
if (StateExists(path)) |
|
426 |
var command = |
|
427 |
new SQLiteCommand( |
|
428 |
"update FileState set FileStatus= :fileStatus where Id = :id ", |
|
429 |
connection); |
|
430 |
|
|
431 |
|
|
432 |
var filePath = path.ToLower(); |
|
433 |
//Load the existing files state and set its properties in one session |
|
434 |
using (new SessionScope()) |
|
435 |
{ |
|
436 |
//Forgetting to use a sessionscope results in two sessions being created, one by |
|
437 |
//FirstOrDefault and one by Save() |
|
438 |
var state = FileState.FindByFilePath(filePath); |
|
439 |
|
|
440 |
//Create a new empty state object if this is a new file |
|
441 |
state = state ?? new FileState(); |
|
442 |
|
|
443 |
state.FilePath = filePath; |
|
444 |
state.Checksum = objectInfo.Hash; |
|
445 |
state.Version = objectInfo.Version; |
|
446 |
state.VersionTimeStamp = objectInfo.VersionTimestamp; |
|
447 |
|
|
448 |
state.FileStatus = FileStatus.Unchanged; |
|
449 |
state.OverlayStatus = FileOverlayStatus.Normal; |
|
450 |
|
|
451 |
|
|
452 |
//Do the save |
|
453 |
state.Save(); |
|
454 |
} |
|
455 |
|
|
456 |
|
|
457 |
command.Parameters.AddWithValue("fileStatus", status); |
|
458 |
|
|
459 |
command.Parameters.AddWithValue("id", id); |
|
460 |
connection.Open(); |
|
461 |
var affected = command.ExecuteNonQuery(); |
|
462 |
return; |
|
463 |
} |
|
464 |
} |
|
465 |
catch (Exception exc) |
|
466 |
{ |
|
467 |
Log.Error(exc.ToString()); |
|
468 |
throw; |
|
469 |
} |
|
470 |
|
|
471 |
}); |
|
517 |
_persistenceAgent.Post(() => StoreInfoDirect(path, objectInfo)); |
|
472 | 518 |
|
473 | 519 |
} |
474 | 520 |
|
521 |
private void StoreInfoDirect(string path, ObjectInfo objectInfo) |
|
522 |
{ |
|
523 |
try |
|
524 |
{ |
|
525 |
|
|
526 |
using (var connection = GetConnection()) |
|
527 |
using (var command = new SQLiteCommand(connection)) |
|
528 |
{ |
|
529 |
if (StateExists(path, connection)) |
|
530 |
command.CommandText = |
|
531 |
"update FileState set FileStatus= :fileStatus where FilePath = :path "; |
|
532 |
else |
|
533 |
{ |
|
534 |
command.CommandText = |
|
535 |
"INSERT INTO FileState (Id,FilePath,Checksum,Version,VersionTimeStamp,FileStatus,OverlayStatus) VALUES (:id,:path,:checksum,:version,:versionTimeStamp,:fileStatus,:overlayStatus)"; |
|
536 |
command.Parameters.AddWithValue("id", Guid.NewGuid()); |
|
537 |
} |
|
538 |
|
|
539 |
command.Parameters.AddWithValue("path", path.ToLower()); |
|
540 |
command.Parameters.AddWithValue("checksum", objectInfo.Hash); |
|
541 |
command.Parameters.AddWithValue("version", objectInfo.Version); |
|
542 |
command.Parameters.AddWithValue("versionTimeStamp", |
|
543 |
objectInfo.VersionTimestamp); |
|
544 |
command.Parameters.AddWithValue("fileStatus", FileStatus.Unchanged); |
|
545 |
command.Parameters.AddWithValue("overlayStatus", |
|
546 |
FileOverlayStatus.Normal); |
|
547 |
|
|
548 |
var affected = command.ExecuteNonQuery(); |
|
549 |
return; |
|
550 |
} |
|
551 |
} |
|
552 |
catch (Exception exc) |
|
553 |
{ |
|
554 |
Log.Error(exc.ToString()); |
|
555 |
throw; |
|
556 |
} |
|
557 |
} |
|
558 |
|
|
475 | 559 |
private bool StateExists(string filePath,SQLiteConnection connection) |
476 | 560 |
{ |
477 |
using(var command=new SQLiteCommand("Select count(*) from FileState where FilePath=:path",connection))
|
|
561 |
using (var command = new SQLiteCommand("Select count(*) from FileState where FilePath=:path", connection))
|
|
478 | 562 |
{ |
479 | 563 |
command.Parameters.AddWithValue("path", filePath.ToLower()); |
480 |
var result=command.ExecuteScalar(); |
|
481 |
new SQLiteCommand( |
|
482 |
"update FileState set FileStatus= :fileStatus where Id = :id ", |
|
483 |
connection); |
|
564 |
var result = command.ExecuteScalar(); |
|
565 |
return ((long)result >= 1); |
|
566 |
} |
|
484 | 567 |
|
485 |
|
|
486 |
|
|
487 | 568 |
} |
488 | 569 |
|
489 | 570 |
public void SetFileStatus(string path, FileStatus status) |
... | ... | |
505 | 586 |
throw new ArgumentException("The path must be rooted", "path"); |
506 | 587 |
Contract.EndContractBlock(); |
507 | 588 |
|
508 |
var connectionString = GetConnectionString(); |
|
509 |
using (var connection = new SQLiteConnection(connectionString))
|
|
589 |
|
|
590 |
using (var connection = GetConnection())
|
|
510 | 591 |
{ |
511 | 592 |
var command = new SQLiteCommand("select FileStatus from FileState where FilePath=:path", connection); |
512 | 593 |
command.Parameters.AddWithValue("path", path.ToLower()); |
513 |
connection.Open(); |
|
594 |
|
|
514 | 595 |
var s = command.ExecuteScalar(); |
515 | 596 |
return (FileStatus)Convert.ToInt32(s); |
516 | 597 |
} |
... | ... | |
529 | 610 |
|
530 | 611 |
private int DeleteDirect(string filePath) |
531 | 612 |
{ |
532 |
try
|
|
613 |
using (log4net.ThreadContext.Stacks["StatusAgent"].Push("DeleteDirect"))
|
|
533 | 614 |
{ |
534 | 615 |
|
535 |
var connectionString = GetConnectionString(); |
|
536 |
using (var connection = new SQLiteConnection(connectionString)) |
|
616 |
try |
|
537 | 617 |
{ |
538 |
var command = new SQLiteCommand("delete FileState where FilePath = :path", |
|
539 |
connection); |
|
540 | 618 |
|
541 |
command.Parameters.AddWithValue("path", filePath.ToLower()); |
|
542 |
connection.Open(); |
|
543 |
var affected = command.ExecuteNonQuery(); |
|
544 |
return affected; |
|
619 |
|
|
620 |
using (var connection = GetConnection()) |
|
621 |
{ |
|
622 |
var command = new SQLiteCommand("delete from FileState where FilePath = :path", |
|
623 |
connection); |
|
624 |
|
|
625 |
command.Parameters.AddWithValue("path", filePath.ToLower()); |
|
626 |
|
|
627 |
var affected = command.ExecuteNonQuery(); |
|
628 |
return affected; |
|
629 |
} |
|
630 |
} |
|
631 |
catch (Exception exc) |
|
632 |
{ |
|
633 |
Log.Error(exc.ToString()); |
|
634 |
throw; |
|
545 | 635 |
} |
546 | 636 |
} |
547 |
catch (Exception exc) |
|
548 |
{ |
|
549 |
Log.Error(exc.ToString()); |
|
550 |
throw; |
|
551 |
} |
|
552 |
|
|
553 | 637 |
} |
554 | 638 |
|
555 | 639 |
public void UpdateFileChecksum(string path, string checksum) |
Also available in: Unified diff