language-extensions/R/src/RExtension.cpp
//--------------------------------------------------------------------------------------------------
// Name: CheckAndGetSession
//
// Description:
//  Looks up the session map for an RSession pointer corresponding to sessionId after converting
//  it into a string.
//  If it is not found, initializes the session only if the funcName is InitSession.
//  else, throws a runtime_error exception stating that the given funcName is called
//  before the session environment is initialized.
//  On the other hand, if it is found, returns the found session only if funcName is NOT InitSession.
//  else, throws a runtime_error exception stating that session cannot reinitialized before cleanup.
//
// Returns:
//  The RSession pointer for given sessionId and taskId
//
CheckAndGetSession
Description:
Looks up the session map for an RSession pointer corresponding to sessionId after converting
it into a string.
If it is not found, initializes the session only if the funcName is InitSession.
else, throws a runtime_error exception stating that the given funcName is called
before the session environment is initialized.
On the other hand, if it is found, returns the found session only if funcName is NOT InitSession.
else, throws a runtime_error exception stating that session cannot reinitialized before cleanup.

The RSession pointer for given sessionId and taskId
CheckAndGetSession Description : Looks up the session map for an RSession pointer corresponding to sessionId after converting it into a string . If it is not found initializes the session only if the funcName is InitSession . else throws a runtime_error exception stating that the given funcName is called before the session environment is initialized . On the other hand if it is found returns the found session only if funcName is NOT InitSession . else throws a runtime_error exception stating that session cannot reinitialized before cleanup . The RSession pointer for given sessionId and taskId
RSession* CheckAndGetSession(string &&funcName, SQLGUID sessionId, SQLUSMALLINT taskId)
{
	string sessionIdString = Utilities::ConvertGuidToString(&sessionId);
	unordered_map<string, unique_ptr<RSession>>::const_iterator it =
		g_RSessionMap.find(sessionIdString);
	RSession* session = nullptr;
	bool isInitSession = funcName.compare("InitSession") == 0;
	bool isCleanupSession = funcName.compare("CleanupSession") == 0;
	if (it == g_RSessionMap.end())
	{
		if (isInitSession)
		{
			string msg = "Starting session: " + sessionIdString + " with task id: " +
				to_string(taskId) + ".";
			LOG(msg);
			g_RSessionMap[sessionIdString] = make_unique<RSession>();
			session = g_RSessionMap[sessionIdString].get();
		}
		else if (!isCleanupSession)
		{
			throw runtime_error("Function " + funcName + " called before session id "
				+ sessionIdString + " is initialized.");
		}
	}
	else
	{
		if (!isInitSession)
		{
			session = it->second.get();
		}
		else
		{
			throw runtime_error("Session " + sessionIdString + " cannot be reinitialized"
				" without cleaning up first.");
		}
	}
	return session;
}
RSession * CheckAndGetSession ( string && funcName , SQLGUID sessionId , SQLUSMALLINT taskId ) { string sessionIdString = Utilities :: ConvertGuidToString ( & sessionId ) ; unordered_map < string , unique_ptr < RSession > > :: const_iterator it = g_RSessionMap . find ( sessionIdString ) ; RSession * session = nullptr ; bool isInitSession = funcName . compare ( " " ) == 0 ; bool isCleanupSession = funcName . compare ( " " ) == 0 ; if ( it == g_RSessionMap . end ( ) ) { if ( isInitSession ) { string msg = " " + sessionIdString + " " + to_string ( taskId ) + " " ; LOG ( msg ) ; g_RSessionMap [ sessionIdString ] = make_unique < RSession > ( ) ; session = g_RSessionMap [ sessionIdString ] . get ( ) ; } else if ( ! isCleanupSession ) { throw runtime_error ( " " + funcName + " " + sessionIdString + " " ) ; } } else { if ( ! isInitSession ) { session = it -> second . get ( ) ; } else { throw runtime_error ( " " + sessionIdString + " " " " ) ; } } return session ; }
Name: CheckAndGetSession
Description:
Looks up the session map for an RSession pointer corresponding to sessionId after converting
it into a string.
Name : CheckAndGetSession Description : Looks up the session map for an RSession pointer corresponding to sessionId after converting it into a string .
// Cleanup session can be called before session is initialized in case it is a // library session. //