def _clean_doc(self, doc, namespace, timestamp):
<EOL>if '<STR_LIT>' in doc:<EOL><INDENT>doc[self.unique_key] = u(doc.pop("<STR_LIT>"))<EOL><DEDENT>if '<STR_LIT>' in doc or '<STR_LIT>' in doc:<EOL><INDENT>raise errors.OperationFailed(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % doc)<EOL><DEDENT>doc['<STR_LIT>'] = namespace<EOL>doc['<STR_LIT>'] = timestamp<EOL>flat_doc = self._formatter.format_document(doc)<EOL>if len(self.field_list) + len(self._dynamic_field_regexes) > <NUM_LIT:0>:<EOL><INDENT>def include_field(field):<EOL><INDENT>return field in self.field_list or any(<EOL>regex.match(field) for regex in self._dynamic_field_regexes<EOL>)<EOL><DEDENT>return dict((k, v) for k, v in flat_doc.items() if include_field(k))<EOL><DEDENT>return flat_doc<EOL>
Reformats the given document before insertion into Solr.

        This method reformats the document in the following ways:
          - removes extraneous fields that aren't defined in schema.xml
          - unwinds arrays in order to find and later flatten sub-documents
          - flattens the document so that there are no sub-documents, and every
            value is associated with its dot-separated path of keys
          - inserts namespace and timestamp metadata into the document in order
            to handle rollbacks

        An example:
          {"a": 2,
           "b": {
             "c": {
               "d": 5
             }
           },
           "e": [6, 7, 8]
          }

        becomes:
          {"a": 2, "b.c.d": 5, "e.0": 6, "e.1": 7, "e.2": 8}