Skip to content

Add CompactionFilter, RepairDb, Flush and GetLiveFilesMetadata #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions RocksDbSharp/CompactionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace RocksDbSharp
{
public class CompactionFilter
{
public IntPtr Handle;
private readonly NameDelegate getNameDelegate;
private readonly FilterDelegate filterDelegate;
private readonly DestructorDelegate destroyDelegate;

public CompactionFilter(NameDelegate nameDelegate,
FilterDelegate filterDelegate,
DestructorDelegate destroyDelegate,
IntPtr state)
{
this.getNameDelegate = nameDelegate;
this.filterDelegate = filterDelegate;
this.destroyDelegate = destroyDelegate;
Handle = Native.Instance.rocksdb_compactionfilter_create(state, destroyDelegate, filterDelegate, getNameDelegate);
}
}
}
20 changes: 20 additions & 0 deletions RocksDbSharp/FlushOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace RocksDbSharp
{
public class FlushOptions: OptionsHandle
{
public FlushOptions()
{
Native.Instance.rocksdb_flushoptions_create();
}

public FlushOptions SetWaitForFlush(bool waitForFlush)
{
Native.Instance.rocksdb_flushoptions_set_wait(Handle, waitForFlush);
return this;
}
}
}
23 changes: 23 additions & 0 deletions RocksDbSharp/LiveFilesMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace RocksDbSharp
{
public class LiveFileMetadata
{
public FileMetadata FileMetadata;
public FileDataMetadata FileDataMetadata;
}

public class FileMetadata
{
public string FileName;
public int FileLevel;
public ulong FileSize;
}

public class FileDataMetadata
{
public string SmallestKeyInFile;
public string LargestKeyInFile;
public ulong NumEntriesInFile;
public ulong NumDeletionsInFile;
}
}
124 changes: 122 additions & 2 deletions RocksDbSharp/RocksDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static RocksDb Open(OptionsHandle options, string path)
return new RocksDb(db, optionsReferences: null, cfOptionsRefs: null);
}

public static void RepairDB(OptionsHandle options, string path)
{
Native.Instance.rocksdb_repair_db(options.Handle, path);
}

public static RocksDb OpenReadOnly(OptionsHandle options, string path, bool errorIfLogFileExists)
{
IntPtr db = Native.Instance.rocksdb_open_for_read_only(options.Handle, path, errorIfLogFileExists);
Expand Down Expand Up @@ -168,7 +173,7 @@ public long Get(byte[] key, long keyLength, byte[] buffer, long offset, long len
}
}

public KeyValuePair<byte[],byte[]>[] MultiGet(byte[][] keys, ColumnFamilyHandle[] cf = null, ReadOptions readOptions = null)
public KeyValuePair<byte[], byte[]>[] MultiGet(byte[][] keys, ColumnFamilyHandle[] cf = null, ReadOptions readOptions = null)
{
return Native.Instance.rocksdb_multi_get(Handle, (readOptions ?? DefaultReadOptions).Handle, keys);
}
Expand Down Expand Up @@ -261,7 +266,7 @@ public void DropColumnFamily(string name)
Native.Instance.rocksdb_drop_column_family(Handle, cf.Handle);
columnFamilies.Remove(name);
}

