Open or create a pristine database at the given path This will create all necessary tables if they don't exist.
(path: P)
| 263 | /// |
| 264 | /// This will create all necessary tables if they don't exist. |
| 265 | pub fn open<P: AsRef<Path>>(path: P) -> PristineResult<Self> { |
| 266 | let path = path.as_ref(); |
| 267 | let is_new_database = !path.exists(); |
| 268 | // Use 8 GB cache for machines with plenty of RAM. The default |
| 269 | // redb cache is 1 GB which causes excessive page eviction when |
| 270 | // the GRAPH table grows beyond that during large imports. |
| 271 | let cache_bytes = 8 * 1024 * 1024 * 1024; // 8 GiB |
| 272 | let (db, backend) = open_database_with_backend(path, true, cache_bytes)?; |
| 273 | check_schema_version(&db.begin_read()?)?; |
| 274 | |
| 275 | // Existing repositories must be checked before the additive table-init |
| 276 | // transaction starts. Binaries predating this generic fence cannot honor |
| 277 | // an additive marker; fence-aware binaries always fail closed here. |
| 278 | if !is_new_database { |
| 279 | let capabilities_txn = db.begin_read()?; |
| 280 | require_supported_repository_capabilities(&capabilities_txn)?; |
| 281 | } |
| 282 | |
| 283 | // Initialize all tables. Re-check in the write transaction so a |
| 284 | // concurrently raised requirement cannot race additive initialization. |
| 285 | let write_txn = db.begin_write()?; |
| 286 | let previous_table_count = if is_new_database { |
| 287 | 0 |
| 288 | } else { |
| 289 | write_txn.list_tables()?.count() + write_txn.list_multimap_tables()?.count() |
| 290 | }; |
| 291 | { |
| 292 | let metadata = write_txn.open_table(PRISTINE_META)?; |
| 293 | let mut requirements = Vec::new(); |
| 294 | for row in metadata.iter()? { |
| 295 | let (key, version) = row?; |
| 296 | if let Some(requirement) = |
| 297 | RequiredRepositoryCapability::from_metadata(key.value(), version.value()) |
| 298 | { |
| 299 | requirements.push(requirement); |
| 300 | } |
| 301 | } |
| 302 | ensure_supported_repository_capabilities(&requirements)?; |
| 303 | drop(metadata); |
| 304 | |
| 305 | // ID mapping tables |
| 306 | write_txn.open_table(EXTERNAL)?; |
| 307 | write_txn.open_table(INTERNAL)?; |
| 308 | write_txn.open_table(NODE_TYPES)?; |
| 309 | |
| 310 | // Graph tables |
| 311 | write_txn.open_multimap_table(GRAPH)?; |
| 312 | write_txn.open_multimap_table(INODE_GRAPH)?; |
| 313 | write_txn.open_multimap_table(POSITION_ATTRS)?; |
| 314 | write_txn.open_multimap_table(INODE_ATTRS)?; |
| 315 | |
| 316 | // View tables |
| 317 | write_txn.open_table(VIEWS)?; |
| 318 | write_txn.open_table(WORKING_COPIES)?; |
| 319 | write_txn.open_table(OPERATIONS)?; |
| 320 | write_txn.open_table(OP_HEADS)?; |
| 321 | write_txn.open_table(EFFECT_RECEIPTS)?; |
| 322 | write_txn.open_table(VIEW_CHANGES)?; |
no test coverage detected