Skip to content

Commit

Permalink
Merge pull request #214 from mbdavid/szurgot
Browse files Browse the repository at this point in the history
Adds a default path to portable platforms.
  • Loading branch information
mbdavid authored Jul 23, 2016
2 parents 56da625 + 2300ebd commit fde05d1
Show file tree
Hide file tree
Showing 8 changed files with 671 additions and 17 deletions.
625 changes: 625 additions & 0 deletions LiteDB.All.sln

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions LiteDB.Platform/LiteDB.Tests.Universal81/TestPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TestBase
{
public TestBase()
{
LitePlatform.Initialize(new LitePlatformWindowsStore(ApplicationData.Current.TemporaryFolder));
LitePlatform.Initialize(new LitePlatformWindowsStore());
}
}

Expand All @@ -24,7 +24,7 @@ public static string GetFullPath(string filename)

public static long GetFileSize(string filename)
{
var folder = ApplicationData.Current.TemporaryFolder;
var folder = ApplicationData.Current.LocalFolder;
var file = AsyncHelpers.RunSync(folder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists));
var properties = AsyncHelpers.RunSync(file.GetBasicPropertiesAsync());

Expand All @@ -33,7 +33,7 @@ public static long GetFileSize(string filename)

public static void FileWriteAllText(string filename, string content)
{
var folder = ApplicationData.Current.TemporaryFolder;
var folder = ApplicationData.Current.LocalFolder;
var file = AsyncHelpers.RunSync(folder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists));
var bytes = Encoding.UTF8.GetBytes(content);

Expand All @@ -42,7 +42,7 @@ public static void FileWriteAllText(string filename, string content)

