Permalink
Browse files

using _id to store docName in docs collection; removing some indexes

  • Loading branch information...
1 parent 75b1382 commit 0e161e886e8444c301349ac00fa101d69c2e113d @zamfi zamfi committed Aug 2, 2012
Showing with 64 additions and 87 deletions.
  1. +64 −87 src/server/db/mongo.coffee
View
@@ -3,8 +3,8 @@
#
# The mongodb database contains two collections:
#
-# - 'docs': document snapshots with 'docName' set to document name.
-# - 'ops': document ops with 'docName' defined and version stored in 'v'.
+# - 'docs': document snapshots, id set to document name.
+# - 'ops': document ops with docName stored in 'd' and version stored in 'v'.
#
# This implementation isn't written to support multiple frontends
# talking to a single mongo backend.
@@ -29,67 +29,52 @@ module.exports = MongoDb = (options) ->
client = new mongodb.Db(options.db, new mongodb.Server(options.hostname, options.port, options.mongoOptions))
- checked = false
- checkIndex = (callback) ->
- return callback?() if checked
-
- client.ensureIndex 'docs', 'docName', {unique: true}, (err1, name) ->
- console.warn "failed to ensure mongo index on docs collection: #{err1}" if err1
-
- client.ensureIndex 'ops', {docName: 1, v: 1}, {unique: true}, (err2, name) ->
- console.warn "failed to ensure mongo index on ops collection: #{err2}" if err2
-
- checked = true if not err1 and not err2
- callback?(err1 or err2 or null)
-
+ client.ensureIndex 'ops', {docName: 1, v: 1}, {unique: true}, (err, name) ->
+ console.warn "failed to ensure mongo index on ops collection: #{err}\nperformance may suffer" if err
# Creates a new document.
# data = {snapshot, type:typename, [meta]}
@create = (docName, data, callback) ->
- checkIndex (err) ->
+ client.collection 'docs', (err, collection) ->
return callback? err if err
- client.collection 'docs', (err, collection) ->
- return callback? err if err
- doc =
- docName: docName
- data: data
+ doc =
+ _id: docName
+ data: data
- collection.insert doc, safe: true, (err, doc) ->
- if err?.code == 11000
- return callback? "Document already exists"
+ collection.insert doc, safe: true, (err, doc) ->
+ if err?.code == 11000
+ return callback? "Document already exists"
- console.warn "failed to create new doc: #{err}" if err
- return callback? err if err
+ console.warn "failed to create new doc: #{err}" if err
+ return callback? err if err
- callback?()
+ callback?()
# Get all ops with version = start to version = end, noninclusive.
# end is trimmed to the size of the document.
# If any documents are passed to the callback, the first one has v = start
# end can be null. If so, returns all documents from start onwards.
# Each document returned is in the form {op:o, meta:m, v:version}.
@getOps = (docName, start, end, callback) ->
- checkIndex (err) ->
- return callback? err if err
- if start == end
- callback null, []
- return
+ if start == end
+ callback null, []
+ return
- client.collection 'ops', (err, collection) ->
- return callback? err if err
+ client.collection 'ops', (err, collection) ->
+ return callback? err if err
- query =
- docName: docName
- v:
- $gte: start
- query.v.$lt = end if end
+ query =
+ docName: docName
+ v:
+ $gte: start
+ query.v.$lt = end if end
- collection.find(query).sort('v').toArray (err, docs) ->
- console.warn "failed to get ops for #{docName}: #{err}" if err
- return callback? err if err
+ collection.find(query).sort('v').toArray (err, docs) ->
+ console.warn "failed to get ops for #{docName}: #{err}" if err
+ return callback? err if err
- callback? null, (doc.opData for doc in docs)
+ callback? null, (doc.opData for doc in docs)
# Write an op to a document.
#
@@ -103,23 +88,21 @@ module.exports = MongoDb = (options) ->
# ... and that would slow it down a bit.)
@writeOp = (docName, opData, callback) ->
# ****** NOT SAFE FOR MULTIPLE PROCESSES. Rewrite me using transactions or something.
- checkIndex (err) ->
+ client.collection 'ops', (err, collection) ->
return callback? err if err
- client.collection 'ops', (err, collection) ->
- return callback? err if err
- doc =
- docName: docName
- opData:
- op: opData.op
- meta: opData.meta
- v: opData.v
+ doc =
+ docName: docName
+ opData:
+ op: opData.op
+ meta: opData.meta
+ v: opData.v
- collection.insert doc, safe: true, (err, doc) ->
- console.warn "failed to save op #{opData} for #{docName}: #{err}" if err
- return callback? err if err
+ collection.insert doc, safe: true, (err, doc) ->
+ console.warn "failed to save op #{opData} for #{docName}: #{err}" if err
+ return callback? err if err
- callback? null, doc
+ callback? null, doc
# Write new snapshot data to the database.
#
@@ -129,51 +112,45 @@ module.exports = MongoDb = (options) ->
#
# This function has UNDEFINED BEHAVIOUR if you call append before calling create().
@writeSnapshot = (docName, data, dbMeta, callback) ->
- checkIndex (err) ->
+ client.collection 'docs', (err, collection) ->
return callback? err if err
- client.collection 'docs', (err, collection) ->
- return callback? err if err
- doc =
- docName: docName
- data: data
+ doc =
+ _id: docName
+ data: data
- collection.update {docName: docName}, doc, safe: true, (err, doc) ->
- console.warn "failed to save snapshot for doc #{docName}: #{err}" if err
- return callback? err if err
+ collection.update {_id: docName}, doc, safe: true, (err, doc) ->
+ console.warn "failed to save snapshot for doc #{docName}: #{err}" if err
+ return callback? err if err
- callback?()
+ callback?()
# Data = {v, snapshot, type}. Error if the document doesn't exist.
@getSnapshot = (docName, callback) ->
- checkIndex (err) ->
+ client.collection 'docs', (err, collection) ->
return callback? err if err
- client.collection 'docs', (err, collection) ->
- return callback? err if err
- collection.findOne { docName: docName }, (err, doc) ->
- return callback? err if err
- if doc != null
- callback null, doc.data
- else
- callback "Document does not exist"
+ collection.findOne { _id: docName }, (err, doc) ->
+ return callback? err if err
+ if doc != null
+ callback null, doc.data
+ else
+ callback "Document does not exist"
# Permanently deletes a document. There is no undo.
@delete = (docName, dbMeta, callback) ->
- checkIndex (err) ->
- return callback? err if err
- client.collection 'ops', (err, collection) ->
- collection.remove { docName: docName }
+ client.collection 'ops', (err, collection) ->
+ collection.remove { docName: docName }
- client.collection 'docs', (err, collection) ->
- return callback? err if err
+ client.collection 'docs', (err, collection) ->
+ return callback? err if err
- collection.remove { docName: docName }, safe: true, (err, count) ->
- return callback? err if err
- if count == 0
- callback? "Document does not exist"
- else
- callback? null
+ collection.remove { _id: docName }, safe: true, (err, count) ->
+ return callback? err if err
+ if count == 0
+ callback? "Document does not exist"
+ else
+ callback? null
# Close the connection to the database
@close = ->

0 comments on commit 0e161e8

Please sign in to comment.