Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21from __future__ import annotations 

22 

23"""Generic file-based datastore code.""" 

24 

25__all__ = ("FileDatastore", ) 

26 

27import hashlib 

28import logging 

29import os 

30import tempfile 

31 

32from sqlalchemy import BigInteger, String 

33 

34from dataclasses import dataclass 

35from typing import ( 

36 TYPE_CHECKING, 

37 Any, 

38 ClassVar, 

39 Dict, 

40 Iterable, 

41 List, 

42 Mapping, 

43 Optional, 

44 Set, 

45 Tuple, 

46 Type, 

47 Union, 

48) 

49 

50from lsst.daf.butler import ( 

51 ButlerURI, 

52 CompositesMap, 

53 Config, 

54 FileDataset, 

55 DatasetId, 

56 DatasetRef, 

57 DatasetType, 

58 DatasetTypeNotSupportedError, 

59 Datastore, 

60 DatastoreCacheManager, 

61 DatastoreDisabledCacheManager, 

62 DatastoreConfig, 

63 DatastoreValidationError, 

64 FileDescriptor, 

65 FileTemplates, 

66 FileTemplateValidationError, 

67 Formatter, 

68 FormatterFactory, 

69 Location, 

70 LocationFactory, 

71 Progress, 

72 StorageClass, 

73 StoredFileInfo, 

74) 

75 

76from lsst.daf.butler import ddl 

77from lsst.daf.butler.registry.interfaces import ( 

78 ReadOnlyDatabaseError, 

79 DatastoreRegistryBridge, 

80) 

81 

82from lsst.daf.butler.core.repoRelocation import replaceRoot 

83from lsst.daf.butler.core.utils import getInstanceOf, getClassOf, transactional 

84from .genericDatastore import GenericBaseDatastore 

85 

86if TYPE_CHECKING: 86 ↛ 87line 86 didn't jump to line 87, because the condition on line 86 was never true

87 from lsst.daf.butler import LookupKey, AbstractDatastoreCacheManager 

88 from lsst.daf.butler.registry.interfaces import DatasetIdRef, DatastoreRegistryBridgeManager 

89 

90log = logging.getLogger(__name__) 

91 

92# String to use when a Python None is encountered 

93NULLSTR = "__NULL_STRING__" 

94 

95 

96class _IngestPrepData(Datastore.IngestPrepData): 

97 """Helper class for FileDatastore ingest implementation. 

98 

99 Parameters 

100 ---------- 

101 datasets : `list` of `FileDataset` 

102 Files to be ingested by this datastore. 

103 """ 

104 def __init__(self, datasets: List[FileDataset]): 

105 super().__init__(ref for dataset in datasets for ref in dataset.refs) 

106 self.datasets = datasets 

107 

108 

109@dataclass(frozen=True) 

110class DatastoreFileGetInformation: 

111 """Collection of useful parameters needed to retrieve a file from 

112 a Datastore. 

113 """ 

114 

115 location: Location 

116 """The location from which to read the dataset.""" 

117 

118 formatter: Formatter 

119 """The `Formatter` to use to deserialize the dataset.""" 

120 

121 info: StoredFileInfo 

122 """Stored information about this file and its formatter.""" 

123 

124 assemblerParams: Dict[str, Any] 

125 """Parameters to use for post-processing the retrieved dataset.""" 

126 

127 formatterParams: Dict[str, Any] 

128 """Parameters that were understood by the associated formatter.""" 

129 

130 component: Optional[str] 

131 """The component to be retrieved (can be `None`).""" 

132 

133 readStorageClass: StorageClass 

134 """The `StorageClass` of the dataset being read.""" 

135 

136 

137class FileDatastore(GenericBaseDatastore): 

138 """Generic Datastore for file-based implementations. 

139 

140 Should always be sub-classed since key abstract methods are missing. 

141 

142 Parameters 

143 ---------- 

144 config : `DatastoreConfig` or `str` 

145 Configuration as either a `Config` object or URI to file. 

146 bridgeManager : `DatastoreRegistryBridgeManager` 

147 Object that manages the interface between `Registry` and datastores. 

148 butlerRoot : `str`, optional 

149 New datastore root to use to override the configuration value. 

150 

151 Raises 

152 ------ 

153 ValueError 

154 If root location does not exist and ``create`` is `False` in the 

155 configuration. 

156 """ 

157 

158 defaultConfigFile: ClassVar[Optional[str]] = None 

159 """Path to configuration defaults. Accessed within the ``config`` resource 

160 or relative to a search path. Can be None if no defaults specified. 

161 """ 

162 

163 root: ButlerURI 

164 """Root directory URI of this `Datastore`.""" 

165 

166 locationFactory: LocationFactory 

167 """Factory for creating locations relative to the datastore root.""" 

168 

169 formatterFactory: FormatterFactory 

170 """Factory for creating instances of formatters.""" 

171 

172 templates: FileTemplates 

173 """File templates that can be used by this `Datastore`.""" 

174 

175 composites: CompositesMap 

176 """Determines whether a dataset should be disassembled on put.""" 

177 

178 defaultConfigFile = "datastores/fileDatastore.yaml" 

179 """Path to configuration defaults. Accessed within the ``config`` resource 

180 or relative to a search path. Can be None if no defaults specified. 

181 """ 

182 

183 @classmethod 

184 def setConfigRoot(cls, root: str, config: Config, full: Config, overwrite: bool = True) -> None: 

185 """Set any filesystem-dependent config options for this Datastore to 

186 be appropriate for a new empty repository with the given root. 

187 

188 Parameters 

189 ---------- 

190 root : `str` 

191 URI to the root of the data repository. 

192 config : `Config` 

193 A `Config` to update. Only the subset understood by 

194 this component will be updated. Will not expand 

195 defaults. 

196 full : `Config` 

197 A complete config with all defaults expanded that can be 

198 converted to a `DatastoreConfig`. Read-only and will not be 

199 modified by this method. 

200 Repository-specific options that should not be obtained 

201 from defaults when Butler instances are constructed 

202 should be copied from ``full`` to ``config``. 

203 overwrite : `bool`, optional 

204 If `False`, do not modify a value in ``config`` if the value 

205 already exists. Default is always to overwrite with the provided 

206 ``root``. 

207 

208 Notes 

209 ----- 

210 If a keyword is explicitly defined in the supplied ``config`` it 

211 will not be overridden by this method if ``overwrite`` is `False`. 

212 This allows explicit values set in external configs to be retained. 

213 """ 

214 Config.updateParameters(DatastoreConfig, config, full, 

215 toUpdate={"root": root}, 

216 toCopy=("cls", ("records", "table")), overwrite=overwrite) 

217 

218 @classmethod 

219 def makeTableSpec(cls, datasetIdColumnType: type) -> ddl.TableSpec: 

220 return ddl.TableSpec( 

221 fields=[ 

222 ddl.FieldSpec(name="dataset_id", dtype=datasetIdColumnType, primaryKey=True), 

223 ddl.FieldSpec(name="path", dtype=String, length=256, nullable=False), 

224 ddl.FieldSpec(name="formatter", dtype=String, length=128, nullable=False), 

225 ddl.FieldSpec(name="storage_class", dtype=String, length=64, nullable=False), 

226 # Use empty string to indicate no component 

227 ddl.FieldSpec(name="component", dtype=String, length=32, primaryKey=True), 

228 # TODO: should checksum be Base64Bytes instead? 

229 ddl.FieldSpec(name="checksum", dtype=String, length=128, nullable=True), 

230 ddl.FieldSpec(name="file_size", dtype=BigInteger, nullable=True), 

231 ], 

232 unique=frozenset(), 

233 ) 

234 

235 def __init__(self, config: Union[DatastoreConfig, str], 

236 bridgeManager: DatastoreRegistryBridgeManager, butlerRoot: str = None): 

237 super().__init__(config, bridgeManager) 

238 if "root" not in self.config: 238 ↛ 239line 238 didn't jump to line 239, because the condition on line 238 was never true

239 raise ValueError("No root directory specified in configuration") 

240 

241 # Name ourselves either using an explicit name or a name 

242 # derived from the (unexpanded) root 

243 if "name" in self.config: 

244 self.name = self.config["name"] 

245 else: 

246 # We use the unexpanded root in the name to indicate that this 

247 # datastore can be moved without having to update registry. 

248 self.name = "{}@{}".format(type(self).__name__, 

249 self.config["root"]) 

250 

251 # Support repository relocation in config 

252 # Existence of self.root is checked in subclass 

253 self.root = ButlerURI(replaceRoot(self.config["root"], butlerRoot), 

254 forceDirectory=True, forceAbsolute=True) 

255 

256 self.locationFactory = LocationFactory(self.root) 

257 self.formatterFactory = FormatterFactory() 

258 

259 # Now associate formatters with storage classes 

260 self.formatterFactory.registerFormatters(self.config["formatters"], 

261 universe=bridgeManager.universe) 

262 

263 # Read the file naming templates 

264 self.templates = FileTemplates(self.config["templates"], 

265 universe=bridgeManager.universe) 

266 

267 # See if composites should be disassembled 

268 self.composites = CompositesMap(self.config["composites"], 

269 universe=bridgeManager.universe) 

270 

271 tableName = self.config["records", "table"] 

272 try: 

273 # Storage of paths and formatters, keyed by dataset_id 

