org.springframework.data.util.CloseableIterator Java Examples

The following examples show how to use org.springframework.data.util.CloseableIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> void iterate(StorageIterator<T> iterator, Class<T> clazz, StoragePagination pagination) {
	MongoMetadata metadata = metadatas.get(clazz);
	Query query = new Query(Criteria.where(MongoMetadata.mongoId).exists(true));
	if (pagination != null) {
		query.skip(pagination.getFirst());
		query.limit(pagination.getSize());
	}
	try (CloseableIterator<T> stream = template.stream(query, clazz, metadata.getOrmName())) {
		while (stream.hasNext()) {
			try {
				// TODO 需要考虑中断
				final T object = stream.next();
				iterator.iterate(object);
			} catch (Throwable throwable) {
				throw new StorageQueryException(throwable);
			}
		}
	}
}
 
Example #2
Source File: MongoAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> void iterateIntersection(StorageIterator<T> iterator, Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	MongoMetadata metadata = metadatas.get(clazz);
	final Iterator<Entry<String, Object>> conditionIterator = condition.entrySet().iterator();
	Criteria criteria = Criteria.where(MongoMetadata.mongoId).exists(true);
	Criteria[] andCriterias = new Criteria[condition.size()];
	int index = 0;
	while (conditionIterator.hasNext()) {
		Entry<String, Object> keyValue = conditionIterator.next();
		String key = keyValue.getKey();
		Object value = keyValue.getValue();
		if (metadata.getPrimaryName().equals(key)) {
			key = MongoMetadata.mongoId;
		}
		andCriterias[index++] = Criteria.where(key).is(value);
	}
	Query query = Query.query(criteria.andOperator(andCriterias));
	if (pagination != null) {
		query.skip(pagination.getFirst());
		query.limit(pagination.getSize());
	}
	try (CloseableIterator<T> stream = template.stream(query, clazz, metadata.getOrmName())) {
		while (stream.hasNext()) {
			try {
				// TODO 需要考虑中断
				final T object = stream.next();
				iterator.iterate(object);
			} catch (Throwable throwable) {
				throw new StorageQueryException(throwable);
			}
		}
	}
}
 
Example #3
Source File: MongoAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> void iterateUnion(StorageIterator<T> iterator, Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	MongoMetadata metadata = metadatas.get(clazz);
	final Iterator<Entry<String, Object>> conditionIterator = condition.entrySet().iterator();
	Criteria criteria = Criteria.where(MongoMetadata.mongoId).exists(true);
	Criteria[] orCriterias = new Criteria[condition.size()];
	int index = 0;
	while (conditionIterator.hasNext()) {
		Entry<String, Object> keyValue = conditionIterator.next();
		String key = keyValue.getKey();
		Object value = keyValue.getValue();
		if (metadata.getPrimaryName().equals(key)) {
			key = MongoMetadata.mongoId;
		}
		orCriterias[index++] = Criteria.where(key).is(value);
	}
	Query query = Query.query(criteria.orOperator(orCriterias));
	if (pagination != null) {
		query.skip(pagination.getFirst());
		query.limit(pagination.getSize());
	}
	try (CloseableIterator<T> stream = template.stream(query, clazz, metadata.getOrmName())) {
		while (stream.hasNext()) {
			try {
				// TODO 需要考虑中断
				final T object = stream.next();
				iterator.iterate(object);
			} catch (Throwable throwable) {
				throw new StorageQueryException(throwable);
			}
		}
	}
}
 
Example #4
Source File: MapKeyValueAdapterUnitTests.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
@Test // DATAKV-99
public void scanShouldIterateOverAvailableEntries() {

	adapter.put("1", object1, COLLECTION_1);
	adapter.put("2", object2, COLLECTION_1);

	CloseableIterator<Map.Entry<Object, Object>> iterator = adapter.entries(COLLECTION_1);

	assertThat(iterator.next()).isEqualTo(new AbstractMap.SimpleEntry<>("1", object1));
	assertThat(iterator.next()).isEqualTo(new AbstractMap.SimpleEntry<>("2", object2));
	assertThat(iterator.hasNext()).isFalse();
}
 
