Task.cpp 7.74 KB
Newer Older
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
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 - 2017 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *          Alexandre Fernandez <nerf@boboop.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "Task.h"

#include "filesystem/IFile.h"
31
#include "filesystem/IDirectory.h"
32
#include "File.h"
33 34 35 36
#include "Folder.h"
#include "Playlist.h"
#include "parser/Task.h"
#include "utils/Filename.h"
37 38 39 40

namespace medialibrary
{

41 42 43 44
const std::string policy::TaskTable::Name = "Task";
const std::string policy::TaskTable::PrimaryKeyColumn = "id_task";
int64_t parser::Task::* const policy::TaskTable::PrimaryKey = &parser::Task::m_id;

45 46 47
namespace parser
{

48 49 50
Task::Task( MediaLibraryPtr ml, sqlite::Row& row )
    : currentService( 0 )
    , m_ml( ml )
51
{
52 53 54 55 56 57 58 59
    row >> m_id
        >> m_step
        >> m_retryCount
        >> mrl
        >> m_fileId
        >> m_parentFolderId
        >> m_parentPlaylistId
        >> parentPlaylistIndex;
60 61
}

62
Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
63
            std::shared_ptr<Folder> parentFolder,
64
            std::shared_ptr<fs::IDirectory> parentFolderFs,
65
            std::shared_ptr<Playlist> parentPlaylist,
66
            unsigned int parentPlaylistIndex )
67 68 69
    : fileFs( std::move( fileFs ) )
    , parentFolder( std::move( parentFolder ) )
    , parentFolderFs( std::move( parentFolderFs ) )
70 71
    , parentPlaylist( std::move( parentPlaylist ) )
    , parentPlaylistIndex( parentPlaylistIndex )
72
    , mrl( this->fileFs->mrl() )
73
    , currentService( 0 )
74 75
    , m_ml( ml )
    , m_step( ParserStep::None )
76
    , m_fileId( 0 )
77 78 79 80 81
{
}

void Task::markStepCompleted( ParserStep stepCompleted )
{
82 83
    m_step = static_cast<ParserStep>( static_cast<uint8_t>( m_step ) |
                                      static_cast<uint8_t>( stepCompleted ) );
84 85 86 87
}

void Task::markStepUncompleted( ParserStep stepUncompleted )
{
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
    m_step = static_cast<ParserStep>( static_cast<uint8_t>( m_step ) &
                                      ( ~ static_cast<uint8_t>( stepUncompleted ) ) );
}

bool Task::saveParserStep()
{
    static const std::string req = "UPDATE " + policy::TaskTable::Name + " SET step = ?, "
            "retry_count = 0 WHERE id_task = ?";
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_step, m_id ) == false )
        return false;
    return true;
}

bool Task::isCompleted() const
{
    return m_step == ParserStep::Completed;
}

bool Task::isStepCompleted( Task::ParserStep step ) const
{
    return ( static_cast<uint8_t>( m_step ) & static_cast<uint8_t>( step ) ) != 0;
}

void Task::startParserStep()
{
    static const std::string req = "UPDATE " + policy::TaskTable::Name + " SET "
            "retry_count = retry_count + 1 WHERE id_task = ?";
    sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_id );
}

118 119 120 121 122 123 124 125 126 127 128 129
bool Task::updateFileId()
{
    assert( m_fileId == 0 );
    assert( file != nullptr && file->id() != 0 );
    static const std::string req = "UPDATE " + policy::TaskTable::Name + " SET "
            "file_id = ? WHERE id_task = ?";
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, file->id(), m_id ) == false )
        return false;
    m_fileId = file->id();
    return true;
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
bool Task::restoreLinkedEntities( )
{
    auto fsFactory = m_ml->fsFactoryForMrl( mrl );
    if ( fsFactory == nullptr )
        return false;

    parentFolderFs = fsFactory->createDirectory( utils::file::directory( mrl ) );
    if ( parentFolderFs == nullptr )
        return false;
    auto files = parentFolderFs->files();
    auto it = std::find_if( begin( files ), end( files ), [this]( std::shared_ptr<fs::IFile> f ) {
        return f->mrl() == mrl;
    });
    if ( it == end( files ) )
    {
        LOG_ERROR( "Failed to restore fs::IFile associated with ", mrl );
        return false;
    }
    fileFs = *it;

    file = File::fetch( m_ml, m_fileId );
151
    if ( file != nullptr )
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
        media = file->media();
    parentFolder = Folder::fetch( m_ml, m_parentFolderId );
    if ( m_parentPlaylistId != 0 )
        parentPlaylist = Playlist::fetch( m_ml, m_parentPlaylistId );

    return true;
}

