arcwriterprocessor.html

来自「网络爬虫开源代码」· HTML 代码 · 共 208 行 · 第 1/2 页

HTML
208
字号
<a name="96" href="#96">96</a>      	<strong>return</strong> DEFAULT_PATH;<a name="97" href="#97">97</a>  	}<a name="98" href="#98">98</a>  <a name="99" href="#99">99</a>      <strong>protected</strong> <strong>void</strong> setupPool(<strong>final</strong> AtomicInteger serialNo) {<a name="100" href="#100">100</a> 		setPool(<strong>new</strong> <a href="../../../../org/archive/io/arc/ARCWriterPool.html">ARCWriterPool</a>(serialNo, <strong>this</strong>, getPoolMaximumActive(),<a name="101" href="#101">101</a>             getPoolMaximumWait()));<a name="102" href="#102">102</a>     }<a name="103" href="#103">103</a>     <a name="104" href="#104">104</a>     <em>/**<em>*</em></em><a name="105" href="#105">105</a> <em>     * Writes a CrawlURI and its associated data to store file.</em><a name="106" href="#106">106</a> <em>     *</em><a name="107" href="#107">107</a> <em>     * Currently this method understands the following uri types: dns, http, </em><a name="108" href="#108">108</a> <em>     * and https.</em><a name="109" href="#109">109</a> <em>     *</em><a name="110" href="#110">110</a> <em>     * @param curi CrawlURI to process.</em><a name="111" href="#111">111</a> <em>     */</em><a name="112" href="#112">112</a>     <strong>protected</strong> <strong>void</strong> innerProcess(<a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi) {<a name="113" href="#113">113</a>         <em class="comment">// If failure, or we haven't fetched the resource yet, return</em><a name="114" href="#114">114</a>         <strong>if</strong> (curi.getFetchStatus() &lt;= 0) {<a name="115" href="#115">115</a>             <strong>return</strong>;<a name="116" href="#116">116</a>         }<a name="117" href="#117">117</a>         <a name="118" href="#118">118</a>         <em class="comment">// If no recorded content at all, don't write record.</em><a name="119" href="#119">119</a>         <strong>long</strong> recordLength = curi.getContentSize();<a name="120" href="#120">120</a>         <strong>if</strong> (recordLength &lt;= 0) {<a name="121" href="#121">121</a>         	<em class="comment">// getContentSize() should be > 0 if any material (even just</em><a name="122" href="#122">122</a>             <em class="comment">// HTTP headers with zero-length body) is available. </em><a name="123" href="#123">123</a>         	<strong>return</strong>;<a name="124" href="#124">124</a>         }<a name="125" href="#125">125</a>         <a name="126" href="#126">126</a>         <a name="127" href="#127">127</a>         <strong>try</strong> {<a name="128" href="#128">128</a>             <strong>if</strong>(shouldWrite(curi)) {<a name="129" href="#129">129</a>                 InputStream is = curi.getHttpRecorder().getRecordedInput().<a name="130" href="#130">130</a>                 getReplayInputStream();<a name="131" href="#131">131</a>                 write(curi, recordLength, is, getHostAddress(curi));<a name="132" href="#132">132</a>             } <strong>else</strong> {<a name="133" href="#133">133</a>                 logger.info(<span class="string">"does not write "</span> + curi.toString());<a name="134" href="#134">134</a>             }<a name="135" href="#135">135</a>         } <strong>catch</strong> (IOException e) {<a name="136" href="#136">136</a>             curi.addLocalizedError(<strong>this</strong>.getName(), e, <span class="string">"WriteRecord: "</span> +<a name="137" href="#137">137</a>                 curi.toString());<a name="138" href="#138">138</a>             logger.log(Level.SEVERE, <span class="string">"Failed write of Record: "</span> +<a name="139" href="#139">139</a>                 curi.toString(), e);<a name="140" href="#140">140</a>         }<a name="141" href="#141">141</a>     }<a name="142" href="#142">142</a>     <a name="143" href="#143">143</a>     <strong>protected</strong> <strong>void</strong> write(<a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi, <strong>long</strong> recordLength, InputStream in,<a name="144" href="#144">144</a>         String ip)<a name="145" href="#145">145</a>     throws IOException {<a name="146" href="#146">146</a>         <a href="../../../../org/archive/io/WriterPoolMember.html">WriterPoolMember</a> writer = getPool().borrowFile();<a name="147" href="#147">147</a>         <strong>long</strong> position = writer.getPosition();<a name="148" href="#148">148</a>         <em class="comment">// See if we need to open a new file because we've exceeed maxBytes.</em><a name="149" href="#149">149</a>         <em class="comment">// Call to checkFileSize will open new file if we're at maximum for</em><a name="150" href="#150">150</a>         <em class="comment">// current file.</em><a name="151" href="#151">151</a>         writer.checkSize();<a name="152" href="#152">152</a>         <strong>if</strong> (writer.getPosition() != position) {<a name="153" href="#153">153</a>             <em class="comment">// We just closed the file because it was larger than maxBytes.</em><a name="154" href="#154">154</a>             <em class="comment">// Add to the totalBytesWritten the size of the first record</em><a name="155" href="#155">155</a>             <em class="comment">// in the file, if any.</em><a name="156" href="#156">156</a>             setTotalBytesWritten(getTotalBytesWritten() +<a name="157" href="#157">157</a>             	(writer.getPosition() - position));<a name="158" href="#158">158</a>             position = writer.getPosition();<a name="159" href="#159">159</a>         }<a name="160" href="#160">160</a>         <a name="161" href="#161">161</a>         <a href="../../../../org/archive/io/arc/ARCWriter.html">ARCWriter</a> w = (ARCWriter)writer;<a name="162" href="#162">162</a>         <strong>try</strong> {<a name="163" href="#163">163</a>             <strong>if</strong> (in instanceof ReplayInputStream) {<a name="164" href="#164">164</a>                 w.write(curi.toString(), curi.getContentType(),<a name="165" href="#165">165</a>                     ip, curi.getLong(A_FETCH_BEGAN_TIME),<a name="166" href="#166">166</a>                     recordLength, (ReplayInputStream)in);<a name="167" href="#167">167</a>             } <strong>else</strong> {<a name="168" href="#168">168</a>                 w.write(curi.toString(), curi.getContentType(),<a name="169" href="#169">169</a>                     ip, curi.getLong(A_FETCH_BEGAN_TIME),<a name="170" href="#170">170</a>                     recordLength, in);<a name="171" href="#171">171</a>             }<a name="172" href="#172">172</a>         } <strong>catch</strong> (IOException e) {<a name="173" href="#173">173</a>             <em class="comment">// Invalidate this file (It gets a '.invalid' suffix).</em><a name="174" href="#174">174</a>             getPool().invalidateFile(writer);<a name="175" href="#175">175</a>             <em class="comment">// Set the writer to null otherwise the pool accounting</em><a name="176" href="#176">176</a>             <em class="comment">// of how many active writers gets skewed if we subsequently</em><a name="177" href="#177">177</a>             <em class="comment">// do a returnWriter call on this object in the finally block.</em><a name="178" href="#178">178</a>             writer = <strong>null</strong>;<a name="179" href="#179">179</a>             <strong>throw</strong> e;<a name="180" href="#180">180</a>         } <strong>finally</strong> {<a name="181" href="#181">181</a>             <strong>if</strong> (writer != <strong>null</strong>) {<a name="182" href="#182">182</a>             	setTotalBytesWritten(getTotalBytesWritten() +<a name="183" href="#183">183</a>             	     (writer.getPosition() - position));<a name="184" href="#184">184</a>                 getPool().returnFile(writer);<a name="185" href="#185">185</a>             }<a name="186" href="#186">186</a>         }<a name="187" href="#187">187</a>         checkBytesWritten();<a name="188" href="#188">188</a>     }<a name="189" href="#189">189</a>     <a name="190" href="#190">190</a>     @Override<a name="191" href="#191">191</a>     <strong>protected</strong> String getFirstrecordStylesheet() {<a name="192" href="#192">192</a>         <strong>return</strong> <span class="string">"/arcMetaheaderBody.xsl"</span>;<a name="193" href="#193">193</a>     }<a name="194" href="#194">194</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?