Skip to content

Commit

Permalink
Change compiler to C# 4 to be compatible with UnitÂ3D
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Nov 25, 2016
1 parent ee746f1 commit f0f60d5
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 14 deletions.
2 changes: 1 addition & 1 deletion LiteDB/Database/LiteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public bool RenameCollection(string oldName, string newName)
/// </summary>
public long Shrink()
{
return this.Shrink(_connectionString?.Password);
return this.Shrink(_connectionString == null ? null : _connectionString.Password);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Engine/Pages/ExtendPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ExtendPage : BasePage
/// <summary>
/// Represent the part or full of the object - if this page has NextPageID the object is bigger than this page
/// </summary>
public Byte[] Data { get; set; }
public byte[] Data { get; set; }

public ExtendPage(uint pageID)
: base(pageID)
Expand Down
7 changes: 4 additions & 3 deletions LiteDB/Engine/Pages/HeaderPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal class HeaderPage : BasePage

/// <summary>
/// Get/Set the pageID that start sequenece with a complete empty pages (can be used as a new page)
/// Must be a field to be used as "ref"
/// </summary>
public uint FreeEmptyPageID;

Expand All @@ -33,17 +34,17 @@ internal class HeaderPage : BasePage
/// <summary>
/// Database user version [2 bytes]
/// </summary>
public ushort UserVersion = 0;
public ushort UserVersion { get; set; }

/// <summary>
/// Password hash in SHA1 [20 bytes]
/// </summary>
public byte[] Password = new byte[20];
public byte[] Password { get; set; }

/// <summary>
/// When using encryption, store salt for password
/// </summary>
public byte[] Salt = new byte[16];
public byte[] Salt { get; set; }

/// <summary>
/// Get a dictionary with all collection pages with pageID link
Expand Down
17 changes: 17 additions & 0 deletions LiteDB/Engine/Update/IDatafileUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LiteDB
{
interface IDatafileUpdate
{
bool IsFileVersion(Stream stream);
void Initialize(Stream stream);
IEnumerable<string> ReadCollections();
IEnumerable<KeyValuePair<string, bool>> ReadIndexes(string colName);
IEnumerable<BsonDocument> ReadDocuments(string colName);
}
}
145 changes: 145 additions & 0 deletions LiteDB/Engine/Update/V6/DbReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace LiteDB.Update.V6
{
internal class DbReader
{
private const int PAGE_SIZE = 4096;

private Stream _stream;


#region DiskService

/// <summary>
/// Read page bytes from disk and convert to Page object
/// </summary>
private T ReadPageDisk<T>(uint pageID)
where T : BasePage_v6
{
// position cursor in stream
_stream.Seek(pageID * (uint)PAGE_SIZE, SeekOrigin.Begin);

var buffer = new byte[PAGE_SIZE];

// read bytes from stream
_stream.Read(buffer, 0, BasePage.PAGE_SIZE);

var reader = new ByteReader(buffer);

// page header
reader.ReadUInt32(); // read PageID
var pageType = (PageType)reader.ReadByte();
var prevPageID = reader.ReadUInt32();
var nextPageID = reader.ReadUInt32();
var itemCount = reader.ReadUInt16();
reader.ReadUInt16(); // FreeBytes
reader.Skip(8); // reserved 8 bytes

T page;

switch (pageType)
{
case PageType.Header: page = this.ReadHeaderPage(reader) as T; break;
case PageType.Collection: page = this.ReadCollectionPage(reader) as T; break;
//case PageType.Index: return new IndexPage(pageID);
//case PageType.Data: return new DataPage(pageID);
//case PageType.Extend: return new ExtendPage(pageID);
//case PageType.Empty: return new EmptyPage(pageID);
default: throw new Exception("Invalid pageType");
}

// setting page header
page.PageID = pageID;
page.PageType = pageType;
page.PrevPageID = prevPageID;
page.NextPageID = nextPageID;
page.ItemCount = itemCount;

return page;
}

/// <summary>
/// Read Header page from ByteReader
/// </summary>
private HeaderPage_v6 ReadHeaderPage(ByteReader reader)
{
var page = new HeaderPage_v6();

reader.Skip(100);

var cols = reader.ReadByte();
for (var i = 0; i < cols; i++)
{
page.CollectionPages.Add(reader.ReadString(), reader.ReadUInt32());
}

return page;
}

/// <summary>
/// Read Collection page from ByteReader
/// </summary>
private CollectionPage_v6 ReadCollectionPage(ByteReader reader)
{
var page = new CollectionPage_v6();

page.CollectionName = reader.ReadString();
page.DocumentCount = reader.ReadInt64();
reader.ReadUInt32(); // FreeDataPageID

page.Indexes = new Dictionary<string, bool>();


return page;
}

#endregion

#region PagerService

private Dictionary<uint, BasePage_v6> _cache = new Dictionary<uint, BasePage_v6>();

/// <summary>
/// Read a page from cache or from disk. If cache exceed 5000 pages, clear cache
/// </summary>
private T GetPage<T>(uint pageID)
where T : BasePage_v6
{
BasePage_v6 page;

if(_cache.Count > 5000) _cache.Clear();

if(_cache.TryGetValue(pageID, out page))
{
return (T)page;
}

page = _cache[pageID] = this.ReadPageDisk<T>(pageID);

return (T)page;
}

/// <summary>
/// Get all pages in sequence using NextPageID
/// </summary>
private IEnumerable<T> GetSeqPages<T>(uint firstPageID)
where T : BasePage_v6
{
var pageID = firstPageID;

while (pageID != uint.MaxValue)
{
var page = this.GetPage<T>(pageID);

pageID = page.NextPageID;

yield return page;
}
}

#endregion
}
}
61 changes: 61 additions & 0 deletions LiteDB/Engine/Update/V6/Pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;

namespace LiteDB.Update.V6
{
internal class BasePage_v6
{
public uint PageID { get; set; }
public PageType PageType { get; set; }
public uint PrevPageID { get; set; }
public uint NextPageID { get; set; }
public int ItemCount { get; set; }
}

internal class HeaderPage_v6 : BasePage_v6
{
public byte[] Password { get; set; }
public Dictionary<string, uint> CollectionPages { get; set; }
}

internal class CollectionPage_v6 : BasePage_v6
{
public string CollectionName { get; set; }
public long DocumentCount { get; set; }
public PageAddress HeaderNode { get; set; }
public Dictionary<string, bool> Indexes { get; set; }
}

internal class IndexPage_v6 : BasePage_v6
{
public Dictionary<ushort, IndexNode_v6> Nodes { get; set; }
}

internal class DataPage_v6 : BasePage_v6
{
public Dictionary<ushort, DataBlock_v6> DataBlocks { get; set; }
}

internal class ExtendPage_v6 : BasePage_v6
{
public byte[] Data { get; set; }
}

internal class DataBlock_v6
{
public PageAddress Position { get; set; }
public PageAddress[] IndexRef { get; set; }
public uint ExtendPageID { get; set; }
public byte[] Data { get; set; }
}

internal class IndexNode_v6
{
public PageAddress Position { get; set; }
public PageAddress[] Prev { get; set; }
public PageAddress[] Next { get; set; }
public ushort KeyLength { get; set; }
public BsonValue Key { get; set; }
public PageAddress DataBlock { get; set; }
}
}
1 change: 1 addition & 0 deletions LiteDB/LiteDB.NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<Compile Include="Utils\Extensions\StreamExtensions.cs" />
<Compile Include="Utils\Extensions\StringExtensions.cs" />
<Compile Include="Utils\Extensions\TypeExtensions.cs" />
<Compile Include="Utils\FileHelper.cs" />
<Compile Include="Utils\FileOptions.cs" />
<Compile Include="Utils\IndexNotFoundException.cs" />
<Compile Include="Utils\LazyLoad.cs" />
Expand Down
3 changes: 3 additions & 0 deletions LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<NoWarn>1591</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>AnyCPU</PlatformTarget>
<LangVersion>4</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -57,6 +58,7 @@
<Compile Include="Database\Collections\Include.cs" />
<Compile Include="Database\Collections\Index.cs" />
<Compile Include="Database\Collections\Insert.cs" />
<Compile Include="Database\Collections\Upsert.cs" />
<Compile Include="Database\Collections\Update.cs" />
<Compile Include="Database\Collections\LiteCollection.cs" />
<Compile Include="Database\LiteDatabase.cs" />
Expand Down Expand Up @@ -107,6 +109,7 @@
<Compile Include="Engine\Structures\IndexNode.cs" />
<Compile Include="Engine\Structures\IndexInfo.cs" />
<Compile Include="Mapper\Attributes\BsonRefAttribute.cs" />
<Compile Include="Utils\FileHelper.cs" />
<Compile Include="Utils\LiteTransaction.cs" />
<Compile Include="Engine\Structures\PageAddress.cs" />
<Compile Include="Mapper\Attributes\BsonFieldAttribute.cs" />
Expand Down
7 changes: 3 additions & 4 deletions LiteDB/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,9 @@ protected virtual EntityMapper BuildEntityMapper(Type type)
var index = (BsonIndexAttribute)memberInfo.GetCustomAttributes(indexAttr, false).FirstOrDefault();

// get data type
var dataType =
(memberInfo as PropertyInfo)?.PropertyType ??
(memberInfo as FieldInfo)?.FieldType ??
typeof(object);
var dataType = memberInfo is PropertyInfo ?
(memberInfo as PropertyInfo).PropertyType :
(memberInfo as FieldInfo).FieldType;

// check if datatype is list/array
var isList = Reflection.IsList(dataType);
Expand Down
4 changes: 3 additions & 1 deletion LiteDB/Mapper/Linq/QueryVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ private Query VisitExpression(Expression expr, string prefix = null)
#else
var type = met.Method.DeclaringType;
#endif
var paramType = (met.Arguments[0] as MemberExpression)?.Expression.NodeType;
var paramType = met.Arguments[0] is MemberExpression ?
(ExpressionType?)(met.Arguments[0] as MemberExpression).Expression.NodeType :
null;

// StartsWith
if (method == "StartsWith")
Expand Down
7 changes: 3 additions & 4 deletions LiteDB/Mapper/Reflection/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ public static GenericSetter CreateGenericSetter(Type type, MemberInfo memberInfo
else return null; // no field on Structs
}
#endif
var dataType =
(memberInfo as PropertyInfo)?.PropertyType ??
(memberInfo as FieldInfo)?.FieldType ??
typeof(object);
var dataType = memberInfo is PropertyInfo ?
(memberInfo as PropertyInfo).PropertyType :
(memberInfo as FieldInfo).FieldType;

var target = Expression.Parameter(typeof(object), "obj");
var value = Expression.Parameter(typeof(object), "val");
Expand Down

0 comments on commit f0f60d5

Please sign in to comment.