public ColumnFamilyHandle GetDefaultColumnFamily()
{
return GetColumnFamily(ColumnFamilies.DefaultName);
Expand Down Expand Up @@ -306,5 +311,120 @@ public void CompactRange(string start, string limit, ColumnFamilyHandle cf = nul
encoding = Encoding.UTF8;
CompactRange(start == null ? null : encoding.GetBytes(start), limit == null ? null : encoding.GetBytes(limit), cf);
}

public void Flush(FlushOptions flushOptions)
{
Native.Instance.rocksdb_flush(Handle, flushOptions.Handle);
}


/// <summary>
/// Returns metadata about the file and data in the file.
/// </summary>
/// <param name="populateFileMetadataOnly">setting it to true only populates FileName,
/// Filesize and filelevel; By default it is false</param>
/// <returns><c>LiveFilesMetadata</c> or null in case of failure</returns>
public List<LiveFileMetadata> GetLiveFilesMetadata(bool populateFileMetadataOnly=false)
{
IntPtr buffer = Native.Instance.rocksdb_livefiles(Handle);
if (buffer == IntPtr.Zero)
{
return null;
}

try
{
List<LiveFileMetadata> filesMetadata = new List<LiveFileMetadata>();

int fileCount = Native.Instance.rocksdb_livefiles_count(buffer);
for (int index = 0; index < fileCount; index++)
{
LiveFileMetadata liveFileMetadata = new LiveFileMetadata();

FileMetadata metadata = new FileMetadata();
IntPtr fileMetadata = Native.Instance.rocksdb_livefiles_name(buffer, index);
string fileName = Marshal.PtrToStringAnsi(fileMetadata);

int level = Native.Instance.rocksdb_livefiles_level(buffer, index);

UIntPtr fS = Native.Instance.rocksdb_livefiles_size(buffer, index);
ulong fileSize = fS.ToUInt64();

metadata.FileName = fileName;
metadata.FileLevel = level;
metadata.FileSize = fileSize;

liveFileMetadata.FileMetadata = metadata;

if (!populateFileMetadataOnly)
{
FileDataMetadata fileDataMetadata = new FileDataMetadata();
var smallestKeyPtr = Native.Instance.rocksdb_livefiles_smallestkey(buffer,
index,
out var smallestKeySize);
string smallestKey = Marshal.PtrToStringAnsi(smallestKeyPtr);

var largestKeyPtr = Native.Instance.rocksdb_livefiles_largestkey(buffer,
index,
out var largestKeySize);
string largestKey = Marshal.PtrToStringAnsi(largestKeyPtr);

ulong entries = Native.Instance.rocksdb_livefiles_entries(buffer, index);
ulong deletions = Native.Instance.rocksdb_livefiles_deletions(buffer, index);

fileDataMetadata.SmallestKeyInFile = smallestKey;
fileDataMetadata.LargestKeyInFile = largestKey;
fileDataMetadata.NumEntriesInFile = entries;
fileDataMetadata.NumDeletionsInFile = deletions;

liveFileMetadata.FileDataMetadata = fileDataMetadata;
}

filesMetadata.Add(liveFileMetadata);
}

return filesMetadata;
}
finally
{
Native.Instance.rocksdb_livefiles_destroy(buffer);
buffer = IntPtr.Zero;
}
}

/// <summary>
/// Lean API to just get Live file names.
/// Refer to GetLiveFilesMetadata() for the complete metadata
/// </summary>
/// <returns></returns>
public List<string> GetLiveFileNames()
{
IntPtr buffer = Native.Instance.rocksdb_livefiles(Handle);
if (buffer == IntPtr.Zero)
{
return new List<string>();
}

try
{
List<string> liveFiles = new List<string>();

int fileCount = Native.Instance.rocksdb_livefiles_count(buffer);

for (int index = 0; index < fileCount; index++)
{
IntPtr fileMetadata = Native.Instance.rocksdb_livefiles_name(buffer, index);
string fileName = Marshal.PtrToStringAnsi(fileMetadata);
liveFiles.Add(fileName);
}

return liveFiles;
}
finally
{
Native.Instance.rocksdb_livefiles_destroy(buffer);
buffer = IntPtr.Zero;
}
}
}
}
6 changes: 3 additions & 3 deletions RocksDbSharp/RocksDbSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Versions.targets.include"/>
<Import Project="..\Versions.targets.include" />
<PropertyGroup>
<Title>RocksDbSharp</Title>
<TargetFrameworks>netstandard1.6;net40;net45</TargetFrameworks>
Expand Down Expand Up @@ -31,7 +31,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
Expand All @@ -42,6 +42,6 @@
<PackageReference Include="System.Reflection" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>
</ItemGroup>

</Project>
Loading