Java Doc for PrimaryIndex.java in  » JMX » je » com » sleepycat » persist » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » je » com.sleepycat.persist 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


com.sleepycat.persist.PrimaryIndex

PrimaryIndex
public class PrimaryIndex extends BasicIndex (Code)
The primary index for an entity class and its primary key.

PrimaryIndex objects are thread-safe. Multiple threads may safely call the methods of a shared PrimaryIndex object.

PrimaryIndex implements EntityIndex to map the primary key type (PK) to the entity type (E).

The Entity annotation may be used to define an entity class and the PrimaryKey annotation may be used to define a primary key as shown in the following example.

 class Employee {
 long id;
 String name;
 Employee(long id, String name) {
 this.id = id;
 this.name = name;
 }
 private Employee() {} // For bindings
 }

To obtain the PrimaryIndex for a given entity class, call EntityStore.getPrimaryIndex EntityStore.getPrimaryIndex , passing the primary key class and the entity class. For example:

 EntityStore store = new EntityStore(...);
  PrimaryIndex   primaryIndex =
 store.getPrimaryIndex(Long.class, Employee.class);

Note that Long.class is passed as the primary key class, but the primary key field has the primitive type long . When a primitive primary key field is used, the corresponding primitive wrapper class is used to access the primary index. For more information on key field types, see PrimaryKey .

The PrimaryIndex provides the primary storage and access methods for the instances of a particular entity class. Entities are inserted and updated in the PrimaryIndex by calling a method in the family of PrimaryIndex.put methods. The PrimaryIndex.put method will insert the entity if no entity with the same primary key already exists. If an entity with the same primary key does exist, it will update the entity and return the existing (old) entity. For example:

 Employee oldEntity;
 oldEntity = primaryIndex.put(new Employee(1, "Jane Smith"));    // Inserts an entity
 assert oldEntity == null;
 oldEntity = primaryIndex.put(new Employee(2, "Joan Smith"));    // Inserts an entity
 assert oldEntity == null;
 oldEntity = primaryIndex.put(new Employee(2, "Joan M. Smith")); // Updates an entity
 assert oldEntity != null;

The PrimaryIndex.putNoReturn method can be used to avoid the overhead of returning the existing entity, when the existing entity is not important to the application. The return type of PrimaryIndex.putNoReturn is void. For example:

 primaryIndex.putNoReturn(new Employee(1, "Jane Smith"));    // Inserts an entity
 primaryIndex.putNoReturn(new Employee(2, "Joan Smith"));    // Inserts an entity
 primaryIndex.putNoReturn(new Employee(2, "Joan M. Smith")); // Updates an entity

The PrimaryIndex.putNoOverwrite method can be used to ensure that an existing entity is not overwritten. PrimaryIndex.putNoOverwrite returns true if the entity was inserted, or false if an existing entity exists and no action was taken. For example:

 boolean inserted;
 inserted = primaryIndex.putNoOverwrite(new Employee(1, "Jane Smith"));    // Inserts an entity
 assert inserted;
 inserted = primaryIndex.putNoOverwrite(new Employee(2, "Joan Smith"));    // Inserts an entity
 assert inserted;
 inserted = primaryIndex.putNoOverwrite(new Employee(2, "Joan M. Smith")); // No action was taken!
 assert !inserted;

Primary key values must be unique, in other words, each instance of a given entity class must have a distinct primary key value. Rather than assigning the unique primary key values yourself, a sequence can be used to assign sequential integer values automatically, starting with the value 1 (one). A sequence is defined using the PrimaryKey.sequence annotation property. For example:

 class Employee {
 long id;
 String name;
 Employee(String name) {
 this.name = name;
 }
 private Employee() {} // For bindings
 }

The name of the sequence used above is "ID". Any name can be used. If the same sequence name is used in more than one entity class, the sequence will be shared by those classes, in other words, a single sequence of integers will be used for all instances of those classes. See PrimaryKey.sequence for more information.

