<commit_before>-- | Higher-level DB functionality.

module Pos.Modern.DB.DB
       ( openNodeDBs
       ) where

import           Control.Monad.Trans.Resource (MonadResource)
import           System.Directory             (createDirectoryIfMissing)
import           System.FilePath              ((</>))
import           Universum

import           Pos.Modern.DB.Functions      (openDB)
import           Pos.Modern.DB.Holder         (runDBHolder)
import           Pos.Modern.DB.Types          (NodeDBs (..))
import           Pos.Modern.DB.Utxo           (prepareUtxoDB)

-- | Open all DBs stored on disk.
openNodeDBs :: MonadResource m => FilePath -> m (NodeDBs ssc)
openNodeDBs fp = do
    let blockPath = fp </> "blocks"
    let utxoPath = fp </> "utxo"
    let miscPath = fp </> "misc"
    mapM_ ensureDirectoryExists [blockPath, utxoPath, miscPath]
    res <- NodeDBs <$> openDB blockPath
                   <*> openDB utxoPath
                   <*> openDB miscPath
    res <$ (runDBHolder res prepareUtxoDB)
  where
    ensureDirectoryExists :: MonadIO m => FilePath -> m ()
    ensureDirectoryExists = liftIO . createDirectoryIfMissing True
<commit_msg>[CSL-306] Add getTipBlock to DB module<commit_after>-- | Higher-level DB functionality.

module Pos.Modern.DB.DB
       ( openNodeDBs
       , getTipBlock
       ) where

import           Control.Monad.Trans.Resource (MonadResource)
import           System.Directory             (createDirectoryIfMissing)
import           System.FilePath              ((</>))
import           Universum

import           Pos.Modern.DB.Block          (getBlock)
import           Pos.Modern.DB.Class          (MonadDB)
import           Pos.Modern.DB.Error          (DBError (DBMalformed))
import           Pos.Modern.DB.Functions      (openDB)
import           Pos.Modern.DB.Holder         (runDBHolder)
import           Pos.Modern.DB.Types          (NodeDBs (..))
import           Pos.Modern.DB.Utxo           (getTip, prepareUtxoDB)
import           Pos.Ssc.Class.Types          (Ssc)
import           Pos.Types                    (Block)

-- | Open all DBs stored on disk.
openNodeDBs :: MonadResource m => FilePath -> m (NodeDBs ssc)
openNodeDBs fp = do
    let blockPath = fp </> "blocks"
    let utxoPath = fp </> "utxo"
    let miscPath = fp </> "misc"
    mapM_ ensureDirectoryExists [blockPath, utxoPath, miscPath]
    res <- NodeDBs <$> openDB blockPath
                   <*> openDB utxoPath
                   <*> openDB miscPath
    res <$ (runDBHolder res prepareUtxoDB)
  where
    ensureDirectoryExists :: MonadIO m => FilePath -> m ()
    ensureDirectoryExists = liftIO . createDirectoryIfMissing True

getTipBlock
    :: (Ssc ssc, MonadDB ssc m, MonadThrow m)
    => m (Block ssc)
getTipBlock = maybe onFailure pure =<< getBlock =<< getTip
  where
    onFailure = throwM $ DBMalformed "there is no block corresponding to tip"

{'fraction_non_alphanumeric': 0.09604715672676838, 'fraction_numerical': 0.0010402219140083217, 'mean_word_length': 2.020942408376963, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323915', 'n_tokens_mistral': 879, 'n_tokens_neox': 847, 'n_words': 221}