com.sleepycat.je.Cursor类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(16.6k)|赞(0)|评价(0)|浏览(197)

本文整理了Java中com.sleepycat.je.Cursor类的一些代码示例,展示了Cursor类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cursor类的具体详情如下:
包路径:com.sleepycat.je.Cursor
类名称:Cursor

Cursor介绍

[英]A database cursor. Cursors are used for operating on collections of records, for iterating over a database, and for saving handles to individual records, so that they can be modified after they have been read.

Cursors which are opened with a transaction instance are transactional cursors and may be used by multiple threads, but only serially. That is, the application must serialize access to the handle. Non-transactional cursors, opened with a null transaction instance, may not be used by multiple threads.

If the cursor is to be used to perform operations on behalf of a transaction, the cursor must be opened and closed within the context of that single transaction.

Once the cursor #close method has been called, the handle may not be accessed again, regardless of the close method's success or failure, with one exception: the close method itself may be called any number of times to simplify error handling.

To obtain a cursor with default attributes:

Cursor cursor = myDatabase.openCursor(txn, null);

To customize the attributes of a cursor, use a CursorConfig object.

CursorConfig config = new CursorConfig(); 
config.setReadUncommitted(true); 
Cursor cursor = myDatabase.openCursor(txn, config);

Modifications to the database during a sequential scan will be reflected in the scan; that is, records inserted behind a cursor will not be returned while records inserted in front of a cursor will be returned.

By default, a cursor is "sticky", meaning that the prior position is maintained by cursor movement operations, and the cursor stays at the prior position when the operation does not succeed. However, it is possible to configure a cursor as non-sticky to enable certain performance benefits. See CursorConfig#setNonSticky for details.

Using Null and Partial DatabaseEntry Parameters

Null can be passed for DatabaseEntry output parameters if the value is not needed. The DatabaseEntry#setPartialproperty can also be used to optimize in certain cases. These provide varying degrees of performance benefits that depend on the specific operation, as described below.

When retrieving a record with a Database or Cursormethod, if only the key is needed by the application then the retrieval of the data item can be suppressed by passing null. If null is passed as the data parameter, the data item will not be returned by the Database or Cursor method.

Suppressing the return of the data item potentially has a large performance benefit. In this case, if the record data is not already in the JE cache, it will not be read from disk. The performance benefit is potentially large because random access disk reads may be reduced. Examples use cases are:

  • Scanning all records in key order, when the data is not needed.
  • Skipping over records quickly with READ_UNCOMMITTED isolation to select records for further processing by examining the key value.

Note that by "record data" we mean both the data parameter for a regular or primary DB, and the pKey parameter for a secondary DB. However, the performance advantage of a key-only operation does not apply to databases configured for duplicates. For a duplicates DB, the data is always available along with the key and does not have to be fetched separately.

The Partial property may also be used to retrieve or update only a portion of a data item. This avoids copying the entire record between the JE cache and the application data parameter. However, this feature has less of a performance benefit than one might assume, since the entire record is always read or written to the database, and the entire record is cached. A partial update may be performed only with Cursor#putCurrent.

A null or partial DatabaseEntry output parameter may also be used in other cases, for example, to retrieve a partial key item. However, in practice this has limited value since the entire key is usually needed by the application, and the benefit of copying a portion of the key is generally very small.

Historical note: Prior to JE 7.0, null could not be passed for output parameters. Instead, DatabaseEntry.setPartial(0, 0, true) was called for a data parameter to avoid reading the record's data. Now, null can be passed instead.
[中]数据库游标。游标用于对记录集合进行操作、对数据库进行迭代以及将句柄保存到单个记录,以便在读取记录后可以对其进行修改。
使用事务实例打开的游标是事务游标,可以由多个线程使用,但只能串行使用。也就是说,应用程序必须序列化对句柄的访问。使用空事务实例打开的非事务游标不能由多个线程使用。
如果要使用游标代表事务执行操作,则必须在单个事务的上下文中打开和关闭游标。
一旦调用了cursor#close方法,无论close方法成功与否,句柄都可能无法再次访问,只有一个例外:close方法本身可能被调用任意次数以简化错误处理。
要获取具有默认属性的光标,请执行以下操作:

