forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layerRegistry.cpp
509 lines (451 loc) · 17.5 KB
/
layerRegistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
///
/// \file Sdf/LayerRegistry.cpp
#include "pxr/pxr.h"
#include "pxr/usd/sdf/layerRegistry.h"
#include "pxr/usd/sdf/assetPathResolver.h"
#include "pxr/usd/sdf/debugCodes.h"
#include "pxr/usd/sdf/fileFormat.h"
#include "pxr/usd/sdf/layer.h"
//#include "pxr/usd/sdf/schema.h"
#include "pxr/usd/ar/resolver.h"
#include "pxr/base/trace/trace.h"
#include "pxr/base/tf/pathUtils.h"
#include "pxr/base/tf/registryManager.h"
#include "pxr/base/tf/staticData.h"
#include <ostream>
using std::string;
PXR_NAMESPACE_OPEN_SCOPE
// A simple layer repr, used for debug and error messages, that includes both
// the identifier and the real path.
static string
Sdf_LayerDebugRepr(
const SdfLayerHandle& layer)
{
return layer ?
"SdfLayer('" +
layer->GetIdentifier() + "', '" +
layer->GetRealPath() + "')" :
"None"
;
}
struct Sdf_RegistryAliases {
std::string identifier;
std::string repositoryPath;
std::string realPath;
};
static Sdf_RegistryAliases
_AssetInfoToAliases(const Sdf_AssetInfo& assetInfo) {
std::string identifierSansArguments, arguments;
TF_VERIFY(Sdf_SplitIdentifier(
assetInfo.identifier, &identifierSansArguments, &arguments));
// The identifier cannot be empty. If it is, GetLayers() will not function
// correctly.
TF_VERIFY(!assetInfo.identifier.empty());
return {
assetInfo.identifier,
assetInfo.assetInfo.repoPath.empty() ? "" :
Sdf_CreateIdentifier(assetInfo.assetInfo.repoPath, arguments),
assetInfo.resolvedPath.empty() ? "" :
Sdf_CreateIdentifier(assetInfo.resolvedPath, arguments)
};
}
Sdf_LayerRegistry::Sdf_LayerRegistry()
{
}
// Ideally, the lifetime of a layer should be synchronized with the registry.
// However--
// a) Update operations can result in a "dangling layer" where a layer
// is evicted from the registry even though a user still retains
// a handle.
// b) There's a known race in expiring layers where a handle is evicted
// before the destructor completes.
// For those two reasons _TryToRemove must "try" to remove and not error
// or warn if an expected key is missing.
static bool
_TryToRemove(const std::string& key, const SdfLayerHandle& layer,
std::unordered_map<std::string, SdfLayerHandle, TfHash>* map) {
if (!key.empty()) {
if (const auto it = map->find(key);
it != std::end(*map) && it->second == layer) {
map->erase(it);
return true;
}
}
return false;
}
static bool
_TryToRemove(
const std::string& key, const SdfLayerHandle& layer,
std::unordered_multimap<std::string, SdfLayerHandle, TfHash>* map) {
const auto range = map->equal_range(key);
if (const auto it = std::find_if(range.first, range.second,
[&layer](const auto& entry){
return entry.second == layer;
}); it != range.second) {
map->erase(it);
return true;
}
return false;
}
void
Sdf_LayerRegistry::_Layers::Update(const SdfLayerHandle& layer,
const Sdf_AssetInfo& oldInfo,
const Sdf_AssetInfo& newInfo) {
const auto oldAliases = _AssetInfoToAliases(oldInfo);
auto newAliases = _AssetInfoToAliases(newInfo);
if (oldAliases.realPath != newAliases.realPath) {
if (_TryToRemove(oldAliases.realPath, layer, &_byRealPath)) {
TF_DEBUG(SDF_LAYER).Msg("Removed realPath '%s' for update.\n",
oldAliases.realPath.c_str());
}
if (!newAliases.realPath.empty()) {
if (const auto insertion = _byRealPath.emplace(
newAliases.realPath, layer);
insertion.second) {
TF_DEBUG(SDF_LAYER).Msg("Updated realPath '%s'.\n",
newAliases.realPath.c_str());
}
else {
// It is uncommon but possible for two distinct handles to have
// the same real path. If an update operation is going to
// generate a dangling layer, then ensure the identifier and
// repository path entries are removed as well by setting the
// identifier and repository path to the empty string so that
// their entries in the repository and identifier maps will be
// removed.
newAliases.repositoryPath = "";
newAliases.identifier = "";
TF_DEBUG(SDF_LAYER).Msg("Updated realPath '%s' would create "
"collision. Dangling layer created "
"instead.\n",
newAliases.realPath.c_str());
}
}
}
if (oldAliases.repositoryPath != newAliases.repositoryPath) {
if (_TryToRemove(oldAliases.repositoryPath, layer,
&_byRepositoryPath)) {
TF_DEBUG(SDF_LAYER).Msg("Removed repositoryPath '%s' for "
"update.\n",
oldAliases.repositoryPath.c_str());
}
if (!newAliases.repositoryPath.empty()) {
_byRepositoryPath.emplace(newAliases.repositoryPath, layer);
TF_DEBUG(SDF_LAYER).Msg("Updated repositoryPath '%s'.\n",
newAliases.repositoryPath.c_str());
}
}
if (oldAliases.identifier != newAliases.identifier) {
if (_TryToRemove(oldAliases.identifier, layer,
&_byIdentifier)) {
TF_DEBUG(SDF_LAYER).Msg("Removed identifier '%s' for "
"update.\n",
oldAliases.identifier.c_str());
}
if (!newAliases.identifier.empty()) {
_byIdentifier.emplace(newAliases.identifier, layer);
TF_DEBUG(SDF_LAYER).Msg("Updated identifier '%s'.\n",
newAliases.identifier.c_str());
}
}
}
std::pair<SdfLayerHandle, bool>
Sdf_LayerRegistry::_Layers::Insert(const SdfLayerHandle& layer,
const Sdf_AssetInfo& assetInfo) {
const auto aliases = _AssetInfoToAliases(assetInfo);
if (const auto it = _byRealPath.find(aliases.realPath);
it != std::end(_byRealPath)) {
return std::make_pair(it->second, false);
}
if (!aliases.realPath.empty()) {
TF_VERIFY(_byRealPath.emplace(aliases.realPath, layer).second);
TF_DEBUG(SDF_LAYER).Msg("Inserted realPath '%s' into registry\n",
aliases.realPath.c_str());
}
if (!aliases.repositoryPath.empty()) {
_byRepositoryPath.emplace(aliases.repositoryPath, layer);
TF_DEBUG(SDF_LAYER).Msg("Inserted repositoryPath '%s' into registry\n",
aliases.repositoryPath.c_str());
}
if (!aliases.identifier.empty()) {
_byIdentifier.emplace(aliases.identifier, layer);
TF_DEBUG(SDF_LAYER).Msg("Inserted identifier '%s' into registry\n",
aliases.identifier.c_str());
}
return std::make_pair(layer, true);
}
bool
Sdf_LayerRegistry::_Layers::Erase(const SdfLayerHandle& layer,
const Sdf_AssetInfo& assetInfo) {
const auto aliases = _AssetInfoToAliases(assetInfo);
// Track whether any entries were actually erased. In general,
// It's the responsibility of the layer destructor to erase
// the entry from the registry, but trying to acquire an expiring
// layer may cause this eviction to happen early. This isn't an
// error, but we do want to track successful erases for TF_DEBUG
// info. Additionally, Update in rare circumstances could create
// a real path collisions due to asset resolver context updates
// and may lead to an early eviction of a layer from the registry.
bool erased = false;
if (_TryToRemove(aliases.realPath, layer, &_byRealPath)) {
erased = true;
TF_DEBUG(SDF_LAYER).Msg("Erased realPath '%s' from registry.\n",
aliases.realPath.c_str());
}
if (_TryToRemove(aliases.repositoryPath, layer,
&_byRepositoryPath)) {
erased = true;
TF_DEBUG(SDF_LAYER).Msg(
"Erased repositoryPath '%s' from registry.\n",
aliases.repositoryPath.c_str());
}
if (_TryToRemove(aliases.identifier, layer,
&_byIdentifier)) {
erased = true;
TF_DEBUG(SDF_LAYER).Msg(
"Erased identifier '%s' from registry.\n",
aliases.repositoryPath.c_str());
}
return erased;
}
void
Sdf_LayerRegistry::Insert(
const SdfLayerHandle& layer, const Sdf_AssetInfo& assetInfo)
{
TRACE_FUNCTION();
if (!layer) {
TF_CODING_ERROR("Expired layer handle");
return;
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::Insert(%s)\n",
Sdf_LayerDebugRepr(layer).c_str());
// Attempt to insert the layer into the registry.
std::pair<SdfLayerHandle, bool> result = _layers.Insert(layer, assetInfo);
if (!result.second) {
// We failed to insert the layer into the registry because there
// is a realPath conflict. This can happen when the same layer is
// created twice in the same location in the same session.
TF_CODING_ERROR("Cannot insert duplicate registry entry for "
"%s layer %s over existing entry for %s layer %s",
layer->GetFileFormat()->GetFormatId().GetText(),
Sdf_LayerDebugRepr(layer).c_str(),
result.first->GetFileFormat()->GetFormatId().GetText(),
Sdf_LayerDebugRepr(result.first).c_str());
}
}
void
Sdf_LayerRegistry::Update(
const SdfLayerHandle& layer,
const Sdf_AssetInfo& oldInfo,
const Sdf_AssetInfo& newInfo)
{
TRACE_FUNCTION();
if (!layer) {
TF_CODING_ERROR("Expired layer handle");
return;
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::Update(%s)\n",
Sdf_LayerDebugRepr(layer).c_str());
_layers.Update(layer, oldInfo, newInfo);
}
void
Sdf_LayerRegistry::Erase(
const SdfLayerHandle& layer,
const Sdf_AssetInfo& assetInfo)
{
bool erased = _layers.Erase(layer, assetInfo);
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::Erase(%s) => %s\n",
Sdf_LayerDebugRepr(layer).c_str(),
erased ? "Success" : "Failed");
}
SdfLayerHandle
Sdf_LayerRegistry::Find(
const string &inputLayerPath,
const string &resolvedPath) const
{
TRACE_FUNCTION();
SdfLayerHandle foundLayer;
if (Sdf_IsAnonLayerIdentifier(inputLayerPath)) {
foundLayer = _FindByIdentifier(inputLayerPath);
} else {
ArResolver& resolver = ArGetResolver();
const string& layerPath = inputLayerPath;
// If the layer path depends on context there may be multiple
// layers with the same identifier but different resolved paths.
// In this case we need to look up the layer by resolved path.
string assetPath, args;
Sdf_SplitIdentifier(inputLayerPath, &assetPath, &args);
if (!resolver.IsContextDependentPath(assetPath)) {
foundLayer = _FindByIdentifier(layerPath);
}
// If the layer path is in repository form and we haven't yet
// found the layer via the identifier, attempt to look up the
// layer by repository path.
const bool isRepositoryPath = resolver.IsRepositoryPath(assetPath);
if (!foundLayer && isRepositoryPath) {
foundLayer = _FindByRepositoryPath(layerPath);
}
// If the layer has not yet been found, this may be some other
// form of path that requires path resolution and lookup in the
// real path index in order to locate.
if (!foundLayer) {
foundLayer = _FindByRealPath(layerPath, resolvedPath);
}
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::Find('%s') => %s\n",
inputLayerPath.c_str(),
Sdf_LayerDebugRepr(foundLayer).c_str());
return foundLayer;
}
SdfLayerHandle
Sdf_LayerRegistry::_FindByIdentifier(
const string& layerPath) const
{
TRACE_FUNCTION();
SdfLayerHandle foundLayer;
const auto& byIdentifier = _layers.ByIdentifier();
const auto identifierIt = byIdentifier.find(layerPath);
if (identifierIt != byIdentifier.end()) {
foundLayer = identifierIt->second;
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::_FindByIdentifier('%s') => %s\n",
layerPath.c_str(),
foundLayer ? "Found" : "Not Found");
return foundLayer;
}
SdfLayerHandle
Sdf_LayerRegistry::_FindByRepositoryPath(
const string& layerPath) const
{
TRACE_FUNCTION();
SdfLayerHandle foundLayer;
if (layerPath.empty())
return foundLayer;
const auto& byRepoPath = _layers.ByRepositoryPath();
const auto repoPathIt = byRepoPath.find(layerPath);
if (repoPathIt != byRepoPath.end()) {
foundLayer = repoPathIt->second;
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::_FindByRepositoryPath('%s') => %s\n",
layerPath.c_str(),
foundLayer ? "Found" : "Not Found");
return foundLayer;
}
SdfLayerHandle
Sdf_LayerRegistry::_FindByRealPath(
const string& layerPath,
const string& resolvedPath) const
{
TRACE_FUNCTION();
SdfLayerHandle foundLayer;
if (layerPath.empty())
return foundLayer;
string searchPath, arguments;
if (!Sdf_SplitIdentifier(layerPath, &searchPath, &arguments))
return foundLayer;
// Ignore errors reported by Sdf_ComputeFilePath. These errors mean we
// weren't able to compute a real path from the given layerPath. However,
// that shouldn't be an error for this function, it just means there was
// nothing to find at that layerPath.
{
TfErrorMark m;
searchPath = !resolvedPath.empty() ?
resolvedPath : Sdf_ComputeFilePath(searchPath);
if (!m.IsClean()) {
std::vector<std::string> errors;
for (const TfError& e : m) {
errors.push_back(e.GetCommentary());
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::_FindByRealPath('%s'): "
"Failed to compute real path: %s\n",
layerPath.c_str(), TfStringJoin(errors, ", ").c_str());
m.Clear();
}
}
searchPath = Sdf_CreateIdentifier(searchPath, arguments);
const auto& byRealPath = _layers.ByRealPath();
const auto realPathIt = byRealPath.find(searchPath);
if (realPathIt != byRealPath.end()) {
foundLayer = realPathIt->second;
}
TF_DEBUG(SDF_LAYER).Msg(
"Sdf_LayerRegistry::_FindByRealPath('%s') => %s\n",
searchPath.c_str(),
foundLayer ? "Found" : "Not Found");
return foundLayer;
}
SdfLayerHandleSet
Sdf_LayerRegistry::GetLayers() const
{
SdfLayerHandleSet layers;
for (const auto& entry : _layers.ByIdentifier()) {
SdfLayerHandle layer = entry.second;
if (TF_VERIFY(layer, "Found expired layer in registry")) {
layers.insert(layer);
}
}
return layers;
}
std::ostream&
operator<<(std::ostream& ostr, const Sdf_LayerRegistry& registry)
{
SdfLayerHandleSet layers = registry.GetLayers();
TF_FOR_ALL(i, layers) {
if (SdfLayerHandle layer = *i) {
ostr << TfStringPrintf(
"%p[ref=%zu]:\n"
" format = %s\n"
" identifier = '%s'\n"
" repositoryPath = '%s'\n"
" realPath = '%s'\n"
" version = '%s'\n"
" assetInfo = \n'%s'\n"
" muted = %s\n"
" anonymous = %s\n"
"\n"
, layer.GetUniqueIdentifier()
, layer->GetCurrentCount()
, layer->GetFileFormat()->GetFormatId().GetText()
, layer->GetIdentifier().c_str()
, layer->GetRepositoryPath().c_str()
, layer->GetRealPath().c_str()
, layer->GetVersion().c_str()
, TfStringify(layer->GetAssetInfo()).c_str()
, (layer->IsMuted() ? "True" : "False")
, (layer->IsAnonymous() ? "True" : "False")
);
}
}
return ostr;
}
PXR_NAMESPACE_CLOSE_SCOPE