Forum Stats

  • 3,770,003 Users
  • 2,253,045 Discussions


checking for existence

525861 Member Posts: 1
edited Aug 14, 2006 11:36AM in Berkeley DB Java Edition
Hello prognosticating, drowsy felines...

I would like to query a BDB JE (2.1.30) database to see whether an
item exists. At present I can call Database.get and ignore the
results if the item is found. However, I'm wondering if there is a
more efficient way to do this without having to pull the data out of
the database.

Any suggestions would be greatly appreciated.

S. David Pullara


  • Currently there's no more efficient way to check existence than Database.get()

    To expand further, in the current architecture, I think there would only be limited additional efficiency gains in an exists() operation if the operation had to be correct in presence of concurrent updates to the database. That's because in most cases the bulk of the cost of the operation is the fetch of the targeted data record node in order to obtain a record lock, which is required to guarantee that there's no concurrent update. An exists() operation could save on the work of passing the data back to the application after it was in hand, but that is probably a relatively small cost in most cases.

    An exists() could be more efficient if it didn't have to be correct in the face of concurrent updates, and we've used that approach in a new method that will be available in future releases, Database.count(). The count() method is geared toward providing a fast, non-transactional count that may be inaccurate if there are concurrent updates, but there seem to be more use cases for that. It seems that an exists() would be used in a mode where the application would want it be guaranteed to be correct.

    If the key does not exist, Database.get() is as efficient as a hypothetical exists() operation, which you've probably guessed.


This discussion has been closed.