同时使用 levelDB 数据库的多个实例
有没有办法从多个程序访问 levelDB 数据库?是否有某种选项可以以只读方式打开数据库?
现在,当从程序打开相同的数据库时,我得到:
/path/to/dir/with/levelDBdatabase/LOCK: Resource temporarily unavailable
干杯!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,LevelDB 就是这样设计的,它不允许打开多个数据库实例。所有选项均适用于单个进程,但如果您有多个线程,则可以获取快照并以只读模式迭代它(允许其他线程同时读取/写入底层数据库)。
您想实现特定的行为吗?如果是这样,请告诉我们这是什么,我们也许可以提供帮助。
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.
我可以通过让每个进程创建自己的目录(例如 $HOME/.leveldb/myprogram_myPID)然后执行以下操作,在 Linux 中执行此操作:
然后 $HOME/.leveldb/myprogram_myPID 可以用作只读 leveldb 数据库并且该进程的多个实例可以同时执行此操作,而无需复制整个数据库。
执行此操作后使用快照访问数据库可能是明智的做法,以避免意外写入。另外,请记住在进程结束时删除新目录。
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:
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.
如果您只需要只读访问权限,则每个进程都可以创建LevelDB 文件夹的副本:
cp -r /path/to/dir/with/levelDBdatabase /path/to/dir/with/levelDBdatabase-copy1
然后,不使用原始的
levelDBdatabase
,而是使用levelDBdatabase-copy1
。程序完成后,可以安全地删除副本。
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
, uselevelDBdatabase-copy1
.When the program is finished, the copy can be deleted safely.