ezdazuzena
ezdazuzena

Reputation: 6770

Multiple instances of a levelDB database at the same time

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!

Upvotes: 13

Views: 9820

Answers (3)

databasesymlinker
databasesymlinker

Reputation: 91

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.

Upvotes: 9

Ruben Verborgh
Ruben Verborgh

Reputation: 3665

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.

Upvotes: 5

Kiril
Kiril

Reputation: 40345

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.

Upvotes: 16

Related Questions