Perl环境下好用的嵌入式k/v数据库
2013-05-02 21:28:27 阿炯

本站赞助商链接,请多关照。 嵌入式数据库是指运行在本机上、不用启动服务端的轻型数据库,它与应用程序紧密集成,被应用程序所启动,并伴随应用程序的退出而终止。

由于其应用程序和数据库管理系统运行在相同的进程空间当中,进行数据操作时可以避免繁琐的进程间通信,因此耗费在通信上的开销自然也就降低到了极低程度。使用简单的函数调用接口来完成所有的数据库操作,而不是在数据库系统中经常用到的SQL语言。这样就避免了对结构化查询语言进行解析和处理所需的开销。

从这个意义上讲,似乎所有单机数据库都可以算嵌入式数据库,比如Access,Paradox,DBF等等,因为它们都不用启动数据库服务器即可使用。然而,我们通常不将上述数据库归入嵌入式数据库,而只将它们归入"桌面数据库",甚至"文件型数据库",因为这些数据库的完备性、存储容量及性能方面存在较大的缺陷。

嵌入式数据库支持的数据都是TB文件级别,或记录数目要至少超过千万。

按目前对数据库的理解分类,大致可分两类:像MySQL那样的关系型数据库和NoSQL数据库,后两者又可分为存储与非存储(内存)型。当然它们之间的功能界定并不是很清晰,甚至在功能上是交叉的。比如像Berkeley DB,早些年它仅是一款k/v存储型的嵌入式库,而被收购之后,它又具备了sql操作语义的特点,但两者并不冲突。

本文将介绍一些嵌入式数据库,它们是支持k/v型的且Perl对其有很好的支持,像FirebirdSQLite、memcachedb、Redis这样的sql风格的嵌入式数据库不在列举了(实事上,MySQL也有嵌入式库)。本地使用视情况推荐Storable、DBM::Deep这两种,非结果保存则Memcached也很不错,网络竞争环境下使用Redis。

Various DBM implementations
NDBM_File, DB_File, GDBM_File, SDBM_File, ODBM_File,Tokyo Cabinet,Kyoto Cabinet,LMDB,MLDBM,Storable,DBM::Deep

DBM Comparisons

Here's a partial table of features the different packages offer:

                         odbm    ndbm    sdbm    gdbm    bsd-db
                         ----    ----    ----    ----    ------
 Linkage comes w/ perl   yes     yes     yes     yes     yes
 Src comes w/ perl       no      no      yes     no      no
 Comes w/ many unix os   yes     yes[0]  no      no      no
 Builds ok on !unix      ?       ?       yes     yes     ?
 Code Size               ?       ?       small   big     big
 Database Size           ?       ?       small   big?    ok[1]
 Speed                   ?       ?       slow    ok      fast
 FTPable                 no      no      yes     yes     yes
 Easy to build          N/A     N/A      yes     yes     ok[2]
 Size limits             1k      4k      1k[3]   none    none
 Byte-order independent  no      no      no      no      yes
 Licensing restrictions  ?       ?       no      yes     no

[0] on mixed universe machines, may be in the bsd compat library, which is often shunned.

[1] Can be trimmed if you compile for one access method.

[2] See DB_File. Requires symbolic links.

[3] By default, but can be redefined.

1、DB_File
DB_File - Perl5 access to Berkeley DB version 1.x.

Perl中对'Berkeley DB'的核心模块的支持仅限于2.0(不含)版本以下,最主要的原因是其被Oracle收购之后,授权协议发生的变化,但在cpan中有对高版本的支持模块,可以自行安装使用。

2、NDBM_File
对ndbm格式的库支持,属核心模块。In 1986 Berkeley produced ndbm (standing for New Database Manager). This added support for having multiple databases open concurrently.

3、GDBM_File
对gnu db格式的库支持,非核心模块。

GDBM_File is a module which allows Perl programs to make use of the facilities provided by the GNU gdbm library. If you intend to use this module you should really have a copy of the gdbm manualpage at hand.

Most of the libgdbm.a functions are available through the GDBM_File interface.

(GNU dbm): A Free/Libre version written by Philip A. Nelson for the GNU project. It added support for arbitrary-length data in the database: previously all data had a fixed maximum length. The last version was released on November 13, 2011.

4、SDBM_File
对sdb格式的库支持,属核心模块。
SDBM_File establishes a connection between a Perl hash variable and a file in SDBM_File format;. You can manipulate the data in the file just as if it were in a Perl hash, but when your program exits, the data will remain in the file, to be used the next time your program runs. Some versions of Unix were excluding ndbm due to licensing issues, so in 1987 Ozan Yigit produced this public domain clone.

