Skip to content

Commit

Permalink
Add FileHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Nov 22, 2016
1 parent 8660cf0 commit b6867c8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Database/Collections/Upsert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LiteDB
public partial class LiteCollection<T>
{
/// <summary>
/// Insert or Update a document in this collection. Returns false if not found document in collection
/// Insert or Update a document in this collection.
/// </summary>
public bool Upsert(T document)
{
Expand All @@ -20,7 +20,7 @@ public bool Upsert(T document)
}

/// <summary>
/// Insert or Update a document in this collection. Returns false if not found document in collection
/// Insert or Update a document in this collection.
/// </summary>
public bool Upsert(BsonValue id, T document)
{
Expand Down
8 changes: 1 addition & 7 deletions Database/LiteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,7 @@ public long Shrink(string password)
if (_connectionString != null)
{
// get temp file ("-temp" sufix)
var tempFile = Path.Combine(Path.GetDirectoryName(_connectionString.Filename), Path.GetFileNameWithoutExtension(_connectionString.Filename) + "-temp" + Path.GetExtension(_connectionString.Filename));

// if temp file exists, just delete (if is in-use exception will be throwed)
if(File.Exists(tempFile))
{
File.Delete(tempFile);
}
var tempFile = FileHelper.GetTempFile(_connectionString.Filename);

// get temp disk based on temp file
var tempDisk = new FileDiskService(tempFile, false);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Disks/FileDiskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public FileDiskService(string filename, FileOptions options)
_options = options;

// journal filename
_journalFilename = Path.Combine(Path.GetDirectoryName(_filename), Path.GetFileNameWithoutExtension(_filename) + "-journal" + Path.GetExtension(_filename));
_journalFilename = FileHelper.GetTempFile(_filename, "-journal", false);
}

public void Initialize(Logger log, string password)
Expand Down
35 changes: 35 additions & 0 deletions Utils/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LiteDB
{
/// <summary>
/// A simple file helper tool with static methods
/// </summary>
internal class FileHelper
{
/// <summary>
/// Create a temp filename based on original filename - checks if file exists (if exists, append counter number)
/// </summary>
public static string GetTempFile(string filename, string sufix = "-temp", bool checkIfExists = true)
{
var count = 0;
var temp = Path.Combine(Path.GetDirectoryName(filename),
Path.GetFileNameWithoutExtension(filename) + sufix +
Path.GetExtension(filename));

while(checkIfExists && File.Exists(temp))
{
temp = Path.Combine(Path.GetDirectoryName(filename),
Path.GetFileNameWithoutExtension(filename) + sufix +
"-" + (++count) +
Path.GetExtension(filename));
}

return temp;
}
}
}

0 comments on commit b6867c8

Please sign in to comment.