   # Calls callback(error) on failure.
   # Calls callback() on success.
   @writeOp = (docName, opData, callback) ->
+    compressor = new Compressor(options['compress'], options['timing'])
     async.auto(
-      write_metadata: (cb) ->
+      compress_op: (cb) ->
+        compressor.encode(JSON.stringify(opData.op), cb)
+
+      compress_meta: (cb) ->
+        compressor.encode(JSON.stringify(opData.meta), cb)
+
+      write_metadata: ['compress_op', 'compress_meta', (cb, results) ->
         request =
           TableName: operations_table,
           Item:
             doc: { S: docName },
             v: { N: opData.v.toString() },
-            op: { S: JSON.stringify(opData.op) },
-            meta: { S: JSON.stringify(opData.meta) },
+            op: { S: results.compress_op },
+            meta: { S: results.compress_meta },
+
+        # Mark a snapshot as being compressed
+        request.Item['c'] = { S: 't' } if options['compress']
 
         operations_rw_queue.push((c) ->
           db.putItem(request, c)
         , 'write Operation('+docName+'-'+opData.v+')', cb)
-
+      ]
     (error, results) ->
       if error?
         console.error('Failed to save Operation('+docName+'-'+opData.v+'): '+util.inspect(error))

  # Calls callback(error) on failure.
  # Calls callback() on success.
  @create = (docName, docData, callback) ->
    async.auto(
      write_metadata: (cb) ->
        request =
          TableName: snapshots_table,
          Item:
            doc: { S: docName },
            v: { N: docData.v.toString() },
            meta: { S: JSON.stringify(docData.meta) },
            type: { S: docData.type },
            created_at: { N: new Date().getTime().toString() }
          Expected:
            doc:
              Exists: false

        snapshots_rw_queue.push((c) ->
          db.putItem(request, c)
        , 'write Snapshot('+docName+'-'+docData.v+')', cb)

      write_data: (cb) ->
        path = snapshots_bucket+'/'+docName+'-'+docData.v+'.snapshot'
        headers = {}
        data = JSON.stringify(docData.snapshot)

        s3_rw_queue.push((c) ->
          s3.put(path, headers, data, c)
        , 'write Snapshot('+docName+'-'+docData.v+')', cb)
    (error, results) ->
      if error?
        if error.message? and error.message.match 'The conditional request failed'

        callback?('Failed to fetch operations')
      else
        data = []
        async.map(results.get_metadata.Items, (operation, cb) ->
          compressor = new Compressor(operation.c?, options['timing'])
          async.auto(
            snapshot: (c) ->
              compressor.decode(operation.op.S, c)

            meta: (c) ->
              compressor.decode(operation.meta.S, c)

          (error, results) ->
            try
              op = JSON.parse(results.snapshot)
            catch error
              op = {}
              console.error('Failure: data was corrupt for Operation('+docName+'-'+operation.v.N+')')

            try
              meta = JSON.parse(results.meta)
            catch error
              meta = {}
              console.error('Failure: metadata was corrupt for Operation('+docName+'-'+operation.v.N+')')

            item = {
              op: op
              meta: meta
            }
            cb(null, item)
          )
        (error, results) ->
          callback? null, results
        )
    )

  # Public: Write an operation to a document.
