11

Is there a way to access a levelDB database from several programs? Is there some kind of option to open the dababase as read only?

For now, when opening the same database from to programs I get:

/path/to/dir/with/levelDBdatabase/LOCK: Resource temporarily unavailable

Cheers!

ezdazuzena
  • 6,120
  • 6
  • 41
  • 71

3 Answers3

16

Unfortunately, LevelDB is designed that way and it doesn't allow more than a single instance of the database to be open. All of the options are for a single process, but if you have multiple threads then you can get a snapshot and iterate over it in read-only mode (allowing other threads to read/write to the underlying database at the same time).

Do you want to achieve a specific behavior? If so, let us know what it is and we might be able to help.

Kiril
  • 39,672
  • 31
  • 167
  • 226
  • Thanks for your answer. I'd might have several instance of a program which search in the same database. A solution I am thinking of is similar to yours, wrapping a class around levelDB that then controls the access to levelDB. – ezdazuzena Feb 07 '12 at 14:32
  • 1
    Yep, sometimes there is just no easy answer. I hope this was helpful! – Kiril Feb 07 '12 at 14:49
  • 1
    well then.. you get the flag and the +1. Cheers – ezdazuzena Feb 08 '12 at 08:12
9

I was able to do this in linux by having each process make a directory of its own (e.g. $HOME/.leveldb/myprogram_myPID) and then do:

ln -s -t $HOME/.leveldb/myprogram_myPID /path/to/dir/with/levelDBdatabase/*
rm $HOME/.leveldb/myprogram_myPID/LOCK
touch $HOME/.leveldb/myprogram_myPID/LOCK

Then $HOME/.leveldb/myprogram_myPID can be used as a read-only leveldb database and multiple instances of the process can do this at the same time without copying the entire database.

It's probably wise to use a snapshot to access the db after doing this to avoid accidentally writing. Also, remember to delete the new directory when the process ends.

4

If you only need read-only access, each process can create a copy of the LevelDB folder:

cp -r /path/to/dir/with/levelDBdatabase /path/to/dir/with/levelDBdatabase-copy1

Then, instead of using the original levelDBdatabase, use levelDBdatabase-copy1.
When the program is finished, the copy can be deleted safely.

Ruben Verborgh
  • 3,545
  • 2
  • 31
  • 43