package com.cloudhopper.mq.queue.impl;
/*
* #%L
* ch-mq
* %%
* Copyright (C) 2012 Cloudhopper by Twitter
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.cloudhopper.mq.util.CompositeKey;
import com.cloudhopper.mq.util.PriorityCompositeKeyUtil;
import com.cloudhopper.datastore.DataStore;
import com.cloudhopper.datastore.DataStoreFatalException;
import com.cloudhopper.datastore.DataStoreIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* DataStoreIterator that returns elements while the queue ID is the same.
* @author garth
*/
public class PerQueueDataStoreIterator implements DataStoreIterator {
private static final Logger logger = LoggerFactory.getLogger(PerQueueDataStoreIterator.class);
public PerQueueDataStoreIterator(int queueId, int queueIdByteLength, DataStoreIterator storeIterator) {
this.storeIterator = storeIterator;
this.queueId = queueId;
this.queueIdByteLength = queueIdByteLength;
this.keyUtil = new PriorityCompositeKeyUtil(queueIdByteLength, 8);
this.firstNext = true;
}
private boolean firstNext;
private final int queueId;
private final int queueIdByteLength;
private final PriorityCompositeKeyUtil keyUtil;
private final DataStoreIterator storeIterator;
public DataStoreIterator.Record getRecord() throws DataStoreFatalException {
return storeIterator.getRecord();
}
public boolean next() throws DataStoreFatalException {
if (firstNext) {
firstNext = false;
return true;
}
if (storeIterator.next()) {
DataStoreIterator.Record record = storeIterator.getRecord();
CompositeKey key = keyUtil.decode(record.getKey());
if (key.getQueueId() != queueId) return false;
return true;
} else {
return false;
}
}
public boolean jump(byte[] key) throws DataStoreFatalException {
throw new UnsupportedOperationException("jump() is not supported in this implementation");
}
public void close() throws DataStoreFatalException {
throw new UnsupportedOperationException("close() is not supported in this implementation");
}
}