void Task::createTable( sqlite::Connection* dbConnection )
{
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::TaskTable::Name + "("
        "id_task INTEGER PRIMARY KEY AUTOINCREMENT,"
        "step INTEGER NOT NULL DEFAULT 0,"
        "retry_count INTEGER NOT NULL DEFAULT 0,"
        "mrl TEXT,"
        "file_id UNSIGNED INTEGER,"
        "parent_folder_id UNSIGNED INTEGER,"
        "parent_playlist_id INTEGER,"
        "parent_playlist_index UNSIGNED INTEGER,"
        "FOREIGN KEY (parent_folder_id) REFERENCES " + policy::FolderTable::Name
        + "(id_folder) ON DELETE CASCADE,"
        "FOREIGN KEY (file_id) REFERENCES " + policy::FileTable::Name
        + "(id_file) ON DELETE CASCADE,"
        "FOREIGN KEY (parent_playlist_id) REFERENCES " + policy::PlaylistTable::Name
        + "(id_playlist) ON DELETE CASCADE"
    ")";
    sqlite::Tools::executeRequest( dbConnection, req );
}

void Task::resetRetryCount( MediaLibraryPtr ml )
{
    static const std::string req = "UPDATE " + policy::TaskTable::Name + " SET "
            "retry_count = 0 WHERE step != ? AND is_present != 0";
    sqlite::Tools::executeUpdate( ml->getConn(), req, parser::Task::ParserStep::Completed );
}

void Task::resetParsing( MediaLibraryPtr ml )
{
    static const std::string req = "UPDATE " + policy::TaskTable::Name + " SET "
            "retry_count = 0, step = ?";
    sqlite::Tools::executeUpdate( ml->getConn(), req, parser::Task::ParserStep::None );
}

std::vector<std::shared_ptr<Task>> Task::fetchUnparsed( MediaLibraryPtr ml )
{
    static const std::string req = "SELECT * FROM " + policy::TaskTable::Name + " t"
        " LEFT JOIN " + policy::FileTable::Name + " f ON f.id_file = t.file_id"
        " WHERE step != ? AND retry_count < 3 AND (f.is_present != 0 OR "
        " t.file_id IS NULL)";
    return Task::fetchAll<Task>( ml, req, parser::Task::ParserStep::Completed );
}

std::shared_ptr<Task>
Task::create( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
              std::shared_ptr<Folder> parentFolder, std::shared_ptr<fs::IDirectory> parentFolderFs,
              std::pair<std::shared_ptr<Playlist>, unsigned int> parentPlaylist )
{
    std::shared_ptr<Task> self = std::make_shared<Task>( ml, std::move( fileFs ),
        std::move( parentFolder ), std::move( parentFolderFs ),
        std::move( parentPlaylist.first ), parentPlaylist.second );
    const std::string req = "INSERT INTO " + policy::TaskTable::Name +
        "(mrl, parent_folder_id, parent_playlist_id, parent_playlist_index) "
        "VALUES(?, ?, ?, ?)";
    if ( insert( ml, self, req, self->mrl, self->parentFolder->id(), sqlite::ForeignKey(
                 self->parentPlaylist ? self->parentPlaylist->id() : 0 ),
                 self->parentPlaylistIndex ) == false )
        return nullptr;
    return self;
220 221 222 223 224
}

}

}