1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.PersistenceManager; 14 import com.triactive.jdo.StateManager; 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.SQLException ; 18 import java.util.NoSuchElementException ; 19 import javax.jdo.JDODataStoreException; 20 import org.apache.log4j.Category; 21 22 23 class NormalMapStore extends AbstractMapStore 24 { 25 private static final Category LOG = Category.getInstance(AbstractMapStore.class); 26 27 private final MapTable mapTable; 28 private final String putNewStmt; 29 private final String putUpdStmt; 30 private final String removeStmt; 31 private final String removeEntryStmt; 32 33 34 public NormalMapStore(MapTable mapTable) 35 { 36 this.mapTable = mapTable; 37 38 storeMgr = mapTable.getStoreManager(); 39 dba = storeMgr.getDatabaseAdapter(); 40 41 ownerMapping = mapTable.getOwnerMapping(); 42 keyMapping = mapTable.getKeyMapping(); 43 valueMapping = mapTable.getValueMapping(); 44 45 ownerColumn = ownerMapping.getColumn(); 46 keyColumn = keyMapping.getColumn(); 47 valueColumn = valueMapping.getColumn(); 48 49 keyType = keyColumn.getType(); 50 valueType = valueColumn.getType(); 51 52 keysAreEmbedded = !(keyMapping instanceof OIDMapping); 53 valuesAreEmbedded = !(valueMapping instanceof OIDMapping); 54 55 loadStmt = "SELECT " + keyColumn.getName() + ',' + valueColumn.getName() + 56 " FROM " + mapTable.getName() + 57 " WHERE " + ownerColumn.getName() + " = ?"; 58 getStmt = "SELECT " + valueColumn.getName() + 59 " FROM " + mapTable.getName() + 60 " WHERE " + ownerColumn.getName() + " = ? AND " 61 + keyColumn.getName() + " = ?"; 62 sizeStmt = "SELECT COUNT(*)" + 63 " FROM " + mapTable.getName() + 64 " WHERE " + ownerColumn.getName() + " = ?"; 65 containsValueStmt = "SELECT " + ownerColumn.getName() + 66 " FROM " + mapTable.getName() + 67 " WHERE " + ownerColumn.getName() + " = ? AND " 68 + valueColumn.getName() + " = ?"; 69 containsEntryStmt = "SELECT " + ownerColumn.getName() + 70 " FROM " + mapTable.getName() + 71 " WHERE " + ownerColumn.getName() + " = ? AND " 72 + keyColumn.getName() + " = ? AND " 73 + valueColumn.getName() + " = ?"; 74 putNewStmt = "INSERT INTO " + mapTable.getName() + 75 " (" + valueColumn.getName() + "," + ownerColumn.getName() + "," + keyColumn.getName() + ")" + 76 " VALUES (?,?,?)"; 77 putUpdStmt = "UPDATE " + mapTable.getName() + 78 " SET " + valueColumn.getName() + " = ?" + 79 " WHERE " + ownerColumn.getName() + " = ? AND " 80 + keyColumn.getName() + " = ?"; 81 removeStmt = "DELETE FROM " + mapTable.getName() + 82 " WHERE " + ownerColumn.getName() + " = ? AND " 83 + keyColumn.getName() + " = ?"; 84 removeEntryStmt = "DELETE FROM " + mapTable.getName() + 85 " WHERE " + ownerColumn.getName() + " = ? AND " 86 + keyColumn.getName() + " = ? AND " 87 + valueColumn.getName() + " = ?"; 88 clearStmt = "DELETE FROM " + mapTable.getName() + 89 " WHERE " + ownerColumn.getName() + " = ?"; 90 } 91 92 93 public boolean allowsNullValues() 94 { 95 return valueColumn.isNullable(); 96 } 97 98 99 public Object put(StateManager sm, Object key, Object value) 100 { 101 validateKeyForWriting(sm, key); 102 validateValueForWriting(sm, value); 103 104 PersistenceManager pm = sm.getPersistenceManager(); 105 Object oldValue; 106 String stmt; 107 108 try 109 { 110 oldValue = get0(sm, key); 111 stmt = putUpdStmt; 112 } 113 catch (NoSuchElementException e) 114 { 115 oldValue = null; 116 stmt = putNewStmt; 117 } 118 119 if (oldValue != value) 120 { 121 try 122 { 123 Connection conn = pm.getConnection(true); 124 125 try 126 { 127 PreparedStatement ps = conn.prepareStatement(stmt); 128 129 try 130 { 131 valueMapping.setObject(pm, ps, 1, value); 132 ownerMapping.setObject(pm, ps, 2, sm.getObject()); 133 keyMapping.setObject(pm, ps, 3, key); 134 135 long startTime = System.currentTimeMillis(); 136 137 ps.executeUpdate(); 138 139 if (LOG.isDebugEnabled()) 140 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + stmt); 141 } 142 finally 143 { 144 ps.close(); 145 } 146 } 147 finally 148 { 149 pm.releaseConnection(conn); 150 } 151 } 152 catch (SQLException e) 153 { 154 throw dba.newDataStoreException("Put request failed: " + stmt, e); 155 } 156 } 157 158 return oldValue; 159 } 160 161 162 public Object remove(StateManager sm, Object key) 163 { 164 if (!validateKeyForReading(sm, key)) 165 return null; 166 167 PersistenceManager pm = sm.getPersistenceManager(); 168 Object oldValue; 169 boolean exists; 170 171 try 172 { 173 oldValue = get0(sm, key); 174 exists = true; 175 } 176 catch (NoSuchElementException e) 177 { 178 oldValue = null; 179 exists = false; 180 } 181 182 if (exists) 183 { 184 try 185 { 186 Connection conn = pm.getConnection(true); 187 188 try 189 { 190 PreparedStatement ps = conn.prepareStatement(removeStmt); 191 192 try 193 { 194 ownerMapping.setObject(pm, ps, 1, sm.getObject()); 195 keyMapping.setObject(pm, ps, 2, key); 196 197 long startTime = System.currentTimeMillis(); 198 199 ps.executeUpdate(); 200 201 if (LOG.isDebugEnabled()) 202 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + removeStmt); 203 } 204 finally 205 { 206 ps.close(); 207 } 208 } 209 finally 210 { 211 pm.releaseConnection(conn); 212 } 213 } 214 catch (SQLException e) 215 { 216 throw dba.newDataStoreException("Remove request failed: " + removeStmt, e); 217 } 218 } 219 220 return oldValue; 221 } 222 223 224 public boolean removeEntry(StateManager sm, Object key, Object value) 225 { 226 if (!validateKeyForReading(sm, key)) 227 return false; 228 if (!validateValueForReading(sm, value)) 229 return false; 230 231 boolean modified; 232 PersistenceManager pm = sm.getPersistenceManager(); 233 234 try 235 { 236 Connection conn = pm.getConnection(true); 237 238 try 239 { 240 PreparedStatement ps = conn.prepareStatement(removeEntryStmt); 241 242 try 243 { 244 ownerMapping.setObject(pm, ps, 1, sm.getObject()); 245 keyMapping.setObject(pm, ps, 2, key); 246 valueMapping.setObject(pm, ps, 3, key); 247 248 long startTime = System.currentTimeMillis(); 249 250 int rowsDeleted = ps.executeUpdate(); 251 252 if (LOG.isDebugEnabled()) 253 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + removeEntryStmt); 254 255 modified = rowsDeleted == 1; 256 } 257 finally 258 { 259 ps.close(); 260 } 261 } 262 finally 263 { 264 pm.releaseConnection(conn); 265 } 266 } 267 catch (SQLException e) 268 { 269 throw dba.newDataStoreException("Remove entry request failed: " + removeEntryStmt, e); 270 } 271 272 return modified; 273 } 274 } 275 | Popular Tags |