274 self._table = bridgeManager.opaque.register( 

275 tableName, self.makeTableSpec(bridgeManager.datasetIdColumnType)) 

276 # Interface to Registry. 

277 self._bridge = bridgeManager.register(self.name) 

278 except ReadOnlyDatabaseError: 

279 # If the database is read only and we just tried and failed to 

280 # create a table, it means someone is trying to create a read-only 

281 # butler client for an empty repo. That should be okay, as long 

282 # as they then try to get any datasets before some other client 

283 # creates the table. Chances are they'rejust validating 

284 # configuration. 

285 pass 

286 

287 # Determine whether checksums should be used - default to False 

288 self.useChecksum = self.config.get("checksum", False) 

289 

290 # Determine whether we can fall back to configuration if a 

291 # requested dataset is not known to registry 

292 self.trustGetRequest = self.config.get("trust_get_request", False) 

293 

294 # Create a cache manager 

295 self.cacheManager: AbstractDatastoreCacheManager 

296 if "cached" in self.config: 296 ↛ 300line 296 didn't jump to line 300, because the condition on line 296 was never false

297 self.cacheManager = DatastoreCacheManager(self.config["cached"], 

298 universe=bridgeManager.universe) 

299 else: 

300 self.cacheManager = DatastoreDisabledCacheManager("", 

301 universe=bridgeManager.universe) 

302 

303 # Check existence and create directory structure if necessary 

304 if not self.root.exists(): 

305 if "create" not in self.config or not self.config["create"]: 305 ↛ 306line 305 didn't jump to line 306, because the condition on line 305 was never true

306 raise ValueError(f"No valid root and not allowed to create one at: {self.root}") 

307 try: 

308 self.root.mkdir() 

309 except Exception as e: 

310 raise ValueError(f"Can not create datastore root '{self.root}', check permissions." 

311 f" Got error: {e}") from e 

312 

313 def __str__(self) -> str: 

314 return str(self.root) 

315 

316 @property 

317 def bridge(self) -> DatastoreRegistryBridge: 

318 return self._bridge 

319 

320 def _artifact_exists(self, location: Location) -> bool: 

321 """Check that an artifact exists in this datastore at the specified 

322 location. 

323 

324 Parameters 

325 ---------- 

326 location : `Location` 

327 Expected location of the artifact associated with this datastore. 

328 

329 Returns 

330 ------- 

331 exists : `bool` 

332 True if the location can be found, false otherwise. 

333 """ 

334 log.debug("Checking if resource exists: %s", location.uri) 

335 return location.uri.exists() 

336 

337 def _delete_artifact(self, location: Location) -> None: 

338 """Delete the artifact from the datastore. 

339 

340 Parameters 

341 ---------- 

342 location : `Location` 

343 Location of the artifact associated with this datastore. 

344 """ 

345 if location.pathInStore.isabs(): 345 ↛ 346line 345 didn't jump to line 346, because the condition on line 345 was never true

346 raise RuntimeError(f"Cannot delete artifact with absolute uri {location.uri}.") 

347 log.debug("Deleting file: %s", location.uri) 

348 location.uri.remove() 

349 log.debug("Successfully deleted file: %s", location.uri) 

350 

351 def addStoredItemInfo(self, refs: Iterable[DatasetRef], infos: Iterable[StoredFileInfo]) -> None: 

352 # Docstring inherited from GenericBaseDatastore 

353 records = [] 

354 for ref, info in zip(refs, infos): 

355 # Component should come from ref and fall back on info 

356 component = ref.datasetType.component() 

357 if component is None and info.component is not None: 357 ↛ 358line 357 didn't jump to line 358, because the condition on line 357 was never true

358 component = info.component 

359 if component is None: 

360 # Use empty string since we want this to be part of the 

361 # primary key. 

362 component = NULLSTR 

363 records.append( 

364 dict(dataset_id=ref.id, formatter=info.formatter, path=info.path, 

365 storage_class=info.storageClass.name, component=component, 

366 checksum=info.checksum, file_size=info.file_size) 

367 ) 

368 self._table.insert(*records) 

369 

370 def getStoredItemsInfo(self, ref: DatasetIdRef) -> List[StoredFileInfo]: 

371 # Docstring inherited from GenericBaseDatastore 

372 

373 # Look for the dataset_id -- there might be multiple matches 

374 # if we have disassembled the dataset. 

375 records = list(self._table.fetch(dataset_id=ref.id)) 

376 

377 results = [] 

378 for record in records: 

379 # Convert name of StorageClass to instance 

380 storageClass = self.storageClassFactory.getStorageClass(record["storage_class"]) 

381 component = record["component"] if (record["component"] 

382 and record["component"] != NULLSTR) else None 

383 

384 info = StoredFileInfo(formatter=record["formatter"], 

385 path=record["path"], 

386 storageClass=storageClass, 

387 component=component, 

388 checksum=record["checksum"], 

389 file_size=record["file_size"]) 

390 results.append(info) 

391 

392 return results 

393 

394 def _registered_refs_per_artifact(self, pathInStore: ButlerURI) -> Set[DatasetId]: 

395 """Return all dataset refs associated with the supplied path. 

396 

397 Parameters 

398 ---------- 

399 pathInStore : `ButlerURI` 

400 Path of interest in the data store. 

401 

402 Returns 

403 ------- 

404 ids : `set` of `int` 

405 All `DatasetRef` IDs associated with this path. 

406 """ 

407 records = list(self._table.fetch(path=str(pathInStore))) 

408 ids = {r["dataset_id"] for r in records} 

409 return ids 

410 

411 def removeStoredItemInfo(self, ref: DatasetIdRef) -> None: 

412 # Docstring inherited from GenericBaseDatastore 

413 self._table.delete(["dataset_id"], {"dataset_id": ref.id}) 

414 

415 def _get_dataset_locations_info(self, ref: DatasetIdRef) -> List[Tuple[Location, StoredFileInfo]]: 

416 r"""Find all the `Location`\ s of the requested dataset in the 

417 `Datastore` and the associated stored file information. 

418 

419 Parameters 

420 ---------- 

421 ref : `DatasetRef` 

422 Reference to the required `Dataset`. 

423 

424 Returns 

425 ------- 

426 results : `list` [`tuple` [`Location`, `StoredFileInfo` ]] 

427 Location of the dataset within the datastore and 

428 stored information about each file and its formatter. 

429 """ 

430 # Get the file information (this will fail if no file) 

431 records = self.getStoredItemsInfo(ref) 

432 

433 # Use the path to determine the location -- we need to take 

434 # into account absolute URIs in the datastore record 

435 locations: List[Tuple[Location, StoredFileInfo]] = [] 

436 for r in records: 

437 uriInStore = ButlerURI(r.path, forceAbsolute=False) 

438 if uriInStore.isabs(): 438 ↛ 439line 438 didn't jump to line 439, because the condition on line 438 was never true

439 location = Location(None, uriInStore) 

440 else: 

441 location = self.locationFactory.fromPath(r.path) 

442 locations.append((location, r)) 

443 return locations 

444 

445 def _can_remove_dataset_artifact(self, ref: DatasetIdRef, location: Location) -> bool: 

446 """Check that there is only one dataset associated with the 

447 specified artifact. 

448 

449 Parameters 

450 ---------- 

451 ref : `DatasetRef` or `FakeDatasetRef` 

452 Dataset to be removed. 

453 location : `Location` 

454 The location of the artifact to be removed. 

455 

456 Returns 

457 ------- 

458 can_remove : `Bool` 

459 True if the artifact can be safely removed. 

460 """ 

461 # Can't ever delete absolute URIs. 

462 if location.pathInStore.isabs(): 462 ↛ 463line 462 didn't jump to line 463, because the condition on line 462 was never true

463 return False 

464 

465 # Get all entries associated with this path 

466 allRefs = self._registered_refs_per_artifact(location.pathInStore) 

467 if not allRefs: 467 ↛ 468line 467 didn't jump to line 468, because the condition on line 467 was never true

468 raise RuntimeError(f"Datastore inconsistency error. {location.pathInStore} not in registry") 

469 

470 # Remove these refs from all the refs and if there is nothing left 

471 # then we can delete 

472 remainingRefs = allRefs - {ref.id} 

473 

474 if remainingRefs: 

475 return False 

476 return True 

477 

478 def _get_expected_dataset_locations_info(self, ref: DatasetRef) -> List[Tuple[Location, 

479 StoredFileInfo]]: 

480 """Predict the location and related file information of the requested 

481 dataset in this datastore. 

482 

483 Parameters 

484 ---------- 

485 ref : `DatasetRef` 

486 Reference to the required `Dataset`. 

487 

488 Returns 

489 ------- 

490 results : `list` [`tuple` [`Location`, `StoredFileInfo` ]] 

491 Expected Location of the dataset within the datastore and 

492 placeholder information about each file and its formatter. 

493 

494 Notes 

495 ----- 

496 Uses the current configuration to determine how we would expect the 

497 datastore files to have been written if we couldn't ask registry. 

498 This is safe so long as there has been no change to datastore 

499 configuration between writing the dataset and wanting to read it. 

500 Will not work for files that have been ingested without using the 

501 standard file template or default formatter. 

502 """ 

503 

504 # If we have a component ref we always need to ask the questions 

505 # of the composite. If the composite is disassembled this routine 

506 # should return all components. If the composite was not 

507 # disassembled the composite is what is stored regardless of 

508 # component request. Note that if the caller has disassembled 

509 # a composite there is no way for this guess to know that 

510 # without trying both the composite and component ref and seeing 

511 # if there is something at the component Location even without 

512 # disassembly being enabled. 

513 if ref.datasetType.isComponent(): 

514 ref = ref.makeCompositeRef() 

515 

516 # See if the ref is a composite that should be disassembled 

517 doDisassembly = self.composites.shouldBeDisassembled(ref) 

518 

519 all_info: List[Tuple[Location, Formatter, StorageClass, Optional[str]]] = [] 

520 

521 if doDisassembly: 

522 for component, componentStorage in ref.datasetType.storageClass.components.items(): 

523 compRef = ref.makeComponentRef(component) 

524 location, formatter = self._determine_put_formatter_location(compRef) 

525 all_info.append((location, formatter, componentStorage, component)) 

526 

527 else: 

528 # Always use the composite ref if no disassembly 

529 location, formatter = self._determine_put_formatter_location(ref) 

530 all_info.append((location, formatter, ref.datasetType.storageClass, None)) 

531 

532 # Convert the list of tuples to have StoredFileInfo as second element 

533 return [(location, StoredFileInfo(formatter=formatter, 

534 path=location.pathInStore.path, 

535 storageClass=storageClass, 

536 component=component, 

537 checksum=None, 

538 file_size=-1)) 

539 for location, formatter, storageClass, component in all_info] 

540 

541 def _prepare_for_get(self, ref: DatasetRef, 

542 parameters: Optional[Mapping[str, Any]] = None) -> List[DatastoreFileGetInformation]: 

543 """Check parameters for ``get`` and obtain formatter and 

544 location. 

545 

546 Parameters 

547 ---------- 

548 ref : `DatasetRef` 

549 Reference to the required Dataset. 

550 parameters : `dict` 

551 `StorageClass`-specific parameters that specify, for example, 

552 a slice of the dataset to be loaded. 

553 

554 Returns 

555 ------- 

556 getInfo : `list` [`DatastoreFileGetInformation`] 

557 Parameters needed to retrieve each file. 

558 """ 

559 log.debug("Retrieve %s from %s with parameters %s", ref, self.name, parameters) 

560 

561 # Get file metadata and internal metadata 

562 fileLocations = self._get_dataset_locations_info(ref) 

563 if not fileLocations: 

564 if not self.trustGetRequest: 

565 raise FileNotFoundError(f"Could not retrieve dataset {ref}.") 

566 # Assume the dataset is where we think it should be 

567 fileLocations = self._get_expected_dataset_locations_info(ref) 

568 

569 # The storage class we want to use eventually 

570 refStorageClass = ref.datasetType.storageClass 

571 

572 if len(fileLocations) > 1: 

573 disassembled = True 

574 else: 

575 disassembled = False 

576 

577 # Is this a component request? 

578 refComponent = ref.datasetType.component() 

579 

580 fileGetInfo = [] 

581 for location, storedFileInfo in fileLocations: 

582 

583 # The storage class used to write the file 

584 writeStorageClass = storedFileInfo.storageClass 

585 

586 # If this has been disassembled we need read to match the write 

587 if disassembled: 

588 readStorageClass = writeStorageClass 

589 else: 

590 readStorageClass = refStorageClass 

591 

592 formatter = getInstanceOf(storedFileInfo.formatter, 

593 FileDescriptor(location, readStorageClass=readStorageClass, 

594 storageClass=writeStorageClass, parameters=parameters), 

595 ref.dataId) 

596 

597 formatterParams, notFormatterParams = formatter.segregateParameters() 

598 

599 # Of the remaining parameters, extract the ones supported by 

600 # this StorageClass (for components not all will be handled) 

601 assemblerParams = readStorageClass.filterParameters(notFormatterParams) 

602 

603 # The ref itself could be a component if the dataset was 

604 # disassembled by butler, or we disassembled in datastore and 

605 # components came from the datastore records 

606 component = storedFileInfo.component if storedFileInfo.component else refComponent 

607 

608 fileGetInfo.append(DatastoreFileGetInformation(location, formatter, storedFileInfo, 

609 assemblerParams, formatterParams, 

610 component, readStorageClass)) 

611 

612 return fileGetInfo 

613 

614 def _prepare_for_put(self, inMemoryDataset: Any, ref: DatasetRef) -> Tuple[Location, Formatter]: 

615 """Check the arguments for ``put`` and obtain formatter and 

616 location. 

617 

618 Parameters 

619 ---------- 

620 inMemoryDataset : `object` 

621 The dataset to store. 

622 ref : `DatasetRef` 

623 Reference to the associated Dataset. 

624 

625 Returns 

626 ------- 

627 location : `Location` 

628 The location to write the dataset. 

629 formatter : `Formatter` 

630 The `Formatter` to use to write the dataset. 

631 

632 Raises 

633 ------ 

634 TypeError 

635 Supplied object and storage class are inconsistent. 

636 DatasetTypeNotSupportedError 

637 The associated `DatasetType` is not handled by this datastore. 

638 """ 

639 self._validate_put_parameters(inMemoryDataset, ref) 

640 return self._determine_put_formatter_location(ref) 

641 

642 def _determine_put_formatter_location(self, ref: DatasetRef) -> Tuple[Location, Formatter]: 

643 """Calculate the formatter and output location to use for put. 

644 

645 Parameters 

646 ---------- 

647 ref : `DatasetRef` 

648 Reference to the associated Dataset. 

649 

650 Returns 

651 ------- 

652 location : `Location` 

653 The location to write the dataset. 

654 formatter : `Formatter` 

655 The `Formatter` to use to write the dataset. 

656 """ 

657 # Work out output file name 

658 try: 

659 template = self.templates.getTemplate(ref) 

660 except KeyError as e: 

661 raise DatasetTypeNotSupportedError(f"Unable to find template for {ref}") from e 

662 

663 # Validate the template to protect against filenames from different 

664 # dataIds returning the same and causing overwrite confusion. 

665 template.validateTemplate(ref) 

666 

667 location = self.locationFactory.fromPath(template.format(ref)) 

668 

669 # Get the formatter based on the storage class 

670 storageClass = ref.datasetType.storageClass 

671 try: 

672 formatter = self.formatterFactory.getFormatter(ref, 

673 FileDescriptor(location, 

674 storageClass=storageClass), 

675 ref.dataId) 

676 except KeyError as e: 

677 raise DatasetTypeNotSupportedError(f"Unable to find formatter for {ref} in datastore " 

678 f"{self.name}") from e 

679 

680 # Now that we know the formatter, update the location 

681 location = formatter.makeUpdatedLocation(location) 

682 

683 return location, formatter 

684 

685 def _overrideTransferMode(self, *datasets: FileDataset, transfer: Optional[str] = None) -> Optional[str]: 

686 # Docstring inherited from base class 

687 if transfer != "auto": 

688 return transfer 

689 

690 # See if the paths are within the datastore or not 

691 inside = [self._pathInStore(d.path) is not None for d in datasets] 

692 

693 if all(inside): 

694 transfer = None 

695 elif not any(inside): 695 ↛ 699line 695 didn't jump to line 699, because the condition on line 695 was never false

696 # Allow ButlerURI to use its own knowledge 

697 transfer = "auto" 

698 else: 

699 raise ValueError("Some datasets are inside the datastore and some are outside." 

700 " Please use an explicit transfer mode and not 'auto'.") 

701 

702 return transfer 

703 

704 def _pathInStore(self, path: Union[str, ButlerURI]) -> Optional[str]: 

705 """Return path relative to datastore root 

706 

707 Parameters 

708 ---------- 

709 path : `str` or `ButlerURI` 

710 Path to dataset. Can be absolute URI. If relative assumed to 

711 be relative to the datastore. Returns path in datastore 

712 or raises an exception if the path it outside. 

713 

714 Returns 

715 ------- 

716 inStore : `str` 

717 Path relative to datastore root. Returns `None` if the file is 

718 outside the root. 

719 """ 

720 # Relative path will always be relative to datastore 

721 pathUri = ButlerURI(path, forceAbsolute=False) 

722 return pathUri.relative_to(self.root) 

723 

724 def _standardizeIngestPath(self, path: Union[str, ButlerURI], *, 

725 transfer: Optional[str] = None) -> Union[str, ButlerURI]: 

726 """Standardize the path of a to-be-ingested file. 

727 

728 Parameters 

729 ---------- 

730 path : `str` or `ButlerURI` 

731 Path of a file to be ingested. 

732 transfer : `str`, optional 

733 How (and whether) the dataset should be added to the datastore. 

734 See `ingest` for details of transfer modes. 

735 This implementation is provided only so 

736 `NotImplementedError` can be raised if the mode is not supported; 

737 actual transfers are deferred to `_extractIngestInfo`. 

738 

739 Returns 

740 ------- 

741 path : `str` or `ButlerURI` 

742 New path in what the datastore considers standard form. If an 

743 absolute URI was given that will be returned unchanged. 

744 

745 Notes 

746 ----- 

747 Subclasses of `FileDatastore` can implement this method instead 

748 of `_prepIngest`. It should not modify the data repository or given 

749 file in any way. 

750 

751 Raises 

752 ------ 

753 NotImplementedError 

754 Raised if the datastore does not support the given transfer mode 

755 (including the case where ingest is not supported at all). 

756 FileNotFoundError 

757 Raised if one of the given files does not exist. 

758 """ 

