<s> import contextlib <EOL> import gc <EOL> import multiprocessing <EOL> import os <EOL> from memsql_loader . util . apsw_storage import APSWStorage <EOL> from memsql_loader . util import paths <EOL> MEMSQL_LOADER_DB = '<STR_LIT>' <EOL> def get_loader_db_path ( ) : <EOL> return os . path . join ( paths . get_data_dir ( ) , MEMSQL_LOADER_DB ) <EOL> class LoaderStorage ( APSWStorage ) : <EOL> _instance = None <EOL> _initialized = False <EOL> _instance_lock = multiprocessing . RLock ( ) <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> with cls . _instance_lock : <EOL> if cls . _instance is None : <EOL> cls . _instance = super ( LoaderStorage , cls ) . __new__ ( <EOL> cls , * args , ** kwargs ) <EOL> cls . _initialized = False <EOL> return cls . _instance <EOL> @ classmethod <EOL> def drop_database ( cls ) : <EOL> with cls . _instance_lock : <EOL> if os . path . isfile ( get_loader_db_path ( ) ) : <EOL> os . remove ( get_loader_db_path ( ) ) <EOL> if os . path . isfile ( get_loader_db_path ( ) + '<STR_LIT>' ) : <EOL> os . remove ( get_loader_db_path ( ) + '<STR_LIT>' ) <EOL> if os . path . isfile ( get_loader_db_path ( ) + '<STR_LIT>' ) : <EOL> os . remove ( get_loader_db_path ( ) + '<STR_LIT>' ) <EOL> cls . _instance = None <EOL> @ classmethod <EOL> @ contextlib . contextmanager <EOL> def fork_wrapper ( cls ) : <EOL> with cls . _instance_lock : <EOL> if cls . _instance is not None : <EOL> cls . _instance . close_connections ( ) <EOL> gc . collect ( <NUM_LIT:2> ) <EOL> yield <EOL> with cls . _instance_lock : <EOL> if cls . _instance is not None : <EOL> cls . _instance . setup_connections ( ) <EOL> def __init__ ( self ) : <EOL> with LoaderStorage . _instance_lock : <EOL> if not LoaderStorage . _initialized : <EOL> super ( LoaderStorage , self ) . __init__ ( get_loader_db_path ( ) ) <EOL> LoaderStorage . _initialized = True <EOL> return <EOL> elif not self . _db or not self . _db_t : <EOL> self . setup_connections ( ) </s>
