2222import java .util .Collection ;
2323import java .util .Iterator ;
2424import java .util .List ;
25+ import java .util .Collections ;
2526import java .util .Objects ;
26- import java .util .concurrent .atomic .AtomicLong ;
2727
2828import ch .rasc .xodusqueue .serializer .BigDecimalXodusQueueSerializer ;
2929import ch .rasc .xodusqueue .serializer .BigIntegerXodusQueueSerializer ;
@@ -57,8 +57,6 @@ public class XodusQueue<T> extends AbstractQueue<T> implements AutoCloseable {
5757
5858 private final XodusQueueSerializer <T > serializer ;
5959
60- private final AtomicLong key = new AtomicLong (0L );
61-
6260 @ SuppressWarnings ("unchecked" )
6361 public XodusQueue (final String databaseDir , final Class <T > entryClass ) {
6462 this .env = Environments .newInstance (databaseDir );
@@ -96,42 +94,17 @@ else if (entryClass == BigDecimal.class) {
9694 else {
9795 this .serializer = new DefaultXodusQueueSerializer <>(entryClass );
9896 }
99-
100- this .key .set (this .lastKey ());
10197 }
10298
10399 public XodusQueue (final String databaseDir , final XodusQueueSerializer <T > serializer ) {
104100 this .env = Environments .newInstance (databaseDir );
105101 this .serializer = serializer ;
106-
107- this .key .set (this .lastKey ());
108102 }
109103
110104 public XodusQueue (final LogConfig logConfig , final EnvironmentConfig environmentConfig ,
111105 final XodusQueueSerializer <T > serializer ) {
112106 this .env = Environments .newInstance (logConfig , environmentConfig );
113107 this .serializer = serializer ;
114-
115- this .key .set (this .lastKey ());
116- }
117-
118- /**
119- * Returns the last key stored in the queue store or 0 if the store is empty.
120- */
121- private long lastKey () {
122- return this .env .computeInReadonlyTransaction (txn -> {
123- Store store = this .env .openStore (STORE_NAME , StoreConfig .WITHOUT_DUPLICATES , txn , false );
124- if (store == null ) {
125- return 0L ;
126- }
127-
128- try (Cursor cursor = store .openCursor (txn )) {
129- if (cursor .getLast ()) {
130- return LongBinding .entryToLong (cursor .getKey ());
131- }
132- }
133- return 0L ;
134- });
135108 }
136109
137110 @ Override
@@ -149,7 +122,6 @@ public boolean offer(T e) {
149122 }
150123
151124 store .putRight (txn , LongBinding .longToEntry (nextKey ), this .serializer .toEntry (e ));
152- this .key .set (nextKey );
153125 });
154126
155127 return true ;
@@ -182,10 +154,6 @@ public boolean addAll(Collection<? extends T> c) {
182154 modified = true ;
183155 }
184156
185- if (modified ) {
186- this .key .set (last );
187- }
188-
189157 return modified ;
190158 });
191159 }
@@ -285,7 +253,7 @@ public Iterator<T> iterator() {
285253 }
286254 }
287255 });
288- return snapshot .iterator ();
256+ return Collections . unmodifiableList ( snapshot ) .iterator ();
289257 }
290258
291259 @ Override
@@ -324,14 +292,23 @@ public <T> T[] toArray(T[] a) {
324292 r [ix ++] = (T ) this .serializer .fromEntry (value );
325293 }
326294 }
295+ if (r .length > ix ) {
296+ r [ix ] = null ;
297+ }
327298 return r ;
328299 }
300+ if (a .length > 0 ) {
301+ a [0 ] = null ;
302+ }
329303 return a ;
330304 });
331305 }
332306
333307 @ Override
334308 public boolean remove (Object o ) {
309+ if (o == null ) {
310+ return false ;
311+ }
335312 return this .env .computeInExclusiveTransaction (txn -> {
336313 Store store = this .env .openStore (STORE_NAME , StoreConfig .WITHOUT_DUPLICATES , txn , false );
337314
@@ -352,6 +329,10 @@ public boolean remove(Object o) {
352329
353330 @ Override
354331 public boolean containsAll (Collection <?> c ) {
332+ Objects .requireNonNull (c );
333+ if (c .isEmpty ()) {
334+ return true ;
335+ }
355336 return this .env .computeInReadonlyTransaction (txn -> {
356337 Store store = this .env .openStore (STORE_NAME , StoreConfig .WITHOUT_DUPLICATES , txn , false );
357338 if (store != null ) {
@@ -428,7 +409,6 @@ public boolean retainAll(Collection<?> c) {
428409 public void clear () {
429410 this .env .executeInExclusiveTransaction (txn -> {
430411 this .env .truncateStore (STORE_NAME , txn );
431- this .key .set (0L );
432412 });
433413 }
434414
0 commit comments