[意譯]Berkeley DB 文檔 - C++入門(mén)篇 - 1.2節(jié) - Berkeley DB 概述
譯者序(轉(zhuǎn)載 -- Berkeley DB簡(jiǎn)介):
Berkeley DB是由美國(guó)Sleepycat Software公司開(kāi)發(fā)的一套開(kāi)放源碼的嵌入式數(shù)據(jù)庫(kù)的程序庫(kù)(database library),它為應(yīng)用程序提供可伸縮的、高性能的、有事務(wù)保護(hù)功能的數(shù)據(jù)管理服務(wù)。Berkeley DB為數(shù)據(jù)的存取和管理提供了一組簡(jiǎn)潔的函數(shù)調(diào)用API接口。
它是一個(gè)經(jīng)典的C-library模式的toolkit,為程序員提供廣泛豐富的函數(shù)集,是為應(yīng)用程序開(kāi)發(fā)者提供工業(yè)級(jí)強(qiáng)度的數(shù)據(jù)庫(kù)服務(wù)而設(shè)計(jì)的。其主要特點(diǎn)如下:
嵌入式(Embedded):它直接鏈接到應(yīng)用程序中,與應(yīng)用程序運(yùn)行于同樣的地址空間中,因此,無(wú)論是在網(wǎng)絡(luò)上不同計(jì)算機(jī)之間還是在同一臺(tái)計(jì)算機(jī)的不同進(jìn)程之間,數(shù)據(jù)庫(kù)操作并不要求進(jìn)程間通訊。
Berkeley DB為多種編程語(yǔ)言提供了API接口,其中包括C、C++、Java、Perl、Tcl、Python和PHP,所有的數(shù)據(jù)庫(kù)操作都在程序庫(kù)內(nèi)部發(fā)生。多個(gè)進(jìn)程,或者同一進(jìn)程的多個(gè)線程可同時(shí)使用數(shù)據(jù)庫(kù),有如各自單獨(dú)使用,底層的服務(wù)如加鎖、事務(wù)日志、共享緩沖區(qū)管理、內(nèi)存管理等等都由程序庫(kù)透明地執(zhí)行。
輕便靈活(Portable):它可以運(yùn)行于幾乎所有的UNIX和Linux系統(tǒng)及其變種系統(tǒng)、Windows操作系統(tǒng)以及多種嵌入式實(shí)時(shí)操作系統(tǒng)之下。它在32位和64位系統(tǒng)上均可運(yùn)行,已經(jīng)被好多高端的因特網(wǎng)服務(wù)器、臺(tái)式機(jī)、掌上電腦、機(jī)頂盒、網(wǎng)絡(luò)交換機(jī)以及其他一些應(yīng)用領(lǐng)域所采用。一旦 Berkeley DB被鏈接到應(yīng)用程序中,終端用戶一般根本感覺(jué)不到有一個(gè)數(shù)據(jù)庫(kù)系統(tǒng)存在。
可伸縮(Scalable):這一點(diǎn)表現(xiàn)在很多方面。Database library本身是很精簡(jiǎn)的(少于300KB的文本空間),但它能夠管理規(guī)模高達(dá)256TB的數(shù)據(jù)庫(kù)。它支持高并發(fā)度,成千上萬(wàn)個(gè)用戶可同時(shí)操縱同一個(gè)數(shù)據(jù)庫(kù)。Berkeley DB能以足夠小的空間占用量運(yùn)行于有嚴(yán)格約束的嵌入式系統(tǒng),也可以在高端服務(wù)器上耗用若干GB的內(nèi)存和若干TB的磁盤(pán)空間。
Berkeley DB在嵌入式應(yīng)用中比關(guān)系數(shù)據(jù)庫(kù)和面向?qū)ο髷?shù)據(jù)庫(kù)要好,有以下兩點(diǎn)原因:
(1)因?yàn)閿?shù)據(jù)庫(kù)程序庫(kù)同應(yīng)用程序在相同的地址空間中運(yùn)行,所以數(shù)據(jù)庫(kù)操作不需要進(jìn)程間的通訊。在一臺(tái)機(jī)器的不同進(jìn)程間或在網(wǎng)絡(luò)中不同機(jī)器間進(jìn)行進(jìn)程通訊所花費(fèi)的開(kāi)銷,要遠(yuǎn)遠(yuǎn)大于函數(shù)調(diào)用的開(kāi)銷;
(2)因?yàn)锽erkeley DB對(duì)所有操作都使用一組API接口,因此不需要對(duì)某種查詢語(yǔ)言進(jìn)行解析,也不用生成執(zhí)行計(jì)劃,大大提高了運(yùn)行效.
正文
Berkeley DB Documentation -- C++ Getting Started Guide
Berkeley DB 文檔 -- C++入門(mén)篇
Berkeley DB Concepts
Berkeley DB 概述
Before continuing, it is useful to describe some of the larger concepts that you will encounter when building a DB application.
先看看構(gòu)建一個(gè)DB應(yīng)用時(shí)你可能遇到的一些概念.
Conceptually, DB databases contain records. Logically each record represents a single entry in the database. Each such record contains two pieces of information: a key and a data. This manual will on occasion describe a a record's key or a record's data when it is necessary to speak to one or the other portion of a database record.
概念上說(shuō),DB數(shù)據(jù)庫(kù)包含了記錄(records),每條技術(shù)是一個(gè)邏輯上的實(shí)體.這種記錄通常包含兩部分的信息,鍵和值.本手冊(cè)會(huì)適當(dāng)?shù)臅r(shí)候來(lái)進(jìn)一步說(shuō)明.
Because of the key/data pairing used for DB databases, they are sometimes thought of as a two-column table. However, data (and sometimes keys, depending on the access method) can hold arbitrarily complex data. Frequently, C structures and other such mechanisms are stored in the record. This effectively turns a 2-column table into a table with n columns, where n-1 of those columns are provided by the structure's fields.
根據(jù)鍵值對(duì)的DB數(shù)據(jù)庫(kù)模型,我們有時(shí)會(huì)認(rèn)為它是一個(gè)只有兩列的表.然而,值(有時(shí)也可以是鍵,這由訪問(wèn)的方式?jīng)Q定)可以是一種復(fù)雜的數(shù)據(jù)結(jié)構(gòu).通常,記錄中保存的是C的結(jié)構(gòu)體或一些類似的東西.這種方式有效的把2列的表轉(zhuǎn)變?yōu)閚列的表,其中n-1列是由結(jié)構(gòu)體提供.
Note that a DB database is very much like a table in a relational database system in that most DB applications use more than one database (just as most relational databases use more than one table).
注意,我們這里的DB數(shù)據(jù)庫(kù)和關(guān)系數(shù)據(jù)庫(kù)中的表十分類似,絕大部分DB應(yīng)用使用不只一個(gè)數(shù)據(jù)庫(kù)(正如大部分關(guān)系數(shù)據(jù)庫(kù)不只一個(gè)表).
Unlike relational systems, however, a DB database contains a single collection of records organized according to a given access method (BTree, Queue, Hash, and so forth). In a relational database system, the underlying access method is generally hidden from you.
但與關(guān)系系統(tǒng)不同的是,DB數(shù)據(jù)庫(kù)可以通過(guò)指定方式(B樹(shù),隊(duì)列,哈希,諸如此類)訪問(wèn)一個(gè)的數(shù)據(jù)集.在關(guān)系數(shù)據(jù)庫(kù)系統(tǒng)中,這些隱藏的訪問(wèn)方式通常是用戶不可見(jiàn)的.
In any case, frequently DB applications are designed so that a single database stores a specific type of data (just as in a relational database system, a single table holds entries containing a specific set of fields). Because most applications are required to manage multiple kinds of data, a DB application will often use multiple databases.
基本上,常用的DB應(yīng)用設(shè)計(jì)是一個(gè)單獨(dú)的數(shù)據(jù)庫(kù)來(lái)保存一個(gè)特定的數(shù)據(jù)類型(正如關(guān)系數(shù)據(jù)庫(kù)中,一張單獨(dú)的表包含一種特定的集合).由于一個(gè)程序通常要包含多種數(shù)據(jù)類型,一個(gè)DB應(yīng)用通常也使用多個(gè)數(shù)據(jù)庫(kù).
For example, consider an accounting application. This kind of an application may manage data based on bank accounts, checking accounts, stocks, bonds, loans, and so forth. An accounting application will also have to manage information about people, banking institutions, customer accounts, and so on. In a traditional relational database, all of these different kinds of information would be stored and managed using a (probably very) complex series of tables. In a DB application, all of this information would instead be divided out and managed using multiple databases.
比如,想像一個(gè)賬目程序.這種程序可以管理銀行帳號(hào),支票帳號(hào),股票,債券,貸款等等.一個(gè)賬目程序還需要管理用戶,用戶帳戶,銀行等等的信息.在傳統(tǒng)的關(guān)系數(shù)據(jù)庫(kù)中,所有的這些信息可能通過(guò)一系列(也許非常非常)復(fù)雜的表來(lái)保存和管理.在DB應(yīng)用中,所有的這些信息則被分離,用多個(gè)數(shù)據(jù)庫(kù)來(lái)管理.
DB applications can efficiently use multiple databases using an optional mechanism called an environment. For more information, see Environments.
DB應(yīng)用可以通過(guò)一種可選的叫做"環(huán)境(environment)"機(jī)制來(lái)有效的使用多個(gè)數(shù)據(jù)庫(kù).更多內(nèi)容參見(jiàn)"Environments"章節(jié).
You interact with most DB APIs using special structures that contain pointers to functions. These callbacks are called methods because they look so much like a method on a C++ class. The variable that you use to access these methods is often referred to as a handle. For example, to use a database you will obtain a handle to that database.
你通過(guò)使用特定包含指針和函數(shù)的結(jié)構(gòu)體和DB的API互交.這些回調(diào)和C++中類的method看上去很像,因此被稱為方法(methods).
用來(lái)訪問(wèn)方法的變量通常是一個(gè)句柄(handle).比如,使用一個(gè)數(shù)據(jù)庫(kù),你必須獲得一個(gè)數(shù)據(jù)庫(kù)的句柄.
Retrieving a record from a database is sometimes called getting the record because the method that you use to retrieve the records is called get(). Similarly, storing database records is sometimes called putting the record because you use the put() method to do this.
從數(shù)據(jù)庫(kù)中找回一條記錄有時(shí)被稱為get一條記錄,原因是找回記錄的方法是get().類似保存一條記錄有時(shí)被稱為put一條記錄,因?yàn)橄鄳?yīng)的方法是put().
When you store, or put, a record to a database using its handle, the record is stored according to whatever sort order is in use by the database. Sorting is mostly performed based on the key, but sometimes the data is considered too. If you put a record using a key that already exists in the database, then the existing record is replaced with the new data. However, if the database supports duplicate records (that is, records with identical keys but different data), then that new record is stored as a duplicate record and any existing records are not overwritten.
當(dāng)你通過(guò)句柄保存,或者說(shuō)是put一條記錄,這條記錄通過(guò)數(shù)據(jù)庫(kù)指定的順序被插入.順序主要是由鍵來(lái)確定,但有時(shí)值也是被考慮的因素.如果你put了一個(gè)鍵已經(jīng)存在的記錄,它會(huì)用新的數(shù)據(jù)替換原有數(shù)據(jù).然而,如果數(shù)據(jù)庫(kù)支持多重(duplicate)記錄(也就是說(shuō)鍵同值不同),那么新的記錄會(huì)作為另一個(gè)副本保存.
If a database supports duplicate records, then you can use a database handle to retrieve only the first record in a set of duplicate records.
如果數(shù)據(jù)庫(kù)支持多重記錄,你可以通過(guò)數(shù)據(jù)庫(kù)找回多重記錄集中的第一條記錄.
In addition to using a database handle, you can also read and write data using a special mechanism called a cursor. Cursors are essentially iterators that you can use to walk over the records in a database. You can use cursors to iterate over a database from the first record to the last, and from the last to the first. You can also use cursors to seek to a record. In the event that a database supports duplicate records, cursors are the only way you can access all the records in a set of duplicates.
再提一點(diǎn)關(guān)于數(shù)據(jù)庫(kù)句柄使用,你可以通過(guò)一種叫做游標(biāo)(cursor)的特定機(jī)制來(lái)讀/寫(xiě)數(shù)據(jù).游標(biāo)本質(zhì)上是用來(lái)遍歷數(shù)據(jù)庫(kù)中記錄的迭代器(iterator).你可以使用游標(biāo)來(lái)查詢記錄.在數(shù)據(jù)庫(kù)支持多重記錄的情況下,游標(biāo)是唯一可以訪問(wèn)這些記錄副本集的方式.
Finally, DB provides a special kind of a database called a secondary database. Secondary databases serve as an index into normal databases (called primary database to distinguish them from secondaries). Secondary databases are interesting because DB records can hold complex data types, but seeking to a given record is performed only based on that record's key. If you wanted to be able to seek to a record based on some piece of information that is not the key, then you enable this through the use of secondary databases.
最后,DB提供了一種特殊的數(shù)據(jù)庫(kù)叫做次級(jí)(secondary)數(shù)據(jù)庫(kù).次級(jí)數(shù)據(jù)庫(kù)作為普通數(shù)據(jù)庫(kù)(為了區(qū)別,稱他為主數(shù)據(jù)庫(kù))的索引而存在.由于DB數(shù)據(jù)庫(kù)可以保存復(fù)雜的數(shù)據(jù)結(jié)構(gòu),但是查詢時(shí)只能通過(guò)記錄的鍵.如果你想通過(guò)不是鍵的部分進(jìn)行查詢,就需要通過(guò)次級(jí)數(shù)據(jù)庫(kù)來(lái)實(shí)現(xiàn)了.