module Hunt.Interpreter.BasicCommand
( BasicCommand (..)
, StatusCmd (..)
)
where
import Control.Applicative
import Control.Monad (mzero)
import Data.Aeson
import qualified Data.Aeson as JS (Value (..))
import Data.Set (Set)
import Data.Text (Text)
import Hunt.Common.ApiDocument
import Hunt.Common.BasicTypes
import Hunt.Index.Schema
import Hunt.Query.Language.Grammar (Query (..))
import Hunt.Utility.Log
data BasicCommand
= Search { icQuery :: Query
, icOffsetSR :: Int
, icMaxSR :: Int
, icWeight :: Bool
, icFields :: Maybe [Text]
}
| Completion { icPrefixCR :: Query
, icMaxCR :: Int
}
| Select { icQuery :: Query }
| InsertList { icDocs :: [ApiDocument] }
| Update { icDoc :: ApiDocument }
| DeleteDocs { icUris :: Set URI }
| DeleteByQuery { icQueryD :: Query }
| InsertContext { icICon :: Context
, icSchema :: ContextSchema
}
| DeleteContext { icDCon :: Context }
| LoadIx { icPath :: FilePath }
| StoreIx { icPath :: FilePath }
| Status { icStatus :: StatusCmd }
| Sequence { icCmdSeq :: [BasicCommand] }
| NOOP
deriving (Show)
data StatusCmd
= StatusGC
| StatusDocTable
| StatusIndex
| StatusContext Context
deriving (Show)
instance ToJSON StatusCmd where
toJSON StatusGC = JS.String "gc"
toJSON StatusDocTable = JS.String "doctable"
toJSON StatusIndex = JS.String "index"
toJSON (StatusContext c) = object ["context" .= c]
instance FromJSON StatusCmd where
parseJSON (Object o) = StatusContext <$> o .: "context"
parseJSON (JS.String "gc" ) = return StatusGC
parseJSON (JS.String "doctable") = return StatusDocTable
parseJSON (JS.String "index" ) = return StatusIndex
parseJSON _ = mzero
instance LogShow BasicCommand where
logShow (InsertList docs) = "InsertList " ++ show (map adUri docs)
logShow (Update doc) = "Update {icDoc = " ++ logShow doc ++ "\", ..}"
logShow (Sequence _) = "Sequence"
logShow o = show o