// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "BufferingInputStream.hxx"
#include "InputStream.hxx"
#include "thread/Name.hxx"

#include <string.h>

BufferingInputStream::BufferingInputStream(InputStreamPtr _input)
	:input(std::move(_input)),
	 mutex(input->mutex),
	 thread(BIND_THIS_METHOD(RunThread)),
	 buffer(input->GetSize())
{
	input->SetHandler(this);

	buffer.SetName("InputCache");

	thread.Start();
}

BufferingInputStream::~BufferingInputStream() noexcept
{
	{
		const std::lock_guard lock{mutex};
		stop = true;
		wake_cond.notify_one();
	}

	thread.Join();
}

void
BufferingInputStream::Check()
{
	if (error)
		std::rethrow_exception(error);

	if (input)
		input->Check();
}

bool
BufferingInputStream::IsAvailable(size_t offset) const noexcept
{
	if (offset >= size() || error)
		return true;

	if (buffer.Read(offset).HasData())
		return true;

	/* if no data is available now, make sure it will be soon */
	if (want_offset == INVALID_OFFSET)
		want_offset = offset;

	return false;
}

size_t
BufferingInputStream::Read(std::unique_lock<Mutex> &lock, size_t offset,
			   std::span<std::byte> dest)
{
	if (offset >= size())
		return 0;

	while (true) {
		auto r = buffer.Read(offset);
		if (r.HasData()) {
			/* yay, we have some data */
			size_t nbytes = std::min(dest.size(), r.defined_buffer.size());
			memcpy(dest.data(), r.defined_buffer.data(), nbytes);
			return nbytes;
		}

		if (error)
			std::rethrow_exception(error);

		if (want_offset == INVALID_OFFSET)
			want_offset = offset;

		client_cond.wait(lock);
	}
}

size_t
BufferingInputStream::FindFirstHole() const noexcept
{
	auto r = buffer.Read(0);
	if (r.undefined_size > 0)
		/* a hole at the beginning */
		return 0;

	if (r.defined_buffer.size() < size())
		/* a hole in the middle */
		return r.defined_buffer.size();

	/* the file has been read completely */
	return INVALID_OFFSET;
}

inline void
BufferingInputStream::RunThreadLocked(std::unique_lock<Mutex> &lock)
{
	while (!stop) {
		if (want_offset != INVALID_OFFSET) {
			assert(want_offset < size());

			const size_t seek_offset = want_offset;
			want_offset = INVALID_OFFSET;
			if (!buffer.Read(seek_offset).HasData())
				input->Seek(lock, seek_offset);
		} else if (input->IsEOF()) {
			/* our input has reached its end: prepare
			   reading the first remaining hole */

			size_t new_offset = FindFirstHole();
			if (new_offset == INVALID_OFFSET) {
				/* the file has been read completely */
				break;
			}

			/* seek to the first hole */
			input->Seek(lock, new_offset);
		} else if (input->IsAvailable()) {
			const auto read_offset = input->GetOffset();
			auto w = buffer.Write(read_offset);

			if (w.empty()) {
				size_t new_offset = FindFirstHole();
				if (new_offset == INVALID_OFFSET)
					/* the file has been read
					   completely */
					break;

				input->Seek(lock, new_offset);

				continue;
			}

			/* enforce an upper limit for each
			   InputStream::Read() call; this is necessary
			   for plugins which are unable to do partial
			   reads, e.g. when reading local files, the
			   read() system call will not return until
			   all requested bytes have been read from the
			   hard disk, instead of returning when "some"
			   data has been read */
			constexpr size_t MAX_READ = 64 * 1024;

			if (w.size() > MAX_READ)
				w = w.first(MAX_READ);

			size_t nbytes = input->Read(lock, w);
			buffer.Commit(read_offset, read_offset + nbytes);

			client_cond.notify_all();
			OnBufferAvailable(lock);
		} else
			wake_cond.wait(lock);
	}
}

void
BufferingInputStream::RunThread() noexcept
{
	SetThreadName("buffering");

	std::unique_lock lock{mutex};

	try {
		RunThreadLocked(lock);
	} catch (...) {
		error = std::current_exception();
		client_cond.notify_all();
		OnBufferAvailable(lock);
	}

	/* clear the "input" attribute while holding the mutex */
	auto _input = std::move(input);

	/* the mutex must be unlocked while an InputStream can be
	   destructed */
	lock.unlock();

	/* and now actually destruct the InputStream */
	_input.reset();
}
