Skip to content

Commit

Permalink
Merge pull request #84 from mbdavid/dev-engine
Browse files Browse the repository at this point in the history
Merge for v2.0.0-beta
  • Loading branch information
mbdavid committed Nov 20, 2015
2 parents 6174b65 + 1934c5d commit f33a0cd
Show file tree
Hide file tree
Showing 136 changed files with 5,079 additions and 3,969 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dlldata.c
*.pidb
*.svclog
*.scc
.vs

# Chutzpah Test files
_Chutzpah*
Expand Down
24 changes: 24 additions & 0 deletions LiteDB.Shell/Commands/Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace LiteDB.Shell.Commands
{
internal class Debug : ConsoleCommand
{
public override bool IsCommand(StringScanner s)
{
return s.Scan(@"debug\s*").Length > 0;
}

public override void Execute(LiteShell shell, StringScanner s, Display d, InputCommand input)
{
var sb = new StringBuilder();
var enabled = !(s.Scan(@"off\s*").Length > 0);

shell.Database.Log.Level = enabled ? Logger.FULL : Logger.NONE;
}
}
}
20 changes: 6 additions & 14 deletions LiteDB.Shell/Commands/Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,10 @@ public override void Execute(LiteShell shell, StringScanner s, Display d, InputC
d.WriteHelp("> spool on|off", "Spool all output in a spool file");
d.WriteHelp("> -- comment", "Do nothing, its just a comment");
d.WriteHelp("> /<command>/", "Support for multi line command");
d.WriteHelp("> debug on|off", "Enabled debug messages from dbengine");
d.WriteHelp("> version", "Show LiteDB version");
d.WriteHelp("> exit", "Close LiteDB shell");

d.WriteHelp();
d.WriteHelp("Transaction commands");
d.WriteHelp("====================");

d.WriteHelp("> begin", "Begins a new transaction");
d.WriteHelp("> commit", "Commit current transaction");
d.WriteHelp("> rollback", "Rollback current transaction");

d.WriteHelp();
d.WriteHelp("Collections commands");
d.WriteHelp("====================");
Expand All @@ -75,7 +68,6 @@ public override void Execute(LiteShell shell, StringScanner s, Display d, InputC
d.WriteHelp("> db.<collection>.find [skip N][limit N]", "Show all documents. Can limit/skip results");
d.WriteHelp("> db.<collection>.find <filter> [skip N][limit N]", "Show filtered documents based on index search. See <filter> syntax below");
d.WriteHelp("> db.<collection>.count <filter>", "Show count rows according query filter");
d.WriteHelp("> db.<collection>.exec <filter> { Action<Object (id), BsonDocument (doc), Collection (col), LiteDatabase (db)> }", "Execute C# code for each document based on filter.");
d.WriteHelp("> db.<collection>.ensureIndex <field> [unique]", "Create a new index document field");
d.WriteHelp("> db.<collection>.indexes", "List all indexes in this collection");
d.WriteHelp("> db.<collection>.drop", "Drop collection and destroy all documents inside");
Expand All @@ -86,9 +78,9 @@ public override void Execute(LiteShell shell, StringScanner s, Display d, InputC
d.WriteHelp("<filter> = <field> [=|>|>=|<|<=|!=|like|contains|in|between] <jsonValue>", "Filter query syntax");
d.WriteHelp("<filter> = (<filter> [and|or] <filter> [and|or] ...)", "Multi queries syntax");
d.WriteHelp("<jsonDoc> = {_id: ... , key: value, key1: value1 }", "Represent a json (extended version) for a BsonDocument. See special data types");
d.WriteHelp("Json Date", "{ mydate: { $date :\"2015-01-01T23:59:59Z\"} }");
d.WriteHelp("Json Guid", "{ myguid: { $guid :\"3a1c34b3-9f66-4d8e-975a-d545d898a4ba\"} }");
d.WriteHelp("Json Binary", "{ mydata: { $binary :\"base64 byte array\"} }");
d.WriteHelp("Json Date", "{ field: { $date :\"2015-01-01T23:59:59Z\"} }");
d.WriteHelp("Json Guid", "{ field: { $guid :\"3a1c34b3-9f66-4d8e-975a-d545d898a4ba\"} }");
d.WriteHelp("Json Binary", "{ field: { $binary :\"base64 byte array\"} }");

d.WriteHelp();
d.WriteHelp("File storage commands");
Expand All @@ -105,8 +97,8 @@ public override void Execute(LiteShell shell, StringScanner s, Display d, InputC
d.WriteHelp("Other commands");
d.WriteHelp("==============");

d.WriteHelp("> db.info", "Get database informations");
d.WriteHelp("> dump", "Display dump database information");
d.WriteHelp("> shrink", "Reduce datafile removing empty pages");
d.WriteHelp("> dump [n m]", "Display dump database pages (from N page to M page)");
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions LiteDB.Shell/Commands/Reset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

#if DEBUG
namespace LiteDB.Shell.Commands
{
/// <summary>
/// Delete database e open again - used for tests only
/// </summary>
internal class Reset : ConsoleCommand
{
public override bool IsCommand(StringScanner s)
{
return s.Scan(@"reset\s+").Length > 0;
}

public override void Execute(LiteShell shell, StringScanner s, Display display, InputCommand input)
{
var filename = s.Scan(@".+");

if (shell.Database != null)
{
shell.Database.Dispose();
}

File.Delete(filename);

shell.Database = new LiteDatabase(filename);
}
}
}
#endif
4 changes: 1 addition & 3 deletions LiteDB.Shell/Commands/Spool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public override void Execute(LiteShell shell, StringScanner s, Display display,
{
if (shell.Database == null) throw LiteException.NoDatabase();

var dbfilename = shell.Database.ConnectionString.Filename;
var path = Path.Combine(Path.GetDirectoryName(dbfilename),
string.Format("{0}-spool-{1:yyyy-MM-dd-HH-mm}.txt", Path.GetFileNameWithoutExtension(dbfilename), DateTime.Now));
var path = Path.GetFullPath(string.Format("LiteDB-spool-{0:yyyy-MM-dd-HH-mm}.txt", DateTime.Now));

_writer = File.CreateText(path);

Expand Down
2 changes: 2 additions & 0 deletions LiteDB.Shell/LiteDB.Shell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\Debug.cs" />
<Compile Include="Commands\Reset.cs" />
<Compile Include="Commands\Version.cs" />
<Compile Include="Commands\Help.cs" />
<Compile Include="Commands\Open.cs" />
Expand Down
4 changes: 2 additions & 2 deletions LiteDB.Shell/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
39 changes: 18 additions & 21 deletions UnitTest/BsonTest.cs → LiteDB.Tests/Bson/BsonTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LiteDB;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;

namespace UnitTest
namespace LiteDB.Tests
{
[TestClass]
public class BsonTest
Expand All @@ -24,7 +19,8 @@ private BsonDocument CreateDoc()
doc["EmptyString"] = "";
doc["maxDate"] = DateTime.MaxValue;
doc["minDate"] = DateTime.MinValue;
doc.Set("Customer.Address.Street", "Av. Cacapava");

doc.Set("Customer.Address.Street", "Av. Caçapava, Nº 122");

doc["Items"] = new BsonArray();

Expand All @@ -47,26 +43,27 @@ public void Bson_Test()
var o = CreateDoc();

var bson = BsonSerializer.Serialize(o);

var json = JsonSerializer.Serialize(o);

var d = BsonSerializer.Deserialize(bson);
var doc = BsonSerializer.Deserialize(bson);

Assert.AreEqual(123, doc["_id"].AsInt32);
Assert.AreEqual(o["_id"].AsInt64, doc["_id"].AsInt64);

Assert.AreEqual(d["_id"], 123);
Assert.AreEqual(d["_id"].AsInt64, o["_id"].AsInt64);
Assert.AreEqual("Av. Caçapava, Nº 122", doc.Get("Customer.Address.Street").AsString);

Assert.AreEqual(o["FirstString"].AsString, d["FirstString"].AsString);
Assert.AreEqual(o["Date"].AsDateTime.ToString(), d["Date"].AsDateTime.ToString());
Assert.AreEqual(o["CustomerId"].AsGuid, d["CustomerId"].AsGuid);
Assert.AreEqual(o["MyNull"].RawValue, d["MyNull"].RawValue);
Assert.AreEqual(o["EmptyString"].AsString, d["EmptyString"].AsString);
Assert.AreEqual(o["FirstString"].AsString, doc["FirstString"].AsString);
Assert.AreEqual(o["Date"].AsDateTime.ToString(), doc["Date"].AsDateTime.ToString());
Assert.AreEqual(o["CustomerId"].AsGuid, doc["CustomerId"].AsGuid);
Assert.AreEqual(o["MyNull"].RawValue, doc["MyNull"].RawValue);
Assert.AreEqual(o["EmptyString"].AsString, doc["EmptyString"].AsString);

Assert.AreEqual(d["maxDate"].AsDateTime, DateTime.MaxValue);
Assert.AreEqual(d["minDate"].AsDateTime, DateTime.MinValue);
Assert.AreEqual(DateTime.MaxValue, doc["maxDate"].AsDateTime);
Assert.AreEqual(DateTime.MinValue, doc["minDate"].AsDateTime);

Assert.AreEqual(o["Items"].AsArray.Count, d["Items"].AsArray.Count);
Assert.AreEqual(o["Items"].AsArray[0].AsDocument["Unit"].AsDouble, d["Items"].AsArray[0].AsDocument["Unit"].AsDouble);
Assert.AreEqual(o["Items"].AsArray[4].AsDateTime.ToString(), d["Items"].AsArray[4].AsDateTime.ToString());
Assert.AreEqual(o["Items"].AsArray.Count, doc["Items"].AsArray.Count);
Assert.AreEqual(o["Items"].AsArray[0].AsDocument["Unit"].AsDouble, doc["Items"].AsArray[0].AsDocument["Unit"].AsDouble);
Assert.AreEqual(o["Items"].AsArray[4].AsDateTime.ToString(), doc["Items"].AsArray[4].AsDateTime.ToString());
}
}
}
19 changes: 7 additions & 12 deletions UnitTest/JsonTest.cs → LiteDB.Tests/Bson/JsonTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LiteDB;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;

namespace UnitTest
namespace LiteDB.Tests
{
[TestClass]
public class JsonTest
Expand Down Expand Up @@ -45,13 +40,13 @@ public void Json_Test()

var json = JsonSerializer.Serialize(o, true);

var d = JsonSerializer.Deserialize(json).AsDocument;
var doc = JsonSerializer.Deserialize(json).AsDocument;

Assert.AreEqual(d["Date"].AsDateTime, o["Date"].AsDateTime);
Assert.AreEqual(d["CustomerId"].AsGuid, o["CustomerId"].AsGuid);
Assert.AreEqual(d["Items"].AsArray.Count, o["Items"].AsArray.Count);
Assert.AreEqual(d["_id"], 123);
Assert.AreEqual(d["_id"].AsInt64, o["_id"].AsInt64);
Assert.AreEqual(o["Date"].AsDateTime, doc["Date"].AsDateTime);
Assert.AreEqual(o["CustomerId"].AsGuid, doc["CustomerId"].AsGuid);
Assert.AreEqual(o["Items"].AsArray.Count, doc["Items"].AsArray.Count);
Assert.AreEqual(123, doc["_id"].AsInt32);
Assert.AreEqual(o["_id"].AsInt64, doc["_id"].AsInt64);
}
}
}
10 changes: 2 additions & 8 deletions UnitTest/ObjectIdTest.cs → LiteDB.Tests/Bson/ObjectIdTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LiteDB;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest
namespace LiteDB.Tests
{
[TestClass]
public class ObjectIdTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LiteDB;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

namespace UnitTest
namespace LiteDB.Tests
{
[TestClass]
public class ConcurrentTest
Expand All @@ -17,65 +14,66 @@ public class ConcurrentTest
[TestMethod]
public void Concurrent_Test()
{
var file = DB.Path();
var N = 300;
var dbname = DB.RandomFilename();
var N = 300; // interate counter

var a = new LiteDatabase(file);
var b = new LiteDatabase(file);
var c = new LiteDatabase(file);
var d = new LiteDatabase(file);
var a = new LiteDatabase(dbname);
var b = new LiteDatabase(dbname);
var c = new LiteDatabase(dbname);
var d = new LiteDatabase(dbname);

// Task A -> Insert 100 documents
// task A -> insert N documents
var ta = Task.Factory.StartNew(() =>
{
var col = a.GetCollection("col1");
col.EnsureIndex("name");

for (var i = 1; i <= N; i++)
{
col.Insert(this.CreateDoc(i, "My String"));
col.Insert(this.CreateDoc(i, "-insert-"));
}
});

// Task B -> Update 100 documents
// task B -> update N documents
var tb = Task.Factory.StartNew(() =>
{
var col = b.GetCollection("col1");
var i = 1;

while (i <= N)
{
var doc = this.CreateDoc(i, "update value");
var doc = this.CreateDoc(i, "-update-");
doc["date"] = new DateTime(2015, 1, 1);
doc["value"] = null;
doc["desc"] = null;

var success = col.Update(doc);
if (success) i++;
}
});

// TasK C -> Delete 99 documents (keep only _id = 1)
// tasK C -> delete N-1 documents (keep only _id = 1)
var tc = Task.Factory.StartNew(() =>
{
var col = c.GetCollection("col1");
var i = 2;

while (i <= N)
{
if(col.Exists(Query.And(Query.EQ("_id", i), Query.EQ("name", "update value"))))
// delete document after update
if(col.Exists(Query.And(Query.EQ("_id", i), Query.EQ("name", "-update-"))))
{
var success = col.Delete(i);
if (success) i++;
}
}
});

// Task D -> Upload 40 files + delete 20
// task D -> upload 40 files + delete 20
var td = Task.Factory.StartNew(() =>
{
for (var i = 1; i <= 40; i++)
{
d.FileStorage.Upload("f" + i, this.CreateMemoryFile(1024 * 512));
d.FileStorage.Upload("f" + i, this.CreateMemoryFile(20000));
}
for (var i = 1; i <= 20; i++)
{
Expand All @@ -91,17 +89,18 @@ public void Concurrent_Test()
c.Dispose();
d.Dispose();

using (var db = new LiteDatabase(file))
using (var db = new LiteDatabase(dbname))
{
var col = db.GetCollection("col1");
var doc = col.FindById(1);

Assert.AreEqual(doc["name"].AsString, "update value");
Assert.AreEqual(doc["date"].AsDateTime, new DateTime(2015, 1, 1));
Assert.AreEqual(doc["value"].IsNull, true);
Assert.AreEqual("-update-", doc["name"].AsString);
Assert.AreEqual(new DateTime(2015, 1, 1), doc["date"].AsDateTime);
Assert.AreEqual(true, doc["desc"].IsNull);
Assert.AreEqual(col.Count(), 1);

Assert.AreEqual(db.FileStorage.FindAll().Count(), 20);
Assert.AreEqual(1, col.Count());
Assert.AreEqual(20, db.FileStorage.FindAll().Count());
}
}

Expand All @@ -121,7 +120,6 @@ private BsonDocument CreateDoc(int id, string name)
private MemoryStream CreateMemoryFile(int size)
{
var buffer = new byte[size];

return new MemoryStream(buffer);
}
}
Expand Down
Loading

0 comments on commit f33a0cd

Please sign in to comment.