|                                                                                                              1
 24
 25  package com.mckoi.database.jdbc;
 26
 27  import com.mckoi.util.Cache;
 28  import com.mckoi.database.global.ObjectTransfer;
 29  import java.util.Vector
  ; 30  import java.io.*;
 31  import java.sql.SQLException
  ; 32
 33
 40
 41  final class RowCache {
 42
 43
 46    private Cache row_cache;
 47
 48
 55    RowCache(int cache_size, int max_size) {
 56      row_cache = new Cache(cache_size, cache_size, 20);
 57    }
 58
 59
 64    synchronized Vector
  getResultPart(Vector  result_block, 65           MConnection connection, int result_id, int row_index, int row_count,
 66           int col_count, int total_row_count) throws IOException, SQLException
  { 67
 68          int orig_row_index = row_index;
 70      int orig_row_count = row_count;
 71
 72      Vector
  rows = new Vector  (); 73
 74          boolean found_notcached = false;
 76          for (int r = 0; r < row_count && !found_notcached; ++r) {
 78        int da_row = row_index + r;
 79              RowRef row_ref = new RowRef(result_id, da_row);
 81              CachedRow row = (CachedRow) row_cache.get(row_ref);
 83        if (row == null) {
 84          row_index = da_row;
 85          if (row_index + row_count > total_row_count) {
 86            row_count = total_row_count - row_index;
 87          }
 88          found_notcached = true;
 89        }
 90        else {
 91          rows.addElement(row);
 92        }
 93      }
 94
 95      Vector
  rows2 = new Vector  (); 96      if (found_notcached) {
 97
 98              found_notcached = false;
 100             for (int r = row_count - 1; r >= 0 && !found_notcached; --r) {
 102         int da_row = row_index + r;
 103                 RowRef row_ref = new RowRef(result_id, da_row);
 105                 CachedRow row = (CachedRow) row_cache.get(row_ref);
 107         if (row == null) {
 108           if (row_index == orig_row_index) {
 109             row_index = row_index - (row_count - (r + 1));
 110             if (row_index < 0) {
 111               row_count = row_count + row_index;
 112               row_index = 0;
 113             }
 114           }
 115           else {
 116             row_count = r + 1;
 117           }
 118           found_notcached = true;
 119         }
 120         else {
 121           rows2.insertElementAt(row, 0);
 122         }
 123       }
 124
 125     }
 126
 127         if (found_notcached) {
 129             ResultPart block = connection.requestResultPart(result_id,
 132                                                       row_index, row_count);
 133
 134       int block_index = 0;
 135       for (int r = 0; r < row_count; ++r) {
 136         Object
  [] arr = new Object  [col_count]; 137         int da_row = (row_index + r);
 138         int col_size = 0;
 139         for (int c = 0; c < col_count; ++c) {
 140           Object
  ob = block.elementAt(block_index); 141           ++block_index;
 142           arr[c] = ob;
 143           col_size += ObjectTransfer.size(ob);
 144         }
 145
 146         CachedRow cached_row = new CachedRow();
 147         cached_row.row = da_row;
 148         cached_row.row_data = arr;
 149
 150                 if (col_size <= 3200) {
 152           row_cache.put(new RowRef(result_id, da_row), cached_row);
 153         }
 154         rows.addElement(cached_row);
 155       }
 156
 157     }
 158
 159             result_block.removeAllElements();
 162     int low = orig_row_index;
 163     int high = orig_row_index + orig_row_count;
 164     for (int r = 0; r < rows.size(); ++r) {
 165       CachedRow row = (CachedRow) rows.elementAt(r);
 166             if (row.row >= low && row.row < high) {
 168         for (int c = 0; c < col_count; ++c) {
 169           result_block.addElement(row.row_data[c]);
 170         }
 171       }
 172     }
 173     for (int r = 0; r < rows2.size(); ++r) {
 174       CachedRow row = (CachedRow) rows2.elementAt(r);
 175             if (row.row >= low && row.row < high) {
 177         for (int c = 0; c < col_count; ++c) {
 178           result_block.addElement(row.row_data[c]);
 179         }
 180       }
 181     }
 182
 183         return result_block;
 185   }
 186
 187
 190   synchronized void clear() {
 191     row_cache.removeAll();
 192   }
 193
 194
 195
 196
 197
 198
 200
 203   private final static class RowRef {
 204     int table_id;
 205     int row;
 206
 207     RowRef(int table_id, int row) {
 208       this.table_id = table_id;
 209       this.row = row;
 210     }
 211
 212     public int hashCode() {
 213       return (int) table_id + (row * 35331);
 214     }
 215
 216     public boolean equals(Object
  ob) { 217       RowRef dest = (RowRef) ob;
 218       return (row == dest.row && table_id == dest.table_id);
 219     }
 220   }
 221
 222
 225   private final static class CachedRow {
 226     int row;
 227     Object
  [] row_data; 228   }
 229
 230 }
 231
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |