/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

/// @file   Inspector.h
/// @author Baudouin Raoult
/// @author Tiago Quintino
/// @date   Mar 2016

#ifndef fdb5_Inspector_H
#define fdb5_Inspector_H

#include <cstdlib>
#include <iosfwd>
#include <vector>

#include "eckit/container/CacheLRU.h"

#include "fdb5/api/helpers/ListIterator.h"
#include "fdb5/config/Config.h"

namespace eckit {
class DataHandle;
}

namespace metkit::mars {
class MarsRequest;
}


namespace fdb5 {

class Key;
class Op;
class CatalogueReader;
class Schema;
class FDBToolRequest;
class EntryVisitor;

//----------------------------------------------------------------------------------------------------------------------

class InspectIterator : public APIIteratorBase<ListElement> {
public:

    InspectIterator();
    ~InspectIterator();

    void emplace(ListElement&& elem);
    bool next(ListElement& elem) override;

private:

    std::vector<ListElement> queue_;
    size_t index_;
};

//----------------------------------------------------------------------------------------------------------------------

class Inspector {

public:  // methods

    explicit Inspector(const Config& dbConfig);

    Inspector(const Inspector&) = delete;
    Inspector& operator=(const Inspector&) = delete;
    Inspector(Inspector&&) = delete;
    Inspector& operator=(Inspector&&) = delete;

    /// Retrieves the data selected by the MarsRequest to the provided DataHandle
    /// @returns  data handle to read from

    ListIterator inspect(const metkit::mars::MarsRequest& request) const;

    /// Give read access to a range of entries according to a request

    void visitEntries(const FDBToolRequest& request, EntryVisitor& visitor) const;

    friend std::ostream& operator<<(std::ostream& s, const Inspector& x) {
        x.print(s);
        return s;
    }

private:  // methods

    void print(std::ostream& out) const;

private:  // data

    mutable eckit::CacheLRU<Key, CatalogueReader*> databases_;

    Config dbConfig_;
};

//----------------------------------------------------------------------------------------------------------------------

}  // namespace fdb5

#endif