Cursor cursor = myDatabase.openCursor(txn, null);

要自定义光标的属性,请使用CursorConfig对象。

CursorConfig config = new CursorConfig(); 
config.setReadUncommitted(true); 
Cursor cursor = myDatabase.openCursor(txn, config);

顺序扫描期间对数据库的修改将反映在扫描中;也就是说,插入到游标后面的记录将不会被返回,而插入到游标前面的记录将被返回。
默认情况下,光标是“粘滞”的,这意味着光标移动操作将保持先前的位置,并且当操作未成功时,光标将保持在先前的位置。但是,可以将游标配置为非粘性,以实现某些性能优势。有关详细信息,请参见CursorConfig#SetNonSicky。

Using Null and Partial DatabaseEntry Parameters

如果不需要值,则可以为DatabaseEntry输出参数传递Null。在某些情况下,还可以使用DatabaseEntry#setPartialproperty进行优化。它们提供不同程度的性能优势,具体取决于具体操作,如下所述。
使用数据库或Cursormethod检索记录时,如果应用程序只需要密钥,则可以通过传递null来抑制对数据项的检索。如果将null作为数据参数传递,则数据库或游标方法将不会返回数据项。
抑制数据项的返回可能有很大的性能优势。在这种情况下,如果记录数据不在JE缓存中,则不会从磁盘读取。由于可以减少随机访问磁盘读取,因此性能优势可能很大。示例用例包括:
*不需要数据时,按键顺序扫描所有记录。
*使用READ_UNCOMMITTED隔离快速跳过记录,通过检查键值来选择要进一步处理的记录。
注意,“记录数据”指的是常规数据库或主数据库的数据参数,以及辅助数据库的pKey参数。但是,仅密钥操作的性能优势不适用于为重复配置的数据库。对于duplicates DB,数据始终与密钥一起可用,不必单独获取。
Partial属性还可用于仅检索或更新数据项的一部分。这样可以避免在JE缓存和应用程序数据参数之间复制整个记录。但是,由于整个记录总是读取或写入到数据库中,并且整个记录都被缓存,因此此功能的性能优势可能比人们想象的要小。部分更新只能在游标为当前时执行。
空或部分DatabaseEntry输出参数也可用于其他情况,例如,检索部分键项。然而,在实践中,这具有有限的价值,因为应用程序通常需要整个密钥,并且复制密钥的一部分的好处通常很小。
历史注释:在JE 7.0之前,输出参数不能传递null。而是数据库条目。为数据参数调用了setPartial(0,0,true),以避免读取记录的数据。现在,可以改为传递null。

代码示例

代码示例来源:origin: internetarchive/heritrix3

/**
 * @return the key to the first item in the database
 * @throws DatabaseException
 */
protected DatabaseEntry getFirstKey() throws DatabaseException {
  DatabaseEntry key = new DatabaseEntry();
  DatabaseEntry value = new DatabaseEntry();
  Cursor cursor = pendingUrisDB.openCursor(null,null);
  OperationStatus status = cursor.getNext(key,value,null);
  cursor.close();
  if(status == OperationStatus.SUCCESS) {
    return key;
  }
  return null;
}

代码示例来源:origin: internetarchive/heritrix3

long schemeAuthorityKeyLong = calcSchemeAuthorityKeyBytes(url);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
byte[] schemeAuthorityKeyBytes = key.getData();
Cursor cursor = alreadySeen.openCursor(null, null);
long forgottenCount = 0l;
for (OperationStatus status = cursor.getSearchKeyRange(key, value, null);
    status == OperationStatus.SUCCESS;
    status = cursor.getNext(key, value, null)) {
  byte[] keyData = key.getData();
      && keyData[1] == schemeAuthorityKeyBytes[1]
      && keyData[2] == schemeAuthorityKeyBytes[2]) {
    cursor.delete();
    forgottenCount++;
  } else {
cursor.close();
long newCount = count.addAndGet(-forgottenCount);
logger.info("forgot " + forgottenCount + " urls from scheme+authority of url " + url + " (leaving " + newCount + " urls from other scheme+authorities)");

代码示例来源:origin: yasserg/crawler4j

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;
  }
}

