com.thematchbox.db.MongoDBIterator Maven / Gradle / Ivy
package com.thematchbox.db;
/* Copyright 2015 theMatchBox
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.
*/
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import java.util.Iterator;
public class MongoDBIterator implements Iterator {
private final DBCursor cursor;
private ObjectFactory objectFactory;
private ObjectCache objectCache;
MongoDBIterator(DBCursor cursor, ObjectFactory objectFactory, ObjectCache objectCache) {
this.cursor = cursor;
this.objectFactory = objectFactory;
this.objectCache = objectCache;
}
public int size() {
return cursor.size();
}
@Override
public boolean hasNext() {
return cursor.hasNext();
}
@Override
public D next() {
try {
DBObject next = cursor.next();
ObjectId id = objectFactory.get(next, DataObject.ID_FIELD);
if (objectCache.containsKey(id)) {
return objectCache.get(id);
} else {
D d = objectFactory.unmarshall(next);
objectCache.put(id, d);
return d;
}
} catch (MongoDBException e) {
MongoDataSource.logger.error(e.getMessage(), e);
return null;
}
}
@Override
public void remove() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy