currenttransaction.html
来自「网络爬虫开源代码」· HTML 代码 · 共 478 行 · 第 1/3 页
HTML
478 行
<a name="312" href="#312">312</a> <a name="313" href="#313">313</a> <strong>if</strong> (cdbMode) {<a name="314" href="#314">314</a> CdbCursors cdbCursors = <strong>null</strong>;<a name="315" href="#315">315</a> WeakHashMap cdbCursorsMap = (WeakHashMap) localCdbCursors.get();<a name="316" href="#316">316</a> <strong>if</strong> (cdbCursorsMap == <strong>null</strong>) {<a name="317" href="#317">317</a> cdbCursorsMap = <strong>new</strong> WeakHashMap();<a name="318" href="#318">318</a> localCdbCursors.set(cdbCursorsMap);<a name="319" href="#319">319</a> } <strong>else</strong> {<a name="320" href="#320">320</a> cdbCursors = (CdbCursors) cdbCursorsMap.get(db);<a name="321" href="#321">321</a> }<a name="322" href="#322">322</a> <strong>if</strong> (cdbCursors == <strong>null</strong>) {<a name="323" href="#323">323</a> cdbCursors = <strong>new</strong> CdbCursors();<a name="324" href="#324">324</a> cdbCursorsMap.put(db, cdbCursors);<a name="325" href="#325">325</a> }<a name="326" href="#326">326</a> <a name="327" href="#327">327</a> <em class="comment">/*</em><a name="328" href="#328">328</a> <em class="comment"> * In CDB mode the cursorConfig specified by the user is ignored</em><a name="329" href="#329">329</a> <em class="comment"> * and only the writeCursor parameter is honored. This is the only</em><a name="330" href="#330">330</a> <em class="comment"> * meaningful cursor attribute for CDB, and here we count on</em><a name="331" href="#331">331</a> <em class="comment"> * writeCursor flag being set correctly by the caller.</em><a name="332" href="#332">332</a> <em class="comment"> */</em><a name="333" href="#333">333</a> List cursors;<a name="334" href="#334">334</a> CursorConfig cdbConfig;<a name="335" href="#335">335</a> <strong>if</strong> (writeCursor) {<a name="336" href="#336">336</a> <strong>if</strong> (cdbCursors.readCursors.size() > 0) {<a name="337" href="#337">337</a> <a name="338" href="#338">338</a> <em class="comment">/*</em><a name="339" href="#339">339</a> <em class="comment"> * Although CDB allows opening a write cursor when a read</em><a name="340" href="#340">340</a> <em class="comment"> * cursor is open, a self-deadlock will occur if a write is</em><a name="341" href="#341">341</a> <em class="comment"> * attempted for a record that is read-locked; we should</em><a name="342" href="#342">342</a> <em class="comment"> * avoid self-deadlocks at all costs</em><a name="343" href="#343">343</a> <em class="comment"> */</em><a name="344" href="#344">344</a> <strong>throw</strong> <strong>new</strong> IllegalStateException(<a name="345" href="#345">345</a> <span class="string">"cannot open CDB write cursor when read cursor is open"</span>);<a name="346" href="#346">346</a> }<a name="347" href="#347">347</a> cursors = cdbCursors.writeCursors;<a name="348" href="#348">348</a> cdbConfig = <strong>new</strong> CursorConfig();<a name="349" href="#349">349</a> DbCompat.setWriteCursor(cdbConfig, <strong>true</strong>);<a name="350" href="#350">350</a> } <strong>else</strong> {<a name="351" href="#351">351</a> cursors = cdbCursors.readCursors;<a name="352" href="#352">352</a> cdbConfig = <strong>null</strong>;<a name="353" href="#353">353</a> }<a name="354" href="#354">354</a> Cursor cursor;<a name="355" href="#355">355</a> <strong>if</strong> (cursors.size() > 0) {<a name="356" href="#356">356</a> Cursor other = ((Cursor) cursors.get(0));<a name="357" href="#357">357</a> cursor = other.dup(false);<a name="358" href="#358">358</a> } <strong>else</strong> {<a name="359" href="#359">359</a> cursor = db.openCursor(<strong>null</strong>, cdbConfig);<a name="360" href="#360">360</a> }<a name="361" href="#361">361</a> cursors.add(cursor);<a name="362" href="#362">362</a> <strong>return</strong> cursor;<a name="363" href="#363">363</a> } <strong>else</strong> {<a name="364" href="#364">364</a> <strong>return</strong> db.openCursor(txn, cursorConfig);<a name="365" href="#365">365</a> }<a name="366" href="#366">366</a> }<a name="367" href="#367">367</a> <a name="368" href="#368">368</a> <em>/**<em>*</em></em><a name="369" href="#369">369</a> <em> * Duplicates a cursor for a given database.</em><a name="370" href="#370">370</a> <em> *</em><a name="371" href="#371">371</a> <em> * @param writeCursor true to open a write cursor in a CDB environment, and</em><a name="372" href="#372">372</a> <em> * ignored for other environments.</em><a name="373" href="#373">373</a> <em> *</em><a name="374" href="#374">374</a> <em> * @param samePosition is passed through to Cursor.dup().</em><a name="375" href="#375">375</a> <em> *</em><a name="376" href="#376">376</a> <em> * @return the open cursor.</em><a name="377" href="#377">377</a> <em> *</em><a name="378" href="#378">378</a> <em> * @throws DatabaseException if a database problem occurs.</em><a name="379" href="#379">379</a> <em> */</em><a name="380" href="#380">380</a> Cursor dupCursor(Cursor cursor, <strong>boolean</strong> writeCursor, <strong>boolean</strong> samePosition)<a name="381" href="#381">381</a> throws DatabaseException {<a name="382" href="#382">382</a> <a name="383" href="#383">383</a> <strong>if</strong> (cdbMode) {<a name="384" href="#384">384</a> WeakHashMap cdbCursorsMap = (WeakHashMap) localCdbCursors.get();<a name="385" href="#385">385</a> <strong>if</strong> (cdbCursorsMap != <strong>null</strong>) {<a name="386" href="#386">386</a> Database db = cursor.getDatabase();<a name="387" href="#387">387</a> CdbCursors cdbCursors = (CdbCursors) cdbCursorsMap.get(db);<a name="388" href="#388">388</a> <strong>if</strong> (cdbCursors != <strong>null</strong>) {<a name="389" href="#389">389</a> List cursors = writeCursor ? cdbCursors.writeCursors<a name="390" href="#390">390</a> : cdbCursors.readCursors;<a name="391" href="#391">391</a> <strong>if</strong> (cursors.contains(cursor)) {<a name="392" href="#392">392</a> Cursor newCursor = cursor.dup(samePosition);<a name="393" href="#393">393</a> cursors.add(newCursor);<a name="394" href="#394">394</a> <strong>return</strong> newCursor;<a name="395" href="#395">395</a> }<a name="396" href="#396">396</a> }<a name="397" href="#397">397</a> }<a name="398" href="#398">398</a> <strong>throw</strong> <strong>new</strong> IllegalStateException(<span class="string">"cursor to dup not tracked"</span>);<a name="399" href="#399">399</a> } <strong>else</strong> {<a name="400" href="#400">400</a> <strong>return</strong> cursor.dup(samePosition);<a name="401" href="#401">401</a> }<a name="402" href="#402">402</a> }<a name="403" href="#403">403</a> <a name="404" href="#404">404</a> <em>/**<em>*</em></em><a name="405" href="#405">405</a> <em> * Closes a cursor.</em><a name="406" href="#406">406</a> <em> *</em><a name="407" href="#407">407</a> <em> * @param cursor the cursor to close.</em><a name="408" href="#408">408</a> <em> *</em><a name="409" href="#409">409</a> <em> * @throws DatabaseException if a database problem occurs.</em><a name="410" href="#410">410</a> <em> */</em><a name="411" href="#411">411</a> <strong>void</strong> closeCursor(Cursor cursor)<a name="412" href="#412">412</a> throws DatabaseException {<a name="413" href="#413">413</a> <a name="414" href="#414">414</a> <strong>if</strong> (cursor == <strong>null</strong>) {<a name="415" href="#415">415</a> <strong>return</strong>;<a name="416" href="#416">416</a> }<a name="417" href="#417">417</a> <strong>if</strong> (cdbMode) {<a name="418" href="#418">418</a> WeakHashMap cdbCursorsMap = (WeakHashMap) localCdbCursors.get();<a name="419" href="#419">419</a> <strong>if</strong> (cdbCursorsMap != <strong>null</strong>) {<a name="420" href="#420">420</a> Database db = cursor.getDatabase();<a name="421" href="#421">421</a> CdbCursors cdbCursors = (CdbCursors) cdbCursorsMap.get(db);<a name="422" href="#422">422</a> <strong>if</strong> (cdbCursors != <strong>null</strong>) {<a name="423" href="#423">423</a> <strong>if</strong> (cdbCursors.readCursors.remove(cursor) ||<a name="424" href="#424">424</a> cdbCursors.writeCursors.remove(cursor)) {<a name="425" href="#425">425</a> cursor.close();<a name="426" href="#426">426</a> <strong>return</strong>;<a name="427" href="#427">427</a> }<a name="428" href="#428">428</a> }<a name="429" href="#429">429</a> }<a name="430" href="#430">430</a> <strong>throw</strong> <strong>new</strong> IllegalStateException(<a name="431" href="#431">431</a> <span class="string">"closing CDB cursor that was not known to be open"</span>);<a name="432" href="#432">432</a> } <strong>else</strong> {<a name="433" href="#433">433</a> cursor.close();<a name="434" href="#434">434</a> }<a name="435" href="#435">435</a> }<a name="436" href="#436">436</a> <a name="437" href="#437">437</a> <em>/**<em>*</em></em><a name="438" href="#438">438</a> <em> * Returns true if a CDB cursor is open and therefore a Database write</em><a name="439" href="#439">439</a> <em> * operation should not be attempted since a self-deadlock may result.</em><a name="440" href="#440">440</a> <em> */</em><a name="441" href="#441">441</a> <strong>boolean</strong> isCDBCursorOpen(Database db)<a name="442" href="#442">442</a> throws DatabaseException {<a name="443" href="#443">443</a> <a name="444" href="#444">444</a> <strong>if</strong> (cdbMode) {<a name="445" href="#445">445</a> WeakHashMap cdbCursorsMap = (WeakHashMap) localCdbCursors.get();<a name="446" href="#446">446</a> <strong>if</strong> (cdbCursorsMap != <strong>null</strong>) {<a name="447" href="#447">447</a> CdbCursors cdbCursors = (CdbCursors) cdbCursorsMap.get(db);<a name="448" href="#448">448</a> <a name="449" href="#449">449</a> <strong>if</strong> (cdbCursors != <strong>null</strong> &&<a name="450" href="#450">450</a> (cdbCursors.readCursors.size() > 0 ||<a name="451" href="#451">451</a> cdbCursors.writeCursors.size() > 0)) {<a name="452" href="#452">452</a> <strong>return</strong> <strong>true</strong>;<a name="453" href="#453">453</a> }<a name="454" href="#454">454</a> }<a name="455" href="#455">455</a> }<a name="456" href="#456">456</a> <strong>return</strong> false;<a name="457" href="#457">457</a> }<a name="458" href="#458">458</a> <a name="459" href="#459">459</a> <strong>static</strong> <strong>final</strong> <strong>class</strong> CdbCursors {<a name="460" href="#460">460</a> <a name="461" href="#461">461</a> List writeCursors = <strong>new</strong> ArrayList();<a name="462" href="#462">462</a> List readCursors = <strong>new</strong> ArrayList();<a name="463" href="#463">463</a> }<a name="464" href="#464">464</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 + -
显示快捷键?