Metadata.cpp 5.77 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 31 32 33 34 35
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2018 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
 *
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.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.
 *****************************************************************************/

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

#include "Metadata.h"

#include "database/SqliteTools.h"

#include <algorithm>

namespace medialibrary
{

36
const std::string Metadata::Table::Name = "Metadata";
37

38
Metadata::Record::Record( uint32_t t, std::string v )
39 40 41 42 43 44
    : m_type( t )
    , m_value( std::move( v ) )
    , m_isSet( true )
{
}

45
Metadata::Record::Record( uint32_t t )
46 47 48 49 50
    : m_type( t )
    , m_isSet( false )
{
}

51
bool Metadata::Record::isSet() const
52 53 54 55
{
    return m_isSet;
}

56
int64_t Metadata::Record::integer() const
57 58 59 60
{
    return atoll( m_value.c_str() );
}

61
const std::string& Metadata::Record::str() const
62 63 64 65
{
    return m_value;
}

66 67 68 69 70 71
void Metadata::Record::unset()
{
    m_isSet = false;
    m_value.clear();
}

72
Metadata::Metadata(MediaLibraryPtr ml , IMetadata::EntityType entityType)
73
    : m_ml( ml )
74
    , m_entityType( entityType )
75 76 77 78 79
    , m_nbMeta( 0 )
    , m_entityId( 0 )
{
}

80
void Metadata::init( int64_t entityId, uint32_t nbMeta )
81 82 83 84 85 86 87 88 89 90 91
{
    if ( isReady() == true )
        return;

    m_nbMeta = nbMeta;
    m_entityId = entityId;
    // Reserve the space for all meta to avoid a race condition where 2 threads
    // would cache different meta, invalidating the potential reference
    // to another IMediaMetadata held by another thread.
    // This guarantees the vector will not grow afterward.
    m_records.reserve( m_nbMeta );
92
    static const std::string req = "SELECT * FROM " + Metadata::Table::Name +
93
            " WHERE id_media = ? AND entity_type = ?";
94 95 96
    auto conn = m_ml->getConn();
    auto ctx = conn->acquireReadContext();
    sqlite::Statement stmt( conn->handle(), req );
97
    stmt.execute( m_entityId, m_entityType );
98 99 100
    for ( sqlite::Row row = stmt.row(); row != nullptr; row = stmt.row() )
    {
        assert( row.load<int64_t>( 0 ) == m_entityId );
101 102 103
        assert( row.load<IMetadata::EntityType>( 1 ) == m_entityType );
        m_records.emplace_back( row.load<decltype(Record::m_type)>( 2 ),
                          row.load<decltype(Record::m_value)>( 3 ) );
104 105 106
    }
}

107
bool Metadata::isReady() const
108 109 110 111
{
    return m_nbMeta != 0;
}

112
IMetadata& Metadata::get( uint32_t type ) const
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
{
    assert( isReady() == true );

    auto it = std::find_if( begin( m_records ), end( m_records ), [type](const Record& r ) {
        return r.m_type == type;
    });
    if ( it == end( m_records ) )
    {
        // Create an unset meta for the given type˙No DB entity will be created until
        // the meta is actually set.
        m_records.emplace_back( type );
        return *( m_records.rbegin() );
    }
    return *it;
}

129
bool Metadata::set( uint32_t type, const std::string& value )
130 131 132 133 134 135 136 137 138 139 140 141
{
    assert( isReady() == true );

    auto it = std::find_if( begin( m_records ), end( m_records ), [type]( const Record& r ) {
        return r.m_type == type;
    });
    if ( it != end( m_records ) )
        (*it).set( value );
    else
        m_records.emplace_back( type, value );
    try
    {
142
        static const std::string req = "INSERT OR REPLACE INTO " + Metadata::Table::Name +
143 144 145
                "(id_media, entity_type, type, value) VALUES(?, ?, ?, ?)";
        return sqlite::Tools::executeInsert( m_ml->getConn(), req, m_entityId, m_entityType,
                                             type, value );
146 147 148 149 150 151 152 153
    }
    catch ( const sqlite::errors::Generic& ex )
    {
        LOG_ERROR( "Failed to update media metadata: ", ex.what() );
        return false;
    }
}

154
bool Metadata::set( uint32_t type, int64_t value )
155 156 157 158 159
{
    auto str = std::to_string( value );
    return set( type, str );
}

160 161 162 163 164 165 166 167
bool Metadata::unset( uint32_t type )
{
    assert( isReady() == true );
    auto it = std::find_if( begin( m_records ), end( m_records ), [type]( const Record& r ) {
        return r.m_type == type;
    });
    if ( it != end( m_records ) )
    {
168
        static const std::string req = "DELETE FROM " + Metadata::Table::Name +
169 170 171 172 173 174 175 176
                " WHERE id_media = ? AND entity_type = ? AND type = ?";
        (*it).unset();
        return sqlite::Tools::executeDelete( m_ml->getConn(), req, m_entityId,
                                             m_entityType, type );
    }
    return true;
}

177
void Metadata::unset( sqlite::Connection* dbConn, IMetadata::EntityType entityType, uint32_t type )
178
{
179
    static const std::string req = "DELETE FROM " + Metadata::Table::Name +
180 181 182 183 184 185
            " WHERE entity_type = ? AND type = ? ";
    sqlite::Tools::executeDelete( dbConn, req, entityType, type );
}

void Metadata::createTable(sqlite::Connection* connection)
{
186 187 188 189 190
    const std::string reqs[] = {
        #include "database/tables/Metadata_v14.sql"
    };
    for ( const auto& req : reqs )
        sqlite::Tools::executeRequest( connection, req );
191 192
}

193
void Metadata::Record::set( const std::string& value )
194 195 196 197 198 199
{
    m_value = value;
    m_isSet = true;
}

}