 #
 ###
 checkpointDocTypeRev = (docType, rev, callback) ->
-    indexer.store.set "indexedrev/#{docType}", rev, callback
+    getStatus docType, (err, status) ->
+        return callback err if err
+        status.rev = rev
+        saveStatus docType, status, callback
 
 ###*
 # Store the last indexed sequence number within the index file

                reindexChanges maxSeqNo, callback


###*
# Recursive function to reindex all docs for a given docType
# get doc in batch of FETCH_AT_ONCE_FOR_REINDEX and add them immediately
#
# Note, this function doesn't use the indexQueue used for realtime events
#
# @params docType {string} value to store
# @params definition {object} definition of fields to store
#
# @return (callback) when done
#
###
reindexDocTypeStep = (docType, definition, callback, skip = 0) ->
    if definition isnt indexdefinitions[docType]
        # a new definition has been added and a new reindexing should be
        # in progress
        log.info "aborting reindex"
        return callback new Error('abort')

    query =
        key: docType
        limit: FETCH_AT_ONCE_FOR_REINDEX
        skip: skip
        include_docs: true
        reduce: false

    db.view "doctypes/all", query, (err, rows) ->
        return callback err if err
        log.info "step #{docType} #{skip}, got #{rows.length} docs"
        status[docType].total = rows.total_rows
        return callback null if rows.length is 0

        docs = rows.toArray()

        indexer.addBatch docs, definition.ftsIndexedFields, (err) ->
            return callback err if err

            if docs.length < FETCH_AT_ONCE_FOR_REINDEX
                setImmediate callback
            else
                skip = skip + FETCH_AT_ONCE_FOR_REINDEX
                reindexDocTypeStep docType, definition, callback, skip

###*
# Reindex a given docType from the beginning
#
# @params docType {string} docType to reindex
#
# @return (callback) when done
#
###
reindexDocTypeAll = (docType, callback) ->
    db.info (err, infos) ->
        return callback err if err
        lastSeq = infos.update_seq

        definition = indexdefinitions[docType]
        reindexDocTypeStep docType, definition, (err) ->
            return callback err if err

            checkpointSeqNumber lastSeq, callback

###*
# Check if given docType definition is the same we used to index it
# if not, fire up a reindexing using reindexDocTypeAll
#
# @params docType {string} docType to test
#
# @return (callback) when done
#
###
maybeReindexDocType = (docType, callback) ->

    definition = indexdefinitions[docType]

    indexer.store.get "indexedrev/#{docType}", (err, lastrev) ->

        log.info """
            Check index revision for #{docType}
                in indexer:#{lastrev} , in ds:#{definition._rev}
        """

        if not lastrev or lastrev isnt definition._rev
            reindexDocTypeAll docType, (err) ->
                return callback err if err
                checkpointDocTypeRev docType, definition._rev, callback
        else
            setImmediate callback

###*
# [TMP] Register default indexes used by most cozy on October 2015,


            setTimeout callbackOnce.bind(null, new Error('timeout')), 10000

            initializeReindexing docType, (err) ->

                return callbackOnce err if err
                checkpointDocTypeRev docType, savedDoc.rev, callbackOnce

    else
        log.info "rev is different, but definition not changed"
        checkpointDocTypeRev docType, savedDoc.rev, callbackOnce

###*
# Store the indexdefintion rev within the index file
