@Override public Position getPosition(Clock sinceClock) { if (Clock.ZERO != sinceClock) { Position pos = _retention.getPosition(sinceClock); if (pos != null) { return pos; } } return new SimplePosition( _retention.getId(), _retention.getOffset(), getStoreIndexStart(), sinceClock); }
@Override public Position get(Position pos, List<Event<K>> list) { if (pos.getId() != _retention.getId()) { if (pos.isIndexed()) { throw new InvalidPositionException("Bootstrap reconnection rejected", pos); } else { Position newPos = getPosition(pos.getClock()); if (newPos == null) { newPos = new SimplePosition( _retention.getId(), _retention.getOffset(), getStoreIndexStart(), pos.getClock()); _logger.warn("Reset position from " + pos + " to " + newPos); } pos = newPos; } } // Reset position if necessary if (pos.getOffset() < _retention.getOrigin()) { Position newPos = new SimplePosition( _retention.getId(), _retention.getOffset(), getStoreIndexStart(), pos.getClock()); _logger.warn("Reset position from " + pos + " to " + newPos); pos = newPos; } // Read from the retention directly Position nextPos = _retention.get(pos, list); // Out of retention and need to start bootstrap if (nextPos == null && pos.isIndexed()) { int index = pos.getIndex(); IndexedIterator<K> iter = _store.keyIterator(); try { iter.reset(index); } catch (ArrayIndexOutOfBoundsException e) { Position newPos = new SimplePosition(_retention.getId(), pos.getOffset(), pos.getClock()); _logger.warn("Reset position from " + pos + " to " + newPos, e); return newPos; } int cnt = 0; int lastIndex = index; while (iter.hasNext()) { lastIndex = iter.index(); K key = iter.next(); index = iter.index(); list.add(new SimpleEvent<K>(key, pos.getClock())); cnt++; if (cnt >= _retention.getBatchSize()) { if (lastIndex == index) { while (iter.hasNext() && iter.index() == index) { key = iter.next(); list.add(new SimpleEvent<K>(key, pos.getClock())); cnt++; } index++; } // Exit loop when enough events are collected break; } } if (cnt > 0) { _logger.info("Read[" + pos.getIndex() + "," + index + ") " + cnt); } if (iter.hasNext()) { return new SimplePosition(_retention.getId(), pos.getOffset(), index, pos.getClock()); } else { Clock newClock; newClock = _retention.getClock(pos.getOffset()); if (newClock == null) newClock = pos.getClock(); return new SimplePosition(_retention.getId(), pos.getOffset(), newClock); } } else { return nextPos; } }