Example #1
0
			uint64_t flush()
			{
				/* flush, compress */
				BgzfDeflateZStreamBaseFlushInfo BDZSBFI = base_type::flush(flushmode);
								
				/* write blocks */
				streamWrite(inbuf.begin(), outbuf.begin(), BDZSBFI);

				if ( flushmode )
				{
					assert ( ! BDZSBFI.movesize );
				}
				else
				{
					if ( BDZSBFI.movesize )
						BgzfDeflateInputBufferBase::pc = BDZSBFI.moveUncompressedRest();
					else
						BgzfDeflateInputBufferBase::pc = BgzfDeflateInputBufferBase::pa;
				}

				/* return number of compressed bytes written */
				return BDZSBFI.getCompressedSize();
			}
			void * run()
			{
				while ( true )
				{
					/* get any id from global list */
					BgzfThreadQueueElement defglob;
					try
					{
						defglob = deflatecontext.deflategloblist.deque();
					}
					catch(std::exception const & ex)
					{
						/* queue is terminated, break loop */
						break;
					}

					libmaus2_lz_bgzf_op_type op = defglob.op;
					uint64_t objectid = 0;

					{
						libmaus2::parallel::ScopePosixMutex S(deflatecontext.deflateqlock);

						switch ( op )
						{
							case libmaus2_lz_bgzf_op_compress_block:
								objectid = deflatecontext.deflatecompqueue.front();
								deflatecontext.deflatecompqueue.pop_front();
								break;
							case libmaus2_lz_bgzf_op_write_block:
							{
								BgzfThreadQueueElement const btqe = deflatecontext.deflatewritequeue.deque();
								objectid = btqe.objectid;
								libmaus2::parallel::ScopePosixMutex O(deflatecontext.deflateoutlock);
								assert ( deflatecontext.deflateB[objectid]->blockid == deflatecontext.deflatenextwriteid );
								break;
							}
							default:
								break;
						}

						assert ( op != libmaus2_lz_bgzf_op_none );
					}

					switch ( op )
					{
						case libmaus2_lz_bgzf_op_compress_block:
						{
							try
							{
								BgzfDeflateZStreamBaseFlushInfo const BDZSBFI = deflatecontext.deflateB[objectid]->flush(true /* full flush */);
								assert ( !BDZSBFI.movesize );
								deflatecontext.deflateB[objectid]->compsize = BDZSBFI.getCompressedSize();
								deflatecontext.deflateB[objectid]->flushinfo = BDZSBFI;
							}
							catch(libmaus2::exception::LibMausException const & ex)
							{
								libmaus2::parallel::ScopePosixMutex S(deflatecontext.deflateexlock);

								if ( deflatecontext.deflateB[objectid]->blockid * 2 + 0 < deflatecontext.deflateexceptionid )
								{
									deflatecontext.deflateexceptionid = deflatecontext.deflateB[objectid]->blockid * 2 + 0;
									libmaus2::exception::LibMausException::unique_ptr_type tex(ex.uclone());
									deflatecontext.deflatepse = UNIQUE_PTR_MOVE(tex);
								}

								deflatecontext.deflateB[objectid]->compsize = 0;
								deflatecontext.deflateB[objectid]->flushinfo = BgzfDeflateZStreamBaseFlushInfo();
							}
							catch(std::exception const & ex)
							{
								libmaus2::parallel::ScopePosixMutex S(deflatecontext.deflateexlock);

								if ( deflatecontext.deflateB[objectid]->blockid * 2 + 0 < deflatecontext.deflateexceptionid )
								{
									deflatecontext.deflateexceptionid = deflatecontext.deflateB[objectid]->blockid * 2 + 0;

									libmaus2::exception::LibMausException se;
									se.getStream() << ex.what() << std::endl;
									se.finish();

									libmaus2::exception::LibMausException::unique_ptr_type tex(se.uclone());
									deflatecontext.deflatepse = UNIQUE_PTR_MOVE(tex);
								}

								deflatecontext.deflateB[objectid]->compsize = 0;
								deflatecontext.deflateB[objectid]->flushinfo = BgzfDeflateZStreamBaseFlushInfo();
							}

							libmaus2::parallel::ScopePosixMutex S(deflatecontext.deflateqlock);
							deflatecontext.deflatewritequeue.enque(
								BgzfThreadQueueElement(
									libmaus2::lz::BgzfThreadOpBase::libmaus2_lz_bgzf_op_write_block,
									objectid,
									deflatecontext.deflateB[objectid]->blockid
								),
								&(deflatecontext.deflategloblist)
							);
							break;
						}
						case libmaus2_lz_bgzf_op_write_block:
						{
							libmaus2::parallel::ScopePosixMutex O(deflatecontext.deflateoutlock);
							try
							{
								#if 0
								deflatecontext.deflateout.write(
									reinterpret_cast<char const *>(deflatecontext.deflateB[objectid]->outbuf.begin()),
									deflatecontext.deflateB[objectid]->compsize
								);
								#endif
								deflatecontext.streamWrite(
									deflatecontext.deflateB[objectid]->inbuf.begin(),
									deflatecontext.deflateB[objectid]->outbuf.begin(),
									deflatecontext.deflateB[objectid]->flushinfo
								);

								deflatecontext.deflateoutbytes += deflatecontext.deflateB[objectid]->compsize;

								#if 0
								std::cerr << "compsize "
									<< deflatecontext.deflateB[objectid]->compsize
									<< " outbytes "
									<< deflatecontext.deflateoutbytes
									<< std::endl;
								#endif

								#if 0 // this test is done in streamWrite()
								if ( ! deflatecontext.deflateout )
								{
									libmaus2::exception::LibMausException se;
									se.getStream() << "BgzfDeflateParallel: output error on output stream." << std::endl;
									se.finish();
									throw se;
								}
								#endif

								if ( deflatecontext.deflateindexstr )
								{
									if ( deflatecontext.deflateB[objectid]->flushinfo.blocks == 0 )
									{

									}
									else if ( deflatecontext.deflateB[objectid]->flushinfo.blocks == 1 )
									{
										libmaus2::util::UTF8::encodeUTF8(deflatecontext.deflateB[objectid]->flushinfo.block_a_u,*(deflatecontext.deflateindexstr));
										libmaus2::util::UTF8::encodeUTF8(deflatecontext.deflateB[objectid]->flushinfo.block_a_c,*(deflatecontext.deflateindexstr));
									}
									else if ( deflatecontext.deflateB[objectid]->flushinfo.blocks == 2 )
									{
										libmaus2::util::UTF8::encodeUTF8(deflatecontext.deflateB[objectid]->flushinfo.block_a_u,*(deflatecontext.deflateindexstr));
										libmaus2::util::UTF8::encodeUTF8(deflatecontext.deflateB[objectid]->flushinfo.block_a_c,*(deflatecontext.deflateindexstr));
										libmaus2::util::UTF8::encodeUTF8(deflatecontext.deflateB[objectid]->flushinfo.block_b_u,*(deflatecontext.deflateindexstr));
										libmaus2::util::UTF8::encodeUTF8(deflatecontext.deflateB[objectid]->flushinfo.block_b_c,*(deflatecontext.deflateindexstr));
									}
								}
							}
							catch(libmaus2::exception::LibMausException const & ex)
							{
								libmaus2::parallel::ScopePosixMutex S(deflatecontext.deflateexlock);

								if ( deflatecontext.deflateB[objectid]->blockid * 2 + 1 < deflatecontext.deflateexceptionid )
								{
									deflatecontext.deflateexceptionid = deflatecontext.deflateB[objectid]->blockid * 2 + 1;
									libmaus2::exception::LibMausException::unique_ptr_type tex(ex.uclone());
									deflatecontext.deflatepse = UNIQUE_PTR_MOVE(tex);
								}
							}
							catch(std::exception const & ex)
							{
								libmaus2::parallel::ScopePosixMutex S(deflatecontext.deflateexlock);

								if ( deflatecontext.deflateB[objectid]->blockid * 2 + 1 < deflatecontext.deflateexceptionid )
								{
									deflatecontext.deflateexceptionid = deflatecontext.deflateB[objectid]->blockid * 2 + 1;

									libmaus2::exception::LibMausException se;
									se.getStream() << ex.what() << std::endl;
									se.finish();

									libmaus2::exception::LibMausException::unique_ptr_type tex(se.uclone());

									deflatecontext.deflatepse = UNIQUE_PTR_MOVE(tex);
								}
							}

							deflatecontext.deflatenextwriteid += 1;
							deflatecontext.deflatewritequeue.setReadyFor(
								BgzfThreadQueueElement(
									libmaus2::lz::BgzfThreadOpBase::libmaus2_lz_bgzf_op_write_block,
									objectid,
									deflatecontext.deflatenextwriteid
									),
								&(deflatecontext.deflategloblist)
							);
							deflatecontext.deflatefreelist.enque(objectid);
							break;
						}
						default:
						{
							break;
						}
					}
				}

				return 0;
			}