public static void DeleteFile(string filename)
{
var folder = ApplicationData.Current.TemporaryFolder;
var folder = ApplicationData.Current.LocalFolder;
var file = AsyncHelpers.RunSync(folder.GetFileAsync(filename));

#pragma warning disable 4014
Expand All @@ -52,7 +52,7 @@ public static void DeleteFile(string filename)

public static string FileReadAllText(string filename)
{
var folder = ApplicationData.Current.TemporaryFolder;
var folder = ApplicationData.Current.LocalFolder;
var file = AsyncHelpers.RunSync(folder.GetFileAsync(filename));
var buffer = AsyncHelpers.RunSync(FileIO.ReadBufferAsync(file));
var arr = buffer.ToArray();
Expand Down
2 changes: 1 addition & 1 deletion LiteDB.Tests/Tests/LoopTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace LiteDB.Tests
{
[TestClass]
public class LoopTest
public class LoopTest : TestBase
{
[TestMethod]
public void Loop_Test()
Expand Down
16 changes: 11 additions & 5 deletions LiteDB/Platform/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ namespace LiteDB.Platform
{
public class FileHandler : IFileHandler
{
private string defaultPath = ".";
public FileHandler(String defaultPath)
{
this.defaultPath = defaultPath;
}

public Stream OpenFileAsStream(string filename, bool readOnly)
{
return new FileStream(filename,
return new FileStream(Path.Combine(defaultPath, filename),
FileMode.Open,
readOnly ? FileAccess.Read : FileAccess.ReadWrite,
readOnly ? FileShare.Read : FileShare.None,
Expand All @@ -19,27 +25,27 @@ public Stream OpenFileAsStream(string filename, bool readOnly)

public Stream CreateFile(string filename, bool overwritten)
{
return new FileStream(filename,
return new FileStream(Path.Combine(defaultPath, filename),
overwritten ? FileMode.Create : FileMode.CreateNew,
FileAccess.ReadWrite,
FileShare.None, LiteDatabase.PAGE_SIZE);
}

public bool FileExists(string filename)
{
return File.Exists(filename);
return File.Exists(Path.Combine(defaultPath, filename));
}

public void DeleteFile(string filename)
{
File.Delete(filename);
File.Delete(Path.Combine(defaultPath, filename));
}

public void OpenExclusiveFile(string filename, Action<Stream> success)
{
try
{
using (var stream = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
using (var stream = File.Open(Path.Combine(defaultPath, filename), FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
success(stream);
}
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Platform/LitePlatformAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LiteDB.Platform
{
public class LitePlatformAndroid : ILitePlatform
{
private readonly LazyLoad<IFileHandler> _fileHandler = new LazyLoad<IFileHandler>(() => new FileHandler());
private readonly LazyLoad<IFileHandler> _fileHandler = new LazyLoad<IFileHandler>(() => new FileHandler(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)));
private readonly LazyLoad<IReflectionHandler> _reflectionHandler = new LazyLoad<IReflectionHandler>(() => new ExpressionReflectionHandler());

public LitePlatformAndroid()
Expand Down
15 changes: 13 additions & 2 deletions LiteDB/Platform/LitePlatformFullDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ namespace LiteDB.Platform
{
public class LitePlatformFullDotNet : ILitePlatform
{
private readonly LazyLoad<IFileHandler> _fileHandler = new LazyLoad<IFileHandler>(() => new FileHandler());
private readonly LazyLoad<IFileHandler> _fileHandler;
private readonly LazyLoad<IReflectionHandler> _reflectionHandler = new LazyLoad<IReflectionHandler>(() => new EmitReflectionHandler());

public LitePlatformFullDotNet()
/// <summary>
/// Default constructor. Will put all files in the application directory unless otherwise specified
/// </summary>
public LitePlatformFullDotNet() : this(".") { }

/// <summary>
/// Constructor which accepts a default directory for all files.
/// Default directory can be overridden by specifying a full path when opening the database.
/// </summary>
/// <param name="defaultPath">Default path where files will be placed.</param>
public LitePlatformFullDotNet(String defaultPath)
{
_fileHandler = new LazyLoad<IFileHandler>(() => new FileHandler(defaultPath));
AddNameCollectionToMapper();
}

Expand Down
16 changes: 14 additions & 2 deletions LiteDB/Platform/LitePlatformWindowsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ public class LitePlatformWindowsStore : ILitePlatform
private readonly LazyLoad<IReflectionHandler> _reflectionHandler;
private readonly Func<string, IEncryption> _encryption;

public LitePlatformWindowsStore(StorageFolder folder, Func<string, IEncryption> encryption = null)
/// <summary>
/// Default construtor. Places all database files in the default application folder.
/// </summary>
public LitePlatformWindowsStore() : this(Windows.Storage.ApplicationData.Current.LocalFolder) { }

/// <summary>
/// Constructor which allows encryption, but sets the default folder to the application data folder.
/// </summary>
/// <param name="encryption"></param>
public LitePlatformWindowsStore(Func<string, IEncryption> encryption) : this(Windows.Storage.ApplicationData.Current.LocalFolder, encryption) { }

// Making this private for now, because putting the folder anywhere but in the application store causes performance issues.
private LitePlatformWindowsStore(StorageFolder folder, Func<string, IEncryption> encryption = null)
{
_fileHandler = new LazyLoad<IFileHandler>(() => new FileHandlerWindowsStore(folder));
_reflectionHandler = new LazyLoad<IReflectionHandler>(() => new ExpressionReflectionHandler());
Expand All @@ -29,7 +41,7 @@ public LitePlatformWindowsStore(StorageFolder folder, Func<string, IEncryption>

public IEncryption GetEncryption(string password)
{
if(_encryption == null) throw new NotImplementedException();
if (_encryption == null) throw new ArgumentException("Encryption requested, but encryption was not set during initialization");

return _encryption(password);
}
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Platform/LitePlatformiOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LiteDB.Platform
{
public class LitePlatformiOS : ILitePlatform
{
private readonly LazyLoad<IFileHandler> _fileHandler = new LazyLoad<IFileHandler>(() => new FileHandler());
private readonly LazyLoad<IFileHandler> _fileHandler = new LazyLoad<IFileHandler>(() => new FileHandler(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)));
private readonly LazyLoad<IReflectionHandler> _reflectionHandler = new LazyLoad<IReflectionHandler>(() => new ExpressionReflectionHandler());

public LitePlatformiOS()
Expand Down

0 comments on commit fde05d1

Please sign in to comment.