/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  componentFactory.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver.
 *
 * kvmstoreserver 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.
 *
 * kvmstoreserver 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 kvmstoreserver.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

// Package serdeHandling contains everything necessary to access a storage
// medium (currently only hard disk in this version). In later versions, the
// area SQL will be covered. This package offers two functions to decide by
// parameter which type of storage media should be used (Factory). A distinction
// is made between driver factory and loader factory. Loader facory provides
// functions that return pre-made readers.
package serdeHandling

import (
	"bytes"
	es "essentials"
	"utils/configMap"
)

// DriverFactory returns a function suitable for the storage medium. A
// distinction is made between saving and deleting. These functions are mainly
// needed at runtime
func DriverFactory(action es.Control) func(node IHandelSerialization) error {

	switch configMap.GetStringOf("serde_driver") {

	case "filesystem":
		if action == es.Delete {
			return deleteFromFileSystem
		}
		return saveToFileSystem

	case "mssql":
		panic("The MSSql driver is not available in this version")

	case "postgres":
		panic("The Postgres (CockroachDB) driver is not available in this version")

	default:
		panic("Unknown store system driver - Please check the VAR -serde_driver- in your config file")
	}
}

// LoaderFactory returns a function suitable for the storage medium.
// These functions are mainly needed at start time (loader)
func LoaderFactory() func(nodeID int) *bytes.Reader {

	switch configMap.GetStringOf("serde_driver") {

	case "filesystem":
		return loadContainerFormDisk
	case "mssql":
		panic("The MSSql driver is not available in this version")

	case "postgres":
		panic("The Postgres (CockroachDB) driver is not available in this version")

	default:
		panic("Unknown store system driver - Please check the VAR -serde_driver- in your config file")
	}

}
