Skip to content

Commit 7c0b12f

Browse files
blahgeekfrasertweedale
authored andcommitted
Add databaseRevision API
1 parent b2c1c83 commit 7c0b12f

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

notmuch.cabal

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ executable hs-notmuch-files
102102
build-depends:
103103
, notmuch
104104

105+
executable hs-notmuch-dbinfo
106+
import: common
107+
if !flag(demos)
108+
buildable: False
109+
hs-source-dirs: tools
110+
main-is: DbInfo.hs
111+
build-depends:
112+
, notmuch
113+
105114
executable hs-notmuch-tag-message
106115
import: common
107116
if !flag(demos)

src/Notmuch.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module Notmuch
7272
, databaseOpenReadOnly
7373
, databasePath
7474
, databaseVersion
75+
, databaseRevision
7576
, Database
7677
-- ** Database modes
7778
, Mode
@@ -146,6 +147,7 @@ import Control.Exception (bracket)
146147
import Control.Monad.Except (MonadError(..))
147148
import Control.Monad.IO.Class (MonadIO(..))
148149
import Data.Foldable (traverse_)
150+
import Foreign.C.Types (CULong)
149151
import GHC.Generics (Generic)
150152

151153
import Control.DeepSeq (NFData)
@@ -240,6 +242,17 @@ databaseOpenReadOnly = database_open
240242
databaseVersion :: MonadIO m => Database a -> m Int
241243
databaseVersion = liftIO . database_get_version
242244

245+
-- | Get the revision and UUID of the database.
246+
--
247+
-- The revision number increases monotonically with each commit to
248+
-- the database (although rollover is possible). The "UUID" is an
249+
-- __opaque__ string that persists until e.g. database compaction.
250+
-- Revision numbers are only comparable where the UUID strings are
251+
-- equal.
252+
--
253+
databaseRevision :: MonadIO m => Database a -> m (CULong, String)
254+
databaseRevision = liftIO . database_get_revision
255+
243256
-- | Look for a particular message in the database.
244257
findMessage
245258
:: (AsNotmuchError e, MonadError e m, MonadIO m)

src/Notmuch/Binding.chs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ database_get_version :: Database a -> IO Int
251251
database_get_version db =
252252
fromIntegral <$> withDatabase db {#call unsafe database_get_version #}
253253

254+
database_get_revision :: Database a -> IO (CULong, String)
255+
database_get_revision db =
256+
withDatabase db $ \db' ->
257+
alloca $ \uuidPtr -> do
258+
rev <- {#call unsafe database_get_revision #} db' uuidPtr
259+
uuid <- peekCString =<< peek uuidPtr
260+
pure (rev, uuid)
261+
254262
-- | Index a file with the default indexing options.
255263
-- (This binding does not yet provide a way to change
256264
-- the indexing options.) Returns the indexed message.

tools/DbInfo.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- This file is part of hs-notmuch - Haskell Notmuch binding
2+
-- Copyright (C) 2022 Yikai Zhao
3+
--
4+
-- hs-notmuch is free software: you can redistribute it and/or modify
5+
-- it under the terms of the GNU General Public License as published by
6+
-- the Free Software Foundation, either version 3 of the License, or
7+
-- (at your option) any later version.
8+
--
9+
-- This program is distributed in the hope that it will be useful,
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
-- GNU General Public License for more details.
13+
--
14+
-- You should have received a copy of the GNU General Public License
15+
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
{-
18+
19+
Print version and revision info for a database.
20+
21+
-}
22+
23+
{-# LANGUAGE LambdaCase #-}
24+
25+
import Control.Monad.Except (runExceptT)
26+
import System.Environment (getArgs)
27+
import System.Exit (die)
28+
29+
import Notmuch
30+
31+
32+
main :: IO ()
33+
main = getArgs >>= \case
34+
[dbDir] -> go dbDir
35+
_ -> putStrLn "usage: hs-notmuch-dbinfo DB-DIR"
36+
37+
go :: String -> IO ()
38+
go dbDir = do
39+
(version, (revision, uuid)) <- runExceptT (
40+
do
41+
db <- databaseOpenReadOnly dbDir
42+
(,) <$> databaseVersion db <*> databaseRevision db
43+
) >>= either (die . (show :: Status -> String)) pure
44+
putStr "Path: " *> putStrLn dbDir
45+
putStr "Version: " *> print version
46+
putStr "Revision: " *> print revision
47+
putStr "UUID: " *> putStrLn uuid

0 commit comments

Comments
 (0)