LevelDB C# read only access with custom comparator

Viewed 94

I'm taking a look at a LevelDB database in C# using the NuGet package LevelDB.Standard (https://github.com/oodrive/leveldb.net)

I've never used LevelDB before, so this is somewhat of a learning exercise.

The database in question uses a bespoke binary structure for the key/value pairs, and I need to perform some testing, mostly to see which keys are available. I've implemented the following basic code:

var comparator = Comparator.Create("custom_comparator_name", (a, b) =>
             Compare(
                 ((NativeArray<byte>)a).ToArray(),
                 ((NativeArray<byte>)b).ToArray(),
                 false)
             );

        var options = new Options { CreateIfMissing = false, Comparator = comparator, };

        using (var db = new DB(options, @"X:\Path_To_LevelDB"))
        {

        }

And for the comparator:

public static int Compare(byte[] key, byte[] value, bool compareOnlyIndexKeys)
    {
        Debug.WriteLine($"Key = {BitConverter.ToString(key)}");
        return 0;
    }

Running this once shows there are around 2000 keys in the database, however running it a second time only displays a single key.

The issue seems to be on the first run, the LevelDB files are updated, such as the Manifest file, which goes from MANIFEST-000001 to MANIFEST-001768

What I need to do is just read from the database, without it performing any changes to the underlying files. I've tried setting the files to read-only, but this throws an exception, as the LevelDB engine can't lock the files.

Do I need to create a copy of the database each time I perform these tests, or is there a way of telling the engine to only do things read-only

Thanks

0 Answers
Related