• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/docs_src/ref/am/
1m4_ignore([dnl
2#include <sys/types.h>
3#include <string.h>
4
5#include <db.h>
6
7DB_ENV *dbenv;
8
9void	second();
10int	getname(DB *, const DBT *, const DBT *, DBT *);
11void	handle_error(int);
12
13int
14main()
15{
16	second();
17	return (0);
18}
19
20void
21handle_error(ret)
22	int ret;
23{}])
24m4_literal([m4_indentv([dnl
25struct student_record {
26	char student_id__LB__4__RB__;
27	char last_name__LB__15__RB__;
28	char first_name__LB__15__RB__;
29};
30m4_blank
31void
32second()
33{
34	DB *dbp, *sdbp;
35	int ret;
36	m4_blank
37	/* Open/create primary */
38	if ((ret = db_create(&dbp, dbenv, 0)) != 0)
39		handle_error(ret);
40	if ((ret = dbp-__GT__open(dbp, NULL,
41	    "students.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
42		handle_error(ret);
43	m4_blank
44	/*
45	 * Open/create secondary.  Note that it supports duplicate data
46	 * items, since last names might not be unique.
47	 */
48	if ((ret = db_create(&sdbp, dbenv, 0)) != 0)
49		handle_error(ret);
50	if ((ret = sdbp-__GT__set_flags(sdbp, DB_DUP | DB_DUPSORT)) != 0)
51		handle_error(ret);
52	if ((ret = sdbp-__GT__open(sdbp, NULL,
53	    "lastname.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
54		handle_error(ret);
55	m4_blank
56	/* Associate the secondary with the primary. */
57	if ((ret = dbp-__GT__associate(dbp, NULL, sdbp, getname, 0)) != 0)
58		handle_error(ret);
59}
60m4_blank
61/*
62 * getname -- extracts a secondary key (the last name) from a primary
63 * 	key/data pair
64 */
65int
66getname(secondary, pkey, pdata, skey)
67	DB *secondary;
68	const DBT *pkey, *pdata;
69	DBT *skey;
70{
71	/*
72	 * Since the secondary key is a simple structure member of the
73	 * record, we don't have to do anything fancy to return it.  If
74	 * we have composite keys that need to be constructed from the
75	 * record, rather than simply pointing into it, then the user's
76	 * function might need to allocate space and copy data.  In
77	 * this case, the DB_DBT_APPMALLOC flag should be set in the
78	 * secondary key DBT.
79	 */
80	memset(skey, 0, sizeof(DBT));
81	skey-__GT__data = ((struct student_record *)pdata-__GT__data)-__GT__last_name;
82	skey-__GT__size = sizeof((struct student_record *)pdata-__GT__data)-__GT__last_name;
83	return (0);
84}])])
85