1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>DataAccessor.java</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
<link rel="up" href="dpl_example.html" title="Chapter 6. A DPL Example" />
<link rel="prev" href="mydbenv-persist.html" title="MyDbEnv" />
<link rel="next" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.java" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.1.6.1</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">DataAccessor.java</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td>
<th width="60%" align="center">Chapter 6. A DPL Example</th>
<td width="20%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dataaccessorclass"></a>DataAccessor.java</h2>
</div>
</div>
</div>
<p>
Now that we have implemented our data classes,
we can write a class that will provide
convenient access to our primary and
secondary indexes.
Note that like our data classes, this class is shared by both our
example programs.
</p>
<p>
If you compare this class against our
<code class="classname">Vendor</code> and
<code class="classname">Inventory</code>
class implementations, you will see that the
primary and secondary indices declared there are
referenced by this class.
</p>
<p>
See <a class="xref" href="dpl_example.html#vendorclass" title="Vendor.java">Vendor.java</a>
and
<a class="xref" href="inventoryclass.html" title="Inventory.java">Inventory.java</a>
for those implementations.
</p>
<pre class="programlisting">package persist.gettingStarted;
import java.io.File;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
public class DataAccessor {
// Open the indices
public DataAccessor(EntityStore store)
throws DatabaseException {
// Primary key for Inventory classes
inventoryBySku = store.getPrimaryIndex(
String.class, Inventory.class);
// Secondary key for Inventory classes
// Last field in the getSecondaryIndex() method must be
// the name of a class member; in this case, an Inventory.class
// data member.
inventoryByName = store.getSecondaryIndex(
inventoryBySku, String.class, "itemName");
// Primary key for Vendor class
vendorByName = store.getPrimaryIndex(
String.class, Vendor.class);
}
// Inventory Accessors
PrimaryIndex<String,Inventory> inventoryBySku;
SecondaryIndex<String,String,Inventory> inventoryByName;
// Vendor Accessors
PrimaryIndex<String,Vendor> vendorByName;
} </pre>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="dpl_example.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">MyDbEnv </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> ExampleDatabasePut.java</td>
</tr>
</table>
</div>
</body>
</html>
|