Example #1
0
void STLRepository::include( const Bytes& relationship, Resources& values )
{
    if ( relationship.empty( ) )
    {
        return;
    }
    
    Resources relationships;
    
    unique_lock< mutex> lock( m_resources_lock );
    
    if ( relationship == restq::SUBSCRIPTION )
    {
        for ( const auto& resource : m_resources )
        {
            if ( resource.lower_bound( "type" )->second == restq::SUBSCRIPTION )
            {
                auto iterators = resource.equal_range( "queues" );
                
                for ( const auto& queue : values )
                {
                    auto key = String::lowercase( String::to_string( queue.lower_bound( "key" )->second ) );
                    
                    for ( auto iterator = iterators.first; iterator not_eq iterators.second; iterator++ )
                    {
                        if ( key == String::lowercase( String::to_string( iterator->second ) ) )
                        {
                            relationships.push_back( resource );
                            break;
                        }
                    }
                }
            }
        }
    }
    else
    {
        for ( const auto& value : values )
        {
            const auto key = String::lowercase( String::to_string( value.lower_bound( "key" )->second ) );
            const auto type = value.lower_bound( "type" )->second;
            const auto foreign_key = String::to_string( type ) + "-key";
            
            for ( const auto& resource : m_resources )
            {
                if ( resource.lower_bound( "type" )->second == relationship )
                {
                    if ( resource.count( foreign_key ) and key == String::lowercase( String::to_string( resource.lower_bound( foreign_key )->second ) ) )
                    {
                        relationships.push_back( resource );
                    }
                }
            }
        }
    }
    
    lock.unlock( );
    
    values.insert( values.end( ), relationships.begin( ), relationships.end( ) );
}
Example #2
0
    ResourcePtr ResourceManager::load(const String &name, int32_t argc, ...)
    {
        ResourcePtr res = nullptr;

        // First, search cache
        auto itr = mResourceCache.find(name);

        if (itr != mResourceCache.end())
        {
            // Found it in cache.
            Resources &resources = itr->second;

            if (resources.size() > 0)
            {
                auto i = resources.find(0);

                if (i != resources.end())
                {
                    // Found in original resource list
                    res = i->second;
                }
                else
                {
                    // Found not in original resource list
                    va_list params;
                    va_start(params, argc);
                    res = create(name, argc, params);
                    va_end(params);

                    if (res != nullptr)
                    {
                        bool ret = res->load();

                        if (ret)
                        {
                            resources.insert(ResPairValue(0, res));
                        }
                        else
                        {
                            res = nullptr;
                        }
                    }
                }
            }
        }
        else
        {
            // Found not, it should create a new instance.
            va_list params;
            va_start(params, argc);
            res = create(name, argc, params);
            va_end(params);

            if (res != nullptr)
            {
                bool ret = res->load();

                if (ret)
                {
                    Resources resources;
                    resources.insert(ResPairValue(0, res));
                    mResourceCache.insert(ResMapPairValue(name, resources));
                }
                else
                {
                    res = nullptr;
                }
            }
        }

        return res;
    }