@Override public CloseableIterator<GeoWaveData<GridCoverage>> toGeoWaveData( final File input, final Collection<ByteArrayId> primaryIndexIds, final String globalVisibility) { final AbstractGridFormat format = GridFormatFinder.findFormat(input); final GridCoverage2DReader reader = format.getReader(input); if (reader == null) { LOGGER.error("Unable to get reader instance, getReader returned null"); return new Wrapper(Collections.emptyIterator()); } try { final GridCoverage2D coverage = reader.read(null); if (coverage != null) { final Map<String, String> metadata = new HashMap<String, String>(); final String coverageName = coverage.getName().toString(); final String[] mdNames = reader.getMetadataNames(coverageName); if ((mdNames != null) && (mdNames.length > 0)) { for (final String mdName : mdNames) { metadata.put(mdName, reader.getMetadataValue(coverageName, mdName)); } } final RasterDataAdapter adapter = new RasterDataAdapter( input.getName(), metadata, coverage, optionProvider.getTileSize(), optionProvider.isBuildPyramid()); final List<GeoWaveData<GridCoverage>> coverages = new ArrayList<GeoWaveData<GridCoverage>>(); coverages.add(new GeoWaveData<GridCoverage>(adapter, primaryIndexIds, coverage)); return new Wrapper(coverages.iterator()) { @Override public void close() throws IOException { reader.dispose(); } }; } else { LOGGER.warn( "Null grid coverage from file '" + input.getAbsolutePath() + "' for discovered geotools format '" + format.getName() + "'"); } } catch (final IOException e) { LOGGER.warn( "Unable to read grid coverage of file '" + input.getAbsolutePath() + "' for discovered geotools format '" + format.getName() + "'", e); } return new Wrapper(Collections.emptyIterator()); }
private void setup() { bKeys = _predicate.streamB.getIndex().keySet().iterator(); currA = null; currB = null; aSeed = null; aInstances = Collections.emptyIterator(); bInstances = Collections.emptyIterator(); }
@Override public Iterator<GetField> iterator() { if (fields == null) { return Collections.emptyIterator(); } return fields.values().iterator(); }
private <T> Iterator<T> getIterator(int type) { if (count == 0) { return Collections.emptyIterator(); } else { return new Enumerator<>(type, true); } }
@Override public Iterator<T> iterator() { if (this.internalList != null) { return this.internalList.iterator(); } return Collections.emptyIterator(); }
@Override public Iterator<Variable<?>> iterator() { if (variables.isEmpty()) { return Collections.emptyIterator(); } return variables.iterator(); }
@Override public Iterator<Message> iterator() { if (getFilterOut()) { return Collections.emptyIterator(); } return Iterators.singletonIterator(this); }
@Override public Iterator<IEntityGroup> findParentGroups(IGroupMember member) throws GroupsException { /* * This method has the potential to be called A LOT, especially if * there's a lot of portal data (portlets & groups). It's important * not to waste time on nonsensical checks. */ if (!IPERSON_CLASS.equals(member.getLeafType())) { // This is going to happen; GaP code is not responsible for // knowing that PAGS only supports groups of IPerson (we are). return Collections.emptyIterator(); } logger.debug("finding containing groups for member key {}", member.getKey()); final Set<IEntityGroup> set = Collections.emptySet(); Iterator<IEntityGroup> rslt = set.iterator(); // default if (member.isGroup()) { // PAGS groups may only contain other PAGS groups (and people, of course) final IEntityGroup ieg = (IEntityGroup) member; if (PagsService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) { rslt = findParentGroupsForGroup((IEntityGroup) member); } } else { rslt = findParentGroupsForEntity((IEntity) member); } return rslt; }
/** * Construct a ChildPathIterator. * * @param converter converter from node data to domain object * @param cache source for children nodes */ public ChildPathIterator(Converter<ChildData, T> converter, PathChildrenCache cache) { Assert.notNull(converter); Assert.notNull(cache); this.converter = converter; List<ChildData> list = cache.getCurrentData(); this.iterator = list == null ? Collections.<ChildData>emptyIterator() : list.iterator(); }
@Override public Iterator<EntityReference> listData() { try { List<Object[]> data = this.docStore.search(DATA_REFERENCE_QUERY, 0, 0, this.context.get()); return new ReferenceIterator(data); } catch (XWikiException ex) { this.logger.warn("Failed to list the database deleted attachments: {}", ex.getMessage()); return Collections.emptyIterator(); } }
@Test public void shouldReturnNoValueTypesIfTheIndexIsEmpty() throws Exception { // Given when(fields.iterator()).thenReturn(Collections.<String>emptyIterator()); // When final Set<Class> types = accessor.valueTypesInIndex(); // Then assertTrue(types.isEmpty()); }
@Override public Iterator<Entry<Integer, Double>> iterator() { // find the first starting inded int START = 0; while (START < status.length && status[START] != OCCUPIED) START++; if (START == status.length) return Collections.emptyIterator(); final int startPos = START; return new Iterator<Entry<Integer, Double>>() { int pos = startPos; int prevPos = -1; @Override public boolean hasNext() { return pos < status.length; } @Override public Entry<Integer, Double> next() { // final int make so that object remains good after we call next again final int oldPos = prevPos = pos++; // find next while (pos < status.length && status[pos] != OCCUPIED) pos++; // and return new object return new Entry<Integer, Double>() { @Override public Integer getKey() { return keys[oldPos]; } @Override public Double getValue() { return table[oldPos]; } @Override public Double setValue(Double value) { double old = table[oldPos]; table[oldPos] = value; return old; } }; } @Override public void remove() { // its ok to just call remove b/c nothing is re-ordered when we remove an element, we just // set the status to DELETED parentRef.remove(keys[prevPos]); } }; }
@Override public Iterator<DeletedAttachment> getData() { try { List<Long> data = this.docStore.search(DATA_RETRIEVE_QUERY, 0, 0, this.context.get()); this.logger.debug("Found [{}] deleted attachments in the database trash", data.size()); return new DeletedAttachmentIterator(data); } catch (XWikiException ex) { this.logger.warn( "Failed to get the list of database deleted attachments: {}", ex.getMessage()); return Collections.emptyIterator(); } }
@Test public void correctlySaysRelIsDeleted() throws Exception { // Given state.relationshipDoDelete(1l, 1, 1l, 2l); Relationship rel = mock(Relationship.class); when(rel.getId()).thenReturn(1l); when(ops.relationshipGetAllProperties(1l)) .thenReturn(Collections.<DefinedProperty>emptyIterator()); // When & Then assertThat(snapshot().isDeleted(rel), equalTo(true)); }
public ChooseStep( final Traversal traversal, final SFunction<Traverser<S>, M> mapFunction, final Map<M, Traversal<S, E>> branches) { super(traversal); this.setFunction( traverser -> { final Traversal<S, E> branch = branches.get(mapFunction.apply(traverser)); if (null == branch) { return Collections.emptyIterator(); } else { branch.addStarts(new SingleIterator<>(traverser)); return branch; } }); }
@CompilerDirectives.TruffleBoundary public static Iterator<Map.Entry<Object, Object>> iterateKeyValues(DynamicObject hash) { assert RubyGuards.isRubyHash(hash); if (HashGuards.isNullHash(hash)) { return Collections.emptyIterator(); } if (HashGuards.isPackedHash(hash)) { return PackedArrayStrategy.iterateKeyValues( (Object[]) Layouts.HASH.getStore(hash), Layouts.HASH.getSize(hash)); } else if (HashGuards.isBucketHash(hash)) { return BucketsStrategy.iterateKeyValues(Layouts.HASH.getFirstInSequence(hash)); } else { throw new UnsupportedOperationException(); } }
private void returnNotifications(ServiceRequest<PublishRequest, PublishResponse> service) { LinkedHashSet<BaseMonitoredItem<?>> items = new LinkedHashSet<>(); lastIterator.forEachRemaining(items::add); itemsById .values() .stream() .filter(item -> item.hasNotifications() || item.isTriggered()) .forEach(items::add); PeekingIterator<BaseMonitoredItem<?>> iterator = Iterators.peekingIterator(items.iterator()); gatherAndSend(iterator, Optional.of(service)); lastIterator = iterator.hasNext() ? iterator : Collections.emptyIterator(); }
/** @author Marko A. Rodriguez (http://markorodriguez.com) */ public final class HadoopEdgeIterator extends HadoopElementIterator<Edge> { private Iterator<Edge> edgeIterator = Collections.emptyIterator(); public HadoopEdgeIterator(final HadoopGraph graph) throws IOException { super(graph); } @Override public Edge next() { try { while (true) { if (this.edgeIterator.hasNext()) return new HadoopEdge(this.edgeIterator.next(), this.graph); if (this.readers.isEmpty()) throw FastNoSuchElementException.instance(); if (this.readers.peek().nextKeyValue()) { this.edgeIterator = this.readers.peek().getCurrentValue().get().edges(Direction.OUT); } else { this.readers.remove().close(); } } } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } } @Override public boolean hasNext() { try { while (true) { if (this.edgeIterator.hasNext()) return true; if (this.readers.isEmpty()) return false; if (this.readers.peek().nextKeyValue()) { this.edgeIterator = this.readers.peek().getCurrentValue().get().edges(Direction.OUT); } else { this.readers.remove().close(); } } } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } } }
/** Combined iterator over current local partitions. */ private abstract class PartitionedIterator<T> implements Iterator<T> { /** Partitions iterator. */ private Iterator<GridDhtLocalPartition> partsIter = ctx.topology().currentLocalPartitions().iterator(); /** Current partition iterator. */ private Iterator<T> currIter = partsIter.hasNext() ? iterator(partsIter.next()) : Collections.<T>emptyIterator(); /** * @param part Partition. * @return Iterator over entries of given partition. */ protected abstract Iterator<T> iterator(GridDhtLocalPartition part); /** {@inheritDoc} */ @Override public boolean hasNext() { if (currIter.hasNext()) return true; while (partsIter.hasNext()) { currIter = iterator(partsIter.next()); if (currIter.hasNext()) return true; } return false; } /** {@inheritDoc} */ @Override public T next() { if (hasNext()) return currIter.next(); else throw new NoSuchElementException(); } /** {@inheritDoc} */ @Override public void remove() { throw new UnsupportedOperationException("remove"); } }
/** * Iterates all {@link com.google.openrtb.OpenRtb.BidResponse.SeatBid.Bid.Builder}s in a {@link * com.google.openrtb.OpenRtb.BidResponse.Builder}, dealing with the intermediate layer of seats * transparently. */ class ResponseBidsIterator implements Iterator<Bid.Builder>, Iterable<Bid.Builder> { private final Iterator<SeatBid.Builder> seatbidIter; private Iterator<Bid.Builder> bidIter = Collections.emptyIterator(); public ResponseBidsIterator(BidResponse.Builder bidResponse) { this.seatbidIter = bidResponse.getSeatbidBuilderList().iterator(); } private void skipSeats() { while (!bidIter.hasNext() && seatbidIter.hasNext()) { SeatBid.Builder seatBid = seatbidIter.next(); bidIter = seatBid.getBidBuilderList().iterator(); } } @Override public boolean hasNext() { skipSeats(); return bidIter.hasNext(); } @Override public Bid.Builder next() { skipSeats(); return bidIter.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public Iterator<Bid.Builder> iterator() { return this; } }
@Override public Iterator<IEntityGroup> findEntitiesForGroup(IEntityGroup group) throws GroupsException { // PAGS groups are synthetic; we don't support this behavior. return Collections.emptyIterator(); }
@Override public WorkloadStreams getStreams(GeneratorFactory gf, boolean hasDbConnected) throws WorkloadException { long workloadStartTimeAsMilli = 0; /** * ************************** * * <p>Initial Insert Operation Generator * * <p>************************** */ // Load Insert Keys MinMaxGenerator<Long> insertKeyGenerator = gf.minMaxGenerator(gf.incrementing(0l, 1l), 0l, 0l); // Insert Fields: Names & Values Iterator<Long> fieldValueLengthGenerator = gf.uniform(1l, 100l); Iterator<Iterator<Byte>> randomFieldValueGenerator = gf.sizedUniformBytesGenerator(fieldValueLengthGenerator); List<Tuple3<Double, String, Iterator<Iterator<Byte>>>> valuedFields = new ArrayList<>(); for (int i = 0; i < NUMBER_OF_FIELDS_IN_RECORD; i++) { valuedFields.add(Tuple.tuple3(1d, FIELD_NAME_PREFIX + i, randomFieldValueGenerator)); } Iterator<Map<String, Iterator<Byte>>> insertValuedFieldGenerator = gf.weightedDiscreteMap(valuedFields, NUMBER_OF_FIELDS_IN_RECORD); Iterator<Operation> initialInsertOperationGenerator = gf.limit( new InsertOperationGenerator( TABLE, gf.prefix(insertKeyGenerator, KEY_NAME_PREFIX), insertValuedFieldGenerator), INITIAL_INSERT_COUNT); /** * ************************** * * <p>Insert Operation Generator * * <p>************************** */ // Transaction Insert Keys InsertOperationGenerator transactionalInsertOperationGenerator = new InsertOperationGenerator( TABLE, gf.prefix(insertKeyGenerator, KEY_NAME_PREFIX), insertValuedFieldGenerator); /** * ************************** * * <p>Read Operation Generator * * <p>************************** */ // Read/Update Keys Iterator<String> requestKeyGenerator = gf.prefix(gf.dynamicRangeUniform(insertKeyGenerator), KEY_NAME_PREFIX); // Read Fields: Names List<Tuple2<Double, String>> fields = new ArrayList<>(); for (int i = 0; i < NUMBER_OF_FIELDS_IN_RECORD; i++) { fields.add(Tuple.tuple2(1d, FIELD_NAME_PREFIX + i)); } Iterator<List<String>> readFieldsGenerator = gf.weightedDiscreteList(fields, NUMBER_OF_FIELDS_TO_READ); ReadOperationGenerator readOperationGenerator = new ReadOperationGenerator(TABLE, requestKeyGenerator, readFieldsGenerator); /** * ************************** * * <p>Update Operation Generator * * <p>************************** */ // Update Fields: Names & Values Iterator<Map<String, Iterator<Byte>>> updateValuedFieldsGenerator = gf.weightedDiscreteMap(valuedFields, NUMBER_OF_FIELDS_TO_UPDATE); UpdateOperationGenerator updateOperationGenerator = new UpdateOperationGenerator(TABLE, requestKeyGenerator, updateValuedFieldsGenerator); /** * ************************** * * <p>Scan Operation Generator * * <p>************************** */ // Scan Fields: Names & Values Iterator<List<String>> scanFieldsGenerator = gf.weightedDiscreteList(fields, NUMBER_OF_FIELDS_TO_READ); // Scan Length: Number of Records Iterator<Integer> scanLengthGenerator = gf.uniform(MIN_SCAN_LENGTH, MAX_SCAN_LENGTH); ScanOperationGenerator scanOperationGenerator = new ScanOperationGenerator( TABLE, requestKeyGenerator, scanLengthGenerator, scanFieldsGenerator); /** * ************************** * * <p>ReadModifyWrite Operation Generator * * <p>************************** */ ReadModifyWriteOperationGenerator readModifyWriteOperationGenerator = new ReadModifyWriteOperationGenerator( TABLE, requestKeyGenerator, readFieldsGenerator, updateValuedFieldsGenerator); /** * ************************** * * <p>Transactional Workload Operations * * <p>************************** */ // proportion of transactions reads/update/insert/scan/read-modify-write List<Tuple2<Double, Iterator<Operation>>> operations = new ArrayList<>(); operations.add(Tuple.tuple2(READ_RATIO, (Iterator<Operation>) readOperationGenerator)); operations.add(Tuple.tuple2(UPDATE_RATIO, (Iterator<Operation>) updateOperationGenerator)); operations.add( Tuple.tuple2(INSERT_RATIO, (Iterator<Operation>) transactionalInsertOperationGenerator)); operations.add(Tuple.tuple2(SCAN_RATIO, (Iterator<Operation>) scanOperationGenerator)); operations.add( Tuple.tuple2( READ_MODIFY_WRITE_RATIO, (Iterator<Operation>) readModifyWriteOperationGenerator)); Iterator<Operation> transactionalOperationGenerator = gf.weightedDiscreteDereferencing(operations); // iterates initialInsertOperationGenerator before starting with // transactionalInsertOperationGenerator Iterator<Operation> workloadOperations = Iterators.concat(initialInsertOperationGenerator, transactionalOperationGenerator); Iterator<Long> startTimesAsMilli = gf.incrementing(workloadStartTimeAsMilli + 1, 100l); Iterator<Long> dependencyTimesAsMilli = gf.constant(workloadStartTimeAsMilli); WorkloadStreams workloadStreams = new WorkloadStreams(); workloadStreams.setAsynchronousStream( Sets.<Class<? extends Operation>>newHashSet(), Sets.<Class<? extends Operation>>newHashSet(), Collections.<Operation>emptyIterator(), gf.assignDependencyTimes( dependencyTimesAsMilli, gf.assignStartTimes(startTimesAsMilli, workloadOperations)), null); return workloadStreams; }
public class Subscription { private static final double MIN_LIFETIME = 10 * 1000.0; private static final double MAX_LIFETIME = 60 * 60 * 1000.0; private static final double MIN_PUBLISHING_INTERVAL = 100.0; private static final double MAX_PUBLISHING_INTERVAL = 60 * 1000.0; private static final int MAX_NOTIFICATIONS = 0xFFFF; private final Logger logger = LoggerFactory.getLogger(getClass()); private volatile Iterator<BaseMonitoredItem<?>> lastIterator = Collections.emptyIterator(); private final AtomicLong itemIds = new AtomicLong(1L); private final Map<UInteger, BaseMonitoredItem<?>> itemsById = Maps.newConcurrentMap(); private final AtomicReference<State> state = new AtomicReference<>(State.Normal); private final AtomicReference<StateListener> stateListener = new AtomicReference<>(); private final AtomicLong sequenceNumber = new AtomicLong(1L); private final Map<UInteger, NotificationMessage> availableMessages = Maps.newConcurrentMap(); private final PublishHandler publishHandler = new PublishHandler(); private final TimerHandler timerHandler = new TimerHandler(); private volatile boolean messageSent = false; private volatile boolean moreNotifications = false; private volatile long keepAliveCounter; private volatile long lifetimeCounter; private volatile double publishingInterval; private volatile long lifetimeCount; private volatile long maxKeepAliveCount; private volatile int maxNotificationsPerPublish; private volatile boolean publishingEnabled; private volatile int priority; private volatile SubscriptionManager subscriptionManager; private final UInteger subscriptionId; public Subscription( SubscriptionManager subscriptionManager, UInteger subscriptionId, double publishingInterval, long maxKeepAliveCount, long lifetimeCount, long maxNotificationsPerPublish, boolean publishingEnabled, int priority) { this.subscriptionManager = subscriptionManager; this.subscriptionId = subscriptionId; setPublishingInterval(publishingInterval); setMaxKeepAliveCount(maxKeepAliveCount); setLifetimeCount(lifetimeCount); setMaxNotificationsPerPublish(maxNotificationsPerPublish); this.publishingEnabled = publishingEnabled; this.priority = priority; resetKeepAliveCounter(); resetLifetimeCounter(); logger.debug( "[id={}] subscription created, interval={}, keep-alive={}, lifetime={}", subscriptionId, publishingInterval, maxKeepAliveCount, lifetimeCount); } public synchronized void modifySubscription(ModifySubscriptionRequest request) { setPublishingInterval(request.getRequestedPublishingInterval()); setMaxKeepAliveCount(request.getRequestedMaxKeepAliveCount().longValue()); setLifetimeCount(request.getRequestedLifetimeCount().longValue()); setMaxNotificationsPerPublish(request.getMaxNotificationsPerPublish().longValue()); this.priority = request.getPriority().intValue(); resetLifetimeCounter(); logger.debug( "[id={}] subscription modified, interval={}, keep-alive={}, lifetime={}", subscriptionId, publishingInterval, maxKeepAliveCount, lifetimeCount); } public synchronized List<BaseMonitoredItem<?>> deleteSubscription() { setState(State.Closed); logger.debug("[id={}] subscription deleted.", subscriptionId); return Lists.newArrayList(itemsById.values()); } public synchronized void setPublishingMode(SetPublishingModeRequest request) { this.publishingEnabled = request.getPublishingEnabled(); resetLifetimeCounter(); logger.debug( "[id={}] {}.", subscriptionId, publishingEnabled ? "publishing enabled." : "publishing disabled."); } public synchronized void addMonitoredItems(List<BaseMonitoredItem<?>> createdItems) { for (BaseMonitoredItem<?> item : createdItems) { itemsById.put(item.getId(), item); } resetLifetimeCounter(); logger.debug("[id={}] created {} MonitoredItems.", subscriptionId, createdItems.size()); } public synchronized void removeMonitoredItems(List<BaseMonitoredItem<?>> deletedItems) { for (BaseMonitoredItem<?> item : deletedItems) { itemsById.remove(item.getId()); } resetLifetimeCounter(); logger.debug("[id={}] deleted {} MonitoredItems.", subscriptionId, deletedItems.size()); } public synchronized Map<UInteger, BaseMonitoredItem<?>> getMonitoredItems() { return itemsById; } /** * Given the requested publishing interval, set it to something reasonable. * * @param requestedPublishingInterval the requested publishing interval. */ private void setPublishingInterval(double requestedPublishingInterval) { if (requestedPublishingInterval < MIN_PUBLISHING_INTERVAL || Double.isNaN(requestedPublishingInterval) || Double.isInfinite(requestedPublishingInterval)) { requestedPublishingInterval = MIN_PUBLISHING_INTERVAL; } if (requestedPublishingInterval > MAX_PUBLISHING_INTERVAL) { requestedPublishingInterval = MAX_PUBLISHING_INTERVAL; } this.publishingInterval = requestedPublishingInterval; } private void setMaxKeepAliveCount(long maxKeepAliveCount) { if (maxKeepAliveCount == 0) maxKeepAliveCount = 3; double keepAliveInterval = maxKeepAliveCount * publishingInterval; // keep alive interval cannot be longer than the max subscription lifetime. if (keepAliveInterval > MAX_LIFETIME) { maxKeepAliveCount = (long) (MAX_LIFETIME / publishingInterval); if (maxKeepAliveCount < UInteger.MAX_VALUE) { if (MAX_LIFETIME % publishingInterval != 0) { maxKeepAliveCount++; } } keepAliveInterval = maxKeepAliveCount * publishingInterval; } // the time between publishes cannot exceed the max publishing interval. if (keepAliveInterval > MAX_PUBLISHING_INTERVAL) { maxKeepAliveCount = (long) (MAX_PUBLISHING_INTERVAL / publishingInterval); if (maxKeepAliveCount < UInteger.MAX_VALUE) { if (MAX_PUBLISHING_INTERVAL % publishingInterval != 0) { maxKeepAliveCount++; } } } this.maxKeepAliveCount = maxKeepAliveCount; } private void setLifetimeCount(long lifetimeCount) { double lifetimeInterval = lifetimeCount * publishingInterval; // lifetime cannot be longer than the max subscription lifetime. if (lifetimeInterval > MAX_LIFETIME) { lifetimeCount = (long) (MAX_LIFETIME / publishingInterval); if (lifetimeCount < UInteger.MAX_VALUE) { if (MAX_LIFETIME % publishingInterval != 0) { lifetimeCount++; } } } // the lifetime must be greater than the keepalive. if (maxKeepAliveCount < UInteger.MAX_VALUE / 3) { if (maxKeepAliveCount * 3 > lifetimeCount) { lifetimeCount = maxKeepAliveCount * 3; } lifetimeInterval = lifetimeCount * publishingInterval; } else { lifetimeCount = UInteger.MAX_VALUE; lifetimeInterval = Double.MAX_VALUE; } // apply the minimum. if (MIN_LIFETIME > publishingInterval && MIN_LIFETIME > lifetimeInterval) { lifetimeCount = (long) (MIN_LIFETIME / publishingInterval); if (lifetimeCount < UInteger.MAX_VALUE) { if (MIN_LIFETIME % publishingInterval != 0) { lifetimeCount++; } } } this.lifetimeCount = lifetimeCount; } private void setMaxNotificationsPerPublish(long maxNotificationsPerPublish) { if (maxNotificationsPerPublish <= 0 || maxNotificationsPerPublish > MAX_NOTIFICATIONS) { maxNotificationsPerPublish = MAX_NOTIFICATIONS; } this.maxNotificationsPerPublish = Ints.saturatedCast(maxNotificationsPerPublish); } private synchronized PublishQueue publishQueue() { return subscriptionManager.getPublishQueue(); } private long currentSequenceNumber() { return sequenceNumber.get(); } private long nextSequenceNumber() { return sequenceNumber.getAndIncrement(); } void resetLifetimeCounter() { lifetimeCounter = lifetimeCount; logger.debug("[id={}] lifetime counter reset to {}", subscriptionId, lifetimeCounter); } private void resetKeepAliveCounter() { keepAliveCounter = maxKeepAliveCount; logger.debug("[id={}] keep-alive counter reset to {}", subscriptionId, maxKeepAliveCount); } private void returnKeepAlive(ServiceRequest<PublishRequest, PublishResponse> service) { ResponseHeader header = service.createResponseHeader(); UInteger sequenceNumber = uint(currentSequenceNumber()); NotificationMessage notificationMessage = new NotificationMessage(sequenceNumber, DateTime.now(), new ExtensionObject[0]); UInteger[] available = getAvailableSequenceNumbers(); UInteger requestHandle = service.getRequest().getRequestHeader().getRequestHandle(); StatusCode[] acknowledgeResults = subscriptionManager.getAcknowledgeResults(requestHandle); PublishResponse response = new PublishResponse( header, subscriptionId, available, moreNotifications, notificationMessage, acknowledgeResults, new DiagnosticInfo[0]); service.setResponse(response); logger.debug( "[id={}] returned keep-alive NotificationMessage sequenceNumber={}.", subscriptionId, sequenceNumber); } void returnStatusChangeNotification(ServiceRequest<PublishRequest, PublishResponse> service) { StatusChangeNotification statusChange = new StatusChangeNotification(new StatusCode(StatusCodes.Bad_Timeout), null); UInteger sequenceNumber = uint(nextSequenceNumber()); NotificationMessage notificationMessage = new NotificationMessage( sequenceNumber, new DateTime(), new ExtensionObject[] {ExtensionObject.encode(statusChange)}); ResponseHeader header = service.createResponseHeader(); PublishResponse response = new PublishResponse( header, subscriptionId, new UInteger[0], false, notificationMessage, new StatusCode[0], new DiagnosticInfo[0]); service.setResponse(response); logger.debug( "[id={}] returned StatusChangeNotification sequenceNumber={}.", subscriptionId, sequenceNumber); } private void returnNotifications(ServiceRequest<PublishRequest, PublishResponse> service) { LinkedHashSet<BaseMonitoredItem<?>> items = new LinkedHashSet<>(); lastIterator.forEachRemaining(items::add); itemsById .values() .stream() .filter(item -> item.hasNotifications() || item.isTriggered()) .forEach(items::add); PeekingIterator<BaseMonitoredItem<?>> iterator = Iterators.peekingIterator(items.iterator()); gatherAndSend(iterator, Optional.of(service)); lastIterator = iterator.hasNext() ? iterator : Collections.emptyIterator(); } /** * Gather {@link MonitoredItemNotification}s and send them using {@code service}, if present. * * @param iterator a {@link PeekingIterator} over the current {@link BaseMonitoredItem}s. * @param service a {@link ServiceRequest}, if available. */ private void gatherAndSend( PeekingIterator<BaseMonitoredItem<?>> iterator, Optional<ServiceRequest<PublishRequest, PublishResponse>> service) { if (service.isPresent()) { List<UaStructure> notifications = Lists.newArrayList(); while (notifications.size() < maxNotificationsPerPublish && iterator.hasNext()) { BaseMonitoredItem<?> item = iterator.peek(); boolean gatheredAllForItem = gather(item, notifications, maxNotificationsPerPublish); if (gatheredAllForItem && iterator.hasNext()) { iterator.next(); } } moreNotifications = iterator.hasNext(); sendNotifications(service.get(), notifications); if (moreNotifications) { gatherAndSend(iterator, Optional.ofNullable(publishQueue().poll())); } } else { if (moreNotifications) { publishQueue().addSubscription(this); } } } private boolean gather( BaseMonitoredItem<?> item, List<UaStructure> notifications, int maxNotifications) { int max = maxNotifications - notifications.size(); return item.getNotifications(notifications, max); } private void sendNotifications( ServiceRequest<PublishRequest, PublishResponse> service, List<UaStructure> notifications) { List<MonitoredItemNotification> dataNotifications = Lists.newArrayList(); List<EventFieldList> eventNotifications = Lists.newArrayList(); notifications.forEach( notification -> { if (notification instanceof MonitoredItemNotification) { dataNotifications.add((MonitoredItemNotification) notification); } else if (notification instanceof EventFieldList) { eventNotifications.add((EventFieldList) notification); } }); List<ExtensionObject> notificationData = Lists.newArrayList(); if (dataNotifications.size() > 0) { DataChangeNotification dataChange = new DataChangeNotification( dataNotifications.toArray(new MonitoredItemNotification[dataNotifications.size()]), new DiagnosticInfo[0]); notificationData.add(ExtensionObject.encode(dataChange)); } if (eventNotifications.size() > 0) { EventNotificationList eventChange = new EventNotificationList( eventNotifications.toArray(new EventFieldList[eventNotifications.size()])); notificationData.add(ExtensionObject.encode(eventChange)); } UInteger sequenceNumber = uint(nextSequenceNumber()); NotificationMessage notificationMessage = new NotificationMessage( sequenceNumber, new DateTime(), notificationData.toArray(new ExtensionObject[notificationData.size()])); availableMessages.put(notificationMessage.getSequenceNumber(), notificationMessage); UInteger[] available = getAvailableSequenceNumbers(); UInteger requestHandle = service.getRequest().getRequestHeader().getRequestHandle(); StatusCode[] acknowledgeResults = subscriptionManager.getAcknowledgeResults(requestHandle); ResponseHeader header = service.createResponseHeader(); PublishResponse response = new PublishResponse( header, subscriptionId, available, moreNotifications, notificationMessage, acknowledgeResults, new DiagnosticInfo[0]); service.setResponse(response); logger.debug( "[id={}] returning {} DataChangeNotification(s) and {} EventNotificationList(s) sequenceNumber={}.", subscriptionId, dataNotifications.size(), eventNotifications.size(), sequenceNumber); } private boolean notificationsAvailable() { return itemsById .values() .stream() .anyMatch(item -> item.hasNotifications() || item.isTriggered()); } private void setState(State state) { State previousState = this.state.getAndSet(state); logger.debug("[id={}] {} -> {}", subscriptionId, previousState, state); StateListener listener = stateListener.get(); if (listener != null) { listener.onStateChange(this, previousState, state); } } public UInteger getId() { return subscriptionId; } public double getPublishingInterval() { return publishingInterval; } public long getMaxKeepAliveCount() { return maxKeepAliveCount; } public long getLifetimeCount() { return lifetimeCount; } public int getMaxNotificationsPerPublish() { return maxNotificationsPerPublish; } public boolean isPublishingEnabled() { return publishingEnabled; } public int getPriority() { return priority; } public synchronized UInteger[] getAvailableSequenceNumbers() { Set<UInteger> uIntegers = availableMessages.keySet(); UInteger[] available = uIntegers.toArray(new UInteger[uIntegers.size()]); Arrays.sort(available); return available; } public synchronized SubscriptionManager getSubscriptionManager() { return subscriptionManager; } public synchronized void setSubscriptionManager(SubscriptionManager subscriptionManager) { this.subscriptionManager = subscriptionManager; } public Session getSession() { return subscriptionManager.getSession(); } public long nextItemId() { return itemIds.getAndIncrement(); } public void setStateListener(StateListener listener) { stateListener.set(listener); } /** * Handle an incoming {@link PublishRequest}. * * @param service The service request that contains the {@link PublishRequest}. */ synchronized void onPublish(ServiceRequest<PublishRequest, PublishResponse> service) { State state = this.state.get(); logger.trace( "[id={}] onPublish(), state={}, keep-alive={}, lifetime={}", subscriptionId, state, keepAliveCounter, lifetimeCounter); if (state == State.Normal) publishHandler.whenNormal(service); else if (state == State.KeepAlive) publishHandler.whenKeepAlive(service); else if (state == State.Late) publishHandler.whenLate(service); else if (state == State.Closing) publishHandler.whenClosing(service); else if (state == State.Closed) publishHandler.whenClosed(service); else throw new RuntimeException("Unhandled subscription state: " + state); } /** The publishing timer has elapsed. */ synchronized void onPublishingTimer() { State state = this.state.get(); logger.trace( "[id={}] onPublishingTimer(), state={}, keep-alive={}, lifetime={}", subscriptionId, state, keepAliveCounter, lifetimeCounter); if (state == State.Normal) timerHandler.whenNormal(); else if (state == State.KeepAlive) timerHandler.whenKeepAlive(); else if (state == State.Late) timerHandler.whenLate(); else if (state == State.Closed) logger.debug("[id={}] onPublish(), state={}", subscriptionId, state); // No-op. else throw new RuntimeException("unhandled subscription state: " + state); } synchronized void startPublishingTimer() { if (state.get() == State.Closed) return; lifetimeCounter--; if (lifetimeCounter < 1) { logger.debug("[id={}] lifetime expired.", subscriptionId); setState(State.Closing); } else { long interval = DoubleMath.roundToLong(publishingInterval, RoundingMode.UP); subscriptionManager .getServer() .getScheduledExecutorService() .schedule(this::onPublishingTimer, interval, TimeUnit.MILLISECONDS); } } public synchronized StatusCode acknowledge(UInteger sequenceNumber) { if (availableMessages.remove(sequenceNumber) != null) { logger.debug("[id={}] sequence number acknowledged: {}", subscriptionId, sequenceNumber); return StatusCode.GOOD; } else { logger.debug("[id={}] sequence number unknown: {}", subscriptionId, sequenceNumber); return new StatusCode(StatusCodes.Bad_SequenceNumberUnknown); } } public synchronized NotificationMessage republish(UInteger sequenceNumber) { resetLifetimeCounter(); return availableMessages.get(sequenceNumber); } private class PublishHandler { private void whenNormal(ServiceRequest<PublishRequest, PublishResponse> service) { boolean publishingEnabled = Subscription.this.publishingEnabled; /* Subscription State Table Row 4 */ if (!publishingEnabled || (publishingEnabled && !moreNotifications)) { publishQueue().addRequest(service); } /* Subscription State Table Row 5 */ else if (publishingEnabled && moreNotifications) { resetLifetimeCounter(); returnNotifications(service); messageSent = true; } else { throw new IllegalStateException("unhandled subscription state"); } } private void whenLate(ServiceRequest<PublishRequest, PublishResponse> service) { boolean publishingEnabled = Subscription.this.publishingEnabled; boolean notificationsAvailable = notificationsAvailable(); /* Subscription State Table Row 10 */ if (publishingEnabled && (notificationsAvailable || moreNotifications)) { setState(State.Normal); resetLifetimeCounter(); returnNotifications(service); messageSent = true; } /* Subscription State Table Row 11 */ else if (!publishingEnabled || (publishingEnabled && !notificationsAvailable && !moreNotifications)) { setState(State.KeepAlive); resetLifetimeCounter(); returnKeepAlive(service); messageSent = true; } else { throw new IllegalStateException("unhandled subscription state"); } } private void whenKeepAlive(ServiceRequest<PublishRequest, PublishResponse> service) { /* Subscription State Table Row 13 */ publishQueue().addRequest(service); } private void whenClosing(ServiceRequest<PublishRequest, PublishResponse> service) { returnStatusChangeNotification(service); setState(State.Closed); } private void whenClosed(ServiceRequest<PublishRequest, PublishResponse> service) { publishQueue().addRequest(service); } } private class TimerHandler { private void whenNormal() { boolean publishRequestQueued = publishQueue().isNotEmpty(); boolean publishingEnabled = Subscription.this.publishingEnabled; boolean notificationsAvailable = notificationsAvailable(); /* Subscription State Table Row 6 */ if (publishRequestQueued && publishingEnabled && notificationsAvailable) { Optional<ServiceRequest<PublishRequest, PublishResponse>> service = Optional.ofNullable(publishQueue().poll()); if (service.isPresent()) { resetLifetimeCounter(); returnNotifications(service.get()); messageSent = true; startPublishingTimer(); } else { whenNormal(); } } /* Subscription State Table Row 7 */ else if (publishRequestQueued && !messageSent && (!publishingEnabled || (publishingEnabled && !notificationsAvailable))) { Optional<ServiceRequest<PublishRequest, PublishResponse>> service = Optional.ofNullable(publishQueue().poll()); if (service.isPresent()) { resetLifetimeCounter(); returnKeepAlive(service.get()); messageSent = true; startPublishingTimer(); } else { whenNormal(); } } /* Subscription State Table Row 8 */ else if (!publishRequestQueued && (!messageSent || (publishingEnabled && notificationsAvailable))) { setState(State.Late); startPublishingTimer(); publishQueue().addSubscription(Subscription.this); } /* Subscription State Table Row 9 */ else if (messageSent && (!publishingEnabled || (publishingEnabled && !notificationsAvailable))) { setState(State.KeepAlive); resetKeepAliveCounter(); startPublishingTimer(); } else { throw new IllegalStateException("unhandled subscription state"); } } private void whenLate() { /* Subscription State Table Row 12 */ startPublishingTimer(); } private void whenKeepAlive() { boolean publishingEnabled = Subscription.this.publishingEnabled; boolean notificationsAvailable = notificationsAvailable(); boolean publishRequestQueued = publishQueue().isNotEmpty(); /* Subscription State Table Row 14 */ if (publishingEnabled && notificationsAvailable && publishRequestQueued) { Optional<ServiceRequest<PublishRequest, PublishResponse>> service = Optional.ofNullable(publishQueue().poll()); if (service.isPresent()) { setState(State.Normal); resetLifetimeCounter(); returnNotifications(service.get()); messageSent = true; startPublishingTimer(); } else { whenKeepAlive(); } } /* Subscription State Table Row 15 */ else if (publishRequestQueued && keepAliveCounter == 1 && (!publishingEnabled || (publishingEnabled && !notificationsAvailable))) { Optional<ServiceRequest<PublishRequest, PublishResponse>> service = Optional.ofNullable(publishQueue().poll()); if (service.isPresent()) { returnKeepAlive(service.get()); resetLifetimeCounter(); resetKeepAliveCounter(); startPublishingTimer(); } else { whenKeepAlive(); } } /* Subscription State Table Row 16 */ else if (keepAliveCounter > 1 && (!publishingEnabled || (publishingEnabled && !notificationsAvailable))) { keepAliveCounter--; startPublishingTimer(); } /* Subscription State Table Row 17 */ else if (!publishRequestQueued && (keepAliveCounter == 1 || (keepAliveCounter > 1 && publishingEnabled && notificationsAvailable))) { setState(State.Late); startPublishingTimer(); publishQueue().addSubscription(Subscription.this); } } } public enum State { Closing, Closed, Normal, KeepAlive, Late } public interface StateListener { void onStateChange(Subscription subscription, State previousState, State currentState); } }
public HttpContent(ContentProvider provider) { this.provider = provider; this.iterator = provider == null ? Collections.<ByteBuffer>emptyIterator() : provider.iterator(); }
@Override public Iterator<String> keyIterator() { return Collections.emptyIterator(); }
@Override public Iterator<Entry<String, T>> dictionaryIterator() { return Collections.emptyIterator(); }
public void doShouldRunReadOnlyLdbcWorkloadWithNothingDbWhileIgnoringScheduledStartTimesAndReturnExpectedMetrics( int threadCount, long operationCount, CompletionTimeService completionTimeService, ConcurrentErrorReporter errorReporter) throws InterruptedException, DbException, WorkloadException, IOException, MetricsCollectionException, CompletionTimeException, DriverConfigurationException, ExecutionException { ControlService controlService = null; Db db = null; Workload workload = null; MetricsService metricsService = null; try { Map<String, String> paramsMap = LdbcSnbInteractiveWorkloadConfiguration.defaultReadOnlyConfigSF1(); paramsMap.put( LdbcSnbInteractiveWorkloadConfiguration.PARAMETERS_DIRECTORY, TestUtils.getResource("/snb/interactive/").getAbsolutePath()); paramsMap.put( LdbcSnbInteractiveWorkloadConfiguration.UPDATES_DIRECTORY, TestUtils.getResource("/snb/interactive/").getAbsolutePath()); // Driver-specific parameters String name = null; String dbClassName = DummyLdbcSnbInteractiveDb.class.getName(); String workloadClassName = LdbcSnbInteractiveWorkload.class.getName(); int statusDisplayInterval = 1; TimeUnit timeUnit = TimeUnit.NANOSECONDS; String resultDirPath = temporaryFolder.newFolder().getAbsolutePath(); double timeCompressionRatio = 1.0; Set<String> peerIds = new HashSet<>(); ConsoleAndFileDriverConfiguration.ConsoleAndFileValidationParamOptions validationParams = null; String dbValidationFilePath = null; boolean calculateWorkloadStatistics = false; long spinnerSleepDuration = 0l; boolean printHelp = false; boolean ignoreScheduledStartTimes = true; long warmupCount = 100; ConsoleAndFileDriverConfiguration configuration = new ConsoleAndFileDriverConfiguration( paramsMap, name, dbClassName, workloadClassName, operationCount, threadCount, statusDisplayInterval, timeUnit, resultDirPath, timeCompressionRatio, peerIds, validationParams, dbValidationFilePath, calculateWorkloadStatistics, spinnerSleepDuration, printHelp, ignoreScheduledStartTimes, warmupCount); configuration = (ConsoleAndFileDriverConfiguration) configuration.applyArgs( MapUtils.loadPropertiesToMap( TestUtils.getResource("/snb/interactive/updateStream.properties"))); controlService = new LocalControlService( timeSource.nowAsMilli() + 1000, configuration, new Log4jLoggingServiceFactory(false), timeSource); LoggingService loggingService = new Log4jLoggingServiceFactory(false).loggingServiceFor("Test"); workload = new LdbcSnbInteractiveWorkload(); workload.init(configuration); db = new DummyLdbcSnbInteractiveDb(); db.init(configuration.asMap(), loggingService, workload.operationTypeToClassMapping()); GeneratorFactory gf = new GeneratorFactory(new RandomDataGeneratorFactory(42L)); Iterator<Operation> operations = gf.limit( WorkloadStreams.mergeSortedByStartTimeExcludingChildOperationGenerators( gf, workload.streams(gf, true)), configuration.operationCount()); Iterator<Operation> timeMappedOperations = gf.timeOffsetAndCompress(operations, controlService.workloadStartTimeAsMilli(), 1.0); WorkloadStreams workloadStreams = new WorkloadStreams(); workloadStreams.setAsynchronousStream( new HashSet<Class<? extends Operation>>(), new HashSet<Class<? extends Operation>>(), Collections.<Operation>emptyIterator(), timeMappedOperations, null); File resultsLog = temporaryFolder.newFile(); SimpleCsvFileWriter csvResultsLogWriter = new SimpleCsvFileWriter(resultsLog, SimpleCsvFileWriter.DEFAULT_COLUMN_SEPARATOR); metricsService = ThreadedQueuedMetricsService.newInstanceUsingBlockingBoundedQueue( timeSource, errorReporter, configuration.timeUnit(), ThreadedQueuedMetricsService.DEFAULT_HIGHEST_EXPECTED_RUNTIME_DURATION_AS_NANO, csvResultsLogWriter, workload.operationTypeToClassMapping(), LOGGING_SERVICE_FACTORY); int boundedQueueSize = DefaultQueues.DEFAULT_BOUND_1000; WorkloadRunner runner = new WorkloadRunner( timeSource, db, workloadStreams, metricsService, errorReporter, completionTimeService, controlService.loggingServiceFactory(), controlService.configuration().threadCount(), controlService.configuration().statusDisplayIntervalAsSeconds(), controlService.configuration().spinnerSleepDurationAsMilli(), controlService.configuration().ignoreScheduledStartTimes(), boundedQueueSize); runner.getFuture().get(); WorkloadResultsSnapshot workloadResults = metricsService.getWriter().results(); SimpleDetailedWorkloadMetricsFormatter metricsFormatter = new SimpleDetailedWorkloadMetricsFormatter(); assertThat( errorReporter.toString() + "\n" + metricsFormatter.format(workloadResults), errorReporter.errorEncountered(), is(false)); assertThat( errorReporter.toString() + "\n" + metricsFormatter.format(workloadResults), workloadResults.latestFinishTimeAsMilli() >= workloadResults.startTimeAsMilli(), is(true)); assertThat( errorReporter.toString() + "\n" + metricsFormatter.format(workloadResults), workloadResults.totalOperationCount(), is(operationCount)); WorkloadResultsSnapshot workloadResultsFromJson = WorkloadResultsSnapshot.fromJson(workloadResults.toJson()); assertThat(errorReporter.toString(), workloadResults, equalTo(workloadResultsFromJson)); assertThat( errorReporter.toString(), workloadResults.toJson(), equalTo(workloadResultsFromJson.toJson())); csvResultsLogWriter.close(); SimpleCsvFileReader csvResultsLogReader = new SimpleCsvFileReader( resultsLog, SimpleCsvFileReader.DEFAULT_COLUMN_SEPARATOR_REGEX_STRING); assertThat( (long) Iterators.size(csvResultsLogReader), is(configuration.operationCount())); // NOT + 1 because I didn't add csv headers csvResultsLogReader.close(); operationCount = metricsService.getWriter().results().totalOperationCount(); double operationsPerSecond = Math.round( ((double) operationCount / workloadResults.totalRunDurationAsNano()) * ONE_SECOND_AS_NANO); double microSecondPerOperation = (double) TimeUnit.NANOSECONDS.toMicros(workloadResults.totalRunDurationAsNano()) / operationCount; System.out.println( format( "[%s threads] Completed %s operations in %s = %s op/sec = 1 op/%s us", threadCount, numberFormatter.format(operationCount), TEMPORAL_UTIL.nanoDurationToString(workloadResults.totalRunDurationAsNano()), doubleNumberFormatter.format(operationsPerSecond), doubleNumberFormatter.format(microSecondPerOperation))); } finally { System.out.println(errorReporter.toString()); if (null != controlService) { controlService.shutdown(); } if (null != db) { db.close(); } if (null != workload) { workload.close(); } if (null != metricsService) { metricsService.shutdown(); } if (null != completionTimeService) { completionTimeService.shutdown(); } } }
@Override public Iterator<Pair<Long, Long>> iterator() { return Collections.emptyIterator(); }
@Override public Iterator<LineItem> iterator() { return (null != lineItems ? Collections.unmodifiableList(lineItems).iterator() : Collections.<LineItem>emptyIterator()); }