Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/LightningDB.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public void CursorShouldMoveToLast()
{
Assert.True(cur.MoveToLast());

var lastKey = UTF8.GetString(cur.Current.Key.Span.ToArray());
var lastValue = UTF8.GetString(cur.Current.Value.Span.ToArray());
var lastKey = UTF8.GetString(cur.Current.Key.CopyToNewArray());
var lastValue = UTF8.GetString(cur.Current.Value.CopyToNewArray());

Assert.Equal("key5", lastKey);
Assert.Equal("key5", lastValue);
Expand All @@ -88,8 +88,8 @@ public void CursorShouldMoveToFirst()
{
cur.MoveToFirst();

var lastKey = UTF8.GetString(cur.Current.Key.Span.ToArray());
var lastValue = UTF8.GetString(cur.Current.Value.Span.ToArray());
var lastKey = UTF8.GetString(cur.Current.Key.CopyToNewArray());
var lastValue = UTF8.GetString(cur.Current.Value.CopyToNewArray());

Assert.Equal("key1", lastKey);
Assert.Equal("key1", lastValue);
Expand All @@ -108,8 +108,8 @@ public void ShouldIterateThroughCursor()

while (cur.MoveNext())
{
var key = UTF8.GetString(cur.Current.Key.Span.ToArray());
var value = UTF8.GetString(cur.Current.Value.Span.ToArray());
var key = UTF8.GetString(cur.Current.Key.CopyToNewArray());
var value = UTF8.GetString(cur.Current.Value.CopyToNewArray());

var name = "key" + ++i;

Expand All @@ -136,7 +136,7 @@ public void CursorShouldDeleteElements()
}
using(var cursor = _txn.CreateCursor(_db))
{
var foundDeleted = cursor.Select(x => UTF8.GetString(x.Key.Span.ToArray()))
var foundDeleted = cursor.Select(x => UTF8.GetString(x.Key.CopyToNewArray()))
.Any(x => x == "key1" || x == "key2");
Assert.False(foundDeleted);
}
Expand All @@ -154,8 +154,8 @@ public void ShouldIterateThroughCursorByEnumerator()
foreach (var pair in cursor)
{
var name = "key" + ++i;
Assert.Equal(name, UTF8.GetString(pair.Key.Span.ToArray()));
Assert.Equal(name, UTF8.GetString(pair.Value.Span.ToArray()));
Assert.Equal(name, UTF8.GetString(pair.Key.CopyToNewArray()));
Assert.Equal(name, UTF8.GetString(pair.Value.CopyToNewArray()));
}
}
}
Expand All @@ -180,7 +180,7 @@ public void ShouldPutMultiple()
}
}

Assert.Equal(values, pairs.Select(x => x.Value.Span.ToArray()).ToArray());
Assert.Equal(values, pairs.Select(x => x.Value.CopyToNewArray()).ToArray());
}

[Fact]
Expand All @@ -198,8 +198,8 @@ public void ShouldGetMultiple()
cur.MoveNext();
cur.GetMultiple();
var resultPair = cur.Current;
Assert.Equal("TestKey", UTF8.GetString(resultPair.Key.Span.ToArray()));
var result = resultPair.Value.Span.ToArray().Split(sizeof(int))
Assert.Equal("TestKey", UTF8.GetString(resultPair.Key.CopyToNewArray()));
var result = resultPair.Value.CopyToNewArray().Split(sizeof(int))
.Select(x => BitConverter.ToInt32(x.ToArray(), 0)).ToArray();
Assert.Equal(original, result);
}
Expand All @@ -219,8 +219,8 @@ public void ShouldMoveNextMultiple()
{
cur.MoveNextMultiple();
var resultPair = cur.Current;
Assert.Equal("TestKey", UTF8.GetString(resultPair.Key.Span.ToArray()));
var result = resultPair.Value.Span.ToArray().Split(sizeof(int))
Assert.Equal("TestKey", UTF8.GetString(resultPair.Key.CopyToNewArray()));
var result = resultPair.Value.CopyToNewArray().Split(sizeof(int))
.Select(x => BitConverter.ToInt32(x.ToArray(), 0)).ToArray();
Assert.Equal(original, result);
}
Expand Down
2 changes: 1 addition & 1 deletion src/LightningDB.Tests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void NamedDatabaseNameExistsInMaster()
using (var cursor = tx.CreateCursor(db))
{
cursor.MoveNext();
Assert.Equal("customdb", UTF8.GetString(cursor.Current.Key.Span.ToArray()));
Assert.Equal("customdb", UTF8.GetString(cursor.Current.Key.CopyToNewArray()));
}
}
}
Expand Down
55 changes: 51 additions & 4 deletions src/LightningDB.Tests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,57 @@
using System.Collections.Generic;
using LightningDB.Native;
using Xunit;
using System.Runtime.InteropServices;