759 if transfer not in (None, "direct") + self.root.transferModes: 759 ↛ 760line 759 didn't jump to line 760, because the condition on line 759 was never true

760 raise NotImplementedError(f"Transfer mode {transfer} not supported.") 

761 

762 # A relative URI indicates relative to datastore root 

763 srcUri = ButlerURI(path, forceAbsolute=False) 

764 if not srcUri.isabs(): 

765 srcUri = self.root.join(path) 

766 

767 if not srcUri.exists(): 

768 raise FileNotFoundError(f"Resource at {srcUri} does not exist; note that paths to ingest " 

769 f"are assumed to be relative to {self.root} unless they are absolute.") 

770 

771 if transfer is None: 

772 relpath = srcUri.relative_to(self.root) 

773 if not relpath: 

774 raise RuntimeError(f"Transfer is none but source file ({srcUri}) is not " 

775 f"within datastore ({self.root})") 

776 

777 # Return the relative path within the datastore for internal 

778 # transfer 

779 path = relpath 

780 

781 return path 

782 

783 def _extractIngestInfo(self, path: Union[str, ButlerURI], ref: DatasetRef, *, 

784 formatter: Union[Formatter, Type[Formatter]], 

785 transfer: Optional[str] = None) -> StoredFileInfo: 

786 """Relocate (if necessary) and extract `StoredFileInfo` from a 

787 to-be-ingested file. 

788 

789 Parameters 

790 ---------- 

791 path : `str` or `ButlerURI` 

792 URI or path of a file to be ingested. 

793 ref : `DatasetRef` 

794 Reference for the dataset being ingested. Guaranteed to have 

795 ``dataset_id not None`. 

796 formatter : `type` or `Formatter` 

797 `Formatter` subclass to use for this dataset or an instance. 

798 transfer : `str`, optional 

799 How (and whether) the dataset should be added to the datastore. 

800 See `ingest` for details of transfer modes. 

801 

802 Returns 

803 ------- 

804 info : `StoredFileInfo` 

805 Internal datastore record for this file. This will be inserted by 

806 the caller; the `_extractIngestInfo` is only resposible for 

807 creating and populating the struct. 

808 

809 Raises 

810 ------ 

811 FileNotFoundError 

812 Raised if one of the given files does not exist. 

813 FileExistsError 

814 Raised if transfer is not `None` but the (internal) location the 

815 file would be moved to is already occupied. 

816 """ 

817 if self._transaction is None: 817 ↛ 818line 817 didn't jump to line 818, because the condition on line 817 was never true

818 raise RuntimeError("Ingest called without transaction enabled") 

819 

820 # Create URI of the source path, do not need to force a relative 

821 # path to absolute. 

822 srcUri = ButlerURI(path, forceAbsolute=False) 

823 

824 # Track whether we have read the size of the source yet 

825 have_sized = False 

826 

827 tgtLocation: Optional[Location] 

828 if transfer is None: 

829 # A relative path is assumed to be relative to the datastore 

830 # in this context 

831 if not srcUri.isabs(): 

832 tgtLocation = self.locationFactory.fromPath(srcUri.ospath) 

833 else: 

834 # Work out the path in the datastore from an absolute URI 

835 # This is required to be within the datastore. 

836 pathInStore = srcUri.relative_to(self.root) 

837 if pathInStore is None: 837 ↛ 838line 837 didn't jump to line 838, because the condition on line 837 was never true

838 raise RuntimeError(f"Unexpectedly learned that {srcUri} is " 

839 f"not within datastore {self.root}") 

840 tgtLocation = self.locationFactory.fromPath(pathInStore) 

841 elif transfer == "direct": 841 ↛ 846line 841 didn't jump to line 846, because the condition on line 841 was never true

842 # Want to store the full URI to the resource directly in 

843 # datastore. This is useful for referring to permanent archive 

844 # storage for raw data. 

845 # Trust that people know what they are doing. 

846 tgtLocation = None 

847 else: 

848 # Work out the name we want this ingested file to have 

849 # inside the datastore 

850 tgtLocation = self._calculate_ingested_datastore_name(srcUri, ref, formatter) 

851 if not tgtLocation.uri.dirname().exists(): 

852 log.debug("Folder %s does not exist yet.", tgtLocation.uri.dirname()) 

853 tgtLocation.uri.dirname().mkdir() 

854 

855 # if we are transferring from a local file to a remote location 

856 # it may be more efficient to get the size and checksum of the 

857 # local file rather than the transferred one 

858 if not srcUri.scheme or srcUri.scheme == "file": 858 ↛ 864line 858 didn't jump to line 864, because the condition on line 858 was never false

859 size = srcUri.size() 

860 checksum = self.computeChecksum(srcUri) if self.useChecksum else None 

861 have_sized = True 

862 

863 # transfer the resource to the destination 

864 tgtLocation.uri.transfer_from(srcUri, transfer=transfer, transaction=self._transaction) 

865 

866 if tgtLocation is None: 866 ↛ 868line 866 didn't jump to line 868, because the condition on line 866 was never true

867 # This means we are using direct mode 

868 targetUri = srcUri 

869 targetPath = str(srcUri) 

870 else: 

871 targetUri = tgtLocation.uri 

872 targetPath = tgtLocation.pathInStore.path 

873 

874 # the file should exist in the datastore now 

875 if not have_sized: 

876 size = targetUri.size() 

877 checksum = self.computeChecksum(targetUri) if self.useChecksum else None 

878 

879 return StoredFileInfo(formatter=formatter, path=targetPath, 

880 storageClass=ref.datasetType.storageClass, 

881 component=ref.datasetType.component(), 

882 file_size=size, checksum=checksum) 

883 

884 def _prepIngest(self, *datasets: FileDataset, transfer: Optional[str] = None) -> _IngestPrepData: 

885 # Docstring inherited from Datastore._prepIngest. 

886 filtered = [] 

887 for dataset in datasets: 

888 acceptable = [ref for ref in dataset.refs if self.constraints.isAcceptable(ref)] 

889 if not acceptable: 

890 continue 

891 else: 

892 dataset.refs = acceptable 

893 if dataset.formatter is None: 

894 dataset.formatter = self.formatterFactory.getFormatterClass(dataset.refs[0]) 

895 else: 

896 assert isinstance(dataset.formatter, (type, str)) 

897 dataset.formatter = getClassOf(dataset.formatter) 

898 dataset.path = self._standardizeIngestPath(dataset.path, transfer=transfer) 

899 filtered.append(dataset) 

900 return _IngestPrepData(filtered) 

901 

902 @transactional 

903 def _finishIngest(self, prepData: Datastore.IngestPrepData, *, transfer: Optional[str] = None) -> None: 

904 # Docstring inherited from Datastore._finishIngest. 

905 refsAndInfos = [] 

906 progress = Progress("lsst.daf.butler.datastores.FileDatastore.ingest", level=logging.DEBUG) 

907 for dataset in progress.wrap(prepData.datasets, desc="Ingesting dataset files"): 

908 # Do ingest as if the first dataset ref is associated with the file 

909 info = self._extractIngestInfo(dataset.path, dataset.refs[0], formatter=dataset.formatter, 

910 transfer=transfer) 

911 refsAndInfos.extend([(ref, info) for ref in dataset.refs]) 

912 self._register_datasets(refsAndInfos) 

913 

914 def _calculate_ingested_datastore_name(self, srcUri: ButlerURI, ref: DatasetRef, 

915 formatter: Union[Formatter, Type[Formatter]]) -> Location: 

916 """Given a source URI and a DatasetRef, determine the name the 

917 dataset will have inside datastore. 

918 

919 Parameters 

920 ---------- 

921 srcUri : `ButlerURI` 

922 URI to the source dataset file. 

923 ref : `DatasetRef` 

924 Ref associated with the newly-ingested dataset artifact. This 

925 is used to determine the name within the datastore. 

926 formatter : `Formatter` or Formatter class. 

927 Formatter to use for validation. Can be a class or an instance. 

928 

929 Returns 

930 ------- 

931 location : `Location` 

932 Target location for the newly-ingested dataset. 

933 """ 

934 # Ingesting a file from outside the datastore. 

935 # This involves a new name. 

936 template = self.templates.getTemplate(ref) 

937 location = self.locationFactory.fromPath(template.format(ref)) 

938 

939 # Get the extension 

940 ext = srcUri.getExtension() 

941 

942 # Update the destination to include that extension 

943 location.updateExtension(ext) 

944 

945 # Ask the formatter to validate this extension 

946 formatter.validateExtension(location) 

947 

948 return location 

949 

950 def _write_in_memory_to_artifact(self, inMemoryDataset: Any, ref: DatasetRef) -> StoredFileInfo: 

951 """Write out in memory dataset to datastore. 

952 

953 Parameters 

954 ---------- 

955 inMemoryDataset : `object` 

956 Dataset to write to datastore. 

957 ref : `DatasetRef` 

958 Registry information associated with this dataset. 

959 

960 Returns 

961 ------- 

962 info : `StoredFileInfo` 

963 Information describin the artifact written to the datastore. 

964 """ 

965 location, formatter = self._prepare_for_put(inMemoryDataset, ref) 

966 uri = location.uri 

967 

968 if not uri.dirname().exists(): 

