
com.cloudhopper.datastore.leveldb.LevelDbDataStoreIterator Maven / Gradle / Ivy
The newest version!
package com.cloudhopper.datastore.leveldb;
/*
* #%L
* ch-datastore
* %%
* 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 java.io.IOException;
import java.util.Map.Entry;
import com.cloudhopper.datastore.DataStoreFatalException;
import com.cloudhopper.datastore.DataStoreIterator;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBIterator;
/**
* DateStoreIterator for LevelDB that uses an iterator.
*
* @author garth
*/
public class LevelDbDataStoreIterator implements DataStoreIterator {
private DBIterator cursor;
private boolean firstNextCall;
private Entry currentEntry;
public LevelDbDataStoreIterator(DB db) {
this.cursor = db.iterator();
this.firstNextCall = true;
}
public boolean next() throws DataStoreFatalException {
if (this.firstNextCall) {
this.firstNextCall = false;
this.cursor.seekToFirst();
}
if (cursor.hasNext()) {
currentEntry = cursor.next();
return true;
} else {
return false;
}
}
public boolean jump(byte[] key) throws DataStoreFatalException {
if (this.firstNextCall) this.firstNextCall = false;
this.cursor.seek(key);
if (cursor.hasNext()) {
currentEntry = cursor.next();
return true;
} else {
return false;
}
}
public Record getRecord() throws DataStoreFatalException {
byte[] key = currentEntry.getKey();
byte[] value = currentEntry.getValue();
if (key == null || value == null) {
throw new DataStoreFatalException("Record key or value was null");
}
return new Record(key, value);
}
public void close() throws DataStoreFatalException {
try {
cursor.close();
cursor = null;
} catch (IOException e) {
throw new DataStoreFatalException("Error closing store", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy