Skip to content

Commit

Permalink
Attempt at caching the entire database. Something is going wrong with…
Browse files Browse the repository at this point in the history
… the re-load. DocPad complains about null outPath conflicts.
  • Loading branch information
balupton committed Nov 24, 2013
1 parent b8d3313 commit 80d4305
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -15,4 +15,5 @@ out/
# =====================================
# PROJECT SPECIFIC FILES

test/render-out/
test/render-out/
test/.docpad.db
25 changes: 22 additions & 3 deletions src/lib/docpad.coffee
Expand Up @@ -782,6 +782,9 @@ class DocPad extends EventEmitterGrouped
# The project directory
rootPath: process.cwd()

# The project's database path
databasePath: '.docpad.db'

# The project's package.json path
packagePath: 'package.json'

Expand Down Expand Up @@ -1429,6 +1432,8 @@ class DocPad extends EventEmitterGrouped
@config.rootPath = pathUtil.resolve(@config.rootPath)
@config.outPath = pathUtil.resolve(@config.rootPath, @config.outPath)
@config.srcPath = pathUtil.resolve(@config.rootPath, @config.srcPath)
@config.databasePath = pathUtil.resolve(@config.rootPath, @config.databasePath)
@config.packagePath = pathUtil.resolve(@config.rootPath, @config.packagePath)

# Resolve Documents, Files, Layouts paths
for type in ['documents','files','layouts']
Expand Down Expand Up @@ -3236,19 +3241,20 @@ class DocPad extends EventEmitterGrouped
docpad = @
config = docpad.getConfig()
locale = docpad.getLocale()
lastGenerateStarted = docpad.generateStarted
database = docpad.getDatabase()

# Check
return next() if opts.collection?.length is 0


# Update generating flag
lastGenerateStarted = docpad.generateStarted
docpad.generateStarted = new Date()
docpad.generateEnded = null
docpad.generating = true

# Update the cached database
docpad.databaseCache = new FilesCollection(database.models)
docpad.databaseCache = new FilesCollection(database.models) if database.models.length

# Destroy Regenerate Timer
docpad.destroyRegenerateTimer()
Expand Down Expand Up @@ -3304,14 +3310,21 @@ class DocPad extends EventEmitterGrouped
docpad.destroyProgress(opts.progress)
opts.progress = null

# Dump the database
databaseCache =
generateStarted: docpad.generateStarted
generateEnded: docpad.generateEnded
models: docpad.getDatabase().toJSON()
require('fs').writeFileSync(config.databasePath, JSON.stringify(databaseCache, null, ' '))

# Prepare
seconds = (docpad.generateEnded - docpad.generateStarted) / 1000
howMany = "#{opts.collection?.length or 0}/#{database.length}"

# Log
opts.progress?.finish()
docpad.log 'info', util.format(locale.renderGenerated, howMany, seconds)
docpad.notify (new Date()).toLocaleTimeString(), title: locale.renderGeneratedNotification
docpad.notify (new Date()).toLocaleTimeString(), {title: locale.renderGeneratedNotification}

# Forward
return next(err)
Expand Down Expand Up @@ -3346,6 +3359,12 @@ class DocPad extends EventEmitterGrouped
addTask 'Reset our collections', (complete) ->
docpad.resetCollections(opts, complete)

if require('fs').existsSync(config.databasePath)
console.log 'Loading cached database'
databaseData = JSON.parse require('fs').readFileSync(config.databasePath)
lastGenerateStarted = databaseData.generateStarted
database.add(databaseData.models)
console.log 'Added', databaseData.models.length

# Do we want to pull in new data?
if opts.reset is true
Expand Down

0 comments on commit 80d4305

Please sign in to comment.