1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: MyRangeCursor.java,v 12.6 2008/02/07 17:12:26 mark Exp $ 7 */ 8 9package com.sleepycat.collections; 10 11import com.sleepycat.compat.DbCompat; 12import com.sleepycat.db.Cursor; 13import com.sleepycat.db.CursorConfig; 14import com.sleepycat.db.DatabaseException; 15import com.sleepycat.util.keyrange.KeyRange; 16import com.sleepycat.util.keyrange.RangeCursor; 17 18class MyRangeCursor extends RangeCursor { 19 20 private DataView view; 21 private boolean isRecnoOrQueue; 22 private boolean writeCursor; 23 24 MyRangeCursor(KeyRange range, 25 CursorConfig config, 26 DataView view, 27 boolean writeAllowed) 28 throws DatabaseException { 29 30 super(range, view.dupsRange, view.dupsOrdered, 31 openCursor(view, config, writeAllowed)); 32 this.view = view; 33 isRecnoOrQueue = view.recNumAllowed && !view.btreeRecNumDb; 34 writeCursor = isWriteCursor(config, writeAllowed); 35 } 36 37 /** 38 * Returns true if a write cursor is requested by the user via the cursor 39 * config, or if this is a writable cursor and the user has not specified a 40 * cursor config. For CDB, a special cursor must be created for writing. 41 * See CurrentTransaction.openCursor. 42 */ 43 private static boolean isWriteCursor(CursorConfig config, 44 boolean writeAllowed) { 45 return DbCompat.getWriteCursor(config) || 46 (config == CursorConfig.DEFAULT && writeAllowed); 47 } 48 49 private static Cursor openCursor(DataView view, 50 CursorConfig config, 51 boolean writeAllowed) 52 throws DatabaseException { 53 54 return view.currentTxn.openCursor 55 (view.db, config, isWriteCursor(config, writeAllowed), 56 view.useTransaction()); 57 } 58 59 protected Cursor dupCursor(Cursor cursor, boolean samePosition) 60 throws DatabaseException { 61 62 return view.currentTxn.dupCursor(cursor, writeCursor, samePosition); 63 } 64 65 protected void closeCursor(Cursor cursor) 66 throws DatabaseException { 67 68 view.currentTxn.closeCursor(cursor); 69 } 70 71 protected boolean checkRecordNumber() { 72 return isRecnoOrQueue; 73 } 74} 75