| 1 | <html> |
| 2 | <body> |
| 3 | The Direct Persistence Layer (DPL) adds a persistent object model to the |
| 4 | Berkeley DB transactional engine. |
| 5 | |
| 6 | <h1>Package Specification</h1> |
| 7 | |
| 8 | <ul> |
| 9 | <li><a href="#intro">Introduction</a></li> |
| 10 | <li><a href="#model">The Entity Model</a></li> |
| 11 | <li><a href="#example">A brief example</a></li> |
| 12 | <li><a href="#whichAPI">Which API to use?</a></li> |
| 13 | <li><a href="#java14and15">Java 1.5 dependencies</a> |
| 14 | <ul> |
| 15 | <li><a href="#genericTypes">Generic Types</a></li> |
| 16 | <li><a href="#annotations">Annotations</a></li> |
| 17 | </ul> |
| 18 | </li> |
| 19 | <li><a href="#bytecode">Bytecode Enhancement</a></li> |
| 20 | </ul> |
| 21 | |
| 22 | <a name="intro"><h2>Introduction</h2></a> |
| 23 | |
| 24 | <p>The Direct Persistence Layer (DPL) was designed to meet the following |
| 25 | requirements.</p> |
| 26 | <ul> |
| 27 | <li>A type safe and convenient API is provided for accessing persistent |
| 28 | objects. The use of Java generic types, although optional, is fully exploited |
| 29 | to provide type safety. For example: |
| 30 | <pre class="code"> |
| 31 | {@literal PrimaryIndex<Long,Employer> employerById = ...;} |
| 32 | long employerId = ...; |
| 33 | Employer employer = employerById.get(employerId);</pre> |
| 34 | </li> |
| 35 | <li>All Java types are allowed to be persistent without requiring that they |
| 36 | implement special interfaces. Persistent fields may be {@code private}, |
| 37 | package-private (default access), {@code protected}, or {@code public}. No |
| 38 | hand-coding of bindings is required. However, each persistent class must have |
| 39 | a default constructor. For example: |
| 40 | <pre class="code"> |
| 41 | {@literal @Persistent} |
| 42 | class Address { |
| 43 | String street; |
| 44 | String city; |
| 45 | String state; |
| 46 | int zipCode; |
| 47 | private Address() {} |
| 48 | }</pre> |
| 49 | </li> |
| 50 | <li>Bytecode enhancement provides fully optimized bindings that do not use Java |
| 51 | reflection.</li> |
| 52 | <li>It is easy to define primary and secondary keys. No external schema is |
| 53 | required and Java annotations may be used for defining all metadata. |
| 54 | Extensions may derive metadata from other sources. For example, the following |
| 55 | Employer class is defined as a persistent entity with a primary key field |
| 56 | {@code id} and the secondary key field {@code name}:</li> |
| 57 | <pre class="code"> |
| 58 | {@literal @Entity} |
| 59 | class Employer { |
| 60 | |
| 61 | {@literal @PrimaryKey(sequence="ID")} |
| 62 | long id; |
| 63 | |
| 64 | {@literal @SecondaryKey(relate=ONE_TO_ONE)} |
| 65 | String name; |
| 66 | |
| 67 | Address address; |
| 68 | |
| 69 | private Employer() {} |
| 70 | }</pre> |
| 71 | <li>Interoperability with external components is supported via the Java |
| 72 | collections framework. Any primary or secondary index can be accessed using a |
| 73 | standard <code>java.util</code> collection. For example: |
| 74 | <pre class="code">{@literal java.util.SortedMap<String,Employer> map = employerByName.sortedMap();}</pre> |
| 75 | </li> |
| 76 | <li>Class evolution is explicitly supported. Compatible changes (adding fields |
| 77 | and type widening) are performed automatically and transparently. For example, |
| 78 | without any special configuration a {@code street2} field may be added to the |
| 79 | {@code Address} class and the type of the {@code zipCode} field may be changed |
| 80 | from {@code int} to {@code long}: |
| 81 | <pre class="code"> |
| 82 | {@literal @Persistent} |
| 83 | class Address { |
| 84 | String street; |
| 85 | String street2; |
| 86 | String city; |
| 87 | String state; |
| 88 | long zipCode; |
| 89 | private Address() {} |
| 90 | }</pre> |
| 91 | Many incompatible class changes, such as renaming fields or refactoring a |
| 92 | single class, can be performed using {@link |
| 93 | com.sleepycat.persist.evolve.Mutations Mutations}. Mutations are automatically |
| 94 | applied lazily as data is accessed, avoiding downtime to convert large |
| 95 | databases during a software upgrade. |
| 96 | <p>Complex refactoring involving multiple classes may be performed using the a |
| 97 | <a href="package-summary.html#storeConversion">store conversion</a>. The DPL |
| 98 | always provides access to your data via a {@code RawStore}, no matter what |
| 99 | changes have been made to persistent classes.</p> |
| 100 | </li> |
| 101 | <br> |
| 102 | <li>The performance of the Berkeley DB transactional engine is not compromised. |
| 103 | Operations are internally mapped directly to the engine API, object bindings |
| 104 | are lightweight, and all engine tuning parameters are available. For example, |
| 105 | a "dirty read" may be performed using an optional {@link |
| 106 | com.sleepycat.db.LockMode LockMode} parameter: |
| 107 | <pre class="code">Employer employer = employerByName.get(null, "Gizmo Inc", LockMode.READ_UNCOMMITTED);</pre> |
| 108 | For high performance applications, {@link com.sleepycat.db.DatabaseConfig |
| 109 | DatabaseConfig} parameters may be used to tune the performance of the Berkeley |
| 110 | DB engine. For example, the size of an internal Btree node can be specified |
| 111 | as follows: |
| 112 | <pre class="code"> |
| 113 | DatabaseConfig config = store.getPrimaryConfig(Employer.class); |
| 114 | config.setNodeMaxEntries(64); |
| 115 | store.setPrimaryConfig(config);</pre> |
| 116 | </li> |
| 117 | </ul> |
| 118 | |
| 119 | <a name="model"><h2>The Entity Model</h2></a> |
| 120 | |
| 121 | <p>The DPL is intended for applications that represent persistent domain |
| 122 | objects using Java classes. An <em>entity class</em> is an ordinary Java class |
| 123 | that has a primary key and is stored and accessed using a primary index. It |
| 124 | may also have any number of secondary keys, and entities may be accessed by |
| 125 | secondary key using a secondary index.</p> |
| 126 | |
| 127 | <p>An entity class may be defined with the {@link |
| 128 | com.sleepycat.persist.model.Entity Entity} annotation. For each entity class, |
| 129 | its primary key may be defined using the {@link |
| 130 | com.sleepycat.persist.model.PrimaryKey PrimaryKey} annotation and any number of |
| 131 | secondary keys may be defined using the {@link |
| 132 | com.sleepycat.persist.model.SecondaryKey SecondaryKey} annotation.</p> |
| 133 | |
| 134 | <p>In the following example, the {@code Person.ssn} (social security number) |
| 135 | field is the primary key and the {@code Person.employerIds} field is a |
| 136 | many-to-many secondary key.</p> |
| 137 | <pre class="code"> |
| 138 | {@literal @Entity} |
| 139 | class Person { |
| 140 | |
| 141 | {@literal @PrimaryKey} |
| 142 | String ssn; |
| 143 | |
| 144 | String name; |
| 145 | Address address; |
| 146 | |
| 147 | {@literal @SecondaryKey(relate=MANY_TO_MANY, relatedEntity=Employer.class)} |
| 148 | {@literal Set<Long> employerIds = new HashSet<Long>();} |
| 149 | |
| 150 | private Person() {} // For bindings |
| 151 | }</pre> |
| 152 | |
| 153 | <p>A set of entity classes constitutes an <em>entity model</em>. In addition |
| 154 | to isolated entity classes, an entity model may contain relationships between |
| 155 | entities. Relationships may be defined using the {@link |
| 156 | com.sleepycat.persist.model.SecondaryKey SecondaryKey} annotation. |
| 157 | Many-to-one, one-to-many, many-to-many and one-to-one relationships are |
| 158 | supported, as well as foreign key constraints.</p> |
| 159 | |
| 160 | <p>In the example above, a relationship between the {@code Person} and {@code |
| 161 | Employer} entities is defined via the {@code Person.employerIds} field. The |
| 162 | {@code relatedEntity=Employer.class} annotation property establishes foreign |
| 163 | key constraints to guarantee that every element of the {@code employerIds} set |
| 164 | is a valid {@code Employer} primary key.</p> |
| 165 | |
| 166 | <p>For more information on the entity model, see the {@link |
| 167 | com.sleepycat.persist.model.AnnotationModel AnnotationModel} and the {@link |
| 168 | com.sleepycat.persist.model.Entity Entity} annotation.</p> |
| 169 | |
| 170 | <p>The root object in the DPL is the {@link com.sleepycat.persist.EntityStore |
| 171 | EntityStore}. An entity store manages any number of objects for each entity |
| 172 | class defined in the model. The store provides access to the primary and |
| 173 | secondary indices for each entity class, for example:</p> |
| 174 | |
| 175 | <pre class="code"> |
| 176 | EntityStore store = new EntityStore(...); |
| 177 | |
| 178 | {@literal PrimaryIndex<String,Person> personBySsn} = |
| 179 | store.getPrimaryIndex(String.class, Person.class);</pre> |
| 180 | |
| 181 | <a name="example"><h2>A brief example</h2></a> |
| 182 | |
| 183 | <p>The following example shows how to define an entity model and how to store |
| 184 | and access persistent objects. Exception handling is omitted for brevity.</p> |
| 185 | |
| 186 | <pre class="code"> |
| 187 | import java.io.File; |
| 188 | import java.util.HashSet; |
| 189 | import java.util.Set; |
| 190 | |
| 191 | import com.sleepycat.db.DatabaseException; |
| 192 | import com.sleepycat.db.Environment; |
| 193 | import com.sleepycat.db.EnvironmentConfig; |
| 194 | import com.sleepycat.persist.EntityCursor; |
| 195 | import com.sleepycat.persist.EntityIndex; |
| 196 | import com.sleepycat.persist.EntityStore; |
| 197 | import com.sleepycat.persist.PrimaryIndex; |
| 198 | import com.sleepycat.persist.SecondaryIndex; |
| 199 | import com.sleepycat.persist.StoreConfig; |
| 200 | import com.sleepycat.persist.model.Entity; |
| 201 | import com.sleepycat.persist.model.Persistent; |
| 202 | import com.sleepycat.persist.model.PrimaryKey; |
| 203 | import com.sleepycat.persist.model.SecondaryKey; |
| 204 | import static com.sleepycat.persist.model.DeleteAction.NULLIFY; |
| 205 | import static com.sleepycat.persist.model.Relationship.ONE_TO_ONE; |
| 206 | import static com.sleepycat.persist.model.Relationship.ONE_TO_MANY; |
| 207 | import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE; |
| 208 | import static com.sleepycat.persist.model.Relationship.MANY_TO_MANY; |
| 209 | |
| 210 | // An entity class. |
| 211 | // |
| 212 | {@literal @Entity} |
| 213 | class Person { |
| 214 | |
| 215 | {@literal @PrimaryKey} |
| 216 | String ssn; |
| 217 | |
| 218 | String name; |
| 219 | Address address; |
| 220 | |
| 221 | {@literal @SecondaryKey(relate=MANY_TO_ONE, relatedEntity=Person.class)} |
| 222 | String parentSsn; |
| 223 | |
| 224 | {@literal @SecondaryKey(relate=ONE_TO_MANY)} |
| 225 | {@literal Set<String> emailAddresses = new HashSet<String>();} |
| 226 | |
| 227 | {@code @SecondaryKey(relate=MANY_TO_MANY, relatedEntity=Employer.class, |
| 228 | onRelatedEntityDelete=NULLIFY)} |
| 229 | {@code Set<Long> employerIds = new HashSet<Long>();} |
| 230 | |
| 231 | Person(String name, String ssn, String parentSsn) { |
| 232 | this.name = name; |
| 233 | this.ssn = ssn; |
| 234 | this.parentSsn = parentSsn; |
| 235 | } |
| 236 | |
| 237 | private Person() {} // For bindings |
| 238 | } |
| 239 | |
| 240 | // Another entity class. |
| 241 | // |
| 242 | {@literal @Entity} |
| 243 | class Employer { |
| 244 | |
| 245 | {@literal @PrimaryKey(sequence="ID")} |
| 246 | long id; |
| 247 | |
| 248 | {@literal @SecondaryKey(relate=ONE_TO_ONE)} |
| 249 | String name; |
| 250 | |
| 251 | Address address; |
| 252 | |
| 253 | Employer(String name) { |
| 254 | this.name = name; |
| 255 | } |
| 256 | |
| 257 | private Employer() {} // For bindings |
| 258 | } |
| 259 | |
| 260 | // A persistent class used in other classes. |
| 261 | // |
| 262 | {@literal @Persistent} |
| 263 | class Address { |
| 264 | String street; |
| 265 | String city; |
| 266 | String state; |
| 267 | int zipCode; |
| 268 | private Address() {} // For bindings |
| 269 | } |
| 270 | |
| 271 | // The data accessor class for the entity model. |
| 272 | // |
| 273 | class PersonAccessor { |
| 274 | |
| 275 | // Person accessors |
| 276 | // |
| 277 | {@literal PrimaryIndex<String,Person> personBySsn;} |
| 278 | {@literal SecondaryIndex<String,String,Person> personByParentSsn;} |
| 279 | {@literal SecondaryIndex<String,String,Person> personByEmailAddresses;} |
| 280 | {@literal SecondaryIndex<Long,String,Person> personByEmployerIds;} |
| 281 | |
| 282 | // Employer accessors |
| 283 | // |
| 284 | {@literal PrimaryIndex<Long,Employer> employerById;} |
| 285 | {@literal SecondaryIndex<String,Long,Employer> employerByName;} |
| 286 | |
| 287 | // Opens all primary and secondary indices. |
| 288 | // |
| 289 | public PersonAccessor(EntityStore store) |
| 290 | throws DatabaseException { |
| 291 | |
| 292 | personBySsn = store.getPrimaryIndex( |
| 293 | String.class, Person.class); |
| 294 | |
| 295 | personByParentSsn = store.getSecondaryIndex( |
| 296 | personBySsn, String.class, "parentSsn"); |
| 297 | |
| 298 | personByEmailAddresses = store.getSecondaryIndex( |
| 299 | personBySsn, String.class, "emailAddresses"); |
| 300 | |
| 301 | personByEmployerIds = store.getSecondaryIndex( |
| 302 | personBySsn, Long.class, "employerIds"); |
| 303 | |
| 304 | employerById = store.getPrimaryIndex( |
| 305 | Long.class, Employer.class); |
| 306 | |
| 307 | employerByName = store.getSecondaryIndex( |
| 308 | employerById, String.class, "name"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Open a transactional Berkeley DB engine environment. |
| 313 | // |
| 314 | EnvironmentConfig envConfig = new EnvironmentConfig(); |
| 315 | envConfig.setAllowCreate(true); |
| 316 | envConfig.setTransactional(true); |
| 317 | Environment env = new Environment(new File("/my/data"), envConfig); |
| 318 | |
| 319 | // Open a transactional entity store. |
| 320 | // |
| 321 | StoreConfig storeConfig = new StoreConfig(); |
| 322 | storeConfig.setAllowCreate(true); |
| 323 | storeConfig.setTransactional(true); |
| 324 | EntityStore store = new EntityStore(env, "PersonStore", storeConfig); |
| 325 | |
| 326 | // Initialize the data access object. |
| 327 | // |
| 328 | PersonAccessor dao = new PersonAccessor(store); |
| 329 | |
| 330 | // Add a parent and two children using the Person primary index. Specifying a |
| 331 | // non-null parentSsn adds the child Person to the sub-index of children for |
| 332 | // that parent key. |
| 333 | // |
| 334 | dao.personBySsn.put(new Person("Bob Smith", "111-11-1111", null)); |
| 335 | dao.personBySsn.put(new Person("Mary Smith", "333-33-3333", "111-11-1111")); |
| 336 | dao.personBySsn.put(new Person("Jack Smith", "222-22-2222", "111-11-1111")); |
| 337 | |
| 338 | // Print the children of a parent using a sub-index and a cursor. |
| 339 | // |
| 340 | {@literal EntityCursor<Person> children =} |
| 341 | dao.personByParentSsn.subIndex("111-11-1111").entities(); |
| 342 | try { |
| 343 | for (Person child : children) { |
| 344 | System.out.println(child.ssn + ' ' + child.name); |
| 345 | } |
| 346 | } finally { |
| 347 | children.close(); |
| 348 | } |
| 349 | |
| 350 | // Get Bob by primary key using the primary index. |
| 351 | // |
| 352 | Person bob = dao.personBySsn.get("111-11-1111"); |
| 353 | assert bob != null; |
| 354 | |
| 355 | // Create two employers. Their primary keys are assigned from a sequence. |
| 356 | // |
| 357 | Employer gizmoInc = new Employer("Gizmo Inc"); |
| 358 | Employer gadgetInc = new Employer("Gadget Inc"); |
| 359 | dao.employerById.put(gizmoInc); |
| 360 | dao.employerById.put(gadgetInc); |
| 361 | |
| 362 | // Bob has two jobs and two email addresses. |
| 363 | // |
| 364 | bob.employerIds.add(gizmoInc.id); |
| 365 | bob.employerIds.add(gadgetInc.id); |
| 366 | bob.emailAddresses.add("[email protected]"); |
| 367 | bob.emailAddresses.add("[email protected]"); |
| 368 | |
| 369 | // Update Bob's record. |
| 370 | // |
| 371 | dao.personBySsn.put(bob); |
| 372 | |
| 373 | // Bob can now be found by both email addresses. |
| 374 | // |
| 375 | bob = dao.personByEmailAddresses.get("[email protected]"); |
| 376 | assert bob != null; |
| 377 | bob = dao.personByEmailAddresses.get("[email protected]"); |
| 378 | assert bob != null; |
| 379 | |
| 380 | // Bob can also be found as an employee of both employers. |
| 381 | // |
| 382 | {@literal EntityIndex<String,Person> employees;} |
| 383 | employees = dao.personByEmployerIds.subIndex(gizmoInc.id); |
| 384 | assert employees.contains("111-11-1111"); |
| 385 | employees = dao.personByEmployerIds.subIndex(gadgetInc.id); |
| 386 | assert employees.contains("111-11-1111"); |
| 387 | |
| 388 | // When an employer is deleted, the onRelatedEntityDelete=NULLIFY for the |
| 389 | // employerIds key causes the deleted ID to be removed from Bob's employerIds. |
| 390 | // |
| 391 | dao.employerById.delete(gizmoInc.id); |
| 392 | bob = dao.personBySsn.get("111-11-1111"); |
| 393 | assert !bob.employerIds.contains(gizmoInc.id); |
| 394 | |
| 395 | store.close(); |
| 396 | env.close(); |
| 397 | </pre> |
| 398 | <p>The example illustrates several characteristics of the DPL:</p> |
| 399 | <ul> |
| 400 | <li>Persistent data and keys are defined in terms of instance fields. For |
| 401 | brevity the example does not show getter and setter methods, although these |
| 402 | would normally exist to provide encapsulation. The DPL accesses fields during |
| 403 | object serialization and deserialization, rather than calling getter/setter |
| 404 | methods, leaving business methods free to enforce arbitrary validation rules. |
| 405 | For example: |
| 406 | <pre class="code"> |
| 407 | {@literal @Persistent} |
| 408 | public class ConstrainedValue { |
| 409 | |
| 410 | private int min; |
| 411 | private int max; |
| 412 | private int value; |
| 413 | |
| 414 | private ConstrainedValue() {} // For bindings |
| 415 | |
| 416 | public ConstrainedValue(int min, int max) { |
| 417 | this.min = min; |
| 418 | this.max = max; |
| 419 | value = min; |
| 420 | } |
| 421 | |
| 422 | public setValue(int value) { |
| 423 | if (value < min || value > max) { |
| 424 | throw new IllegalArgumentException("out of range"); |
| 425 | } |
| 426 | this.value = value; |
| 427 | } |
| 428 | } |
| 429 | </pre> |
| 430 | The above {@code setValue} method would not work if it were called during |
| 431 | object deserialization, since the order of setting fields is arbitrary. The |
| 432 | {@code min} and {@code max} fields may not be set before the {@code value} is |
| 433 | set. |
| 434 | </li> |
| 435 | <br> |
| 436 | <li>The example creates a transactional store and therefore all operations are |
| 437 | transaction protected. Because no explicit transactions are used, auto-commit |
| 438 | is used implicitly. |
| 439 | |
| 440 | <p>Explicit transactions may also be used to group multiple operations in a |
| 441 | single transaction, and all access methods have optional transaction |
| 442 | parameters. For example, the following two operations are performed atomically |
| 443 | in a transaction: |
| 444 | <pre class="code"> |
| 445 | Transaction txn = env.beginTransaction(null, null); |
| 446 | dao.employerById.put(txn, gizmoInc); |
| 447 | dao.employerById.put(txn, gadgetInc); |
| 448 | txn.commit(); |
| 449 | </pre> |
| 450 | </li> |
| 451 | <li>To provide maximum performance, the DPL operations map directly to the |
| 452 | Btree operations of the Berkeley DB engine. Unlike other persistence |
| 453 | approaches, keys and indices are exposed for direct access and performance |
| 454 | tuning. |
| 455 | <p>Queries are implemented by calling methods of the primary and secondary |
| 456 | indices. An {@link com.sleepycat.persist.EntityJoin EntityJoin} class is also |
| 457 | available for performing equality joins. For example, the following code |
| 458 | queries all of Bob's children that work for Gizmo Inc: |
| 459 | <pre class="code"> |
| 460 | {@literal EntityJoin<String,Person> join = new EntityJoin(dao.personBySsn);} |
| 461 | |
| 462 | join.addCondition(dao.personByParentSsn, "111-11-1111"); |
| 463 | join.addCondition(dao.personByEmployerIds, gizmoInc.id); |
| 464 | |
| 465 | {@literal ForwardCursor<Person> results = join.entities();} |
| 466 | try { |
| 467 | for (Person person : results) { |
| 468 | System.out.println(person.ssn + ' ' + person.name); |
| 469 | } |
| 470 | } finally { |
| 471 | results.close(); |
| 472 | } |
| 473 | </li> |
| 474 | <li>Object relationships are based on keys. When a {@code Person} with a given |
| 475 | employer ID in its {@code employerIds} set is stored, the {@code Person} object |
| 476 | becomes part of the collection of employees for that employer. This collection |
| 477 | of employees is accessed using a {@link |
| 478 | com.sleepycat.persist.SecondaryIndex#subIndex SecondaryIndex.subIndex} for the |
| 479 | employer ID, as shown below: |
| 480 | <pre class="code"> |
| 481 | {@literal EntityCursor<Person> employees =} |
| 482 | dao.personByEmployerIds.subIndex(gizmoInc.id).entities(); |
| 483 | try { |
| 484 | for (Person employee : employees) { |
| 485 | System.out.println(employee.ssn + ' ' + employee.name); |
| 486 | } |
| 487 | } finally { |
| 488 | employees.close(); |
| 489 | } |
| 490 | </pre></li> |
| 491 | <li>Note that when Bob's employer is deleted in the example, the {@code Person} |
| 492 | object for Bob is refetched to see the change to its {@code employerIds}. This |
| 493 | is because objects are accessed by value, not by reference. In other words, no |
| 494 | object cache or "persistence context" is maintained by the DPL. The low level |
| 495 | caching of the embedded Berkeley DB engine, combined with lightweight object |
| 496 | bindings, provides maximum performance.</li> |
| 497 | </ul> |
| 498 | |
| 499 | <a name="whichAPI"><h2>Which API to use?</h2></a> |
| 500 | |
| 501 | <p>The Berkeley DB engine has a {@link com.sleepycat.db Base API}, a {@link |
| 502 | com.sleepycat.collections Collections API} and a {@link com.sleepycat.persist |
| 503 | Direct Persistence Layer (DPL)}. Follow these guidelines if you are not sure |
| 504 | which API to use:</p> |
| 505 | <ul> |
| 506 | <li>When Java classes are used to represent domain objects in an application, |
| 507 | the DPL is recommended. The more domain classes, the more value there is in |
| 508 | using annotations to define your schema.</li> |
| 509 | <br> |
| 510 | <li>When porting an application between Berkeley DB and Berkeley DB Java |
| 511 | Edition, or when you've chosen not to use Java classes to represent domain |
| 512 | objects, then the Base API is recommended. You may also prefer to use this API |
| 513 | if you have very few domain classes.</li> |
| 514 | <br> |
| 515 | <li>The Collections API is useful for interoperating with external components |
| 516 | because it conforms to the standard Java Collections Framework. It is |
| 517 | therefore useful in combination with both the Base API and the DPL. You may |
| 518 | prefer this API because it provides the familiar Java Collections |
| 519 | interface.</li> |
| 520 | </ul> |
| 521 | |
| 522 | <a name="java14and15"><h2>Java 1.5 dependencies</h2></a> |
| 523 | |
| 524 | <p>The DPL uses two features of Java 1.5: generic types and annotations. If |
| 525 | you wish to avoid using these two Java 1.5 features, the DPL provides options |
| 526 | for doing so.</p> |
| 527 | |
| 528 | <a name="genericTypes"><h3>Generic Types</h3></a> |
| 529 | |
| 530 | <p>Generic types are used to provide type safety, especially for the {@link |
| 531 | com.sleepycat.persist.PrimaryIndex PrimaryIndex}, {@link |
| 532 | com.sleepycat.persist.SecondaryIndex SecondaryIndex}, and {@link |
| 533 | com.sleepycat.persist.EntityCursor EntityCursor} classes. If you don't wish to |
| 534 | use generic types, you can simply not declare your index and cursor objects |
| 535 | using generic type parameters. This is the same as using the Java 1.5 |
| 536 | Collections Framework without using generic types.</p> |
| 537 | |
| 538 | <a name="annotations"><h3>Annotations</h3></a> |
| 539 | |
| 540 | <p>If you don't wish to use annotations, you can provide another source of |
| 541 | metadata by implementing an {@link com.sleepycat.persist.model.EntityModel |
| 542 | EntityModel} class. For example, naming conventions, static members, or an XML |
| 543 | configuration file might be used as a source of metadata. However, if you |
| 544 | don't use annotations then you won't be able to use bytecode enhancement, which |
| 545 | is described next.</p> |
| 546 | |
| 547 | <a name="bytecode"><h2>Bytecode Enhancement</h2></a> |
| 548 | |
| 549 | <p>The persistent fields of a class may be private, package-private, protected |
| 550 | or public. The DPL can access persistent fields either by bytecode enhancement |
| 551 | or by reflection.</p> |
| 552 | |
| 553 | <p>Bytecode enhancement may be used to fully optimize binding performance and |
| 554 | to avoid the use of Java reflection. In applications that are CPU bound, |
| 555 | avoiding Java reflection can have a significant performance impact.</p> |
| 556 | |
| 557 | <p>Bytecode enhancement may be performed either at runtime or at build time |
| 558 | (offline). When enhancement is performed at runtime, persistent classes are |
| 559 | enhanced as they are loaded. When enhancement is performed offline, class |
| 560 | files are enhanced during a post-compilation step. |
| 561 | Enhanced classes are used to efficiently access all fields and default |
| 562 | constructors, including non-public members.</p> |
| 563 | |
| 564 | <p>See {@link com.sleepycat.persist.model.ClassEnhancer ClassEnhancer} for |
| 565 | bytecode enhancement configuration details.</p> |
| 566 | |
| 567 | <p>If bytecode enhancement is not used as described above, the DPL will use |
| 568 | reflection for accessing persistent fields and the default constructor. The |
| 569 | {@link java.lang.reflect.AccessibleObject#setAccessible |
| 570 | AccessibleObject.setAccessible} method is called by the DPL to enable access to |
| 571 | non-public fields and constructors. If you are running under a Java security |
| 572 | manager you must configure your security policy to allow the following |
| 573 | permission:</p> |
| 574 | |
| 575 | <p>{@code permission java.lang.reflect.ReflectPermission "suppressAccessChecks";} |
| 576 | |
| 577 | <p>There are three cases where setting the above permission is <em>not</em> |
| 578 | required:</p> |
| 579 | <ol> |
| 580 | <li>If you are not running under a Java Security Manager, then access to |
| 581 | non-public members via reflection is not restricted. This is the default for |
| 582 | J2SE.</li> |
| 583 | <br> |
| 584 | <li>If all persistent fields and default constructors are {@code public} then |
| 585 | they can be accessed via reflection without special permissions, even when |
| 586 | running under a Java Security Manager. However, declaring {@code public} |
| 587 | instance fields is not recommended because it discourages encapsulation.</li> |
| 588 | <br> |
| 589 | <li>If bytecode enhancement is used as described above, then reflection will |
| 590 | not be used.</li> |
| 591 | </ol> |
| 592 | |
| 593 | <p>It is well known that executing generated code is faster than reflection. |
| 594 | However, this performance difference may or may not impact a given application |
| 595 | since it may be overshadowed by other factors. Performance testing in a |
| 596 | realistic usage scenario is the best way to determine the impact. If you are |
| 597 | determined to avoid the use of reflection then option 3 above is |
| 598 | recommended.</p> |
| 599 | |
| 600 | </body> |
| 601 | </html> |