Constant lmdb_zero::put::NOOVERWRITE []

pub const NOOVERWRITE: Flags = Flags{bits: ffi::MDB_NOOVERWRITE,}

Enter the new key/data pair only if the key does not already appear in the database. The function will return KEYEXIST if the key already appears in the database, even if the database supports duplicates (DUPSORT).

Examples

In a 1:1 database

let db = lmdb::Database::open(
  &env, None, &lmdb::DatabaseOptions::defaults())
  .unwrap();
let txn = lmdb::WriteTransaction::new(&env).unwrap();
{
  let mut access = txn.access();
  access.put(&db, "Fruit", "Apple", lmdb::put::Flags::empty()).unwrap();
  // By default, collisions overwrite the old value
  access.put(&db, "Fruit", "Orange", lmdb::put::Flags::empty()).unwrap();
  assert_eq!("Orange", access.get::<str,str>(&db, "Fruit").unwrap());
  // But `NOOVERWRITE` prevents that
  assert!(access.put(&db, "Fruit", "Durian", lmdb::put::NOOVERWRITE).is_err());
  assert_eq!("Orange", access.get::<str,str>(&db, "Fruit").unwrap());
}
txn.commit().unwrap();

In a DUPSORT database

let db = lmdb::Database::open(
  &env, Some("reversed"),
  &lmdb::DatabaseOptions::create_multimap_unsized::<str,str>())
  .unwrap();
let txn = lmdb::WriteTransaction::new(&env).unwrap();
{
  let mut access = txn.access();
  // Ordinarily, we can add multiple items per key
  access.put(&db, "Fruit", "Apple", lmdb::put::Flags::empty()).unwrap();
  access.put(&db, "Fruit", "Orange", lmdb::put::Flags::empty()).unwrap();
  let mut cursor = txn.cursor(&db).unwrap();
  cursor.seek_k::<str,str>(&access, "Fruit").unwrap();
  assert_eq!(2, cursor.count().unwrap());

  // But this can be prevented with `NOOVERWRITE`
  access.put(&db, "Veggie", "Carrot", lmdb::put::NOOVERWRITE).unwrap();
  assert!(access.put(&db, "Veggie", "Squash", lmdb::put::NOOVERWRITE).is_err());
  cursor.seek_k::<str,str>(&access, "Veggie").unwrap();
  assert_eq!(1, cursor.count().unwrap());
}
txn.commit().unwrap();