Package com.sleepycat.je

Examples of com.sleepycat.je.Cursor


                        final DatabaseEntry data = new DatabaseEntry();

                        queue.clear();
                        OperationStatus status;
                        if (batchSize > 1) {
                            Cursor cursor = database.openCursor(null, CursorConfig.DEFAULT);
                            try {
                                status = cursor.getFirst(key, data, null);

                                BatchEvent batch = new BatchEvent();
                                for (int i = 0; status == OperationStatus.SUCCESS && i < batchSize; ++i) {
                                    SimpleEvent event = createEvent(data);
                                    if (event != null) {
                                        batch.addEvent(event);
                                    }
                                    status = cursor.getNext(key, data, null);
                                }
                                try {
                                    manager.send(batch);
                                } catch (Exception ioe) {
                                    LOGGER.error("Error sending events", ioe);
                                    break;
                                }
                                cursor.close();
                                cursor = null;
                                Transaction txn = environment.beginTransaction(null, null);
                                try {
                                    for (Event event : batch.getEvents()) {
                                        try {
                                            Map<String, String> headers = event.getHeaders();
                                            key = new DatabaseEntry(headers.get(FlumeEvent.GUID).getBytes(UTF8));
                                            database.delete(txn, key);
                                        } catch (Exception ex) {
                                            LOGGER.error("Error deleting key from database", ex);
                                        }
                                    }
                                    txn.commit();
                                } catch (Exception ex) {
                                    LOGGER.error("Unable to commit transaction", ex);
                                    if (txn != null) {
                                        txn.abort();
                                    }
                                }
                            } catch (Exception ex) {
                                LOGGER.error("Error reading database", ex);
                                shutdown = true;
                                break;
                            } finally {
                                if (cursor != null) {
                                    cursor.close();
                                }
                            }
                        } else {
                            Transaction txn = environment.beginTransaction(null, null);
                            Cursor cursor = database.openCursor(txn, null);
                            try {
                                status = cursor.getFirst(key, data, LockMode.RMW);
                                while (status == OperationStatus.SUCCESS) {
                                    SimpleEvent event = createEvent(data);
                                    if (event != null) {
                                        try {
                                            manager.doSend(event);
                                        } catch (Exception ioe) {
                                            errors = true;
                                            LOGGER.error("Error sending event", ioe);
                                            break;
                                        }
                                        if (!errors) {
                                            try {
                                                cursor.delete();
                                            } catch (Exception ex) {
                                                LOGGER.error("Unable to delete event", ex);
                                            }
                                        }
                                    }
                                    status = cursor.getNext(key, data, LockMode.RMW);
                                }
                                if (cursor != null) {
                                    cursor.close();
                                    cursor = null;
                                }
                                txn.commit();
                                txn = null;
                            } catch (Exception ex) {
                                LOGGER.error("Error reading or writing to database", ex);
                                shutdown = true;
                                break;
                            } finally {
                                if (cursor != null) {
                                    cursor.close();
                                }
                                if (txn != null) {
                                    txn.abort();
                                }
                            }
View Full Code Here


      try {
         currentTransaction.beginTransaction(null);
         for (Database db : new Database[]{cacheDb, expiryDb}) {
            long recordCount = ois.readLong();
            log.debugf("clearing and reading %s records from stream", recordCount);
            Cursor cursor = null;
            try {
               cursor = db.openCursor(currentTransaction.getTransaction(), null);
               for (int i = 0; i < recordCount; i++) {
                  byte[] keyBytes = (byte[]) ois.readObject();
                  byte[] dataBytes = (byte[]) ois.readObject();

                  DatabaseEntry key = new DatabaseEntry(keyBytes);
                  DatabaseEntry data = new DatabaseEntry(dataBytes);
                  cursor.put(key, data);
               }
            } finally {
               if (cursor != null) cursor.close();
            }
         }
         completeCurrentTransaction(true);
      } catch (Exception caught) {
         completeCurrentTransaction(false);
View Full Code Here

         currentTransaction.beginTransaction(null);
         for (Database db : new Database[]{cacheDb, expiryDb}) {
            long recordCount = db.count();
            oos.writeLong(recordCount);
            if (trace) log.tracef("writing %s records to stream", recordCount);
            Cursor cursor = null;
            try {
               cursor = db.openCursor(currentTransaction.getTransaction(), null);
               DatabaseEntry key = new DatabaseEntry();
               DatabaseEntry data = new DatabaseEntry();
               int recordsWritten = 0;
               while (cursor.getNext(key, data, null) ==
                     OperationStatus.SUCCESS) {
                  oos.writeObject(key.getData());
                  oos.writeObject(data.getData());
                  recordsWritten++;
               }
               if (trace) log.tracef("wrote %s records to stream", recordsWritten);
               if (recordsWritten != recordCount)
                  log.unexpectedNumberRecordsWritten(recordCount, recordsWritten);
            } finally {
               if (cursor != null) cursor.close();
            }
         }
         completeCurrentTransaction(true);
      } catch (Exception caught) {
         completeCurrentTransaction(false);
View Full Code Here

        Cursor openCursor(Transaction txn, CursorConfig config)
            throws DatabaseException {

            OperationStatus status;
            Cursor cursor = db.openCursor(txn, config);
            try {
                DatabaseEntry data = BasicIndex.NO_RETURN_ENTRY;
                status = cursor.getSearchKey(key, data, null);
            } catch (DatabaseException e) {
                try {
                    cursor.close();
                } catch (DatabaseException ignored) {}
                throw e;
            }
            if (status == OperationStatus.SUCCESS) {
                return cursor;
            } else {
                cursor.close();
                return null;
            }
        }
View Full Code Here

            this.doKeys = doKeys;
            try {
                cursors = new Cursor[conditions.size()];
                for (int i = 0; i < cursors.length; i += 1) {
                    Condition cond = conditions.get(i);
                    Cursor cursor = cond.openCursor(txn, config);
                    if (cursor == null) {
                        /* Leave joinCursor null. */
                        doClose(null);
                        return;
                    }
View Full Code Here

                        firstException = e;
                    }
                }
            }
            for (int i = 0; i < cursors.length; i += 1) {
                Cursor cursor = cursors[i];
                if (cursor != null) {
                    try {
                        cursor.close();
                        cursors[i] = null;
                    } catch (DatabaseException e) {
                        if (firstException == null) {
                            firstException = e;
                        }
View Full Code Here

                                       KeyRange pkeyRange,
                                       ValueAdapter<V> adapter,
                                       CursorConfig config)
        throws DatabaseException {

        Cursor cursor = db.openCursor(txn, config);
        RangeCursor rangeCursor =
            new RangeCursor(singleKeyRange, pkeyRange, sortedDups, cursor);
        return new SubIndexCursor<V>(rangeCursor, adapter);
    }
View Full Code Here

        if (concurrentDB) {
            cursorConfig = new CursorConfig();
            DbCompat.setWriteCursor(cursorConfig, true);
        }
        boolean failed = true;
        Cursor cursor = db.openCursor(txn, cursorConfig);
        LockMode lockMode = locking ? LockMode.RMW : null;
        try {
            while (true) {
                OperationStatus status =
                    cursor.getSearchKey(keyEntry, dataEntry, lockMode);
                if (status == OperationStatus.SUCCESS) {
                    E existing =
                        entityBinding.entryToObject(keyEntry, dataEntry);
                    entityBinding.objectToData(entity, dataEntry);
                    cursor.put(keyEntry, dataEntry);
                    failed = false;
                    return existing;
                } else {
                    entityBinding.objectToData(entity, dataEntry);
                    status = cursor.putNoOverwrite(keyEntry, dataEntry);
                    if (status != OperationStatus.KEYEXIST) {
                        failed = false;
                        return null;
                    }
                }
            }
        } finally {
            cursor.close();
            if (autoCommit) {
                if (failed) {
                    txn.abort();
                } else {
                    txn.commit();
View Full Code Here

            long count = 0;
            DatabaseEntry key = NO_RETURN_ENTRY;
            DatabaseEntry data = NO_RETURN_ENTRY;
            CursorConfig cursorConfig = locking ?
                CursorConfig.READ_UNCOMMITTED : null;
            Cursor cursor = db.openCursor(null, cursorConfig);
            try {
                OperationStatus status = cursor.getFirst(key, data, null);
                while (status == OperationStatus.SUCCESS) {
                    if (sortedDups) {
                        count += cursor.count();
                    } else {
                        count += 1;
                    }
                    status = cursor.getNextNoDup(key, data, null);
                }
            } finally {
                cursor.close();
            }
            return count;
        }
    }
View Full Code Here

                                       KeyRange range,
                                       ValueAdapter<V> adapter,
                                       CursorConfig config)
        throws DatabaseException {

        Cursor cursor = db.openCursor(txn, config);
        RangeCursor rangeCursor =
            new RangeCursor(range, null/*pkRange*/, sortedDups, cursor);
        return new BasicCursor<V>(rangeCursor, adapter, isUpdateAllowed());
    }
View Full Code Here

TOP

Related Classes of com.sleepycat.je.Cursor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.