namespace LightningDB.Tests
{
[Collection("SharedFileSystem")]
public class TransactionDupFixedTests : IDisposable
{
private LightningEnvironment _env;

public TransactionDupFixedTests(SharedFileSystem fileSystem)
{
var path = fileSystem.CreateNewDirectoryForTest();
_env = new LightningEnvironment(path);
_env.Open();
}

public void Dispose()
{
_env.Dispose();
}


[Fact]
public void CanCountTransactionEntries()
{
LightningDatabase db;

using (var openDbTxn = _env.BeginTransaction())
{
db = openDbTxn.OpenDatabase();
}

var key = MemoryMarshal.Cast<char, byte>("abcd");

using(var writeTxn = _env.BeginTransaction())
{
writeTxn.Put(db, key, MemoryMarshal.Cast<char,byte>("Value1"));
writeTxn.Put(db, key, MemoryMarshal.Cast<char, byte>("Value2"));
writeTxn.Commit();
}

using (var delTxn = _env.BeginTransaction())
{
delTxn.Delete(db, key, null);//should not throw
delTxn.Commit();
}
}
}


[Collection("SharedFileSystem")]
public class TransactionTests : IDisposable
{
private LightningEnvironment _env;
Expand Down Expand Up @@ -145,7 +192,7 @@ public void TransactionShouldSupportCustomComparer()
Func<int, int, int> comparison = (l, r) => l.CompareTo(r);
var options = new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create};
Func<MDBValue, MDBValue, int> compareWith =
(l, r) => comparison(BitConverter.ToInt32(l.Span.ToArray(), 0), BitConverter.ToInt32(r.Span.ToArray(), 0));
(l, r) => comparison(BitConverter.ToInt32(l.CopyToNewArray(), 0), BitConverter.ToInt32(r.CopyToNewArray(), 0));
options.CompareWith(Comparer<MDBValue>.Create(new Comparison<MDBValue>(compareWith)));

using (var txnT = _env.BeginTransaction())
Expand All @@ -170,7 +217,7 @@ public void TransactionShouldSupportCustomComparer()
{
int order = 0;
while (c.MoveNext())
Assert.Equal(keysSorted[order++], BitConverter.ToInt32(c.Current.Key.Span.ToArray(), 0));
Assert.Equal(keysSorted[order++], BitConverter.ToInt32(c.Current.Key.CopyToNewArray(), 0));
}
}

Expand All @@ -181,7 +228,7 @@ public void TransactionShouldSupportCustomDupSorter()

var txn = _env.BeginTransaction();
var options = new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create | DatabaseOpenFlags.DuplicatesFixed};
Func<MDBValue, MDBValue, int> compareWith = (l, r) => comparison(BitConverter.ToInt32(l.Span.ToArray(), 0), BitConverter.ToInt32(r.Span.ToArray(), 0));
Func<MDBValue, MDBValue, int> compareWith = (l, r) => comparison(BitConverter.ToInt32(l.CopyToNewArray(), 0), BitConverter.ToInt32(r.CopyToNewArray(), 0));
options.FindDuplicatesWith(Comparer<MDBValue>.Create(new Comparison<MDBValue>(compareWith)));
var db = txn.OpenDatabase(configuration: options);

Expand All @@ -197,7 +244,7 @@ public void TransactionShouldSupportCustomDupSorter()
int order = 0;

while (c.MoveNext())
Assert.Equal(valuesSorted[order++], BitConverter.ToInt32(c.Current.Value.Span.ToArray(), 0));
Assert.Equal(valuesSorted[order++], BitConverter.ToInt32(c.Current.Value.CopyToNewArray(), 0));
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/LightningDB/EnvironmentInfo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
namespace LightningDB
{
/// <summary>
/// Information about the environment.
/// </summary>
public class EnvironmentInfo
{
/// <summary>
/// ID of the last used page
/// </summary>
public int LastPageNumber { get; set; }

/// <summary>
/// ID of the last committed transaction
/// </summary>
public int LastTransactionId { get; set; }

/// <summary>
/// Size of the data memory map
/// </summary>
public int MapSize { get; set; }
}
}
70 changes: 70 additions & 0 deletions src/LightningDB/GetResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace LightningDB
{
/// <summary>
/// A struct describing the result of a TryGet operation
/// </summary>
public readonly struct GetResult
{
public GetResult(GetResultCode resultCode, int valueLength)
{
ResultCode = resultCode;
ValueLength = valueLength;
}

/// <summary>
/// A success/error code for the Get operation
/// </summary>
public GetResultCode ResultCode { get; }

/// <summary>
/// If the key was found, this represents the length of the value
/// in the database.
///
/// <para>
/// If the key was not found, this value is 0 and has no meaning.
/// </para>
/// </summary>
public int ValueLength { get; }
}

/// <summary>
/// An enumeration describing the result of a TryGet operation
/// </summary>
public enum GetResultCode
{
/// <summary>
/// Success! The requested key was found and the value was copied to
/// the provided buffer.
/// <para>
/// GetResult.Length represents the length of the value found in
/// the database. (This is also the length of data copied into
/// the provided buffer.)
/// </para>
/// </summary>
Success,

/// <summary>
/// Failure. The requested key was found, but the provided buffer was
/// too small to contain the full value. No data has been retrived.
/// The Get operation should be retried with a buffer at least the
/// length represented in GetResult.Length
/// <para>
/// GetResult.Length represents the length of the value found in
/// the database. (This is the minimum buffer length needed for
/// the operation to succeed)
/// </para>
/// </summary>
DestinationTooSmall,

/// <summary>
/// Failure. The requested key was not found in the database. No data has
/// been retrived.
/// <para>
/// GetResult.Length has no meaning for this result. It's value
/// is set to zero.
/// </para>
/// </summary>
KeyNotFound
}

}
Loading