969 log.debug("Folder %s does not exist yet so creating it.", uri.dirname()) 

970 uri.dirname().mkdir() 

971 

972 if self._transaction is None: 972 ↛ 973line 972 didn't jump to line 973, because the condition on line 972 was never true

973 raise RuntimeError("Attempting to write artifact without transaction enabled") 

974 

975 def _removeFileExists(uri: ButlerURI) -> None: 

976 """Remove a file and do not complain if it is not there. 

977 

978 This is important since a formatter might fail before the file 

979 is written and we should not confuse people by writing spurious 

980 error messages to the log. 

981 """ 

982 try: 

983 uri.remove() 

984 except FileNotFoundError: 

985 pass 

986 

987 # Register a callback to try to delete the uploaded data if 

988 # something fails below 

989 self._transaction.registerUndo("artifactWrite", _removeFileExists, uri) 

990 

991 # For a local file, simply use the formatter directly 

992 if uri.isLocal: 

993 formatter.write(inMemoryDataset) 

994 log.debug("Successfully wrote python object to local file at %s", uri) 

995 else: 

996 # This is a remote URI, so first try bytes and write directly else 

997 # fallback to a temporary file 

998 try: 

999 serializedDataset = formatter.toBytes(inMemoryDataset) 

1000 log.debug("Writing bytes directly to %s", uri) 

1001 uri.write(serializedDataset, overwrite=True) 

1002 log.debug("Successfully wrote bytes directly to %s", uri) 

1003 except NotImplementedError: 

1004 with tempfile.NamedTemporaryFile(suffix=uri.getExtension()) as tmpFile: 

1005 # Need to configure the formatter to write to a different 

1006 # location and that needs us to overwrite internals 

1007 tmpLocation = Location(*os.path.split(tmpFile.name)) 

1008 log.debug("Writing dataset to temporary location at %s", tmpLocation.uri) 

1009 with formatter._updateLocation(tmpLocation): 

1010 formatter.write(inMemoryDataset) 

1011 uri.transfer_from(tmpLocation.uri, transfer="copy", overwrite=True) 

1012 

1013 # Cache if required 

1014 self.cacheManager.move_to_cache(tmpLocation.uri, ref) 

1015 

1016 log.debug("Successfully wrote dataset to %s via a temporary file.", uri) 

1017 

1018 # URI is needed to resolve what ingest case are we dealing with 

1019 return self._extractIngestInfo(uri, ref, formatter=formatter) 

1020 

1021 def _read_artifact_into_memory(self, getInfo: DatastoreFileGetInformation, 

1022 ref: DatasetRef, isComponent: bool = False) -> Any: 

1023 """Read the artifact from datastore into in memory object. 

1024 

1025 Parameters 

1026 ---------- 

1027 getInfo : `DatastoreFileGetInformation` 

1028 Information about the artifact within the datastore. 

1029 ref : `DatasetRef` 

1030 The registry information associated with this artifact. 

1031 isComponent : `bool` 

1032 Flag to indicate if a component is being read from this artifact. 

1033 

1034 Returns 

1035 ------- 

1036 inMemoryDataset : `object` 

1037 The artifact as a python object. 

1038 """ 

1039 location = getInfo.location 

1040 uri = location.uri 

1041 log.debug("Accessing data from %s", uri) 

1042 

1043 # Cannot recalculate checksum but can compare size as a quick check 

1044 # Do not do this if the size is negative since that indicates 

1045 # we do not know. 

1046 recorded_size = getInfo.info.file_size 

1047 resource_size = uri.size() 

1048 if recorded_size >= 0 and resource_size != recorded_size: 1048 ↛ 1049line 1048 didn't jump to line 1049, because the condition on line 1048 was never true

1049 raise RuntimeError("Integrity failure in Datastore. " 

1050 f"Size of file {uri} ({resource_size}) " 

1051 f"does not match size recorded in registry of {recorded_size}") 

1052 

1053 # For the general case we have choices for how to proceed. 

1054 # 1. Always use a local file (downloading the remote resource to a 

1055 # temporary file if needed). 

1056 # 2. Use a threshold size and read into memory and use bytes. 

1057 # Use both for now with an arbitrary hand off size. 

1058 # This allows small datasets to be downloaded from remote object 

1059 # stores without requiring a temporary file. 

1060 

1061 formatter = getInfo.formatter 

1062 nbytes_max = 10_000_000 # Arbitrary number that we can tune 

1063 if resource_size <= nbytes_max and formatter.can_read_bytes(): 

1064 serializedDataset = uri.read() 

1065 log.debug("Deserializing %s from %d bytes from location %s with formatter %s", 

1066 f"component {getInfo.component}" if isComponent else "", 

1067 len(serializedDataset), uri, formatter.name()) 

1068 try: 

1069 result = formatter.fromBytes(serializedDataset, 

1070 component=getInfo.component if isComponent else None) 

1071 except Exception as e: 

1072 raise ValueError(f"Failure from formatter '{formatter.name()}' for dataset {ref.id}" 

1073 f" ({ref.datasetType.name} from {uri}): {e}") from e 

1074 else: 

1075 # Read from file. 

1076 

1077 # Have to update the Location associated with the formatter 

1078 # because formatter.read does not allow an override. 

1079 # This could be improved. 

1080 location_updated = False 

1081 msg = "" 

1082 

1083 # First check in cache for local version. 

1084 # The cache will only be relevant for remote resources. 

1085 if not uri.isLocal: 

1086 cached_file = self.cacheManager.find_in_cache(ref, uri.getExtension()) 

1087 if cached_file is not None: 1087 ↛ 1088line 1087 didn't jump to line 1088, because the condition on line 1087 was never true

1088 msg = f"(via cache read of remote file {uri})" 

1089 uri = cached_file 

1090 location_updated = True 

1091 

1092 with uri.as_local() as local_uri: 

1093 

1094 # URI was remote and file was downloaded 

1095 if uri != local_uri: 

1096 cache_msg = "" 

1097 location_updated = True 

1098 

1099 # Cache the downloaded file if needed. 

1100 cached_uri = self.cacheManager.move_to_cache(local_uri, ref) 

1101 if cached_uri is not None: 1101 ↛ 1102line 1101 didn't jump to line 1102, because the condition on line 1101 was never true

1102 local_uri = cached_uri 

1103 cache_msg = " and cached" 

1104 

1105 msg = f"(via download to local file{cache_msg})" 

1106 

1107 # Calculate the (possibly) new location for the formatter 

1108 # to use. 

1109 newLocation = Location(*local_uri.split()) if location_updated else None 

1110 

1111 log.debug("Reading%s from location %s %s with formatter %s", 

1112 f" component {getInfo.component}" if isComponent else "", 

1113 uri, msg, formatter.name()) 

1114 try: 

1115 with formatter._updateLocation(newLocation): 

1116 result = formatter.read(component=getInfo.component if isComponent else None) 

1117 except Exception as e: 

1118 raise ValueError(f"Failure from formatter '{formatter.name()}' for dataset {ref.id}" 

1119 f" ({ref.datasetType.name} from {uri}): {e}") from e 

1120 

1121 return self._post_process_get(result, getInfo.readStorageClass, getInfo.assemblerParams, 

1122 isComponent=isComponent) 

1123 

1124 def exists(self, ref: DatasetRef) -> bool: 

1125 """Check if the dataset exists in the datastore. 

1126 

1127 Parameters 

1128 ---------- 

1129 ref : `DatasetRef` 

1130 Reference to the required dataset. 

1131 

1132 Returns 

1133 ------- 

1134 exists : `bool` 

1135 `True` if the entity exists in the `Datastore`. 

1136 """ 

1137 fileLocations = self._get_dataset_locations_info(ref) 

1138 

1139 # if we are being asked to trust that registry might not be correct 

1140 # we ask for the expected locations and check them explicitly 

1141 if not fileLocations: 

1142 if not self.trustGetRequest: 

1143 return False 

1144 fileLocations = self._get_expected_dataset_locations_info(ref) 

1145 for location, _ in fileLocations: 

1146 if not self._artifact_exists(location): 

1147 return False 

1148 

1149 return True 

1150 

1151 def getURIs(self, ref: DatasetRef, 

1152 predict: bool = False) -> Tuple[Optional[ButlerURI], Dict[str, ButlerURI]]: 

1153 """Return URIs associated with dataset. 

1154 

1155 Parameters 

1156 ---------- 

1157 ref : `DatasetRef` 

1158 Reference to the required dataset. 

1159 predict : `bool`, optional 

1160 If the datastore does not know about the dataset, should it 

1161 return a predicted URI or not? 

1162 

1163 Returns 

1164 ------- 

1165 primary : `ButlerURI` 

1166 The URI to the primary artifact associated with this dataset. 

1167 If the dataset was disassembled within the datastore this 

1168 may be `None`. 

1169 components : `dict` 

1170 URIs to any components associated with the dataset artifact. 

1171 Can be empty if there are no components. 

1172 """ 

1173 

1174 primary: Optional[ButlerURI] = None 

1175 components: Dict[str, ButlerURI] = {} 

1176 

1177 # if this has never been written then we have to guess 

1178 if not self.exists(ref): 

1179 if not predict: 

1180 raise FileNotFoundError("Dataset {} not in this datastore".format(ref)) 

1181 

1182 doDisassembly = self.composites.shouldBeDisassembled(ref) 

1183 

