Java Doc for EntityCursor.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.EntityCursor

EntityCursor
public interface EntityCursor extends ForwardCursor<V>(Code)
Traverses entity values or key values and allows deleting or updating the entity at the current cursor position. The value type (V) is either an entity class or a key class, depending on how the cursor was opened.

EntityCursor objects are not thread-safe. Cursors should be opened, used and closed by a single thread.

Cursors are opened using the EntityIndex.keys and EntityIndex.entities family of methods. These methods are available for objects of any class that implements EntityIndex : PrimaryIndex , SecondaryIndex , and the indices returned by SecondaryIndex.keysIndex and SecondaryIndex.subIndex . A ForwardCursor , which implements a subset of cursor operations, is also available via the EntityJoin.keys and EntityJoin.entities methods.

Values are always returned by a cursor in key order, where the key is defined by the underlying EntityIndex . For example, a cursor on a SecondaryIndex returns values ordered by secondary key, while an index on a PrimaryIndex or a SecondaryIndex.subIndex returns values ordered by primary key.

WARNING: Cursors must always be closed to prevent resource leaks which could lead to the index becoming unusable or cause an OutOfMemoryError. To ensure that a cursor is closed in the face of exceptions, call EntityCursor.close in a finally block. For example, the following code traverses all Employee entities and closes the cursor whether or not an exception occurs:

 class Employee {
 long id;
 String department;
 String name;
 private Employee() {}
 }
 EntityStore store = ...
  PrimaryIndex   primaryIndex =
 store.getPrimaryIndex(Long.class, Employee.class);
  EntityCursor   cursor = primaryIndex.entities();
 try {
 for (Employee entity = cursor.first();
 entity != null;
 entity = cursor.next()) {
 // Do something with the entity...
 }
 } finally {
 cursor.close();
 }

Initializing the Cursor Position

When it is opened, a cursor is not initially positioned on any value; in other words, it is uninitialized. Most methods in this interface initialize the cursor position but certain methods, for example, EntityCursor.current and EntityCursor.delete , throw IllegalStateException when called for an uninitialized cursor.

Note that the EntityCursor.next and EntityCursor.prev methods return the first or last value respectively for an uninitialized cursor. This allows the loop in the example above to be rewritten as follows:

  EntityCursor   cursor = primaryIndex.entities();
 try {
 Employee entity;
 while ((entity = cursor.next()) != null) {
 // Do something with the entity...
 }
 } finally {
 cursor.close();
 }

Cursors and Iterators

The EntityCursor.iterator method can be used to return a standard Java Iterator that returns the same values that the cursor returns. For example:

  EntityCursor   cursor = primaryIndex.entities();
 try {
  Iterator   i = cursor.iterator();
 while (i.hasNext()) {
 Employee entity = i.next();
 // Do something with the entity...
 }
 } finally {
 cursor.close();
 }

The Iterable interface is also extended by EntityCursor to allow using the cursor as the target of a Java "foreach" statement:

  EntityCursor   cursor = primaryIndex.entities();
 try {
 for (Employee entity : cursor) {
 // Do something with the entity...
 }
 } finally {
 cursor.close();
 }

The iterator uses the cursor directly, so any changes to the cursor position impact the iterator and vice versa. The iterator advances the cursor by calling EntityCursor.next() when Iterator.hasNext or Iterator.next is called. Because of this interaction, to keep things simple it is best not to mix the use of an EntityCursor Iterator with the use of the EntityCursor traversal methods such as EntityCursor.next() , for a single EntityCursor object.

Key Ranges

A key range may be specified when opening the cursor, to restrict the key range of the cursor to a subset of the complete range of keys in the index. A fromKey and/or toKey parameter may be specified when calling EntityIndex.keys(ObjectbooleanObjectboolean) or EntityIndex.entities(ObjectbooleanObjectboolean) . The key arguments may be specified as inclusive or exclusive values.

Whenever a cursor with a key range is moved, the key range bounds will be checked, and the cursor will never be positioned outside the range. The EntityCursor.first cursor value is the first existing value in the range, and the EntityCursor.last cursor value is the last existing value in the range. For example, the following code traverses Employee entities with keys from 100 (inclusive) to 200 (exclusive):

  EntityCursor   cursor = primaryIndex.entities(100, true, 200, false);
 try {
 for (Employee entity : cursor) {
 // Do something with the entity...
 }
 } finally {
 cursor.close();
 }

