Skip to content

Commit

Permalink
Add Regex to custom type #515
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Mar 11, 2017
1 parent a12a3fe commit 61cb54a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions LiteDB.Tests/LiteDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Concurrency\ProcessTest.cs" />
<Compile Include="Database\AutoIndexDatabaseTest.cs" />
<Compile Include="Database\UpgradeScriptTest.cs" />
<Compile Include="Mapper\CustomTypeTest.cs" />
<Compile Include="Mapper\DictionaryTest.cs" />
<Compile Include="Document\ImplicitTest.cs" />
<Compile Include="Engine\AutoIndexEngineTest.cs" />
Expand Down
51 changes: 51 additions & 0 deletions LiteDB.Tests/Mapper/CustomTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace LiteDB.Tests
{
#region Model

public class CustomType
{
public Regex Re1 { get; set; }
public Regex Re2 { get; set; }

public Uri Url { get; set; }
public TimeSpan Ts { get; set; }
}

#endregion

[TestClass]
public class CustomTypeTest
{
[TestMethod]
public void CustomType_Test()
{
var mapper = new BsonMapper();
var o = new CustomType
{
Re1 = new Regex("^a+"),
Re2 = new Regex("^a*", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace),
Url = new Uri("http://www.litedb.org"),
Ts = TimeSpan.FromSeconds(10)
};

var doc = mapper.ToDocument(o);
var no = mapper.ToObject<CustomType>(doc);

Assert.AreEqual("^a+", doc["Re1"].AsString);
Assert.AreEqual("^a*", doc["Re2"].AsDocument["p"].AsString);

Assert.AreEqual(o.Re1.ToString(), no.Re1.ToString());
Assert.AreEqual(o.Re2.ToString(), no.Re2.ToString());
Assert.AreEqual(o.Re2.Options, no.Re2.Options);
Assert.AreEqual(o.Url, no.Url);
Assert.AreEqual(o.Ts, no.Ts);
}
}
}
3 changes: 3 additions & 0 deletions LiteDB/Database/LiteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public LiteDatabase(ConnectionString connectionString, BsonMapper mapper = null)

var options = new FileOptions
{
#if !NET35
Async = _connectionString.Async,
#endif
InitialSize = _connectionString.InitialSize,
LimitSize = _connectionString.LimitSize,
Journal = _connectionString.Journal
Expand Down
39 changes: 30 additions & 9 deletions LiteDB/Engine/Disks/FileDiskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
#if !NET35
using System.Threading.Tasks;
#endif

namespace LiteDB
{
Expand Down Expand Up @@ -66,11 +69,10 @@ public void Initialize(Logger log, string password)
if (_options.FileMode == FileMode.ReadOnly) _options.Journal = false;

// open/create file using read only/exclusive options
_stream = new FileStream(_filename,
_stream = CreateFileStream(_filename,
_options.FileMode == FileMode.ReadOnly ? System.IO.FileMode.Open : System.IO.FileMode.OpenOrCreate,
_options.FileMode == FileMode.ReadOnly ? FileAccess.Read : FileAccess.ReadWrite,
_options.FileMode == FileMode.Exclusive ? FileShare.None : FileShare.ReadWrite,
BasePage.PAGE_SIZE);
_options.FileMode == FileMode.Exclusive ? FileShare.None : FileShare.ReadWrite);

// if file is new, initialize
if (_stream.Length == 0)
Expand Down Expand Up @@ -179,11 +181,10 @@ public void WriteJournal(ICollection<byte[]> pages)
if (_journal == null)
{
// open or create datafile if not exists
_journal = new FileStream(_journalFilename,
_journal = CreateFileStream(_journalFilename,
System.IO.FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.ReadWrite,
BasePage.PAGE_SIZE);
FileShare.ReadWrite);
}

// lock first byte
Expand Down Expand Up @@ -223,11 +224,10 @@ public IEnumerable<byte[]> ReadJournal()
{
try
{
_journal = new FileStream(_journalFilename,
_journal = CreateFileStream(_journalFilename,
System.IO.FileMode.Open,
FileAccess.ReadWrite,
FileShare.ReadWrite,
BasePage.PAGE_SIZE);
FileShare.ReadWrite);

// just avoid initialize recovery if journal is empty
if (_journal.Length == 0) yield break;
Expand Down Expand Up @@ -335,5 +335,26 @@ public void Unlock(LockState state)
}

#endregion

/// <summary>
/// Create a new filestream. Can be synced over async task (netstandard)
/// </summary>
private FileStream CreateFileStream(string path, System.IO.FileMode mode, FileAccess access, FileShare share)
{
#if !NET35
if (_options.Async)
{
return this.SyncOverAsync(() => new FileStream(path, mode, access, share, BasePage.PAGE_SIZE));
}
#endif
return new FileStream(path, mode, access, share, BasePage.PAGE_SIZE);
}

#if !NET35
private T SyncOverAsync<T>(Func<T> f)
{
return Task.Run<T>(f).ConfigureAwait(false).GetAwaiter().GetResult();
}
#endif
}
}
5 changes: 5 additions & 0 deletions LiteDB/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public BsonMapper(Func<Type, object> customTypeInstantiator = null)
RegisterType<Uri>(uri => uri.AbsoluteUri, bson => new Uri(bson.AsString));
RegisterType<DateTimeOffset>(value => new BsonValue(value.UtcDateTime), bson => bson.AsDateTime.ToUniversalTime());
RegisterType<TimeSpan>(value => new BsonValue(value.Ticks), bson => new TimeSpan(bson.AsInt64));
RegisterType<Regex>(
r => r.Options == RegexOptions.None ? new BsonValue(r.ToString()) : new BsonDocument { { "p", r.ToString() }, { "o", (int)r.Options } },
value => value.IsString ? new Regex(value) : new Regex(value.AsDocument["p"].AsString, (RegexOptions)value.AsDocument["o"].AsInt32)
);


#endregion Register CustomTypes

Expand Down
10 changes: 10 additions & 0 deletions LiteDB/Utils/ConnectionString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public class ConnectionString
/// </summary>
public bool Upgrade { get; set; }

#if !NET35
/// <summary>
/// "async": Use "sync over async" to UWP apps access any directory
/// </summary>
public bool Async { get; set; }
#endif

/// <summary>
/// Initialize empty connection string
/// </summary>
Expand Down Expand Up @@ -105,6 +112,9 @@ public ConnectionString(string connectionString)
this.LimitSize = values.GetFileSize(@"limit size", long.MaxValue);
this.Log = values.GetValue("log", Logger.NONE);
this.Upgrade = values.GetValue("upgrade", false);
#if !NET35
this.Async = values.GetValue("async", false);
#endif
}
}
}
3 changes: 3 additions & 0 deletions LiteDB/Utils/FileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class FileOptions
public long InitialSize { get; set; }
public long LimitSize { get; set; }
public FileMode FileMode { get; set; }
#if !NET35
public bool Async { get; set; }
#endif

public FileOptions()
{
Expand Down

0 comments on commit 61cb54a

Please sign in to comment.