Any method in the family of PrimaryIndex.put methods may be used to insert entities where the primary key is assigned from a sequence. When the PrimaryIndex.put method returns, the primary key field of the entity object will be set to the assigned key value. For example:

 Employee employee;
 employee = new Employee("Jane Smith");
 primaryIndex.putNoReturn(employee);    // Inserts an entity
 assert employee.id == 1;
 employee = new Employee("Joan Smith");
 primaryIndex.putNoReturn(employee);    // Inserts an entity
 assert employee.id == 2;

This begs the question: How do you update an existing entity, without assigning a new primary key? The answer is that the PrimaryIndex.put methods will only assign a new key from the sequence if the primary key field is zero or null (for reference types). If an entity with a non-zero and non-null key field is passed to a PrimaryIndex.put method, any existing entity with that primary key value will be updated. For example:

 Employee employee;
 employee = new Employee("Jane Smith");
 primaryIndex.putNoReturn(employee);    // Inserts an entity
 assert employee.id == 1;
 employee = new Employee("Joan Smith");
 primaryIndex.putNoReturn(employee);    // Inserts an entity
 assert employee.id == 2;
 employee.name = "Joan M. Smith";
 primaryIndex.putNoReturn(employee);    // Updates an existing entity
 assert employee.id == 2;

Since PrimaryIndex implements the EntityIndex interface, it shares the common index methods for retrieving and deleting entities, opening cursors and using transactions. See EntityIndex for more information on these topics.

Note that when using an index, keys and values are stored and retrieved by value not by reference. In other words, if an entity object is stored and then retrieved, or retrieved twice, each object will be a separate instance. For example, in the code below the assertion will always fail.

 MyKey key = ...;
 MyEntity entity1 = new MyEntity(key, ...);
 index.put(entity1);
 MyEntity entity2 = index.get(key);
 assert entity1 == entity2; // always fails!
 

author:
   Mark Hayes



Constructor Summary
public  PrimaryIndex(Database database, Class<PK> keyClass, EntryBinding keyBinding, Class<E> entityClass, EntityBinding entityBinding)
     Creates a primary index without using an EntityStore.

This constructor is not normally needed and is provided for applications that wish to use custom bindings along with the Direct Persistence Layer.


Method Summary
public  Eget(PK key)
    
public  Eget(Transaction txn, PK key, LockMode lockMode)
    
public  DatabasegetDatabase()
     Returns the underlying database for this index.
public  EntityBindinggetEntityBinding()
     Returns the entity binding for this index.
public  Class<E>getEntityClass()
     Returns the entity class for this index.
public  EntryBindinggetKeyBinding()
     Returns the primary key binding for this index.
public  Class<PK>getKeyClass()
     Returns the primary key class for this index.
public  Map<PK, E>map()
    
public  Eput(E entity)
     Inserts an entity and returns null, or updates it if the primary key already exists and returns the existing entity.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.

Auto-commit is used implicitly if the store is transactional.


Parameters:
  entity - the entity to be inserted or updated.
public  Eput(Transaction txn, E entity)
     Inserts an entity and returns null, or updates it if the primary key already exists and returns the existing entity.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.


Parameters:
  txn - the transaction used to protect this operation, null to useauto-commit, or null if the store is non-transactional.
Parameters:
  entity - the entity to be inserted or updated.
public  booleanputNoOverwrite(E entity)
     Inserts an entity and returns true, or returns false if the primary key already exists.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.

Auto-commit is used implicitly if the store is transactional.


Parameters:
  entity - the entity to be inserted.
public  booleanputNoOverwrite(Transaction txn, E entity)
     Inserts an entity and returns true, or returns false if the primary key already exists.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.


Parameters:
  txn - the transaction used to protect this operation, null to useauto-commit, or null if the store is non-transactional.
Parameters:
  entity - the entity to be inserted.
public  voidputNoReturn(E entity)
     Inserts an entity, or updates it if the primary key already exists (does not return the existing entity).
public  voidputNoReturn(Transaction txn, E entity)
     Inserts an entity, or updates it if the primary key already exists (does not return the existing entity).
public synchronized  SortedMap<PK, E>sortedMap()
    


Constructor Detail
PrimaryIndex
public PrimaryIndex(Database database, Class<PK> keyClass, EntryBinding keyBinding, Class<E> entityClass, EntityBinding entityBinding) throws DatabaseException(Code)
Creates a primary index without using an EntityStore.

