Skip to content

Commit

Permalink
Remove multi disk implementation and keep only stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Dec 26, 2017
1 parent 5a9c6a0 commit 83992dc
Show file tree
Hide file tree
Showing 33 changed files with 220 additions and 727 deletions.
4 changes: 1 addition & 3 deletions LiteDB.Demo/LiteDB_Paging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public void Init()
{
File.Delete(filename);

var disk = new FileDiskService(filename);

_engine = new LiteEngine(disk, cacheSize: 50000);
_engine = new LiteEngine(filename);
}

public void Populate(IEnumerable<BsonDocument> docs)
Expand Down
6 changes: 3 additions & 3 deletions LiteDB.Tests/Concurrency/Locker_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class Locker_Tests
[TestMethod]
public void Loop_With_Update()
{
using (var tmp = new TempFile())
using (var file = new TempFile())
{
// initialize database with 4
using (var db = new LiteEngine(tmp.Filename))
using (var db = new LiteEngine(file.Filename))
{
db.Insert("col", new BsonDocument { { "Number", 1 } }, BsonType.Int32);
db.Insert("col", new BsonDocument { { "Number", 2 } }, BsonType.Int32);
Expand All @@ -21,7 +21,7 @@ public void Loop_With_Update()
db.Insert("col", new BsonDocument { { "Number", 5 } }, BsonType.Int32);
}

using (var db = new LiteEngine(tmp.Filename))
using (var db = new LiteEngine(file.Filename))
{
foreach (var doc in db.Find("col", Query.All(), 0, 1000))
{
Expand Down
2 changes: 1 addition & 1 deletion LiteDB.Tests/Concurrency/Thread_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void Thread_Insert_Update()
{
{ "_id", i },
{ "updated", true },
{ "name", TempFile.LoremIpsum(5, 10, 1, 5, 1) }
{ "name", LoremIpsum.Generate(5, 10, 1, 5, 1) }
};

if (db.Update("col", doc)) i++;
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion LiteDB.Tests/Engine/Bulk_Insert_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private IEnumerable<BsonDocument> GetDocs(int initial, int count, int type = 1)
{ "_id", i },
{ "name", Guid.NewGuid().ToString() },
{ "first", "John" },
{ "lorem", TempFile.LoremIpsum(3, 5, 2, 3, 3) }
{ "lorem", LoremIpsum.Generate(3, 5, 2, 3, 3) }
};
}
}
Expand Down
3 changes: 1 addition & 2 deletions LiteDB.Tests/Engine/DropCollection_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class DropCollection_Tests
[TestMethod]
public void DropCollection()
{
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteEngine())
{
Assert.IsFalse(db.GetCollectionNames().Any(x => x == "col"));

Expand Down
8 changes: 4 additions & 4 deletions LiteDB.Tests/Engine/Encrypted_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Encrypted_Database()
Assert.IsTrue(plain.ReadAsText().Contains("Mauricio David"));

// create a database with password
using (var db = new LiteEngine(encrypt.Filename, "abc123"))
using (var db = new LiteEngine(new ConnectionString { Filename = encrypt.Filename, Password = "abc123" }))
{
db.Insert("col", new BsonDocument { { "name", "Mauricio David" } });
}
Expand All @@ -34,7 +34,7 @@ public void Encrypted_Database()
// try access using wrong password
try
{
using (var db = new LiteEngine(encrypt.Filename, "abc1234"))
using (var db = new LiteEngine(new ConnectionString { Filename = encrypt.Filename, Password = "abc1234" }))
{
Assert.Fail(); // can't work
}
Expand All @@ -45,14 +45,14 @@ public void Encrypted_Database()
}

// open encrypted db and read document
using (var db = new LiteEngine(encrypt.Filename, "abc123"))
using (var db = new LiteEngine(new ConnectionString { Filename = encrypt.Filename, Password = "abc123" }))
{
var doc = db.Find("col", Query.All()).First();

Assert.AreEqual("Mauricio David", doc["name"].AsString);

// let's remove password to work CheckIntegrety
db.Shrink(null, null);
db.Shrink(null);
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions LiteDB.Tests/Engine/Engine_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void Engine_Insert_Documents()
[TestMethod]
public void Engine_Upsert_Documents()
{
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteEngine())
{
var doc1 = new BsonDocument { { "_id", 1 }, { "name", "John" } };

Expand All @@ -62,8 +61,7 @@ public void Engine_Upsert_Documents()
[TestMethod]
public void Engine_Delete_Documents()
{
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteEngine())
{
var doc1 = new BsonDocument { { "_id", 1 }, { "name", "John" } };
var doc2 = new BsonDocument { { "_id", 2 }, { "name", "Doe" } };
Expand All @@ -79,8 +77,7 @@ public void Engine_Delete_Documents()

public void Engine_QueryUpdate_Documents()
{
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteEngine())
{
db.EnsureIndex("col", "name");

Expand Down
3 changes: 1 addition & 2 deletions LiteDB.Tests/Engine/IndexOrder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class IndexOrder_Tests
[TestMethod]
public void Index_Order()
{
using (var tmp = new TempFile())
using (var db = new LiteEngine(tmp.Filename))
using (var db = new LiteEngine())
{
db.Insert("col", new BsonDocument { { "text", "D" } });
db.Insert("col", new BsonDocument { { "text", "A" } });
Expand Down
4 changes: 2 additions & 2 deletions LiteDB.Tests/Engine/MemoryStream_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public void Engine_Using_MemoryStream()
{
var mem = new MemoryStream();

using (var db = new LiteEngine(mem))
using (var db = new LiteEngine(new ConnectionString(), mem))
{
db.Insert("col", new BsonDocument { { "_id", 1 } , { "name", "John" } });
db.Insert("col", new BsonDocument { { "_id", 2 }, { "name", "Doe" } });
}

using (var db = new LiteEngine(mem))
using (var db = new LiteEngine(new ConnectionString(), mem))
{
var john = db.Find("col", Query.EQ("_id", 1)).FirstOrDefault();
var doe = db.Find("col", Query.EQ("_id", 2)).FirstOrDefault();
Expand Down
2 changes: 1 addition & 1 deletion LiteDB.Tests/Engine/Performance_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private IEnumerable<BsonDocument> GetDocs(int count)
{ "_id", i },
{ "name", Guid.NewGuid().ToString() },
{ "type", rnd.Next(1, 100) },
{ "lorem", TempFile.LoremIpsum(3, 5, 2, 3, 3) }
{ "lorem", LoremIpsum.Generate(3, 5, 2, 3, 3) }
};
}
}
Expand Down
5 changes: 2 additions & 3 deletions LiteDB.Tests/Engine/Query_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Query_Using_Fullscan_Search()

public void ExecuteQuery(bool createIndex)
{
using (var db = new LiteEngine(new MemoryStream()))
using (var db = new LiteEngine())
{
db.Insert("col", new BsonDocument[]
{
Expand Down Expand Up @@ -83,8 +83,7 @@ public void ExecuteQuery(bool createIndex)
[TestMethod]
public void Query_Using_First_Linq()
{
using (var file = new TempFile())
using (var db = new LiteEngine(file.Filename))
using (var db = new LiteEngine())
{
db.Insert("col", new BsonDocument[]
{
Expand Down
2 changes: 1 addition & 1 deletion LiteDB.Tests/Engine/ReadOnly_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Datafile_Read_Only_Access()
db.Insert("col", new BsonDocument { { "_id", 1 } });
}

using (var r = new LiteEngine(new FileDiskService(file.Filename, new FileOptions { ReadOnly = true })))
using (var r = new LiteEngine(new ConnectionString { Filename = file.Filename, ReadOnly = true }))
{
var doc = r.Find("col", Query.EQ("_id", 1)).FirstOrDefault();

Expand Down
4 changes: 2 additions & 2 deletions LiteDB.Tests/Engine/Shrink_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Shrink_Large_Files()
}

// re-open, again, but now with password
using (var db = new LiteEngine(file.Filename, "abc123"))
using (var db = new LiteEngine(new ConnectionString { Filename = file.Filename, Password = "abc123" }))
{
DoTest(db);

Expand All @@ -106,7 +106,7 @@ private IEnumerable<BsonDocument> GetDocs(int initial, int count, int type = 1)
{ "_id", i },
{ "name", Guid.NewGuid().ToString() },
{ "first", "John" },
{ "lorem", TempFile.LoremIpsum(3, 5, 2, 3, 3) }
{ "lorem", LoremIpsum.Generate(3, 5, 2, 3, 3) }
};
}
}
Expand Down
42 changes: 42 additions & 0 deletions LiteDB.Tests/Utils/LoremIpsum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LiteDB.Tests
{
public class LoremIpsum
{
public static string Generate(int minWords, int maxWords,
int minSentences, int maxSentences,
int numParagraphs)
{
var words = new[] { "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",
"adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod",
"tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat" };

var rand = new Random(DateTime.Now.Millisecond);
var numSentences = rand.Next(maxSentences - minSentences) + minSentences + 1;
var numWords = rand.Next(maxWords - minWords) + minWords + 1;

var result = new StringBuilder();

for (int p = 0; p < numParagraphs; p++)
{
for (int s = 0; s < numSentences; s++)
{
for (int w = 0; w < numWords; w++)
{
if (w > 0) { result.Append(" "); }
result.Append(words[rand.Next(words.Length)]);
}
result.Append(". ");
}
result.AppendLine();
}

return result.ToString();
}
}
}
70 changes: 6 additions & 64 deletions LiteDB.Tests/Utils/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,12 @@ public class TempFile : IDisposable

public string Filename { get; private set; }

public TempFile(string ext = "db", bool checkIntegrity = true)
public TempFile(bool checkIntegrity = true)
{
this.Filename = Path.Combine(Path.GetTempPath(), string.Format("test-{0}.{1}", Guid.NewGuid(), ext));
this.Filename = Path.Combine(Path.GetTempPath(), string.Format("test-{0}.{1}", Guid.NewGuid(), ".db"));
_checkIntegrity = checkIntegrity;
}

public void CreateDatafile()
{
using (var s = new FileStream(Filename, System.IO.FileMode.CreateNew))
{
LiteEngine.CreateDatabase(s);
}
}

public IDiskService Disk()
{
return new FileDiskService(Filename);
}

public IDiskService Disk(FileOptions options)
{
return new FileDiskService(Filename, options);
}

public string Conn(string connectionString)
{
return "filename=\"" + this.Filename + "\";" + connectionString;
}

#region Dispose

private bool _disposed;
Expand Down Expand Up @@ -98,21 +75,21 @@ private void CheckIntegrity()
{
var cols = db.GetCollectionNames().ToArray();

foreach(var col in cols)
foreach (var col in cols)
{
var indexes = db.GetIndexes(col).ToArray();

foreach(var idx in indexes)
foreach (var idx in indexes)
{
var q = db.Find(col, Query.All(idx.Field));

foreach(var doc in q)
foreach (var doc in q)
{
// document are ok!
}

// lets drop this index (if not _id)
if(idx.Field != "_id")
if (idx.Field != "_id")
{
db.DropIndex(col, idx.Field);
}
Expand All @@ -126,40 +103,5 @@ private void CheckIntegrity()
db.Shrink();
}
}

#region LoremIpsum Generator

public static string LoremIpsum(int minWords, int maxWords,
int minSentences, int maxSentences,
int numParagraphs)
{
var words = new[] { "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",
"adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod",
"tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat" };

var rand = new Random(DateTime.Now.Millisecond);
var numSentences = rand.Next(maxSentences - minSentences) + minSentences + 1;
var numWords = rand.Next(maxWords - minWords) + minWords + 1;

var result = new StringBuilder();

for (int p = 0; p < numParagraphs; p++)
{
for (int s = 0; s < numSentences; s++)
{
for (int w = 0; w < numWords; w++)
{
if (w > 0) { result.Append(" "); }
result.Append(words[rand.Next(words.Length)]);
}
result.Append(". ");
}
result.AppendLine();
}

return result.ToString();
}

#endregion
}
}
Loading

0 comments on commit 83992dc

Please sign in to comment.