{-# LINE 1 "src/Database/EJDB2/Options.hsc" #-}
{-# LANGUAGE CPP #-}

module Database.EJDB2.Options
        ( Options(..), zero, OptionsB, options, build ) where

import           Data.ByteString.Char8

import           Foreign
import           Foreign.C.String
import           Foreign.C.Types

import qualified Database.EJDB2.KV                as KV
import qualified Database.EJDB2.HTTP              as HTTP



-- | EJDB open options
data Options = Options { kv :: !KV.Options -- ^ IWKV storage options
                       , http :: !HTTP.Options -- ^ HTTP/Websocket server options
                       , noWal :: !Bool -- ^ Do not use write-ahead-log. Default: false
                       , sortBufferSz :: !Word32 -- ^ Max sorting buffer size. If exceeded an overflow temp file for sorted data will created. Default 16Mb, min: 1Mb
                       , documentBufferSz :: !Word32 -- ^ Initial size of buffer in bytes used to process/store document during query execution. Default 64Kb, min: 16Kb
                       }

-- | Create default Options
zero :: Options
zero = Options { kv = KV.zero
               , http = HTTP.zero
               , noWal = False
               , sortBufferSz = 0
               , documentBufferSz = 0
               }

-- | Storable version of 'Options'
data OptionsB = OptionsB { options :: Options
                         , kvB :: !KV.OptionsB
                         , httpB :: !HTTP.OptionsB
                         }

-- | Create Storable version of 'Options'
build :: Options -> IO OptionsB
build options = do
        kvB <- KV.build (kv options)
        httpB <- HTTP.build (http options)
        return $ OptionsB options kvB httpB

instance Storable OptionsB where
        sizeOf _ = (128)
{-# LINE 49 "src/Database/EJDB2/Options.hsc" #-}
        alignment _ = 8
{-# LINE 50 "src/Database/EJDB2/Options.hsc" #-}
        peek ptr = do
           kvB <- (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 52 "src/Database/EJDB2/Options.hsc" #-}
           let kv = KV.options kvB
           httpB <- (\hsc_ptr -> peekByteOff hsc_ptr 72) ptr
{-# LINE 54 "src/Database/EJDB2/Options.hsc" #-}
           let http = HTTP.options httpB
           (CBool no_wal) <- (\hsc_ptr -> peekByteOff hsc_ptr 112) ptr
{-# LINE 56 "src/Database/EJDB2/Options.hsc" #-}
           (CUInt sort_buffer_sz) <- (\hsc_ptr -> peekByteOff hsc_ptr 116) ptr
{-# LINE 57 "src/Database/EJDB2/Options.hsc" #-}
           (CUInt document_buffer_sz) <- (\hsc_ptr -> peekByteOff hsc_ptr 120) ptr
{-# LINE 58 "src/Database/EJDB2/Options.hsc" #-}
           return $ OptionsB (Options kv http (toBool no_wal) sort_buffer_sz document_buffer_sz) kvB httpB
        poke ptr (OptionsB (Options _ http noWal sort_buffer_sz document_buffer_sz) kvB httpB) = do
           (\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr kvB
{-# LINE 61 "src/Database/EJDB2/Options.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 72) ptr httpB
{-# LINE 62 "src/Database/EJDB2/Options.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 112) ptr (CBool $ fromBool noWal)
{-# LINE 63 "src/Database/EJDB2/Options.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 116) ptr (CUInt sort_buffer_sz)
{-# LINE 64 "src/Database/EJDB2/Options.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 120) ptr (CUInt document_buffer_sz)
{-# LINE 65 "src/Database/EJDB2/Options.hsc" #-}