void TransRec::optimizeForMemory() { // Dump large annotations to disk. for (int i = 0 ; i < annotations.size(); ++i) { auto& annotation = annotations[i]; if (annotation.second.find_first_of('\n') != std::string::npos || annotation.second.size() > 72) { auto saved = writeAnnotation(annotation, /* compress */ true); if (saved.length > 0) { // Strip directory name from the file name. size_t pos = saved.fileName.find_last_of('/'); if (pos != std::string::npos) { saved.fileName = saved.fileName.substr(pos+1); } auto newAnnotation = folly::sformat( "file:{}:{}:{}", saved.fileName, saved.offset, saved.length); std::swap(annotation.second, newAnnotation); } else { annotation.second = "<unknown: write failed>"; } } } }
void JsonTextWriter::writeToStream( const Text & text, std::ostream & os ) { os << "{\n\tconfig: "; writeConfig( text.getConfig(), os ); os << ",\n\tcontent: '" << text.getContent() << "',\n\tnodes: [\n\t\t"; uint nodesCount = text.getNodes().size(); if ( nodesCount > 0 ) { writeNode( *text.getNodes().at( 0 ), os ); for ( uint i = 1; i < nodesCount; ++ i ) { os << ",\n\t\t"; writeNode( *text.getNodes().at( i ), os ); } } os << "\n\t],\n\tannotations: [\n\t\t"; for ( uint i = 0; i < nodesCount; ++ i ) { const Node & node = *text.getNodes().at( i ); for ( uint j = 0; j < node.getTransitionCount(); ++ j ) { if ( i != 0 || j != 0 ) os << ",\n\t\t"; writeAnnotation( *node.getTransition( j ), os ); } } os << "\n\t]\n}"; }