/* namequery.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2021 Askar Almatov

    This file is part of Trafficmeter.

    Trafficmeter is free software: you can redistribute it and/or modify it under the terms of the GNU General
    Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    Trafficmeter 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 General Public
    License for more details.

    You should have received a copy of the GNU General Public License along with Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#ifndef __NAMEQUERY_H
#define __NAMEQUERY_H 1

#include <set>
#include <libiclass/ipv4>

#include "path.h"

namespace trafficmeter
{
    /**********************************************************************************************************/
    class NameQuery
    {
    public:
        struct Row;

                                        NameQuery( const Path& dbPath );
                                        ~NameQuery();

        void                            addNetwork( iclass::ipnet4t network );
        bool                            hasRows();
        Row                             getRow();
        std::set<Row>                   fetchSmall( int nRowsLimit );
        std::set<Row>                   fetchLarge( int nRowsLimit );
        long                            nFileRecords() const;
        long                            fileSize() const;
        const iclass::ipset4t&          networks() const;

    protected:
        struct Db_;

        const Path                      path_;
        iclass::ipset4t                 networks_;
        Db_*                            db_;
    };

    /**********************************************************************************************************/
    struct NameQuery::Row
    {
        iclass::ipaddr4t                ip;
        std::string                     name;

        bool                            operator<( const Row& ) const;
    };
}

/*
****************************************************************************************************************
****************************************************************************************************************
*/

namespace trafficmeter
{
    //----------------------------------------------------------------------------------------------------------
    inline const iclass::ipset4t&
    NameQuery::networks() const
    {
        return networks_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    NameQuery::Row::operator<( const Row& rhs ) const
    {
        if ( name.size() != rhs.name.size() )
        {
            return name.size() < rhs.name.size();
        }

        if ( name != rhs.name )
        {
            return name < rhs.name;
        }

        return ip < rhs.ip;
    }
}

#endif  // __NAMEQUERY_H
