use of com.sleepycat.je.Cursor in project crawler4j by yasserg.
the class WorkQueues method delete.
public void delete(int count) {
synchronized (mutex) {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
Transaction txn = beginTransaction();
try (Cursor cursor = openCursor(txn)) {
OperationStatus result = cursor.getFirst(key, value, null);
int matches = 0;
while ((matches < count) && (result == OperationStatus.SUCCESS)) {
cursor.delete();
matches++;
result = cursor.getNext(key, value, null);
}
}
commit(txn);
}
}
use of com.sleepycat.je.Cursor in project crawler4j by yasserg.
the class WorkQueues method get.
public List<WebURL> get(int max) {
synchronized (mutex) {
List<WebURL> results = new ArrayList<>(max);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
Transaction txn = beginTransaction();
try (Cursor cursor = openCursor(txn)) {
OperationStatus result = cursor.getFirst(key, value, null);
int matches = 0;
while ((matches < max) && (result == OperationStatus.SUCCESS)) {
if (value.getData().length > 0) {
results.add(webURLBinding.entryToObject(value));
matches++;
}
result = cursor.getNext(key, value, null);
}
}
commit(txn);
return results;
}
}
use of com.sleepycat.je.Cursor in project GeoGig by boundlessgeo.
the class JEObjectDatabase method deleteAll.
@Override
public long deleteAll(Iterator<ObjectId> ids, final BulkOpListener listener) {
checkWritable();
long count = 0;
UnmodifiableIterator<List<ObjectId>> partition = partition(ids, getBulkPartitionSize());
final DatabaseEntry data = new DatabaseEntry();
// do not retrieve data
data.setPartial(0, 0, true);
while (partition.hasNext()) {
List<ObjectId> nextIds = Lists.newArrayList(partition.next());
Collections.sort(nextIds);
final Transaction transaction = newTransaction();
CursorConfig cconfig = new CursorConfig();
final Cursor cursor = objectDb.openCursor(transaction, cconfig);
try {
DatabaseEntry key = new DatabaseEntry(new byte[ObjectId.NUM_BYTES]);
for (ObjectId id : nextIds) {
// copy id to key object without allocating new byte[]
id.getRawValue(key.getData());
OperationStatus status = cursor.getSearchKey(key, data, LockMode.DEFAULT);
if (OperationStatus.SUCCESS.equals(status)) {
OperationStatus delete = cursor.delete();
if (OperationStatus.SUCCESS.equals(delete)) {
listener.deleted(id);
count++;
} else {
listener.notFound(id);
}
} else {
listener.notFound(id);
}
}
cursor.close();
} catch (Exception e) {
cursor.close();
abort(transaction);
Throwables.propagate(e);
}
commit(transaction);
}
return count;
}
use of com.sleepycat.je.Cursor in project crawler4j by yasserg.
the class InProcessPagesDB method removeURL.
public boolean removeURL(WebURL webUrl) {
synchronized (mutex) {
DatabaseEntry key = getDatabaseEntryKey(webUrl);
DatabaseEntry value = new DatabaseEntry();
Transaction txn = beginTransaction();
try (Cursor cursor = openCursor(txn)) {
OperationStatus result = cursor.getSearchKey(key, value, null);
if (result == OperationStatus.SUCCESS) {
result = cursor.delete();
if (result == OperationStatus.SUCCESS) {
return true;
}
}
} finally {
commit(txn);
}
}
return false;
}
use of com.sleepycat.je.Cursor in project GeoGig by boundlessgeo.
the class JEObjectDatabase method lookUpInternal.
@Override
protected List<ObjectId> lookUpInternal(final byte[] partialId) {
checkOpen();
DatabaseEntry key;
{
byte[] keyData = partialId.clone();
key = new DatabaseEntry(keyData);
}
DatabaseEntry data = new DatabaseEntry();
// do not retrieve data
data.setPartial(0, 0, true);
List<ObjectId> matches;
CursorConfig cursorConfig = new CursorConfig();
cursorConfig.setReadUncommitted(true);
Transaction transaction = null;
Cursor cursor = objectDb.openCursor(transaction, cursorConfig);
try {
// position cursor at the first closest key to the one looked up
OperationStatus status = cursor.getSearchKeyRange(key, data, LockMode.READ_UNCOMMITTED);
if (SUCCESS.equals(status)) {
matches = new ArrayList<ObjectId>(2);
final byte[] compKey = new byte[partialId.length];
while (SUCCESS.equals(status)) {
byte[] keyData = key.getData();
System.arraycopy(keyData, 0, compKey, 0, compKey.length);
if (Arrays.equals(partialId, compKey)) {
matches.add(new ObjectId(keyData));
} else {
break;
}
status = cursor.getNext(key, data, LockMode.READ_UNCOMMITTED);
}
} else {
matches = Collections.emptyList();
}
return matches;
} finally {
cursor.close();
}
}
Aggregations