@@ -2646,6 +2646,82 @@ impl<KC, DC, C, CDUP> Database<KC, DC, C, CDUP> {
26462646 }
26472647 }
26482648
2649+ /// Removes this database entirely.
2650+ ///
2651+ /// # Safety
2652+ ///
2653+ /// Ensure that no other copies of the database exist before calling, as
2654+ /// they will become invalid.
2655+ /// Do not remove a database if an existing transaction has modified it.
2656+ /// Doing so can cause database corruption or other errors.
2657+ ///
2658+ /// ```
2659+ /// # use std::fs;
2660+ /// # use std::path::Path;
2661+ /// # use heed::EnvOpenOptions;
2662+ /// use heed::Database;
2663+ /// use heed::types::*;
2664+ /// use heed::byteorder::BigEndian;
2665+ ///
2666+ /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
2667+ /// # let dir = tempfile::tempdir()?;
2668+ /// # let env = unsafe { EnvOpenOptions::new()
2669+ /// # .map_size(10 * 1024 * 1024) // 10MB
2670+ /// # .max_dbs(3000)
2671+ /// # .open(dir.path())?
2672+ /// # };
2673+ /// /// List databases in an env
2674+ #[ cfg_attr( not( master3) , doc = concat!(
2675+ "fn list_dbs(env: &heed::Env, rotxn: &heed::RoTxn<'_>) -> heed::Result<Vec<String>> {\n " ,
2676+ " let names_db: Database<Str, DecodeIgnore> =" ,
2677+ ) ) ]
2678+ #[ cfg_attr( master3, doc = concat!(
2679+ "fn list_dbs(\n " ,
2680+ " env: &heed::Env,\n " ,
2681+ " rotxn: &heed::RoTxn<'_>,\n " ,
2682+ ") -> Result<Vec<String>, Box<dyn std::error::Error>> {\n " ,
2683+ " // mdb-master3 uses null-terminated C strings as DB names\n " ,
2684+ " let names_db: Database<Bytes, DecodeIgnore> =" ,
2685+ ) ) ]
2686+ /// env.open_database(&rotxn, None)?
2687+ /// .expect("the unnamed database always exists");
2688+ /// let mut names = Vec::new();
2689+ /// for item in names_db.iter(&rotxn)? {
2690+ /// let (name, ()) = item?;
2691+ #[ cfg_attr( master3, doc = concat!(
2692+ " let name = std::ffi::CStr::from_bytes_with_nul(name)?.to_str()?;" ,
2693+ ) ) ]
2694+ /// names.push(name.to_owned());
2695+ /// }
2696+ /// Ok(names)
2697+ /// }
2698+ ///
2699+ /// type BEI32 = I32<BigEndian>;
2700+ ///
2701+ /// let mut rwtxn = env.write_txn()?;
2702+ /// let db: Database<BEI32, Str> = env.create_database(&mut rwtxn, Some("iter-i32"))?;
2703+ /// rwtxn.commit()?;
2704+ ///
2705+ /// let rotxn = env.read_txn()?;
2706+ /// let db_names = list_dbs(&env, &rotxn)?;
2707+ /// assert_eq!(db_names, vec!["iter-i32".to_owned()]);
2708+ /// drop(rotxn);
2709+ ///
2710+ /// let mut rwtxn = env.write_txn()?;
2711+ /// unsafe { db.remove(&mut rwtxn)? };
2712+ /// let db_names = list_dbs(&env, &rwtxn)?;
2713+ /// assert!(db_names.is_empty());
2714+ /// rwtxn.commit()?;
2715+ /// # Ok(()) }
2716+ /// ```
2717+ pub unsafe fn remove ( self , rwtxn : & mut RwTxn ) -> Result < ( ) > {
2718+ assert_eq_env_db_txn ! ( self , rwtxn) ;
2719+
2720+ unsafe {
2721+ mdb_result ( ffi:: mdb_drop ( rwtxn. txn . txn_ptr ( ) . as_mut ( ) , self . dbi , 1 ) ) . map_err ( Into :: into)
2722+ }
2723+ }
2724+
26492725 /// Change the codec types of this database, specifying the codecs.
26502726 ///
26512727 /// # Safety
0 commit comments