1184 if doDisassembly: 

1185 

1186 for component, componentStorage in ref.datasetType.storageClass.components.items(): 

1187 compRef = ref.makeComponentRef(component) 

1188 compLocation, _ = self._determine_put_formatter_location(compRef) 

1189 

1190 # Add a URI fragment to indicate this is a guess 

1191 components[component] = ButlerURI(compLocation.uri.geturl() + "#predicted") 

1192 

1193 else: 

1194 

1195 location, _ = self._determine_put_formatter_location(ref) 

1196 

1197 # Add a URI fragment to indicate this is a guess 

1198 primary = ButlerURI(location.uri.geturl() + "#predicted") 

1199 

1200 return primary, components 

1201 

1202 # If this is a ref that we have written we can get the path. 

1203 # Get file metadata and internal metadata 

1204 fileLocations = self._get_dataset_locations_info(ref) 

1205 

1206 guessing = False 

1207 if not fileLocations: 

1208 if not self.trustGetRequest: 1208 ↛ 1209line 1208 didn't jump to line 1209, because the condition on line 1208 was never true

1209 raise RuntimeError(f"Unexpectedly got no artifacts for dataset {ref}") 

1210 fileLocations = self._get_expected_dataset_locations_info(ref) 

1211 guessing = True 

1212 

1213 if len(fileLocations) == 1: 

1214 # No disassembly so this is the primary URI 

1215 uri = fileLocations[0][0].uri 

1216 if guessing and not uri.exists(): 1216 ↛ 1217line 1216 didn't jump to line 1217, because the condition on line 1216 was never true

1217 raise FileNotFoundError(f"Expected URI ({uri}) does not exist") 

1218 primary = uri 

1219 

1220 else: 

1221 for location, storedFileInfo in fileLocations: 

1222 if storedFileInfo.component is None: 1222 ↛ 1223line 1222 didn't jump to line 1223, because the condition on line 1222 was never true

1223 raise RuntimeError(f"Unexpectedly got no component name for a component at {location}") 

1224 uri = location.uri 

1225 if guessing and not uri.exists(): 1225 ↛ 1226line 1225 didn't jump to line 1226, because the condition on line 1225 was never true

1226 raise FileNotFoundError(f"Expected URI ({uri}) does not exist") 

1227 components[storedFileInfo.component] = uri 

1228 

1229 return primary, components 

1230 

1231 def getURI(self, ref: DatasetRef, predict: bool = False) -> ButlerURI: 

1232 """URI to the Dataset. 

1233 

1234 Parameters 

1235 ---------- 

1236 ref : `DatasetRef` 

1237 Reference to the required Dataset. 

1238 predict : `bool` 

1239 If `True`, allow URIs to be returned of datasets that have not 

1240 been written. 

1241 

1242 Returns 

1243 ------- 

1244 uri : `str` 

1245 URI pointing to the dataset within the datastore. If the 

1246 dataset does not exist in the datastore, and if ``predict`` is 

1247 `True`, the URI will be a prediction and will include a URI 

1248 fragment "#predicted". 

1249 If the datastore does not have entities that relate well 

1250 to the concept of a URI the returned URI will be 

1251 descriptive. The returned URI is not guaranteed to be obtainable. 

1252 

1253 Raises 

1254 ------ 

1255 FileNotFoundError 

1256 Raised if a URI has been requested for a dataset that does not 

1257 exist and guessing is not allowed. 

1258 RuntimeError 

1259 Raised if a request is made for a single URI but multiple URIs 

1260 are associated with this dataset. 

1261 

1262 Notes 

1263 ----- 

1264 When a predicted URI is requested an attempt will be made to form 

1265 a reasonable URI based on file templates and the expected formatter. 

1266 """ 

1267 primary, components = self.getURIs(ref, predict) 

1268 if primary is None or components: 1268 ↛ 1269line 1268 didn't jump to line 1269, because the condition on line 1268 was never true

1269 raise RuntimeError(f"Dataset ({ref}) includes distinct URIs for components. " 

1270 "Use Dataastore.getURIs() instead.") 

1271 return primary 

1272 

1273 def get(self, ref: DatasetRef, parameters: Optional[Mapping[str, Any]] = None) -> Any: 

1274 """Load an InMemoryDataset from the store. 

1275 

1276 Parameters 

1277 ---------- 

1278 ref : `DatasetRef` 

1279 Reference to the required Dataset. 

1280 parameters : `dict` 

1281 `StorageClass`-specific parameters that specify, for example, 

1282 a slice of the dataset to be loaded. 

1283 

1284 Returns 

1285 ------- 

1286 inMemoryDataset : `object` 

1287 Requested dataset or slice thereof as an InMemoryDataset. 

1288 

1289 Raises 

1290 ------ 

1291 FileNotFoundError 

1292 Requested dataset can not be retrieved. 

1293 TypeError 

1294 Return value from formatter has unexpected type. 

1295 ValueError 

1296 Formatter failed to process the dataset. 

1297 """ 

1298 allGetInfo = self._prepare_for_get(ref, parameters) 

1299 refComponent = ref.datasetType.component() 

1300 

1301 # Supplied storage class for the component being read 

1302 refStorageClass = ref.datasetType.storageClass 

1303 

1304 # Create mapping from component name to related info 

1305 allComponents = {i.component: i for i in allGetInfo} 

1306 

1307 # By definition the dataset is disassembled if we have more 

1308 # than one record for it. 

1309 isDisassembled = len(allGetInfo) > 1 

1310 

1311 # Look for the special case where we are disassembled but the 

1312 # component is a derived component that was not written during 

1313 # disassembly. For this scenario we need to check that the 

1314 # component requested is listed as a derived component for the 

1315 # composite storage class 

1316 isDisassembledReadOnlyComponent = False 

1317 if isDisassembled and refComponent: 

1318 # The composite storage class should be accessible through 

1319 # the component dataset type 

1320 compositeStorageClass = ref.datasetType.parentStorageClass 

1321 

1322 # In the unlikely scenario where the composite storage 

1323 # class is not known, we can only assume that this is a 

1324 # normal component. If that assumption is wrong then the 

1325 # branch below that reads a persisted component will fail 

1326 # so there is no need to complain here. 

1327 if compositeStorageClass is not None: 1327 ↛ 1330line 1327 didn't jump to line 1330, because the condition on line 1327 was never false

1328 isDisassembledReadOnlyComponent = refComponent in compositeStorageClass.derivedComponents 

1329 

1330 if isDisassembled and not refComponent: 

1331 # This was a disassembled dataset spread over multiple files 

1332 # and we need to put them all back together again. 

1333 # Read into memory and then assemble 

1334 

1335 # Check that the supplied parameters are suitable for the type read 

1336 refStorageClass.validateParameters(parameters) 

1337 

1338 # We want to keep track of all the parameters that were not used 

1339 # by formatters. We assume that if any of the component formatters 

1340 # use a parameter that we do not need to apply it again in the 

1341 # assembler. 

1342 usedParams = set() 

1343 

1344 components: Dict[str, Any] = {} 

1345 for getInfo in allGetInfo: 

1346 # assemblerParams are parameters not understood by the 

1347 # associated formatter. 

1348 usedParams.update(set(getInfo.formatterParams)) 

1349 

1350 component = getInfo.component 

1351 

1352 if component is None: 1352 ↛ 1353line 1352 didn't jump to line 1353, because the condition on line 1352 was never true

1353 raise RuntimeError(f"Internal error in datastore assembly of {ref}") 

1354 

1355 # We do not want the formatter to think it's reading 

1356 # a component though because it is really reading a 

1357 # standalone dataset -- always tell reader it is not a 

1358 # component. 

1359 components[component] = self._read_artifact_into_memory(getInfo, ref, isComponent=False) 

1360 

1361 inMemoryDataset = ref.datasetType.storageClass.delegate().assemble(components) 

1362 

1363 # Any unused parameters will have to be passed to the assembler 

1364 if parameters: 

1365 unusedParams = {k: v for k, v in parameters.items() if k not in usedParams} 

1366 else: 

1367 unusedParams = {} 

1368 

1369 # Process parameters 

1370 return ref.datasetType.storageClass.delegate().handleParameters(inMemoryDataset, 

1371 parameters=unusedParams) 

1372 

1373 elif isDisassembledReadOnlyComponent: 

1374 

1375 compositeStorageClass = ref.datasetType.parentStorageClass 

1376 if compositeStorageClass is None: 1376 ↛ 1377line 1376 didn't jump to line 1377, because the condition on line 1376 was never true

1377 raise RuntimeError(f"Unable to retrieve derived component '{refComponent}' since" 

1378 "no composite storage class is available.") 

1379 

1380 if refComponent is None: 1380 ↛ 1382line 1380 didn't jump to line 1382, because the condition on line 1380 was never true

1381 # Mainly for mypy 

1382 raise RuntimeError(f"Internal error in datastore {self.name}: component can not be None here") 

1383 

1384 # Assume that every derived component can be calculated by 

1385 # forwarding the request to a single read/write component. 

1386 # Rather than guessing which rw component is the right one by 

1387 # scanning each for a derived component of the same name, 

1388 # we ask the storage class delegate directly which one is best to 

1389 # use. 

1390 compositeDelegate = compositeStorageClass.delegate() 

1391 forwardedComponent = compositeDelegate.selectResponsibleComponent(refComponent, 

1392 set(allComponents)) 

1393 

1394 # Select the relevant component 

1395 rwInfo = allComponents[forwardedComponent] 

1396 

1397 # For now assume that read parameters are validated against 

1398 # the real component and not the requested component 

1399 forwardedStorageClass = rwInfo.formatter.fileDescriptor.readStorageClass 

1400 forwardedStorageClass.validateParameters(parameters) 

1401 

1402 # Unfortunately the FileDescriptor inside the formatter will have 

1403 # the wrong write storage class so we need to create a new one 

1404 # given the immutability constraint. 

1405 writeStorageClass = rwInfo.info.storageClass 

1406 

1407 # We may need to put some thought into parameters for read 

1408 # components but for now forward them on as is 

1409 readFormatter = type(rwInfo.formatter)(FileDescriptor(rwInfo.location, 

1410 readStorageClass=refStorageClass, 

1411 storageClass=writeStorageClass, 

1412 parameters=parameters), 

1413 ref.dataId) 

1414 

1415 # The assembler can not receive any parameter requests for a 

1416 # derived component at this time since the assembler will 

1417 # see the storage class of the derived component and those 

1418 # parameters will have to be handled by the formatter on the 

1419 # forwarded storage class. 

1420 assemblerParams: Dict[str, Any] = {} 

1421 

1422 # Need to created a new info that specifies the derived 

1423 # component and associated storage class 

1424 readInfo = DatastoreFileGetInformation(rwInfo.location, readFormatter, 

1425 rwInfo.info, assemblerParams, {}, 

1426 refComponent, refStorageClass) 

1427 

1428 return self._read_artifact_into_memory(readInfo, ref, isComponent=True) 

1429 

1430 else: 

1431 # Single file request or component from that composite file 

1432 for lookup in (refComponent, None): 1432 ↛ 1437line 1432 didn't jump to line 1437, because the loop on line 1432 didn't complete

1433 if lookup in allComponents: 1433 ↛ 1432line 1433 didn't jump to line 1432, because the condition on line 1433 was never false

1434 getInfo = allComponents[lookup] 

1435 break 

1436 else: 

1437 raise FileNotFoundError(f"Component {refComponent} not found " 

1438 f"for ref {ref} in datastore {self.name}") 

1439 

1440 # Do not need the component itself if already disassembled 

1441 if isDisassembled: 

1442 isComponent = False 

1443 else: 

1444 isComponent = getInfo.component is not None 

1445 

1446 # For a disassembled component we can validate parametersagainst 

1447 # the component storage class directly 

1448 if isDisassembled: 

1449 refStorageClass.validateParameters(parameters) 

1450 else: 

1451 # For an assembled composite this could be a derived 

1452 # component derived from a real component. The validity 

1453 # of the parameters is not clear. For now validate against 

1454 # the composite storage class 

1455 getInfo.formatter.fileDescriptor.storageClass.validateParameters(parameters) 

1456 

1457 return self._read_artifact_into_memory(getInfo, ref, isComponent=isComponent) 

1458 

1459 @transactional 

1460 def put(self, inMemoryDataset: Any, ref: DatasetRef) -> None: 

1461 """Write a InMemoryDataset with a given `DatasetRef` to the store. 

1462 

1463 Parameters 

1464 ---------- 

1465 inMemoryDataset : `object` 

1466 The dataset to store. 

1467 ref : `DatasetRef` 

1468 Reference to the associated Dataset. 

1469 

1470 Raises 

1471 ------ 

1472 TypeError 

1473 Supplied object and storage class are inconsistent. 

1474 DatasetTypeNotSupportedError 

1475 The associated `DatasetType` is not handled by this datastore. 

1476 

1477 Notes 

1478 ----- 

1479 If the datastore is configured to reject certain dataset types it 

1480 is possible that the put will fail and raise a 

1481 `DatasetTypeNotSupportedError`. The main use case for this is to 

1482 allow `ChainedDatastore` to put to multiple datastores without 

1483 requiring that every datastore accepts the dataset. 

1484 """ 

1485 

1486 doDisassembly = self.composites.shouldBeDisassembled(ref) 

1487 # doDisassembly = True 

1488 

1489 artifacts = [] 

1490 if doDisassembly: 

1491 components = ref.datasetType.storageClass.delegate().disassemble(inMemoryDataset) 

1492 for component, componentInfo in components.items(): 

1493 # Don't recurse because we want to take advantage of 

1494 # bulk insert -- need a new DatasetRef that refers to the 

1495 # same dataset_id but has the component DatasetType 

1496 # DatasetType does not refer to the types of components 

1497 # So we construct one ourselves. 

1498 compRef = ref.makeComponentRef(component) 

1499 storedInfo = self._write_in_memory_to_artifact(componentInfo.component, compRef) 

1500 artifacts.append((compRef, storedInfo)) 

1501 else: 

1502 # Write the entire thing out 

1503 storedInfo = self._write_in_memory_to_artifact(inMemoryDataset, ref) 

1504 artifacts.append((ref, storedInfo)) 

1505 

1506 self._register_datasets(artifacts) 

1507 

1508 @transactional 

1509 def trash(self, ref: DatasetRef, ignore_errors: bool = True) -> None: 

1510 """Indicate to the datastore that a dataset can be removed. 

1511 

1512 Parameters 

1513 ---------- 

1514 ref : `DatasetRef` 

1515 Reference to the required Dataset. 

1516 ignore_errors : `bool` 

1517 If `True` return without error even if something went wrong. 

1518 Problems could occur if another process is simultaneously trying 

1519 to delete. 

1520 

1521 Raises 

1522 ------ 

1523 FileNotFoundError 

1524 Attempt to remove a dataset that does not exist. 

1525 """ 

1526 # Get file metadata and internal metadata 

1527 log.debug("Trashing %s in datastore %s", ref, self.name) 

1528 

1529 fileLocations = self._get_dataset_locations_info(ref) 

1530 

1531 if not fileLocations: 

1532 err_msg = f"Requested dataset to trash ({ref}) is not known to datastore {self.name}" 

1533 if ignore_errors: 

1534 log.warning(err_msg) 

1535 return 

1536 else: 

1537 raise FileNotFoundError(err_msg) 

1538 

1539 for location, storedFileInfo in fileLocations: 

1540 if not self._artifact_exists(location): 1540 ↛ 1541line 1540 didn't jump to line 1541, because the condition on line 1540 was never true

1541 err_msg = f"Dataset is known to datastore {self.name} but " \ 

1542 f"associated artifact ({location.uri}) is missing" 

1543 if ignore_errors: 

1544 log.warning(err_msg) 

1545 return 

1546 else: 

1547 raise FileNotFoundError(err_msg) 

1548 

1549 # Mark dataset as trashed 

1550 try: 

1551 self._move_to_trash_in_registry(ref) 

1552 except Exception as e: 

1553 if ignore_errors: 

1554 log.warning(f"Attempted to mark dataset ({ref}) to be trashed in datastore {self.name} " 

1555 f"but encountered an error: {e}") 

1556 pass 

1557 else: 

1558 raise 

1559 

1560 @transactional 

1561 def emptyTrash(self, ignore_errors: bool = True) -> None: 

1562 """Remove all datasets from the trash. 

1563 

1564 Parameters 

1565 ---------- 

1566 ignore_errors : `bool` 

1567 If `True` return without error even if something went wrong. 

1568 Problems could occur if another process is simultaneously trying 

1569 to delete. 

1570 """ 

1571 log.debug("Emptying trash in datastore %s", self.name) 

1572 # Context manager will empty trash iff we finish it without raising. 

1573 with self.bridge.emptyTrash() as trashed: 

1574 for ref in trashed: 

1575 fileLocations = self._get_dataset_locations_info(ref) 

1576 

1577 if not fileLocations: 1577 ↛ 1578line 1577 didn't jump to line 1578, because the condition on line 1577 was never true

1578 err_msg = f"Requested dataset ({ref}) does not exist in datastore {self.name}" 

1579 if ignore_errors: 

1580 log.warning(err_msg) 

1581 continue 

1582 else: 

1583 raise FileNotFoundError(err_msg) 

1584 

1585 for location, _ in fileLocations: 

1586 

1587 if not self._artifact_exists(location): 1587 ↛ 1588line 1587 didn't jump to line 1588, because the condition on line 1587 was never true

1588 err_msg = f"Dataset {location.uri} no longer present in datastore {self.name}" 

1589 if ignore_errors: 

1590 log.warning(err_msg) 

1591 continue 

1592 else: 

1593 raise FileNotFoundError(err_msg) 

1594 

1595 # Can only delete the artifact if there are no references 

1596 # to the file from untrashed dataset refs. 

1597 if self._can_remove_dataset_artifact(ref, location): 

1598 # Point of no return for this artifact 

1599 log.debug("Removing artifact %s from datastore %s", location.uri, self.name) 

1600 try: 

1601 self._delete_artifact(location) 

1602 except Exception as e: 

1603 if ignore_errors: 

1604 log.critical("Encountered error removing artifact %s from datastore %s: %s", 

1605 location.uri, self.name, e) 

1606 else: 

1607 raise 

1608 

1609 # Now must remove the entry from the internal registry even if 

1610 # the artifact removal failed and was ignored, 

1611 # otherwise the removal check above will never be true 

1612 try: 

1613 # There may be multiple rows associated with this ref 

1614 # depending on disassembly 

1615 self.removeStoredItemInfo(ref) 

1616 except Exception as e: 

1617 if ignore_errors: 

1618 log.warning("Error removing dataset %s (%s) from internal registry of %s: %s", 

1619 ref.id, location.uri, self.name, e) 

1620 continue 

1621 else: 

1622 raise FileNotFoundError( 

1623 f"Error removing dataset {ref.id} ({location.uri}) from internal registry " 

1624 f"of {self.name}" 

1625 ) from e 

1626 

1627 @transactional 

1628 def forget(self, refs: Iterable[DatasetRef]) -> None: 

1629 # Docstring inherited. 

1630 refs = list(refs) 

1631 self.bridge.forget(refs) 

1632 self._table.delete(["dataset_id"], *[{"dataset_id": ref.getCheckedId()} for ref in refs]) 

1633 

1634 def validateConfiguration(self, entities: Iterable[Union[DatasetRef, DatasetType, StorageClass]], 

1635 logFailures: bool = False) -> None: 

1636 """Validate some of the configuration for this datastore. 

1637 

1638 Parameters 

1639 ---------- 

1640 entities : iterable of `DatasetRef`, `DatasetType`, or `StorageClass` 

1641 Entities to test against this configuration. Can be differing 

1642 types. 

1643 logFailures : `bool`, optional 

1644 If `True`, output a log message for every validation error 

1645 detected. 

1646 

1647 Raises 

1648 ------ 

1649 DatastoreValidationError 

1650 Raised if there is a validation problem with a configuration. 

1651 All the problems are reported in a single exception. 

1652 

1653 Notes 

1654 ----- 

1655 This method checks that all the supplied entities have valid file 

1656 templates and also have formatters defined. 

1657 """ 

1658 

1659 templateFailed = None 

1660 try: 

1661 self.templates.validateTemplates(entities, logFailures=logFailures) 

1662 except FileTemplateValidationError as e: 

1663 templateFailed = str(e) 

1664 

1665 formatterFailed = [] 

1666 for entity in entities: 

1667 try: 

1668 self.formatterFactory.getFormatterClass(entity) 

1669 except KeyError as e: 

1670 formatterFailed.append(str(e)) 

1671 if logFailures: 1671 ↛ 1666line 1671 didn't jump to line 1666, because the condition on line 1671 was never false

1672 log.critical("Formatter failure: %s", e) 

1673 

1674 if templateFailed or formatterFailed: 

1675 messages = [] 

1676 if templateFailed: 1676 ↛ 1677line 1676 didn't jump to line 1677, because the condition on line 1676 was never true

1677 messages.append(templateFailed) 

1678 if formatterFailed: 1678 ↛ 1680line 1678 didn't jump to line 1680, because the condition on line 1678 was never false

1679 messages.append(",".join(formatterFailed)) 

1680 msg = ";\n".join(messages) 

1681 raise DatastoreValidationError(msg) 

1682 

1683 def getLookupKeys(self) -> Set[LookupKey]: 

1684 # Docstring is inherited from base class 

1685 return self.templates.getLookupKeys() | self.formatterFactory.getLookupKeys() | \ 

1686 self.constraints.getLookupKeys() 

1687 

1688 def validateKey(self, lookupKey: LookupKey, 

1689 entity: Union[DatasetRef, DatasetType, StorageClass]) -> None: 

1690 # Docstring is inherited from base class 

1691 # The key can be valid in either formatters or templates so we can 

1692 # only check the template if it exists 

1693 if lookupKey in self.templates: 

1694 try: 

1695 self.templates[lookupKey].validateTemplate(entity) 

1696 except FileTemplateValidationError as e: 

1697 raise DatastoreValidationError(e) from e 

1698 

1699 def export(self, refs: Iterable[DatasetRef], *, 

1700 directory: Optional[Union[ButlerURI, str]] = None, 

1701 transfer: Optional[str] = "auto") -> Iterable[FileDataset]: 

1702 # Docstring inherited from Datastore.export. 

1703 if transfer is not None and directory is None: 1703 ↛ 1704line 1703 didn't jump to line 1704, because the condition on line 1703 was never true

1704 raise RuntimeError(f"Cannot export using transfer mode {transfer} with no " 

1705 "export directory given") 

1706 

1707 # Force the directory to be a URI object 

1708 directoryUri: Optional[ButlerURI] = None 

1709 if directory is not None: 1709 ↛ 1712line 1709 didn't jump to line 1712, because the condition on line 1709 was never false

1710 directoryUri = ButlerURI(directory, forceDirectory=True) 

1711 

1712 if transfer is not None and directoryUri is not None: 1712 ↛ 1717line 1712 didn't jump to line 1717, because the condition on line 1712 was never false

1713 # mypy needs the second test 

1714 if not directoryUri.exists(): 1714 ↛ 1715line 1714 didn't jump to line 1715, because the condition on line 1714 was never true

1715 raise FileNotFoundError(f"Export location {directory} does not exist") 

1716 

1717 progress = Progress("lsst.daf.butler.datastores.FileDatastore.export", level=logging.DEBUG) 

1718 for ref in progress.wrap(refs, "Exporting dataset files"): 

1719 fileLocations = self._get_dataset_locations_info(ref) 

1720 if not fileLocations: 1720 ↛ 1721line 1720 didn't jump to line 1721, because the condition on line 1720 was never true

1721 raise FileNotFoundError(f"Could not retrieve dataset {ref}.") 

1722 # For now we can not export disassembled datasets 

1723 if len(fileLocations) > 1: 

1724 raise NotImplementedError(f"Can not export disassembled datasets such as {ref}") 

1725 location, storedFileInfo = fileLocations[0] 

1726 

1727 pathInStore = location.pathInStore.path 

1728 if transfer is None: 1728 ↛ 1731line 1728 didn't jump to line 1731, because the condition on line 1728 was never true

1729 # TODO: do we also need to return the readStorageClass somehow? 

1730 # We will use the path in store directly 

1731 pass 

1732 elif transfer == "direct": 1732 ↛ 1734line 1732 didn't jump to line 1734, because the condition on line 1732 was never true

1733 # Use full URIs to the remote store in the export 

1734 pathInStore = str(location.uri) 

1735 else: 

1736 # mypy needs help 

1737 assert directoryUri is not None, "directoryUri must be defined to get here" 

1738 storeUri = ButlerURI(location.uri) 

1739 

1740 # if the datastore has an absolute URI to a resource, we 

1741 # have two options: 

1742 # 1. Keep the absolute URI in the exported YAML 

1743 # 2. Allocate a new name in the local datastore and transfer 

1744 # it. 

1745 # For now go with option 2 

1746 if location.pathInStore.isabs(): 1746 ↛ 1747line 1746 didn't jump to line 1747, because the condition on line 1746 was never true

1747 template = self.templates.getTemplate(ref) 

1748 newURI = ButlerURI(template.format(ref), forceAbsolute=False) 

1749 pathInStore = str(newURI.updatedExtension(location.pathInStore.getExtension())) 

1750 

1751 exportUri = directoryUri.join(pathInStore) 

1752 exportUri.transfer_from(storeUri, transfer=transfer) 

1753 

1754 yield FileDataset(refs=[ref], path=pathInStore, formatter=storedFileInfo.formatter) 

1755 

1756 @staticmethod 

1757 def computeChecksum(uri: ButlerURI, algorithm: str = "blake2b", block_size: int = 8192) -> Optional[str]: 

1758 """Compute the checksum of the supplied file. 

1759 

1760 Parameters 

1761 ---------- 

1762 uri : `ButlerURI` 

1763 Name of resource to calculate checksum from. 

1764 algorithm : `str`, optional 

1765 Name of algorithm to use. Must be one of the algorithms supported 

1766 by :py:class`hashlib`. 

1767 block_size : `int` 

1768 Number of bytes to read from file at one time. 

1769 

1770 Returns 

1771 ------- 

1772 hexdigest : `str` 

1773 Hex digest of the file. 

1774 

1775 Notes 

1776 ----- 

1777 Currently returns None if the URI is for a remote resource. 

1778 """ 

1779 if algorithm not in hashlib.algorithms_guaranteed: 1779 ↛ 1780line 1779 didn't jump to line 1780, because the condition on line 1779 was never true

1780 raise NameError("The specified algorithm '{}' is not supported by hashlib".format(algorithm)) 

1781 

1782 if not uri.isLocal: 1782 ↛ 1783line 1782 didn't jump to line 1783, because the condition on line 1782 was never true

1783 return None 

1784 

1785 hasher = hashlib.new(algorithm) 

1786 

1787 with uri.as_local() as local_uri: 

1788 with open(local_uri.ospath, "rb") as f: 

1789 for chunk in iter(lambda: f.read(block_size), b""): 

1790 hasher.update(chunk) 

1791 

1792 return hasher.hexdigest() 

1793 

1794 def needs_expanded_data_ids( 

1795 self, 

1796 transfer: Optional[str], 

1797 entity: Optional[Union[DatasetRef, DatasetType, StorageClass]] = None, 

1798 ) -> bool: 

1799 # Docstring inherited. 

1800 # This _could_ also use entity to inspect whether the filename template 

1801 # involves placeholders other than the required dimensions for its 

1802 # dataset type, but that's not necessary for correctness; it just 

1803 # enables more optimizations (perhaps only in theory). 

1804 return transfer not in ("direct", None)