//------------------------------------------------------------------------------
/*
    This file is part of rippled: https://github.com/ripple/rippled
    Copyright (c) 2012, 2013 Ripple Labs Inc.

    Permission to use, copy, modify, and/or distribute this software for any
    purpose  with  or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL ,  DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <test/nodestore/TestBase.h>
#include <test/unit_test/SuiteJournal.h>
#include <xrpld/nodestore/DummyScheduler.h>
#include <xrpld/nodestore/Manager.h>
#include <xrpld/unity/rocksdb.h>
#include <xrpl/beast/utility/temp_dir.h>
#include <algorithm>

namespace ripple {

namespace NodeStore {

// Tests the Backend interface
//
class Backend_test : public TestBase
{
public:
    void
    testBackend(
        std::string const& type,
        std::uint64_t const seedValue,
        int numObjsToTest = 2000)
    {
        DummyScheduler scheduler;

        testcase("Backend type=" + type);

        Section params;
        beast::temp_dir tempDir;
        params.set("type", type);
        params.set("path", tempDir.path());

        beast::xor_shift_engine rng(seedValue);

        // Create a batch
        auto batch = createPredictableBatch(numObjsToTest, rng());

        using namespace beast::severities;
        test::SuiteJournal journal("Backend_test", *this);

        {
            // Open the backend
            std::unique_ptr<Backend> backend = Manager::instance().make_Backend(
                params, megabytes(4), scheduler, journal);
            backend->open();

            // Write the batch
            storeBatch(*backend, batch);

            {
                // Read it back in
                Batch copy;
                fetchCopyOfBatch(*backend, &copy, batch);
                BEAST_EXPECT(areBatchesEqual(batch, copy));
            }

            {
                // Reorder and read the copy again
                std::shuffle(batch.begin(), batch.end(), rng);
                Batch copy;
                fetchCopyOfBatch(*backend, &copy, batch);
                BEAST_EXPECT(areBatchesEqual(batch, copy));
            }
        }

        // rwdb backend does not keep table/data after close
        if (type != "rwdb")
        {
            // Re-open the backend
            std::unique_ptr<Backend> backend = Manager::instance().make_Backend(
                params, megabytes(4), scheduler, journal);
            backend->open();

            // Read it back in
            Batch copy;
            fetchCopyOfBatch(*backend, &copy, batch);
            // Canonicalize the source and destination batches
            std::sort(batch.begin(), batch.end(), LessThan{});
            std::sort(copy.begin(), copy.end(), LessThan{});
            BEAST_EXPECT(areBatchesEqual(batch, copy));
        }
    }

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

    void
    run() override
    {
        std::uint64_t const seedValue = 50;

        testBackend("memory", seedValue);
        testBackend("rwdb", seedValue);
        testBackend("nudb", seedValue);

#if RIPPLE_ROCKSDB_AVAILABLE
        testBackend("rocksdb", seedValue);
#endif

#ifdef RIPPLE_ENABLE_SQLITE_BACKEND_TESTS
        testBackend("sqlite", seedValue);
#endif
    }
};

BEAST_DEFINE_TESTSUITE(Backend, NodeStore, ripple);

}  // namespace NodeStore
}  // namespace ripple
