1 24 25 package com.mckoi.database; 26 27 import com.mckoi.util.Cache; 28 import com.mckoi.util.IntegerVector; 29 30 57 58 abstract class TableBackedCache { 59 60 63 private TableName backed_by_table; 64 65 69 private IntegerVector added_list; 70 71 75 private IntegerVector removed_list; 76 77 80 private boolean transaction_active; 81 82 85 private TransactionModificationListener listener; 86 87 90 protected TableBackedCache(TableName table) { 91 this.backed_by_table = table; 92 93 added_list = new IntegerVector(); 94 removed_list = new IntegerVector(); 95 } 96 97 100 private void addRowsToList(int[] from, IntegerVector list) { 101 if (from != null) { 102 for (int i = 0; i < from.length; ++i) { 103 list.addInt(from[i]); 104 } 105 } 106 } 107 108 112 final void attachTo(TableDataConglomerate conglomerate) { 113 TableName table_name = backed_by_table; 115 listener = new TransactionModificationListener() { 116 public void tableChange(TableModificationEvent evt) { 117 } 119 public void tableCommitChange(TableCommitModificationEvent evt) { 120 TableName table_name = evt.getTableName(); 121 if (table_name.equals(backed_by_table)) { 122 synchronized (removed_list) { 123 addRowsToList(evt.getAddedRows(), added_list); 124 addRowsToList(evt.getRemovedRows(), removed_list); 125 } 126 } 127 } 128 }; 129 conglomerate.addTransactionModificationListener(table_name, listener); 130 } 131 132 135 final void detatchFrom(TableDataConglomerate conglomerate) { 136 TableName table_name = backed_by_table; 138 conglomerate.removeTransactionModificationListener(table_name, listener); 139 } 140 141 148 final void transactionStarted() { 149 transaction_active = true; 150 internalPurgeCache(); 151 } 152 153 160 final void transactionFinished() { 161 transaction_active = false; 162 internalPurgeCache(); 163 } 164 165 169 private void internalPurgeCache() { 170 IntegerVector add, remove; 172 synchronized (removed_list) { 173 add = new IntegerVector(added_list); 174 remove = new IntegerVector(removed_list); 175 added_list.clear(); 177 removed_list.clear(); 178 } 179 purgeCacheOfInvalidatedEntries(add, remove); 181 } 182 183 192 abstract void purgeCacheOfInvalidatedEntries( 193 IntegerVector added_rows, IntegerVector removed_rows); 194 195 } 196 197 | Popular Tags |