5、ODBM_File
对odb格式的库支持,非核心模块。
ODBM_File establishes a connection between a Perl hash variable and a file in ODBM_File format;. You can manipulate the data in the file just as if it were in a Perl hash, but when your program exits, the data will remain in the file, to be used the next time your program runs.

6、QDBM|Tokyo Cabinet|Kyoto Cabinet
为什么把上面三款软件放在一块呢,除了它们功能相似外,它们同出一个作者之手。前两者使用c开发,而后者使用c++开发,以'Tokyo Cabinet'目前最为稳定。

它们支持多种数据存储方式、高性能、超大容量、宽松的授权、多语言API支持而出名,比之于上面所提及的软件,也许你真的应该试试。而对于使用QDBM,作者的建议是使用后者代替之。

Tokyo Cabinet is a library of routines for managing a database. The database is a simple data file containing records, each is a pair of a key and a value. Every key and value is serial bytes with variable length. Both binary data and character string can be used as a key and a value. There is neither concept of data tables nor data types. Records are organized in hash table, B+ tree, or fixed-length array.

Tokyo Cabinet is developed as the successor of GDBM and QDBM on the following purposes. They are achieved and Tokyo Cabinet replaces conventional DBM products.

improves space efficiency : smaller size of database file.
improves time efficiency : faster processing speed.
improves parallelism : higher performance in multi-thread environment.
improves usability : simplified API.
improves robustness : database file is not corrupted even under catastrophic situation.
supports 64-bit architecture : enormous memory space and database file are available.


Tokyo Cabinet is written in the C language, and provided as API of C, Perl, Ruby, Java, and Lua. Tokyo Cabinet is available on platforms which have API conforming to C99 and POSIX. Tokyo Cabinet is a free software licensed under the GNU Lesser General Public License.

7、Memcached
注意:它虽然是k/v型的数据库,但它内部的数据不可存储,同时也不是嵌入式的。这里介绍它,主要是因为Perl对其支持很友好,另外它支持复杂的数据结构(像hash of hash,array),而上面所提及的库都不支持,但它的k/v存储时有大小限制。

8、Lightning MDB
LMDB 是一个快而小的 key-value 数据存储服务,是由 OpenLDAP 项目的 Symas 开发的。使用内存映射文件,因此读取的性能跟内存数据库一样。其大小受限于虚拟地址空间的大小,超过后很容易崩溃。

9、UnQLite
UnQLite 是一个嵌入式的软件库,实现了自容器、无服务端、跨平台、零配置和支持事务的 NoSQL 数据库引擎。UnQLite 是一个文档存储数据库,类似于 MongoDB, Redis, CouchDB 等。同时也是一个标准的 K/V 存储,类似 BerkeleyDB, LevelDB 等。

10、MLDBM

MLDBM - store multi-level Perl hash structure in single level tied hash

这也是一个对Perl支持友好的嵌入式k/v库,它能存放较为复杂的数据结构,通常与tie方法一起使用。经过实测,对字符比较敏感,即对多字符集支持差。可与SDBM_File、Storable等内置类共同使用。

11、Storable
该核心模块用于将Perl中运行时的变量数据结构等存储到文件之中,或从文件中读取变量的结构值。可以存储像标量、数组、哈希或引用对象。它需要全部导入内存后才能继续脚本的执行,同时需要显式的保存的文件中。采用C语言实现,运行效率与存储效率非常的不错,适用于存储量不大的应用环境中,比如中间结果存储。在其中实现了深度复制,对Perl原生支持。

12、DBM::Deep
纯Perl实现的支持事务、多层级的哈希/数组数据库管理模块,真正的多级hash/数组支持、Unicode支持、混合运行OO/tie()调用、文件跨平台的支持、ACID事务,且速度非常快。可以处理数以百万计的键值对和无限的水平层级且没有显的缓速,兼容Unix、Mac OS X和Windows平台。整体效率上比Storable要差一些,但其不用像它那样完全加载到内存后才能使用。同样对Perl原生支持且需要另外安装,好在依赖很少且为内置模块。

13、Redis
它跟Memcached一样,有着高效的内存运算能力,且能提供丰富的数据类型(5种类型)与结果保存,跨网络读写,分布式锁;但其对hash类型的数据结构支持较弱,更不用提(hash of hash,array)此类了,对多字符集的支持也不太友好。



参考链接
Dbm
Embedded_database
该文章最后由 阿炯 于 2021-05-06 22:42:58 更新,目前是第 2 版。