comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 235:3e29395ebac6

The XML documents stored aside users, collections and collection items are now compressed/decompressed transparently. artifacts/trunk@1642 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 04 Apr 2011 09:28:54 +0000
parents 16cd059945e5
children b35d32e507b6
comparison
equal deleted inserted replaced
234:23d642319a0b 235:3e29395ebac6
22 import java.sql.Timestamp; 22 import java.sql.Timestamp;
23 import java.sql.Types; 23 import java.sql.Types;
24 24
25 import java.util.ArrayList; 25 import java.util.ArrayList;
26 import java.util.Date; 26 import java.util.Date;
27 import java.util.HashMap;
27 28
28 import org.apache.log4j.Logger; 29 import org.apache.log4j.Logger;
29 30
30 import org.w3c.dom.Document; 31 import org.w3c.dom.Document;
31 32
767 final UserFactory factory, 768 final UserFactory factory,
768 final Object context 769 final Object context
769 ) { 770 ) {
770 final User [] user = new User[1]; 771 final User [] user = new User[1];
771 772
773 final byte [] roleData = XMLUtils.toByteArray(role, true);
774
772 SQLExecutor exec = new SQLExecutor() { 775 SQLExecutor exec = new SQLExecutor() {
773 public boolean doIt() throws SQLException { 776 public boolean doIt() throws SQLException {
774 777
775 prepareStatement(SQL_USERS_NEXT_ID); 778 prepareStatement(SQL_USERS_NEXT_ID);
776 result = stmnt.executeQuery(); 779 result = stmnt.executeQuery();
788 prepareStatement(SQL_USERS_INSERT); 791 prepareStatement(SQL_USERS_INSERT);
789 792
790 stmnt.setInt(1, id); 793 stmnt.setInt(1, id);
791 stmnt.setString(2, identifier); 794 stmnt.setString(2, identifier);
792 stmnt.setString(3, name); 795 stmnt.setString(3, name);
793
794 byte [] roleData = role == null
795 ? null
796 : XMLUtils.toByteArray(role);
797 796
798 if (roleData == null) { 797 if (roleData == null) {
799 stmnt.setNull(4, Types.BIGINT); 798 stmnt.setNull(4, Types.BIGINT);
800 } 799 }
801 else { 800 else {
896 } 895 }
897 // omit id 896 // omit id
898 String name = result.getString(2); 897 String name = result.getString(2);
899 byte [] roleData = result.getBytes(3); 898 byte [] roleData = result.getBytes(3);
900 899
901 Document role = XMLUtils.fromByteArray(roleData); 900 Document role = XMLUtils.fromByteArray(roleData, true);
902 901
903 user[0] = factory.createUser( 902 user[0] = factory.createUser(
904 identifier, name, role, context); 903 identifier, name, role, context);
905 return true; 904 return true;
906 } 905 }
924 // omit id 923 // omit id
925 String identifier = result.getString(2); 924 String identifier = result.getString(2);
926 String name = result.getString(3); 925 String name = result.getString(3);
927 byte [] roleData = result.getBytes(4); 926 byte [] roleData = result.getBytes(4);
928 927
929 Document role = XMLUtils.fromByteArray(roleData); 928 Document role = XMLUtils.fromByteArray(roleData, true);
930 User user = factory.createUser( 929 User user = factory.createUser(
931 identifier, name, role, context); 930 identifier, name, role, context);
932 users.add(user); 931 users.add(user);
933 } 932 }
934 return true; 933 return true;
955 if (!isValidIdentifier(ownerIdentifier)) { 954 if (!isValidIdentifier(ownerIdentifier)) {
956 logger.debug("Invalid owner id: '" + ownerIdentifier + "'"); 955 logger.debug("Invalid owner id: '" + ownerIdentifier + "'");
957 return null; 956 return null;
958 } 957 }
959 958
960 final ArtifactCollection [] collection = 959 final ArtifactCollection [] collection = new ArtifactCollection[1];
961 new ArtifactCollection[1]; 960
961 final byte [] data = XMLUtils.toByteArray(attribute, true);
962 962
963 SQLExecutor exec = new SQLExecutor() { 963 SQLExecutor exec = new SQLExecutor() {
964 public boolean doIt() throws SQLException { 964 public boolean doIt() throws SQLException {
965 // fetch owner id 965 // fetch owner id
966 prepareStatement(SQL_USERS_SELECT_ID_BY_GID); 966 prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
1001 stmnt.setNull(5, Types.BIGINT); 1001 stmnt.setNull(5, Types.BIGINT);
1002 } 1002 }
1003 else { 1003 else {
1004 stmnt.setLong(5, ttl); 1004 stmnt.setLong(5, ttl);
1005 } 1005 }
1006
1007 byte [] data = XMLUtils.toByteArray(attribute);
1008 1006
1009 if (data == null) { 1007 if (data == null) {
1010 stmnt.setNull(6, Types.BINARY); 1008 stmnt.setNull(6, Types.BINARY);
1011 } 1009 }
1012 else { 1010 else {
1067 if (!result.next()) { 1065 if (!result.next()) {
1068 logger.debug("No such collection"); 1066 logger.debug("No such collection");
1069 return false; 1067 return false;
1070 } 1068 }
1071 1069
1072 String collectionName = result.getString(2); 1070 String collectionName = result.getString(2);
1073 String ownerId = result.getString(3); 1071 String ownerId = result.getString(3);
1074 Date creationTime = 1072 Date creationTime =
1075 new Date(result.getTimestamp(4).getTime()); 1073 new Date(result.getTimestamp(4).getTime());
1076 Date lastAccess = 1074 Date lastAccess =
1077 new Date(result.getTimestamp(5).getTime()); 1075 new Date(result.getTimestamp(5).getTime());
1078 Document attr = 1076 Document attr =
1079 XMLUtils.fromByteArray(result.getBytes(6)); 1077 XMLUtils.fromByteArray(result.getBytes(6), true);
1080 1078
1081 ArtifactCollection collection = 1079 ArtifactCollection collection =
1082 collectionFactory.createCollection( 1080 collectionFactory.createCollection(
1083 collectionId, 1081 collectionId,
1084 collectionName, 1082 collectionName,
1128 prepareStatement(SQL_COLLECTIONS_SELECT_ALL); 1126 prepareStatement(SQL_COLLECTIONS_SELECT_ALL);
1129 } 1127 }
1130 1128
1131 result = stmnt.executeQuery(); 1129 result = stmnt.executeQuery();
1132 1130
1131 HashMap<String, LazyBackendUser> users =
1132 new HashMap<String, LazyBackendUser>();
1133
1133 while (result.next()) { 1134 while (result.next()) {
1134 String collectionIdentifier = result.getString(1); 1135 String collectionIdentifier = result.getString(1);
1135 String collectionName = result.getString(2); 1136 String collectionName = result.getString(2);
1136 Date creationTime = 1137 Date creationTime =
1137 new Date(result.getTimestamp(3).getTime()); 1138 new Date(result.getTimestamp(3).getTime());
1144 creationTime, 1145 creationTime,
1145 data, 1146 data,
1146 context); 1147 context);
1147 1148
1148 if (userIdentifier != null) { 1149 if (userIdentifier != null) {
1149 collection.setUser(new LazyBackendUser( 1150 LazyBackendUser user = users.get(userIdentifier);
1150 userIdentifier, userFactory, Backend.this, context)); 1151 if (user == null) {
1152 user = new LazyBackendUser(
1153 userIdentifier, userFactory,
1154 Backend.this, context);
1155 users.put(userIdentifier, user);
1156 }
1157 collection.setUser(user);
1151 } 1158 }
1152 1159
1153 collections.add(collection); 1160 collections.add(collection);
1154 } 1161 }
1155 return true; 1162 return true;
1215 if (!isValidIdentifier(artifactId)) { 1222 if (!isValidIdentifier(artifactId)) {
1216 logger.debug("artifact id is not valid: " + artifactId); 1223 logger.debug("artifact id is not valid: " + artifactId);
1217 return null; 1224 return null;
1218 } 1225 }
1219 1226
1220 final Document [] document = new Document[1]; 1227 final byte [][] data = new byte[1][1];
1221 1228
1222 SQLExecutor exec = new SQLExecutor() { 1229 SQLExecutor exec = new SQLExecutor() {
1223 public boolean doIt() throws SQLException { 1230 public boolean doIt() throws SQLException {
1224 prepareStatement(SQL_COLLECTION_ITEM_GET_ATTRIBUTE); 1231 prepareStatement(SQL_COLLECTION_ITEM_GET_ATTRIBUTE);
1225 stmnt.setString(1, collectionId); 1232 stmnt.setString(1, collectionId);
1227 result = stmnt.executeQuery(); 1234 result = stmnt.executeQuery();
1228 if (!result.next()) { 1235 if (!result.next()) {
1229 logger.debug("No such collection item"); 1236 logger.debug("No such collection item");
1230 return false; 1237 return false;
1231 } 1238 }
1232 document[0] = XMLUtils.fromByteArray(result.getBytes(1)); 1239 data[0] = result.getBytes(1);
1233 return true; 1240 return true;
1234 } 1241 }
1235 }; 1242 };
1236 1243
1237 return exec.runRead() ? document[0] : null; 1244 return exec.runRead()
1245 ? XMLUtils.fromByteArray(data[0], true)
1246 : null;
1238 } 1247 }
1239 1248
1240 public boolean setCollectionAttribute( 1249 public boolean setCollectionAttribute(
1241 final String collectionId, 1250 final String collectionId,
1242 final String artifactId, 1251 final String artifactId,
1249 if (!isValidIdentifier(artifactId)) { 1258 if (!isValidIdentifier(artifactId)) {
1250 logger.debug("artifact id is not valid: " + artifactId); 1259 logger.debug("artifact id is not valid: " + artifactId);
1251 return false; 1260 return false;
1252 } 1261 }
1253 1262
1254 final byte [] data = XMLUtils.toByteArray(attribute); 1263 final byte [] data = XMLUtils.toByteArray(attribute, true);
1255 1264
1256 return new SQLExecutor() { 1265 return new SQLExecutor() {
1257 public boolean doIt() throws SQLException { 1266 public boolean doIt() throws SQLException {
1258 1267
1259 // set the column in collection items 1268 // set the column in collection items
1293 if (!isValidIdentifier(artifactId)) { 1302 if (!isValidIdentifier(artifactId)) {
1294 logger.debug("Invalid artifact id: '" + artifactId + "'"); 1303 logger.debug("Invalid artifact id: '" + artifactId + "'");
1295 return false; 1304 return false;
1296 } 1305 }
1297 1306
1307 final byte [] data = XMLUtils.toByteArray(attribute, true);
1308
1298 SQLExecutor exec = new SQLExecutor() { 1309 SQLExecutor exec = new SQLExecutor() {
1299 public boolean doIt() throws SQLException { 1310 public boolean doIt() throws SQLException {
1300 // fetch artifact id 1311 // fetch artifact id
1301 prepareStatement(SQL_GET_ID); 1312 prepareStatement(SQL_GET_ID);
1302 stmnt.setString(1, artifactId); 1313 stmnt.setString(1, artifactId);
1342 // insert new collection item 1353 // insert new collection item
1343 prepareStatement(SQL_COLLECTION_ITEMS_INSERT); 1354 prepareStatement(SQL_COLLECTION_ITEMS_INSERT);
1344 stmnt.setInt(1, ci_id); 1355 stmnt.setInt(1, ci_id);
1345 stmnt.setInt(2, cid); 1356 stmnt.setInt(2, cid);
1346 stmnt.setInt(3, aid); 1357 stmnt.setInt(3, aid);
1347
1348 byte [] data = XMLUtils.toByteArray(attribute);
1349 1358
1350 if (data == null) { 1359 if (data == null) {
1351 stmnt.setNull(4, Types.BINARY); 1360 stmnt.setNull(4, Types.BINARY);
1352 } 1361 }
1353 else { 1362 else {

http://dive4elements.wald.intevation.org