I am iterating over a Mongo DB collection. This collection has millions of documents in it. I am using java API for this iteration. This iteration exercise can go on for a while and we dont want to impact performance of the application. So we are planning to use Thread.sleep during this iteration. But we are facing cursor timeout kind of problems. Anyone faced this before? Also, is it possible to read data in chunks e.g. 100 documents at a time?
Instead of using a cursor over the entire collection you can try paging through the collection by the _id. So each time query for 100 documents (order by _id) and keep the last _id you encounter. Then on each consecutive query use a condition to fetch documents where _id > last _id from previous fetch.
Cursors are not a concept directly associated with collections in Java, as they are in some other programming languages or database systems. In Java, collections are typically traversed using iterators or enhanced for loops, rather than cursors.
In Java, an Iterator is one of the Java cursors. Java Iterator is an interface that is practiced in order to iterate over a collection of Java object components entirety one by one. It is free to use in the Java programming language since the Java 1.2 Collection framework. It belongs to java.util package.
Though Java Iterator was introduced in Java 1.2, however, it is still not the oldest tool available to traverse through the elements of the Collection object. The oldest Iterator in the Java programming language is the Enumerator predated Iterator. Java Iterator interface succeeds the enumerator iterator that was practiced in the beginning to traverse over some accessible collections like the ArrayLists.
The Java Iterator is also known as the universal cursor of Java as it is appropriate for all the classes of the Collection framework. The Java Iterator also helps in the operations like READ and REMOVE. When we compare the Java Iterator interface with the enumeration iterator interface, we can say that the names of the methods available in Java Iterator are more precise and straightforward to use.
When a user needs to use the Java Iterator, then it's compulsory for them to make an instance of the Iterator interface from the collection of objects they desire to traverse over. After that, the received Iterator maintains the trail of the components in the underlying collection to make sure that the user will traverse over each of the elements of the collection of objects.
If the user modifies the underlying collection while traversing over an Iterator leading to that collection, then the Iterator will typically acknowledge it and will throw an exception in the next time when the user will attempt to get the next component from the Iterator.
Now it's time to execute a Java program to illustrate the advantage of the Java Iterator interface. The below code produces an ArrayList of city names. Then we initialize an iterator applying the iterator () method of the ArrayList. After that, the list is traversed to represent each element.
In this article, we will learn about cursor in the collection framework. If we want to get objects from the collection one by one, we have to use the cursor. Using the cursor, we can retrieve or traverse the object from the collection one by one. There are three cursors in the collection framework-
The iterator cursor is introduced to overcome the limitation of the enumeration cursor. An iterator is used to retrieve elements from the collection one by one. The iterator is a universal cursor which means it is applicable for all collection classes.
The listiterator cursor is introduced to overcome the limitation of the iterator cursor. The listiterator is also used to retrieve elements from the collection one by one. The listiterator is a bi-directional cursor which means we can move forward and backward direction.
In this article, we have seen how we can use the cursor to retrieve the object from the collection one by one. Thanks for reading and hope you like it. If you have any suggestions or queries on this article, please share your thoughts.
In Java, most people expect that each call to Iterator.next() will return a distinct object. This has led some of us to define an alternative model which we called a 'cursor'. (Note that others use the term 'cursor' to draw a different distinction.) The pattern of a cursor is:
Yes, it is possible to define an Iterator that returns the same thing ever time, but we have concerns that people will use this as a gun to shoot their toes off. Thus the preference for the JDBC-ish pattern.
The Iterator pattern works well for a homogenous collection of objects that you want to hand out through a common interface. For example, you have a collection of Strings and you want them dealt out in a particular order so you use a Comparator (behind the scenes) and your consuming code only has to call next() a few times to achieve it's objective.
The Factory pattern creates an instance of an Object according to a given specification - sometimes provided as arguments, sometimes inferred. The Builder pattern is similar but is usually geared towards completing a single step along the way to the completion of an instance of a composite collection of Objects.
If this is what you're after, then this approach violates the Principle of Least Surprise. As a developer if an object is being quietly mutated behind the scenes, I'd like to know about it since I want to know the threading and performance issues that could arise.
Since the problem you appear to face is one of maintaining a collection of objects between stateless web calls you're probably better off using a simple List and provide the index to the current entry as part of the response to the client. When the client requests the contents of the List simply hand it to JAXB to marshal it into XML/JSON (it'll use an Iterator).
Might be worth watching josh bloch's last puzzler presentation. one of the puzzles evoles around the EnumMap.entries() iterator recycling the same instance of Map.Entry for each entry, it certainly breaks the principle of least surprise.
Provides utility methods for working with cursors and iterators. Method Summarystatic YCursorconcatenate(YCursor c1, YCursor c2)
Creates a new cursor that provides a logical view on the concatenation of the two given cursors.static YCursorcreateCursor(java.util.Collection c)
Creates a YCursor view of the given collection.static EdgeCursorcreateEdgeCursor(java.util.Collection c)
Creates a cursor view of the given collection.static java.util.IteratorcreateIterator(YCursor cursor)
Creates a first-to-last Iterator view of the given cursor.static NodeCursorcreateNodeCursor(java.util.Collection c)
Creates a cursor view of the given collection.static java.util.IteratorcreateReverseIterator(YCursor cursor)
Creates a last-to-first Iterator view of the given cursor.static java.lang.Object[]toArray(YCursor cursor, java.lang.Object[] dest)
Creates or fills an array with the values provided by the cursor Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait Method DetailtoArraypublic static java.lang.Object[] toArray(YCursor cursor, java.lang.Object[] dest)Creates or fills an array with the values provided by the cursorParameters:cursor - the cursordest - the array to fill with the values or null if the method should create the array itselfReturns:dest or a newly created array filled with the values from cursorcreateCursorpublic static YCursor createCursor(java.util.Collection c)Creates a YCursor view of the given collection. Note that the returned cursor does not support the prev and toLast operations.Parameters:c - The collection.Returns:The cursor view of the given collection.See Also:createEdgeCursor(java.util.Collection), createNodeCursor(java.util.Collection)createEdgeCursorpublic static EdgeCursor createEdgeCursor(java.util.Collection c)Creates a cursor view of the given collection. Note that the returned cursor does not support the operations prev, toLast, and EdgeCursor.cyclicPrev().
Parameters:cursor - The cursor.Returns:The iterator view of the given cursor.createReverseIteratorpublic static java.util.Iterator createReverseIterator(YCursor cursor)Creates a last-to-first Iterator view of the given cursor. Note that the returned iterator does not support the remove operation.
In Java, all types of enumerations and iterators (such as Iterator, ListIterator, SplitIterator) are simply navigational cursors and the main purpose of these cursors is to iterate over the elements of the collection. Each cursor has its own features, advantages and disadvantages.
In this article, we will walk through these iterators, important methods, advantages, and disadvantages with examples. We will also talk about the performance of the different cursors in the different situations, and best practices also.
To overcome some of the above disadvantages of the Enumeration, Java introduced the java.util.Iterator interface in JDK1.2. Iterator can be used to iterate over the elements of any collection classes present in Java, hence it is a universal cursor. Iterating using Iterator we can perform both read and remove operations on the elements of the collection.
ListIterator can be used to iterate in forward as well as backward directions over the elements of any List-implemented collection classes. Using ListIterator we can perform read, remove, add, and update operations while iterating over the elements of the collection.
We can use the ListIterator to iterate in forward as well as backward direction over the elements of List implemented collection classes with the support of adding, updating, and removing any element from the collection.
c80f0f1006