Duplicate Keys

When using a cursor for a SecondaryIndex , the keys in the index may be non-unique (duplicates) if SecondaryKey.relate is Relationship.MANY_TO_ONE MANY_TO_ONE or Relationship.MANY_TO_MANYMANY_TO_MANY . For example, a MANY_TO_ONE Employee.department secondary key is non-unique because there are multiple Employee entities with the same department key value. The EntityCursor.nextDup , EntityCursor.prevDup , EntityCursor.nextNoDup and EntityCursor.prevNoDup methods may be used to control how non-unique keys are returned by the cursor.

EntityCursor.nextDup and EntityCursor.prevDup return the next or previous value only if it has the same key as the current value, and null is returned when a different key is encountered. For example, these methods can be used to return all employees in a given department.

EntityCursor.nextNoDup and EntityCursor.prevNoDup return the next or previous value with a unique key, skipping over values that have the same key. For example, these methods can be used to return the first employee in each department.

For example, the following code will find the first employee in each department with EntityCursor.nextNoDup until it finds a department name that matches a particular regular expression. For each matching department it will find all employees in that department using EntityCursor.nextDup .

  SecondaryIndex   secondaryIndex =
 store.getSecondaryIndex(primaryIndex, String.class, "department");
 String regex = ...;
  EntityCursor   cursor = secondaryIndex.entities();
 try {
 for (Employee entity = cursor.first();
 entity != null;
 entity = cursor.nextNoDup()) {
 if (entity.department.matches(regex)) {
 while (entity != null) {
 // Do something with the matching entities...
 entity = cursor.nextDup();
 }
 }
 }
 } finally {
 cursor.close();
 }

Updating and Deleting Entities with a Cursor

The EntityCursor.update and EntityCursor.delete methods operate on the entity at the current cursor position. Cursors on any type of index may be used to delete entities. For example, the following code deletes all employees in departments which have names that match a particular regular expression:

  SecondaryIndex   secondaryIndex =
 store.getSecondaryIndex(primaryIndex, String.class, "department");
 String regex = ...;
  EntityCursor   cursor = secondaryIndex.entities();
 try {
 for (Employee entity = cursor.first();
 entity != null;
 entity = cursor.nextNoDup()) {
 if (entity.department.matches(regex)) {
 while (entity != null) {
 cursor.delete();
 entity = cursor.nextDup();
 }
 }
 }
 } finally {
 cursor.close();
 }

Note that the cursor can be moved to the next (or previous) value after deleting the entity at the current position. This is an important property of cursors, since without it you would not be able to easily delete while processing multiple values with a cursor. A cursor positioned on a deleted entity is in a special state. In this state, EntityCursor.current will return null, EntityCursor.delete will return false, and EntityCursor.update will return false.

The EntityCursor.update method is supported only if the value type is an entity class (not a key class) and the underlying index is a PrimaryIndex ; in other words, for a cursor returned by one of the PrimaryIndex.entities methods. For example, the following code changes all employee names to uppercase:

  EntityCursor   cursor = primaryIndex.entities();
 try {
 for (Employee entity = cursor.first();
 entity != null;
 entity = cursor.next()) {
 entity.name = entity.name.toUpperCase();
 cursor.update(entity);
 }
 } finally {
 cursor.close();
 }

author:
   Mark Hayes




Method Summary
 voidclose()
     Closes the cursor.
 intcount()
     Returns the number of values (duplicates) for the key at the cursor position, or returns zero if all values for the key have been deleted, Returns one or zero if the underlying index has unique keys.
 Vcurrent()
     Returns the value at the cursor position, or null if the value at the cursor position has been deleted.
 Vcurrent(LockMode lockMode)
     Returns the value at the cursor position, or null if the value at the cursor position has been deleted.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT.
 booleandelete()
     Deletes the entity at the cursor position.
throws:
  IllegalStateException - if the cursor is uninitialized.
throws:
  UnsupportedOperationException - if the index is read only.
 EntityCursor<V>dup()
     Duplicates the cursor at the cursor position.
 Vfirst()
     Moves the cursor to the first value and returns it, or returns null if the cursor range is empty.
 Vfirst(LockMode lockMode)
     Moves the cursor to the first value and returns it, or returns null if the cursor range is empty.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT.
 Iterator<V>iterator()
     Returns an iterator over the key range, starting with the value following the current position or at the first value if the cursor is uninitialized.
 Iterator<V>iterator(LockMode lockMode)
     Returns an iterator over the key range, starting with the value following the current position or at the first value if the cursor is uninitialized.
Parameters:
  lockMode - the lock mode to use for all operations performedusing the iterator, or null to use LockMode.DEFAULT.
 Vlast()
     Moves the cursor to the last value and returns it, or returns null if the cursor range is empty.
 Vlast(LockMode lockMode)
     Moves the cursor to the last value and returns it, or returns null if the cursor range is empty.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT.
 Vnext()
     Moves the cursor to the next value and returns it, or returns null if there are no more values in the cursor range.
 Vnext(LockMode lockMode)
     Moves the cursor to the next value and returns it, or returns null if there are no more values in the cursor range.
 VnextDup()
     Moves the cursor to the next value with the same key (duplicate) and returns it, or returns null if no more values are present for the key at the current position.
 VnextDup(LockMode lockMode)
     Moves the cursor to the next value with the same key (duplicate) and returns it, or returns null if no more values are present for the key at the current position.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT.
 VnextNoDup()
     Moves the cursor to the next value with a different key and returns it, or returns null if there are no more unique keys in the cursor range.
 VnextNoDup(LockMode lockMode)
     Moves the cursor to the next value with a different key and returns it, or returns null if there are no more unique keys in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.first .
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT.
 Vprev()
     Moves the cursor to the previous value and returns it, or returns null if there are no preceding values in the cursor range.
 Vprev(LockMode lockMode)
     Moves the cursor to the previous value and returns it, or returns null if there are no preceding values in the cursor range.
 VprevDup()
     Moves the cursor to the previous value with the same key (duplicate) and returns it, or returns null if no preceding values are present for the key at the current position.
 VprevDup(LockMode lockMode)
     Moves the cursor to the previous value with the same key (duplicate) and returns it, or returns null if no preceding values are present for the key at the current position.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT.
 VprevNoDup()
     Moves the cursor to the preceding value with a different key and returns it, or returns null if there are no preceding unique keys in the cursor range.
 VprevNoDup(LockMode lockMode)
     Moves the cursor to the preceding value with a different key and returns it, or returns null if there are no preceding unique keys in the cursor range.
 booleanupdate(V entity)
     Replaces the entity at the cursor position with the given entity.
Parameters:
  entity - the entity to replace the entity at the current position.



Method Detail
close
void close() throws DatabaseException(Code)
Closes the cursor.



count
int count() throws DatabaseException(Code)
Returns the number of values (duplicates) for the key at the cursor position, or returns zero if all values for the key have been deleted, Returns one or zero if the underlying index has unique keys.

LockMode.DEFAULT is used implicitly.

the number of duplicates, or zero if all values for the currentkey have been deleted.
throws:
  IllegalStateException - if the cursor is uninitialized.



current
V current() throws DatabaseException(Code)
Returns the value at the cursor position, or null if the value at the cursor position has been deleted.

LockMode.DEFAULT is used implicitly.

the value at the cursor position, or null if it has beendeleted.
throws:
  IllegalStateException - if the cursor is uninitialized.



current
V current(LockMode lockMode) throws DatabaseException(Code)
Returns the value at the cursor position, or null if the value at the cursor position has been deleted.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the value at the cursor position, or null if it has beendeleted.
throws:
  IllegalStateException - if the cursor is uninitialized.



delete
boolean delete() throws DatabaseException(Code)
Deletes the entity at the cursor position.
throws:
  IllegalStateException - if the cursor is uninitialized.
throws:
  UnsupportedOperationException - if the index is read only. true if successful or false if the entity at the currentposition has been deleted.



dup
EntityCursor<V> dup() throws DatabaseException(Code)
Duplicates the cursor at the cursor position. The returned cursor will be initially positioned at the same position as this current cursor, and will inherit this cursor's Transaction and CursorConfig . the duplicated cursor.



first
V first() throws DatabaseException(Code)
Moves the cursor to the first value and returns it, or returns null if the cursor range is empty.

LockMode.DEFAULT is used implicitly.

the first value, or null if the cursor range is empty.



first
V first(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the first value and returns it, or returns null if the cursor range is empty.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the first value, or null if the cursor range is empty.



iterator
Iterator<V> iterator()(Code)
Returns an iterator over the key range, starting with the value following the current position or at the first value if the cursor is uninitialized.

LockMode.DEFAULT is used implicitly.

the iterator.



iterator
Iterator<V> iterator(LockMode lockMode)(Code)
Returns an iterator over the key range, starting with the value following the current position or at the first value if the cursor is uninitialized.
Parameters:
  lockMode - the lock mode to use for all operations performedusing the iterator, or null to use LockMode.DEFAULT. the iterator.



last
V last() throws DatabaseException(Code)
Moves the cursor to the last value and returns it, or returns null if the cursor range is empty.

LockMode.DEFAULT is used implicitly.

the last value, or null if the cursor range is empty.



last
V last(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the last value and returns it, or returns null if the cursor range is empty.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the last value, or null if the cursor range is empty.



next
V next() throws DatabaseException(Code)
Moves the cursor to the next value and returns it, or returns null if there are no more values in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.first .

LockMode.DEFAULT is used implicitly.

the next value, or null if there are no more values in thecursor range.



next
V next(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the next value and returns it, or returns null if there are no more values in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.first .
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the next value, or null if there are no more values in thecursor range.



nextDup
V nextDup() throws DatabaseException(Code)
Moves the cursor to the next value with the same key (duplicate) and returns it, or returns null if no more values are present for the key at the current position.

LockMode.DEFAULT is used implicitly.

the next value with the same key, or null if no more values arepresent for the key at the current position.
throws:
  IllegalStateException - if the cursor is uninitialized.



nextDup
V nextDup(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the next value with the same key (duplicate) and returns it, or returns null if no more values are present for the key at the current position.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the next value with the same key, or null if no more values arepresent for the key at the current position.
throws:
  IllegalStateException - if the cursor is uninitialized.



nextNoDup
V nextNoDup() throws DatabaseException(Code)
Moves the cursor to the next value with a different key and returns it, or returns null if there are no more unique keys in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.first .

LockMode.DEFAULT is used implicitly.

the next value with a different key, or null if there are nomore unique keys in the cursor range.



nextNoDup
V nextNoDup(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the next value with a different key and returns it, or returns null if there are no more unique keys in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.first .
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the next value with a different key, or null if there are nomore unique keys in the cursor range.



prev
V prev() throws DatabaseException(Code)
Moves the cursor to the previous value and returns it, or returns null if there are no preceding values in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.last .

LockMode.DEFAULT is used implicitly.

the previous value, or null if there are no preceding values inthe cursor range.



prev
V prev(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the previous value and returns it, or returns null if there are no preceding values in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.last .
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the previous value, or null if there are no preceding values inthe cursor range.



prevDup
V prevDup() throws DatabaseException(Code)
Moves the cursor to the previous value with the same key (duplicate) and returns it, or returns null if no preceding values are present for the key at the current position.

LockMode.DEFAULT is used implicitly.

the previous value with the same key, or null if no precedingvalues are present for the key at the current position.
throws:
  IllegalStateException - if the cursor is uninitialized.



prevDup
V prevDup(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the previous value with the same key (duplicate) and returns it, or returns null if no preceding values are present for the key at the current position.
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the previous value with the same key, or null if no precedingvalues are present for the key at the current position.
throws:
  IllegalStateException - if the cursor is uninitialized.



prevNoDup
V prevNoDup() throws DatabaseException(Code)
Moves the cursor to the preceding value with a different key and returns it, or returns null if there are no preceding unique keys in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.last .

LockMode.DEFAULT is used implicitly.

the previous value with a different key, or null if there are nopreceding unique keys in the cursor range.



prevNoDup
V prevNoDup(LockMode lockMode) throws DatabaseException(Code)
Moves the cursor to the preceding value with a different key and returns it, or returns null if there are no preceding unique keys in the cursor range. If the cursor is uninitialized, this method is equivalent to EntityCursor.last .
Parameters:
  lockMode - the lock mode to use for this operation, or null touse LockMode.DEFAULT. the previous value with a different key, or null if there are nopreceding unique keys in the cursor range.



update
boolean update(V entity) throws DatabaseException(Code)
Replaces the entity at the cursor position with the given entity.
Parameters:
  entity - the entity to replace the entity at the current position. true if successful or false if the entity at the currentposition was previously deleted.
throws:
  IllegalStateException - if the cursor is uninitialized.
throws:
  UnsupportedOperationException - if the index is read only or ifthe value type is not an entity type.



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