This constructor is not normally needed and is provided for applications that wish to use custom bindings along with the Direct Persistence Layer. Normally, EntityStore.getPrimaryIndexgetPrimaryIndex is used instead.

Note that when this constructor is used directly, primary keys cannot be automatically assigned from a sequence. The key assignment feature requires knowledge of the primary key field, which is only available if an EntityStore is used. Of course, primary keys may be assigned from a sequence manually before calling the put methods in this class.


Parameters:
  database - the primary database.
Parameters:
  keyClass - the class of the primary key.
Parameters:
  keyBinding - the binding to be used for primary keys.
Parameters:
  entityClass - the class of the entities stored in this index.
Parameters:
  entityBinding - the binding to be used for entities.




Method Detail
get
public E get(PK key) throws DatabaseException(Code)



get
public E get(Transaction txn, PK key, LockMode lockMode) throws DatabaseException(Code)



getDatabase
public Database getDatabase()(Code)
Returns the underlying database for this index. the database.



getEntityBinding
public EntityBinding getEntityBinding()(Code)
Returns the entity binding for this index. the entity binding.



getEntityClass
public Class<E> getEntityClass()(Code)
Returns the entity class for this index. the entity class.



getKeyBinding
public EntryBinding getKeyBinding()(Code)
Returns the primary key binding for this index. the key binding.



getKeyClass
public Class<PK> getKeyClass()(Code)
Returns the primary key class for this index. the key class.



map
public Map<PK, E> map()(Code)



put
public E put(E entity) throws DatabaseException(Code)
Inserts an entity and returns null, or updates it if the primary key already exists and returns the existing entity.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.

Auto-commit is used implicitly if the store is transactional.


Parameters:
  entity - the entity to be inserted or updated. the existing entity that was updated, or null if the entity wasinserted.



put
public E put(Transaction txn, E entity) throws DatabaseException(Code)
Inserts an entity and returns null, or updates it if the primary key already exists and returns the existing entity.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.


Parameters:
  txn - the transaction used to protect this operation, null to useauto-commit, or null if the store is non-transactional.
Parameters:
  entity - the entity to be inserted or updated. the existing entity that was updated, or null if the entity wasinserted.



putNoOverwrite
public boolean putNoOverwrite(E entity) throws DatabaseException(Code)
Inserts an entity and returns true, or returns false if the primary key already exists.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.

Auto-commit is used implicitly if the store is transactional.


Parameters:
  entity - the entity to be inserted. true if the entity was inserted, or false if an entity with thesame primary key is already present.



putNoOverwrite
public boolean putNoOverwrite(Transaction txn, E entity) throws DatabaseException(Code)
Inserts an entity and returns true, or returns false if the primary key already exists.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.


Parameters:
  txn - the transaction used to protect this operation, null to useauto-commit, or null if the store is non-transactional.
Parameters:
  entity - the entity to be inserted. true if the entity was inserted, or false if an entity with thesame primary key is already present.



putNoReturn
public void putNoReturn(E entity) throws DatabaseException(Code)
Inserts an entity, or updates it if the primary key already exists (does not return the existing entity). This method may be used instead of PrimaryIndex.put(Object) to save the overhead of returning the existing entity.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.

Auto-commit is used implicitly if the store is transactional.


Parameters:
  entity - the entity to be inserted or updated.



putNoReturn
public void putNoReturn(Transaction txn, E entity) throws DatabaseException(Code)
Inserts an entity, or updates it if the primary key already exists (does not return the existing entity). This method may be used instead of PrimaryIndex.put(Transaction,Object) to save the overhead of returning the existing entity.

If a PrimaryKey.sequence is used and the primary key field of the given entity is null or zero, this method will assign the next value from the sequence to the primary key field of the given entity.


Parameters:
  txn - the transaction used to protect this operation, null to useauto-commit, or null if the store is non-transactional.
Parameters:
  entity - the entity to be inserted or updated.



sortedMap
public synchronized SortedMap<PK, E> sortedMap()(Code)



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.