📄 second.so
字号:
m4_comment([$Id: second.so,v 10.12 2004/09/15 19:40:07 bostic Exp $])m4_ref_title(Access Methods, Secondary indices, [secondary indices, secondary @indices], am/close, am/cursor)m4_p([dnlA secondary index, put simply, is a way to efficiently access recordsin a database (the primary) by means of some piece of information otherthan the usual (primary) key. In m4_db, this index is simply anotherdatabase whose keys are these pieces of information (the secondarykeys), and whose data are the primary keys. Secondary indices can becreated manually by the application; there is no disadvantage, otherthan complexity, to doing so. However, when the secondary key can bemechanically derived from the primary key and datum that it points to,as is frequently the case, m4_db can automatically and transparentlymanage secondary indices.])m4_p([dnlAs an example of how secondary indices might be used, consider adatabase containing a list of students at a college, each of whom hasa unique student ID number. A typical database would use the studentID number as the key; however, one might also reasonably want to beable to look up students by last name. To do this, one would constructa secondary index in which the secondary key was this last name.])m4_p([In SQL, this would be done by executing something like the following:])m4_indent([dnlCREATE TABLE students(student_id CHAR(4) NOT NULL, lastname CHAR(15), firstname CHAR(15), PRIMARY KEY(student_id));CREATE INDEX lname ON students(lastname);])m4_p([In m4_db, this would work as follows (am4_linkweb(second.javas, [Java API example is also available])):])include(ref/am/second1.cs)m4_p([dnlFrom the application's perspective, putting things into the databaseworks exactly as it does without a secondary index; one can simplyinsert records into the primary database. In SQL one would do thefollowing:])m4_indent([dnlINSERT INTO student VALUES ("WC42", "Churchill ", "Winston ");])m4_p([and in m4_db, one does:])include(ref/am/second2.cs)m4_p([dnlInternally, a record with secondary key "Churchill" is inserted intothe secondary database (in addition to the insertion of "WC42" into theprimary, of course).])m4_p([Deletes are similar. The SQL clause:])m4_indent([dnlDELETE FROM student WHERE (student_id = "WC42");])m4_p([looks like:])include(ref/am/second3.cs)m4_p([dnlDeletes can also be performed on the secondary index directly; a deletedone this way will delete the "real" record in the primary as well. Ifthe secondary supports duplicates and there are duplicate occurrences ofthe secondary key, then all records with that secondary key are removedfrom both the secondary index and the primary database. InSQL:])m4_indent([dnlDELETE FROM lname WHERE (lastname = "Churchill ");])m4_p([In m4_db:])include(ref/am/second4.cs)m4_p([dnlGets on a secondary automatically return the primary datum. Ifm4_ref(dbh_pget) or m4_ref(dbc_pget) is used in lieu of m4_ref(dbh_get)or m4_ref(dbc_get), the primary key is returned as well. Thus, theequivalent of:])m4_indent([dnlSELECT * from lname WHERE (lastname = "Churchill ");])m4_p([would be:])include(ref/am/second5.cs)m4_p([dnlTo create a secondary index to a m4_db database, open the database thatis to become a secondary index normally, then pass it as the "secondary"argument to the m4_refT(dbh_associate) for some primary database.])m4_p([dnlAfter a m4_ref(dbh_associate) call is made, the secondary indices becomealternate interfaces to the primary database. All updates to theprimary will be automatically reflected in each secondary index that hasbeen associated with it. All get operations using the m4_ref(dbh_get)or m4_refT(dbc_get)s on the secondary index return the primary datumassociated with the specified (or otherwise current, in the case ofcursor operations) secondary key. The m4_ref(dbh_pget) andm4_refT(dbc_pget)s also become usable; these behave just likem4_ref(dbh_get) and m4_ref(dbc_get), but return the primary key inaddition to the primary datum, for those applications that need it aswell.])m4_p([dnlCursor get operations on a secondary index perform as expected; althoughthe data returned will by default be those of the primary database, aposition in the secondary index is maintained normally, and records willappear in the order determined by the secondary key and the comparisonfunction or other structure of the secondary database.])m4_p([dnlDelete operations on a secondary index delete the item from the primarydatabase and all relevant secondaries, including the current one.])m4_p([dnlPut operations of any kind are forbidden on secondary indices, as thereis no way to specify a primary key for a newly put item. Instead, theapplication should use the m4_ref(dbc_put) or m4_ref(dbh_put) methodson the primary database.])m4_p([dnlAny number of secondary indices may be associated with a given primarydatabase, up to limitations on available memory and the number of openfile descriptors.])m4_p([dnlNote that although m4_db guarantees that updates made using anym4_ref(Db) handle with an associated secondary will be reflected in thethat secondary, associating each primary handle with all the appropriatesecondaries is the responsibility of the application and is not enforcedby m4_db. It is generally unsafe, but not forbidden by m4_db, to modifya database that has secondary indices without having those indices openand associated. Similarly, it is generally unsafe, but not forbidden,to modify a secondary index directly. Applications that violate theserules face the possibility of outdated or incorrect results if thesecondary indices are later used.])m4_p([dnlIf a secondary index becomes outdated for any reason, it should bediscarded using the m4_ref(dbh_remove) method and a new one createdusing the m4_ref(dbh_associate) method. If a secondary index is nolonger needed, all of its handles should be closed using them4_ref(dbh_close) method, and then the database should be removed usinga new database handle and the m4_ref(dbh_remove) method.])m4_p([dnlClosing a primary database handle automatically dis-associates allsecondary database handles associated with it.])m4_page_footer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -