public void AddAttachment(string key, Guid?etag, byte[] data, JObject headers) { var attachmentPosition = Mutator.Attachments.FindValue(key); headers["@etag"] = new JValue(DocumentDatabase.CreateSequentialUuid().ToString()); if (attachmentPosition == null) { Mutator.IncrementAttachmentCount(); } else { EnsureValidEtag(key, attachmentPosition.Value, etag); } var position = Writer.Position; headers.WriteTo(new BsonWriter(Writer)); BinaryWriter.Write7BitEncodedInt(data.Length); Writer.Write(data, 0, data.Length); Mutator.Attachments.Add(key, position); logger.DebugFormat("Adding attachment {0}", key); }