module Main (main) where

import Joins (readFileToTable, sortTableOnCol, nestedLoopJoin, sortMergeJoin, partitionTableOnId)

import Control.Monad.Par (runPar, get, spawnP, fork, put, new)



main :: IO ()
main = do

    astub <- readFileToTable "..//data_berka/stubs/account.asc" ";"
    tstub <- readFileToTable "..//data_berka/stubs/trans.asc" ";"

    -- Set 1 for 9 partitions, 9 for just 1 partition
    let apart = partitionTableOnId astub "account_id" 1
        tpart = partitionTableOnId tstub "account_id" 1

    print $
        runPar $ do 
            j <- spawnP (sortMergeJoin (head apart) (head tpart) "account_id")
            k <- spawnP (sortMergeJoin ((!!1) apart) ((!!1) tpart) "account_id")
            l <- spawnP (sortMergeJoin ((!!2) apart) ((!!2) tpart) "account_id")
            m <- spawnP (sortMergeJoin ((!!3) apart) ((!!3) tpart) "account_id")
            n <- spawnP (sortMergeJoin ((!!4) apart) ((!!4) tpart) "account_id")
            o <- spawnP (sortMergeJoin ((!!5) apart) ((!!5) tpart) "account_id")
            p <- spawnP (sortMergeJoin ((!!6) apart) ((!!6) tpart) "account_id")
            q <- spawnP (sortMergeJoin ((!!7) apart) ((!!7) tpart) "account_id")
            r <- spawnP (sortMergeJoin ((!!8) apart) ((!!8) tpart) "account_id")


            -- j <- spawnP (nestedLoopJoin (head apart) (head tpart) "account_id")
            -- k <- spawnP (nestedLoopJoin ((!!1) apart) ((!!1) tpart) "account_id")
            -- l <- spawnP (nestedLoopJoin ((!!2) apart) ((!!2) tpart) "account_id")
            -- m <- spawnP (nestedLoopJoin ((!!3) apart) ((!!3) tpart) "account_id")
            -- n <- spawnP (nestedLoopJoin ((!!4) apart) ((!!4) tpart) "account_id")
            -- o <- spawnP (nestedLoopJoin ((!!5) apart) ((!!5) tpart) "account_id")
            -- p <- spawnP (nestedLoopJoin ((!!6) apart) ((!!6) tpart) "account_id")
            -- q <- spawnP (nestedLoopJoin ((!!7) apart) ((!!7) tpart) "account_id")
            -- r <- spawnP (nestedLoopJoin ((!!8) apart) ((!!8) tpart) "account_id")

            a <- get j
            b <- get k
            c <- get l
            d <- get m
            e <- get n
            f <- get o
            g <- get p
            h <- get q
            i <- get r
            
            return $ a <> b <> c <> d <> e <> f <> g <> h <> i