Example #5
Source File: MapKeyValueAdapterUnitTests.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
@Test // DATAKV-99
public void scanDoesNotMixResultsFromMultipleKeyspaces() {

	adapter.put("1", object1, COLLECTION_1);
	adapter.put("2", object2, COLLECTION_2);

	CloseableIterator<Map.Entry<Object, Object>> iterator = adapter.entries(COLLECTION_1);

	assertThat(iterator.next()).isEqualTo(new AbstractMap.SimpleEntry<>("1", object1));
	assertThat(iterator.hasNext()).isFalse();
}
 
Example #6
Source File: ForwardingCloseableIteratorUnitTests.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
@Test // DATAKV-99
public void hasNextShouldDelegateToWrappedIterator() {

	when(iteratorMock.hasNext()).thenReturn(true);

	CloseableIterator<Entry<K, V>> iterator = new ForwardingCloseableIterator<>(iteratorMock);

	try {
		assertThat(iterator.hasNext()).isTrue();
		verify(iteratorMock, times(1)).hasNext();
	} finally {
		iterator.close();
	}
}
 
Example #7
Source File: ForwardingCloseableIteratorUnitTests.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
@Test // DATAKV-99
@SuppressWarnings("unchecked")
public void nextShouldDelegateToWrappedIterator() {

	when(iteratorMock.next()).thenReturn((Entry<K, V>) mock(Map.Entry.class));

	CloseableIterator<Entry<K, V>> iterator = new ForwardingCloseableIterator<>(iteratorMock);

	try {
		assertThat(iterator.next()).isNotNull();
		verify(iteratorMock, times(1)).next();
	} finally {
		iterator.close();
	}
}
 
Example #8
Source File: ForwardingCloseableIteratorUnitTests.java    From spring-data-keyvalue with Apache License 2.0 5 votes vote down vote up
@Test // DATAKV-99
public void nextShouldThrowErrorWhenWrappedIteratorHasNoMoreElements() {

	when(iteratorMock.next()).thenThrow(new NoSuchElementException());

	CloseableIterator<Entry<K, V>> iterator = new ForwardingCloseableIterator<>(iteratorMock);

	try {
		assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(iterator::next);
	} finally {
		iterator.close();
	}
}
 
Example #9
Source File: VaultKeyValueAdapter.java    From spring-vault with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIterator<Entry<Object, Object>> entries(String keyspace) {

	List<String> list = doList(keyspace);
	Iterator<String> iterator = list.iterator();

	return new CloseableIterator<Entry<Object, Object>>() {
		@Override
		public void close() {

		}

		@Override
		public boolean hasNext() {
			return iterator.hasNext();
		}

		@Override
		public Entry<Object, Object> next() {

			final String key = iterator.next();

			return new Entry<Object, Object>() {
				@Override
				public Object getKey() {
					return key;
				}

				@Nullable
				@Override
				public Object getValue() {
					return get(key, keyspace);
				}

				@Override
				public Object setValue(Object value) {
					throw new UnsupportedOperationException();
				}
			};
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
	};
}
 
Example #10
Source File: HazelcastKeyValueAdapter.java    From spring-data-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIterator<Map.Entry<Object, Object>> entries(String keyspace) {
    Iterator<Entry<Object, Object>> iterator = this.getMap(keyspace).entrySet().iterator();
    return new ForwardingCloseableIterator<>(iterator);
}
 
Example #11
Source File: MapKeyValueAdapter.java    From spring-data-keyvalue with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIterator<Entry<Object, Object>> entries(String keyspace) {
	return new ForwardingCloseableIterator<>(getKeySpaceMap(keyspace).entrySet().iterator());
}
 
Example #12
Source File: KeyValueAdapter.java    From spring-data-keyvalue with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link CloseableIterator} that iterates over all entries.
 *
 * @param keyspace must not be {@literal null}.
 * @return
 */
CloseableIterator<Map.Entry<Object, Object>> entries(String keyspace);