代码示例来源:origin: yasserg/crawler4j

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);
  }
}

代码示例来源:origin: voldemort/voldemort

@Override
public void transfer() throws Exception {
  cursor = srcDB.openCursor(null, null);
  DatabaseEntry keyEntry = new DatabaseEntry();
  DatabaseEntry valueEntry = new DatabaseEntry();
  List<Versioned<byte[]>> vals;
  long startTime = System.currentTimeMillis();
  int scanCount = 0;
  int keyCount = 0;
  while(cursor.getNext(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
    keyCount++;
    vals = StoreBinaryFormat.fromByteArray(valueEntry.getData());
    scanCount += vals.size();
    // pull out the real key
    byte[] stripedKey = StoreBinaryFormat.extractKey(keyEntry.getData());
    OperationStatus putStatus = dstDB.put(null, new DatabaseEntry(stripedKey), valueEntry);
    if(OperationStatus.SUCCESS != putStatus) {
      String errorStr = "Put failed with " + putStatus + " for key"
               + BdbConvertData.writeAsciiString(keyEntry.getData());
      logger.error(errorStr);
      throw new Exception(errorStr);
    }
    if(scanCount % 1000000 == 0)
      logger.info("Reverted " + scanCount + " entries in "
            + (System.currentTimeMillis() - startTime) / 1000 + " secs");
  }
  logger.info("Reverted " + scanCount + " entries and " + keyCount + " keys in "
        + (System.currentTimeMillis() - startTime) / 1000 + " secs");
}

代码示例来源:origin: addthis/hydra

@Override
public byte[] floorKey(byte[] key) {
  final DatabaseEntry target = new DatabaseEntry(key);
  final DatabaseEntry dk = new DatabaseEntry(key);
  Cursor cursor = null;
  final DatabaseEntry dvs = new DatabaseEntry();
  dvs.setPartial(0, 0, true);
  try {
    cursor = bdb.openCursor(null, CursorConfig.READ_UNCOMMITTED);
    OperationStatus status = cursor.getSearchKeyRange(dk, dvs, lockMode);
      status = cursor.getLast(dk, dvs, lockMode);
    while (status == opSuccess && (comparison = bdb.compareKeys(target, dk)) < 0) {
      status = cursor.getPrev(dk, dvs, lockMode);
  } finally {
    if (cursor != null) {
      cursor.close();

代码示例来源:origin: com.fasterxml.storemate/storemate-backend-bdb-je

Cursor crsr = _store.openCursor(null, config);
final DatabaseEntry keyEntry;
final DatabaseEntry valueEntry = new DatabaseEntry();
keyEntry = new DatabaseEntry();
OperationStatus status = crsr.getFirst(keyEntry, valueEntry, null);
try {
  int index = 0;
  for (; status == OperationStatus.SUCCESS; status = crsr.getNext(keyEntry, valueEntry, null)) {
    try {
      all.add(dbToValue(valueEntry));
      String keyStr = "N/A";
      try {
        K key = rawToKey(keyEntry.getData(), keyEntry.getOffset(), keyEntry.getSize());
        keyStr = key.toString();
      } catch (Exception e2) {
  crsr.close();

代码示例来源:origin: addthis/hydra

@Override
public byte[] lastKey() {
  Cursor c = bdb.openCursor(null, CursorConfig.READ_UNCOMMITTED);
  try {
    DatabaseEntry dk = new DatabaseEntry();
    if (c.getLast(dk, new DatabaseEntry(), lockMode) == opSuccess) {
      return dk.getData();
    }
    return null;
  } finally {
    c.close();
  }
}

代码示例来源:origin: addthis/hydra

/**
 * internal/raw bdb delete
 */
@Override public byte[] delete(byte[] key) throws DatabaseException {
  DatabaseEntry dk = new DatabaseEntry(key);
  DatabaseEntry dv = new DatabaseEntry();
  dv.setPartial(0, 0, true);
  Cursor cursor = bdb.openCursor(null, CursorConfig.READ_UNCOMMITTED);
  try {
    if (cursor.getSearchKey(dk, dv, lockMode) == opSuccess && cursor.delete() == opSuccess && cursor.getPrev(dk, dv, lockMode) == opSuccess) {
      return dk.getData();
    }
  } finally {
    cursor.close();
  }
  return null;
}

代码示例来源:origin: addthis/hydra

/**
 * return first key or value
 */
private byte[] first(boolean key) {
  Cursor c = bdb.openCursor(null, cursorConfig);
  try {
    DatabaseEntry dk = new DatabaseEntry();
    DatabaseEntry dv = new DatabaseEntry();
    if (c.getFirst(dk, dv, lockMode) == opSuccess) {
      gets.incrementAndGet();
      bytesIn.addAndGet(dk.getSize() + dv.getSize());
      return key ? dk.getData() : dv.getData();
    }
    return null;
  } finally {
    c.close();
  }
}

代码示例来源:origin: voldemort/voldemort

DatabaseEntry keyEntry = new DatabaseEntry();
DatabaseEntry valueEntry = new DatabaseEntry();
OperationStatus status;
valueEntry.setPartial(true);
try {
  if(!positioned) {
    positioned = true;
    keyEntry.setData(StoreBinaryFormat.makePartitionKey(partition));
    status = cursor.getSearchKeyRange(keyEntry,
                     valueEntry,
                     LockMode.READ_UNCOMMITTED);
  } else {
    status = cursor.getNext(keyEntry, valueEntry, LockMode.READ_UNCOMMITTED);

代码示例来源:origin: com.sleepycat/je

boolean endInclusive) {
final DatabaseEntry key = new DatabaseEntry();
final DatabaseEntry noData = new DatabaseEntry();
noData.setPartial(0, 0, true);
            beginKey.getSize());
      if (c.getSearchKeyRange(key, noData, lockMode) !=
        OperationStatus.SUCCESS) {
        return 0;
      if (!beginInclusive && key.equals(beginKey)) {
        if (c.getNext(key, noData, lockMode) !=
          OperationStatus.SUCCESS) {
          return 0;
      if (c.getFirst(key, noData, lockMode) !=
        OperationStatus.SUCCESS) {
        return 0;
    c.close();

代码示例来源:origin: org.jboss.cache/jbosscache-core

/**
* Internal version of removeData(Fqn) that allows passing a transaction.
*/
private void doRemoveData(Transaction txn, Fqn name)
   throws Exception
{
 DatabaseEntry dataEntry = new DatabaseEntry();
 dataEntry.setPartial(0, 0, true);
 DatabaseEntry keyEntry = makeKeyEntry(name);
 Cursor cursor = cacheDb.openCursor(txn, null);
 try
 {
   OperationStatus status =
      cursor.getSearchKey(keyEntry, dataEntry, LockMode.RMW);
   if (status == OperationStatus.SUCCESS)
   {
    cursor.putCurrent(makeDataEntry(null));
   }
 }
 finally
 {
   cursor.close();
 }
}

代码示例来源:origin: co.paralleluniverse/galaxy

@Override
public short findAllocation(long ref) {
  final DatabaseEntry key = new DatabaseEntry();
  final DatabaseEntry data = new DatabaseEntry();
  try (Cursor cursor = allocationDirectory.openCursor(null, CursorConfig.DEFAULT)) {
    OperationStatus retVal = cursor.getSearchKeyRange(key, data, null);
    if (retVal == OperationStatus.SUCCESS) {
      ownerDirectory.put(null, key, SERVER);
      return Shorts.fromByteArray(data.getData());
    } else if (retVal == OperationStatus.NOTFOUND)
      return (short) -1;
    throw new AssertionError();
  }
}

代码示例来源:origin: yasserg/crawler4j

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;
  }
}

代码示例来源:origin: opensourceBIM/BIMserver

@Override
public List<byte[]> getDuplicates(String tableName, byte[] keyBytes, DatabaseSession databaseSession) throws BimserverDatabaseException {
  DatabaseEntry key = new DatabaseEntry(keyBytes);
  DatabaseEntry value = new DatabaseEntry();
  try {
    TableWrapper tableWrapper = getTableWrapper(tableName);
    Cursor cursor = tableWrapper.getDatabase().openCursor(getTransaction(databaseSession, tableWrapper), getCursorConfig(tableWrapper));
    try {
      OperationStatus operationStatus = cursor.getSearchKey(key, value, LockMode.DEFAULT);
      List<byte[]> result = new ArrayList<byte[]>();
      while (operationStatus == OperationStatus.SUCCESS) {
        result.add(value.getData());
        operationStatus = cursor.getNextDup(key, value, LockMode.DEFAULT);
      }
      return result;
    } finally {
      cursor.close();
    }
  } catch (DatabaseException e) {
    LOGGER.error("", e);
  }
  return null;
}

代码示例来源:origin: voldemort/voldemort

private boolean fetchNextKey() {
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    valueEntry.setPartial(true);
    try {
      OperationStatus status = cursor.getNext(keyEntry,
                          valueEntry,
                          LockMode.READ_UNCOMMITTED);
      if(OperationStatus.NOTFOUND == status) {
        // we have reached the end of the cursor
        return false;
      }
      if(bdbEngine.isPartitionScanSupported())
        current = new ByteArray(StoreBinaryFormat.extractKey(keyEntry.getData()));
      else
        current = new ByteArray(keyEntry.getData());
      return true;
    } catch(DatabaseException e) {
      bdbEngine.bdbEnvironmentStats.reportException(e);
      logger.error(e);
      throw new PersistenceFailureException(e);
    }
  }
}

代码示例来源:origin: com.sleepycat/je

cursor = makeCursor(txn);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
  bucket.fillDataEntry(data);
  OperationStatus status = cursor.putCurrent(data);
  status = cursor.getNext(key, data, LockMode.DEFAULT);
  if (status != OperationStatus.SUCCESS) {
    return lastOnDiskVLSN;
final DatabaseEntry noData = new DatabaseEntry();
noData.setPartial(0, 0, true);
int deleteCount = 0;
  OperationStatus status = cursor.delete();
} while (cursor.getNext(key, noData, LockMode.DEFAULT) ==
     OperationStatus.SUCCESS);
OperationStatus status = cursor.getLast(key, data,
                    LockMode.DEFAULT);
  cursor.close();

代码示例来源:origin: voldemort/voldemort

protected boolean makeMore() {
    DatabaseEntry keyEntry = new DatabaseEntry();
    DatabaseEntry valueEntry = new DatabaseEntry();
    try {
      OperationStatus status = cursor.getNext(keyEntry,
                          valueEntry,
                          LockMode.READ_UNCOMMITTED);
      if(OperationStatus.NOTFOUND == status) {
        // we have reached the end of the cursor
        return false;
      }
      ByteArray key = null;
      if(bdbEngine.isPartitionScanSupported())
        key = new ByteArray(StoreBinaryFormat.extractKey(keyEntry.getData()));
      else
        key = new ByteArray(keyEntry.getData());
      for(Versioned<byte[]> val: StoreBinaryFormat.fromByteArray(valueEntry.getData()))
        this.cache.add(Pair.create(key, val));
      return true;
    } catch(DatabaseException e) {
      bdbEngine.bdbEnvironmentStats.reportException(e);
      logger.error(e);
      throw new PersistenceFailureException(e);
    }
  }
}

代码示例来源:origin: org.deephacks/graphene-core

public Map<byte[], byte[]> listAll() {
 Map<byte[], byte[]> map = new HashMap<>();
 try (Cursor cursor = db.get().openCursor(tm.getInternalTx(), null)) {
  DatabaseEntry firstKey = new DatabaseEntry(RowKey.getMinId().getKey());
  DatabaseEntry entry = new DatabaseEntry();
  if (cursor.getSearchKeyRange(firstKey, entry, LockMode.RMW) == OperationStatus.SUCCESS) {
   map.put(firstKey.getData(), entry.getData());
  }
  while (cursor.getNextNoDup(firstKey, entry, LockMode.RMW) == OperationStatus.SUCCESS) {
   map.put(firstKey.getData(), entry.getData());
  }
 }
 return map;
}

相关文章