Skip to content

Commit

Permalink
Added compact option to Environment.CopyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalukyanov committed Jan 18, 2015
1 parent 4d6e4f0 commit 7a49ae4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/LightningDB.Tests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ public void EnvironmentShouldBeClosed()
Assert.AreEqual(false, _env.IsOpened);
}

[Test]
public void EnvironmentShouldBeCopied()
[TestCase(true)]
[TestCase(false)]
public void EnvironmentShouldBeCopied(bool compact)
{
//arrange
_env = new LightningEnvironment(_path, EnvironmentOpenFlags.None);
_env.Open();

//act
_env.CopyTo(_pathCopy);
_env.CopyTo(_pathCopy, compact);

//assert
if (Directory.GetFiles(_pathCopy).Length == 0)
Expand Down
17 changes: 17 additions & 0 deletions src/LightningDB/EnvironmentCopyFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LightningDB
{
public enum EnvironmentCopyFlags
{
None = 0,

/// <summary>
/// MDB_CP_COMPACT. Compacting copy: Omit free space from copy, and renumber all pages sequentially.
/// </summary>
Compact = 0x01
}
}
1 change: 1 addition & 0 deletions src/LightningDB/LightningDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="Collections\CursorEnumerator.cs" />
<Compile Include="CursorGetByOperation.cs" />
<Compile Include="CursorPutOptions.cs" />
<Compile Include="EnvironmentCopyFlags.cs" />
<Compile Include="Factories\CursorManager.cs" />
<Compile Include="Factories\DatabaseHandleCacheEntry.cs" />
<Compile Include="Factories\DatabaseManager.cs" />
Expand Down
9 changes: 7 additions & 2 deletions src/LightningDB/LightningEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,16 @@ internal LightningDatabase OpenDatabase(string name, LightningTransaction tran,
/// This function may be used to make a backup of an existing environment.
/// </summary>
/// <param name="path">The directory in which the copy will reside. This directory must already exist and be writable but must otherwise be empty.</param>
public void CopyTo(string path)
/// <param name="compact">Omit empty pages when copying.</param>
public void CopyTo(string path, bool compact = false)
{
this.EnsureOpened();

NativeMethods.Execute(lib => lib.mdb_env_copy(_handle, path));
var flags = compact
? EnvironmentCopyFlags.Compact
: EnvironmentCopyFlags.None;

NativeMethods.Execute(lib => lib.mdb_env_copy2(_handle, path, flags));
}

//TODO: tests
Expand Down
13 changes: 12 additions & 1 deletion src/LightningDB/Native/INativeLibraryFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,18 @@ interface INativeLibraryFacade
/// <param name="env">An environment handle returned by mdb_env_create(). It must have already been opened successfully.</param>
/// <param name="path">The directory in which the copy will reside. This directory must already exist and be writable but must otherwise be empty.</param>
/// <returns>A non-zero error value on failure and 0 on success.</returns>
int mdb_env_copy(IntPtr env, string path); //OK
int mdb_env_copy(IntPtr env, string path);

/// <summary>
/// Copy an LMDB environment to the specified path, with options.
/// This function may be used to make a backup of an existing environment. No lockfile is created, since it gets recreated at need.
/// This call can trigger significant file size growth if run in parallel with write transactions, because it employs a read-only transaction.
/// </summary>
/// <param name="env">An environment handle returned by mdb_env_create(). It must have already been opened successfully.</param>
/// <param name="path">The directory in which the copy will reside. This directory must already exist and be writable but must otherwise be empty.</param>
/// <param name="copyFlags">Special options for this operation. This parameter must be set to 0 or by bitwise OR'ing together one or more of the values described here.</param>
/// <returns>A non-zero error value on failure and 0 on success.</returns>
int mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags);

/// <summary>
/// Return information about the LMDB environment.
Expand Down
24 changes: 24 additions & 0 deletions src/LightningDB/Native/NativeLibraryFacades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Native32BitLibraryFacade : INativeLibraryFacade
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy(IntPtr env, string path);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);

Expand Down Expand Up @@ -226,6 +229,11 @@ int INativeLibraryFacade.mdb_env_copy(IntPtr env, string path)
return Native32BitLibraryFacade.mdb_env_copy(env, path);
}

int INativeLibraryFacade.mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags)
{
return Native32BitLibraryFacade.mdb_env_copy2(env, path, copyFlags);
}

int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
{
return Native32BitLibraryFacade.mdb_env_info(env, out stat);
Expand Down Expand Up @@ -358,6 +366,9 @@ class Native64BitLibraryFacade : INativeLibraryFacade
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy(IntPtr env, string path);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);

Expand Down Expand Up @@ -499,6 +510,11 @@ int INativeLibraryFacade.mdb_env_copy(IntPtr env, string path)
return Native64BitLibraryFacade.mdb_env_copy(env, path);
}

int INativeLibraryFacade.mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags)
{
return Native64BitLibraryFacade.mdb_env_copy2(env, path, copyFlags);
}

int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
{
return Native64BitLibraryFacade.mdb_env_info(env, out stat);
Expand Down Expand Up @@ -631,6 +647,9 @@ class FallbackLibraryFacade : INativeLibraryFacade
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy(IntPtr env, string path);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);

Expand Down Expand Up @@ -784,6 +803,11 @@ int INativeLibraryFacade.mdb_env_copy(IntPtr env, string path)
return FallbackLibraryFacade.mdb_env_copy(env, path);
}

int INativeLibraryFacade.mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags)
{
return FallbackLibraryFacade.mdb_env_copy2(env, path, copyFlags);
}

int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
{
return FallbackLibraryFacade.mdb_env_info(env, out stat);
Expand Down
8 changes: 8 additions & 0 deletions src/LightningDB/Native/NativeLibraryFacades.tt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ namespace LightningDB.Native
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy(IntPtr env, string path);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags);

[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);

Expand Down Expand Up @@ -245,6 +248,11 @@ namespace LightningDB.Native
return <#= pair.Value #>LibraryFacade.mdb_env_copy(env, path);
}

int INativeLibraryFacade.mdb_env_copy2(IntPtr env, string path, EnvironmentCopyFlags copyFlags)
{
return <#= pair.Value #>LibraryFacade.mdb_env_copy2(env, path, copyFlags);
}

int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
{
return <#= pair.Value #>LibraryFacade.mdb_env_info(env, out stat);
Expand Down

0 comments on commit 7a49ae4

Please sign in to comment.