Program: 33938
Project: 33938
Buggy source:
@Override public ResultDocument getSourceDocument(String uniqueId,FetchType fetchType) throws Exception {
  if (!FetchType.NONE.equals(fetchType)) {
    DB db=pool.getDB(database);
    DBCollection coll=db.getCollection(RESULT_STORAGE_COLLECTION);
    DBObject search=new BasicDBObject(MongoConstants.StandardFields._ID,uniqueId);
    DBObject result=coll.findOne(search);
    if (null != result) {
      boolean compressed=(boolean)result.removeField(COMPRESSED);
      long timestamp=(long)result.removeField(TIMESTAMP);
      ResultDocument.Type type=ResultDocument.Type.valueOf((String)result.removeField(TYPE));
      ResultDocument.Builder dBuilder=ResultDocument.newBuilder();
      dBuilder.setType(type);
      dBuilder.setCompressed(compressed);
      dBuilder.setUniqueId(uniqueId);
      dBuilder.setTimestamp(timestamp);
      if (result.containsField(METADATA)) {
        DBObject metadata=(DBObject)result.removeField(METADATA);
        for (        String key : metadata.keySet()) {
          dBuilder.addMetadata(Metadata.newBuilder().setKey(key).setValue((String)metadata.get(VALUE)).build());
        }
      }
      if (FetchType.FULL.equals(fetchType)) {
        ByteString document=null;
        if (!compressed && type.equals(ResultDocument.Type.BSON)) {
          document=ByteString.copyFrom(BSON.encode(result));
        }
 else         if (!compressed && type.equals(ResultDocument.Type.TEXT)) {
          document=ByteString.copyFrom(((String)result.get(DOC)).getBytes(UTF_8_CHARSET));
        }
 else {
          byte[] bytes=(byte[])result.get(DOC);
          if (compressed) {
            bytes=CommonCompression.uncompressZlib(bytes);
          }
          if (type.equals(ResultDocument.Type.BSON)) {
            BSONObject object=BSON.decode(bytes);
            object.put(MongoConstants.StandardFields._ID,uniqueId);
            document=ByteString.copyFrom(BSON.encode(object));
          }
 else {
            document=ByteString.copyFrom(bytes);
          }
        }
        dBuilder.setDocument(document);
      }
      return dBuilder.build();
    }
  }
  return null;
}

Fixed version (ground truth):
@Override public ResultDocument getSourceDocument(String uniqueId,FetchType fetchType) throws Exception {
  if (!FetchType.NONE.equals(fetchType)) {
    DB db=pool.getDB(database);
    DBCollection coll=db.getCollection(RESULT_STORAGE_COLLECTION);
    DBObject search=new BasicDBObject(MongoConstants.StandardFields._ID,uniqueId);
    DBObject result=coll.findOne(search);
    if (null != result) {
      boolean compressed=(boolean)result.removeField(COMPRESSED);
      long timestamp=(long)result.removeField(TIMESTAMP);
      ResultDocument.Type type=ResultDocument.Type.valueOf((String)result.removeField(TYPE));
      ResultDocument.Builder dBuilder=ResultDocument.newBuilder();
      dBuilder.setType(type);
      dBuilder.setCompressed(compressed);
      dBuilder.setUniqueId(uniqueId);
      dBuilder.setTimestamp(timestamp);
      if (result.containsField(METADATA)) {
        DBObject metadata=(DBObject)result.removeField(METADATA);
        for (        String key : metadata.keySet()) {
          dBuilder.addMetadata(Metadata.newBuilder().setKey(key).setValue((String)metadata.get(key)));
        }
      }
      if (FetchType.FULL.equals(fetchType)) {
        ByteString document=null;
        if (!compressed && type.equals(ResultDocument.Type.BSON)) {
          document=ByteString.copyFrom(BSON.encode(result));
        }
 else         if (!compressed && type.equals(ResultDocument.Type.TEXT)) {
          document=ByteString.copyFrom(((String)result.get(DOC)).getBytes(UTF_8_CHARSET));
        }
 else {
          byte[] bytes=(byte[])result.get(DOC);
          if (compressed) {
            bytes=CommonCompression.uncompressZlib(bytes);
          }
          if (type.equals(ResultDocument.Type.BSON)) {
            BSONObject object=BSON.decode(bytes);
            object.put(MongoConstants.StandardFields._ID,uniqueId);
            document=ByteString.copyFrom(BSON.encode(object));
          }
 else {
            document=ByteString.copyFrom(bytes);
          }
        }
        dBuilder.setDocument(document);
      }
      return dBuilder.build();
    }
  }
  return null;
}