All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.emc.mongoose.load.generator.StorageItemInput Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha5
Show newest version
package com.emc.mongoose.load.generator;

import com.github.akurilov.commons.io.collection.BufferingInputBase;
import com.emc.mongoose.api.model.io.task.IoTask;

import com.emc.mongoose.api.model.item.DataItemFactory;
import com.emc.mongoose.api.model.item.Item;
import com.emc.mongoose.api.model.item.ItemFactory;
import com.emc.mongoose.api.model.storage.StorageDriver;

import java.io.IOException;
import java.util.List;

/**
 Created by andrey on 02.12.16.
 */
public final class StorageItemInput
extends BufferingInputBase {

	private final StorageDriver> storageDriver;
	private final ItemFactory itemFactory;
	private final String path;
	private final String prefix;
	private final int idRadix;

	private boolean poisonedFlag = false;

	public StorageItemInput(
		final StorageDriver> storageDriver, final int batchSize,
		final ItemFactory itemFactory, final String path, final String prefix, final int idRadix
	) {
		super(batchSize);
		this.storageDriver = storageDriver;
		this.itemFactory = itemFactory;
		this.path = path;
		this.prefix = prefix;
		this.idRadix = idRadix;
	}

	@Override
	protected final int loadMoreItems(final I lastItem)
	throws IOException {
		if(poisonedFlag) {
			return 0;
		}
		final List newItems = storageDriver.list(
			itemFactory, path, prefix, idRadix, lastItem, capacity
		);
		final int n = newItems.size();
		I nextItem;
		for(int i = 0; i < n; i ++) {
			nextItem = newItems.get(i);
			if(null == nextItem) {
				poisonedFlag = true;
				return i;
			} else {
				items.add(nextItem);
			}
		}
		return n;
	}

	@Override
	public final String toString() {
		return (itemFactory instanceof DataItemFactory ? "Data" : "") +
			"ItemsFromPath(" + path + ")";
	}

	@Override
	public final void reset()
	throws IOException {
		super.reset();
		poisonedFlag = false;
	}
}