1 34 package smallsql.database; 35 36 39 class IndexScrollStatus { 40 private final IndexNode rootPage; 41 private final Expressions expressions; 43 private final java.util.Stack nodeStack = new java.util.Stack (); 45 private LongTreeList longList; 46 private LongTreeListEnum longListEnum = new LongTreeListEnum(); 47 48 49 IndexScrollStatus(IndexNode rootPage, Expressions expressions){ 50 this.rootPage = rootPage; 51 this.expressions= expressions; 52 reset(); 53 } 54 55 56 59 final void reset(){ 60 nodeStack.clear(); 61 boolean asc = (expressions.get(0).getAlias() != SQLTokenizer.DESC_STR); 62 nodeStack.push( new IndexNodeScrollStatus(rootPage, asc, true, 0) ); 63 } 64 65 66 70 final long getRowOffset( boolean scroll){ 71 if(longList != null){ 72 long rowOffset = scroll ? 73 longList.getNext(longListEnum) : 74 longList.getPrevious(longListEnum); 75 if(rowOffset < 0){ 76 longList = null; 78 }else{ 79 return rowOffset; 80 } 81 } 82 while(true){ 83 IndexNodeScrollStatus status = (IndexNodeScrollStatus)nodeStack.peek(); 84 int level = status.level; 85 if(!status.asc ^ scroll){ 86 int idx = ++status.idx; 88 if(idx == -1){ 89 if(status.nodeValue != null){ 90 if(status.nodeValue instanceof IndexNode){ 91 level++; 92 nodeStack.push( 93 new IndexNodeScrollStatus( (IndexNode)status.nodeValue, 94 (expressions.get(level).getAlias() != SQLTokenizer.DESC_STR), 95 scroll, level)); 96 continue; 97 }else 98 return getReturnValue(status.nodeValue); 99 } 100 idx = ++status.idx; 102 } 103 if(idx >= status.nodes.length){ 104 if(nodeStack.size() > 1){ 106 nodeStack.pop(); 107 continue; 108 }else{ 109 return -1; 111 } 112 } 113 IndexNode node = status.nodes[idx]; 114 nodeStack.push( new IndexNodeScrollStatus(node, status.asc, scroll, level) ); 115 }else{ 116 int idx = --status.idx; 118 if(idx == -1){ 119 if(status.nodeValue != null){ 120 if(status.nodeValue instanceof IndexNode){ 121 level++; 122 nodeStack.push( 123 new IndexNodeScrollStatus( (IndexNode)status.nodeValue, 124 (expressions.get(level).getAlias() != SQLTokenizer.DESC_STR), 125 scroll, level)); 126 continue; 127 }else 128 return getReturnValue(status.nodeValue); 129 } 130 } 132 if(idx < 0){ 133 if(nodeStack.size() > 1){ 135 nodeStack.pop(); 136 continue; 137 }else{ 138 return -1; 140 } 141 } 142 IndexNode node = status.nodes[idx]; 143 nodeStack.push( new IndexNodeScrollStatus(node, status.asc, scroll, level) ); 144 } 145 } 146 } 147 148 149 153 final void afterLast(){ 154 longList = null; 155 nodeStack.setSize(1); 156 ((IndexNodeScrollStatus)nodeStack.peek()).afterLast(); 157 } 158 159 160 private final long getReturnValue( Object value){ 161 if(rootPage.getUnique()){ 162 return ((Long )value).longValue(); 163 }else{ 164 longList = (LongTreeList)value; 165 longListEnum.reset(); 166 return longList.getNext(longListEnum); } 168 169 } 170 171 172 173 174 } 175 | Popular Tags |