@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);
  }
Exemple #2
0
 private void configureAnnotation(AnnotationNode node, Annotation annotation) {
   Class type = annotation.annotationType();
   if (type == Retention.class) {
     Retention r = (Retention) annotation;
     RetentionPolicy value = r.value();
     setRetentionPolicy(value, node);
     node.setMember(
         "value",
         new PropertyExpression(
             new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)),
             value.toString()));
   } else if (type == Target.class) {
     Target t = (Target) annotation;
     ElementType[] elements = t.value();
     ListExpression elementExprs = new ListExpression();
     for (ElementType element : elements) {
       elementExprs.addExpression(
           new PropertyExpression(
               new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
     }
     node.setMember("value", elementExprs);
   } else {
     Method[] declaredMethods = type.getDeclaredMethods();
     for (int i = 0; i < declaredMethods.length; i++) {
       Method declaredMethod = declaredMethods[i];
       try {
         Object value = declaredMethod.invoke(annotation);
         Expression valueExpression = annotationValueToExpression(value);
         if (valueExpression == null) continue;
         node.setMember(declaredMethod.getName(), valueExpression);
       } catch (IllegalAccessException e) {
       } catch (InvocationTargetException e) {
       }
     }
   }
 }
 @Override
 public Position getPosition() {
   return _retention.getPosition();
 }
  @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;
    }
  }