Search in sources :

Example 1 with Cursor

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);
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor)

Example 2 with Cursor

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;
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) ArrayList(java.util.ArrayList) WebURL(edu.uci.ics.crawler4j.url.WebURL) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor)

Example 3 with Cursor

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;
}
Also used : Transaction(com.sleepycat.je.Transaction) ObjectId(org.locationtech.geogig.api.ObjectId) OperationStatus(com.sleepycat.je.OperationStatus) List(java.util.List) ArrayList(java.util.ArrayList) DatabaseEntry(com.sleepycat.je.DatabaseEntry) CursorConfig(com.sleepycat.je.CursorConfig) Cursor(com.sleepycat.je.Cursor) ExecutionException(java.util.concurrent.ExecutionException) EnvironmentLockedException(com.sleepycat.je.EnvironmentLockedException)

Example 4 with Cursor

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;
}
Also used : Transaction(com.sleepycat.je.Transaction) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) Cursor(com.sleepycat.je.Cursor)

Example 5 with Cursor

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();
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) ObjectId(org.locationtech.geogig.api.ObjectId) OperationStatus(com.sleepycat.je.OperationStatus) DatabaseEntry(com.sleepycat.je.DatabaseEntry) CursorConfig(com.sleepycat.je.CursorConfig) Cursor(com.sleepycat.je.Cursor)

Aggregations

Cursor (com.sleepycat.je.Cursor)5 DatabaseEntry (com.sleepycat.je.DatabaseEntry)5 OperationStatus (com.sleepycat.je.OperationStatus)5 Transaction (com.sleepycat.je.Transaction)5 CursorConfig (com.sleepycat.je.CursorConfig)2 ArrayList (java.util.ArrayList)2 ObjectId (org.locationtech.geogig.api.ObjectId)2 EnvironmentLockedException (com.sleepycat.je.EnvironmentLockedException)1 WebURL (edu.uci.ics.crawler4j.url.WebURL)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1