Skip to content

Commit

Permalink
Add CachedResolver Python cache miss resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScheller committed Oct 29, 2023
1 parent 522c89c commit 2960ede
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ clear
# Clear existing build data and invoke cmake
rm -R build
rm -R dist
set -e # Exit on error
cmake . -B build
cmake --build build --clean-first # make clean all
cmake --install build # make install
Expand Down
6 changes: 5 additions & 1 deletion src/CachedResolver/PythonExpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ def _ResolveAnchored(anchorPath, path):
class Resolver:
@staticmethod
@log_function_args
def ResolveAndCache(assetPath):
def ResolveAndCache(assetPath, context):
"""Return the resolved path for the given assetPath or an empty
ArResolvedPath if no asset exists at that path.
Args:
assetPath (str): An unresolved asset path.
Returns:
Ar.ResolvedPath: The resolved path.
"""

#raise Exception(">>>>>>>{}".format(dir(context)))
print(":::::::::::::::::::::::::::::::::::::::::::::::::: Context ->", context.GetMappingPairs())
context.AddMappingPair("debug", "How cool is this!")
return Ar.ResolvedPath("Debug Working")

# if not assetPath:
Expand Down
38 changes: 23 additions & 15 deletions src/CachedResolver/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,45 +148,53 @@ CachedResolver::_Resolve(
if (ctx) {
// Search for mapping pairs
auto &mappingPairs = ctx->GetMappingPairs();

auto map_find = mappingPairs.find(assetPath);
if(map_find != mappingPairs.end()){
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, map_find->second);
if (resolvedPath) {
return resolvedPath;
}
return resolvedPath;
// Assume that a map hit is always valid.
// if (resolvedPath) {
// return resolvedPath;
// }
}
// Search for cached pairs
auto &cachedPairs = ctx->GetCachedPairs();
auto cache_find = cachedPairs.find(assetPath);
if(cache_find != cachedPairs.end()){
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, cache_find->second);
if (resolvedPath) {
return resolvedPath;
}
return resolvedPath;
// Assume that a cache hit is always valid.
// if (resolvedPath) {
// return resolvedPath;
// }
}
// Perform query if caches don't have a hit.
std::string pythonResult;
{
// Perform query if caches don't have a hit.
/*
We perform the resource/thread lock in the context itself to
allow for resolver multithreading with different contexts
Is this approach in general a hacky solution? Yes, we are circumventing C++'s
'constants' mechanism by redirecting our queries into Python. This way we can modify our
'const' read locked resolver context. While it works, be aware that potential side effects may occur.
'constants' mechanism by redirecting our queries into Python in which we
write-access our Resolver Context. This way we can modify our 'const' C++ read
locked resolver context. While it works, be aware that potential side effects may occur.
*/
const std::lock_guard<std::mutex> lock(g_resolver_query_mutex);

TF_DEBUG(CACHEDRESOLVER_RESOLVER).Msg("Resolver::_Resolve('%s') -> No cache hit, switching to Python query\n", assetPath.c_str());
ArResolvedPath pythonResult;

int state = TfPyInvokeAndExtract(DEFINE_STRING(AR_CACHEDRESOLVER_USD_PYTHON_EXPOSE_MODULE_NAME),
"Resolver.ResolveAndCache",
&pythonResult, assetPath);
&pythonResult, assetPath, *ctx);
if (!state) {
std::cerr << "Failed to call Resolver._Resolve in " << DEFINE_STRING(AR_CACHEDRESOLVER_USD_PYTHON_EXPOSE_MODULE_NAME) << ".py. ";
std::cerr << "Failed to call Resolver.ResolveAndCache in " << DEFINE_STRING(AR_CACHEDRESOLVER_USD_PYTHON_EXPOSE_MODULE_NAME) << ".py. ";
std::cerr << "Please verify that the python code is valid!" << std::endl;
}
if (pythonResult) {
return pythonResult;
}
}
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, pythonResult);
if (resolvedPath) {
return resolvedPath;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/CachedResolver/testenv/testCachedResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def setUpClass(cls):
def test_debug(self):
resolver = Ar.GetResolver()
resolved_path = resolver.Resolve("debug")
raise Exception(">> {}".format(resolved_path.GetPathString()))
print("::::::::::::::::::::::::::::::::::::::: Result Call 1 ->", resolved_path)
resolved_path = resolver.Resolve("debug")
print("::::::::::::::::::::::::::::::::::::::: Result Call 2 ->", resolved_path)

def _test_CreateIdentifier(self):
resolver = Ar.GetResolver()
Expand Down

0 comments on commit 2960ede

Please sign in to comment.