Project

General

Profile

« Previous | Next » 

Revision 2526

Added by sgarg over 19 years ago

Bugfix to the previous commit.

Also made changes so that while indexing the path in buildIndex(), instead of seperate database calls to the database for each path, one call is made with all the paths in it.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
1178 1178
            nodeRecordMap.put(nodeId, currentNode);
1179 1179
        }
1180 1180

  
1181
        DocumentIdentifier doc = null;
1182
        try {
1183
            doc = new DocumentIdentifier(docid);
1184
        } catch (AccessionNumberException e){
1185
            MetaCatUtil.debugMessage("AccessionNumber Exception while "
1186
                                     + " inserting path index "
1187
                                     + "in DocumentImpl.buildIndex for "
1188
                                     + "document " + docid, 10);
1189
            MetaCatUtil.debugMessage(e.getMessage(), 10);
1190
        }
1191

  
1181 1192
        // Opening separate db connection for deleting and writing
1182 1193
        // XML Index -- be sure that it is all in one db transaction
1183 1194
        int serialNumber = -1;
1184 1195
        DBConnection dbConn = null;
1185 1196
        try {
1186
            dbConn = DBConnectionPool.getDBConnection(
1187
                "DocumentImpl.buildIndex");
1197
            dbConn = DBConnectionPool.getDBConnection("DocumentImpl.buildIndex");
1188 1198
            serialNumber = dbConn.getCheckOutSerialNumber();
1189 1199
            dbConn.setAutoCommit(false);
1190 1200
            //make sure record is done
......
1193 1203
            // Delete the previous index entries for this document
1194 1204
            deleteNodeIndex(dbConn);
1195 1205

  
1196
	    // Delete all the entries in xml_queryresult
1206
            // Delete all the entries in xml_queryresult
1197 1207
            PreparedStatement pstmt = dbConn.prepareStatement(
1198 1208
                    "DELETE FROM xml_queryresult WHERE docid = ?");
1199
            pstmt.setString(1, docid);
1209
            pstmt.setString(1, doc.getIdentifier());
1200 1210
            pstmt.execute();
1201 1211
            pstmt.close();
1202 1212
            dbConn.increaseUsageCount(1);
......
1204 1214
            // Delete all the entries in xml_queryresult
1205 1215
            pstmt = dbConn.prepareStatement(
1206 1216
                    "DELETE FROM xml_path_index WHERE docid = ?");
1207
            pstmt.setString(1, docid);
1217
            pstmt.setString(1, doc.getIdentifier());
1208 1218
            pstmt.execute();
1209 1219
            pstmt.close();
1210 1220
            dbConn.increaseUsageCount(1);
......
1223 1233
                        atRootElement = false;
1224 1234
                    }
1225 1235
                    traverseParents(nodeRecordMap, rootNodeId,
1226
                            currentNode.getNodeId(),
1227
                            currentNode.getNodeId(), "", pathList);
1236
                                    currentNode.getNodeId(),
1237
                                    currentNode.getNodeId(), "", pathList);
1228 1238
                    updateNodeIndex(dbConn, pathList);
1229 1239
                }
1230 1240
            }
1231 1241

  
1242
            dbConn.commit();
1243
        } catch (SQLException e) {
1244
            MetaCatUtil.debugMessage("SQL Exception while inserting path index "
1245
                                     + "in DocumentImpl.buildIndex for "
1246
                                     + "document " + docid, 10);
1247
            MetaCatUtil.debugMessage(e.getMessage(), 10);
1248
            try {
1249
                dbConn.rollback();
1250
            } catch (SQLException sqle) {
1251
                MetaCatUtil.debugMessage("Error while rolling back "
1252
                                         + "commit in DocumentImpl"
1253
                                         + ".buildIndex" + "\n"
1254
                                         + sqle.getMessage(), 10);
1255
            }
1256
        } finally {
1257
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1258
        }
1259

  
1260
        try {
1261
            dbConn = DBConnectionPool.getDBConnection("DocumentImpl.buildIndex-pathIndex");
1262
            serialNumber = dbConn.getCheckOutSerialNumber();
1263
            dbConn.setAutoCommit(false);
1264

  
1232 1265
            ResultSet rs = null;
1233 1266
            PreparedStatement pstmt1 = null;
1234 1267

  
1268
            String pathList = "";
1235 1269
            for (int i = 0; i < MetaCatUtil.pathsForIndexing.size(); i++) {
1236
              MetaCatUtil.formattedDebugMessage("Indexing the path '"
1237
                                                + (String) MetaCatUtil
1238
                                                .pathsForIndexing.elementAt(i)
1239
                                                + "' for docid " + docid
1240
                                                + "... ", 30, false, true);
1270
                pathList = pathList + "i.path = '" +
1271
                    (String)MetaCatUtil.pathsForIndexing.get(i) + "' OR ";
1272
            }
1241 1273

  
1242
              pstmt = dbConn.prepareStatement("SELECT DISTINCT n.docid, "
1243
                                              + "n.nodedata, n.nodedatanumerical, n.parentnodeid"
1244
                                              + " FROM xml_nodes n, xml_index i WHERE"
1245
                                              + " i.path = ? and n.parentnodeid=i.nodeid and"
1246
                                              + " n.nodetype LIKE 'TEXT' and n.docid = ?");
1247
              pstmt.setString(1, (String) MetaCatUtil.
1248
                              pathsForIndexing.elementAt(i));
1249
              pstmt.setString(2, docid);
1250
              pstmt.execute();
1251
              rs = pstmt.getResultSet();
1274
            pathList = pathList.substring(0,pathList.length()-4);
1252 1275

  
1253
              int count = 0;
1254
              MetaCatUtil.debugMessage("Executed the select statement for: "
1255
                                       + (String) MetaCatUtil.pathsForIndexing
1256
                                       .elementAt(i), 60);
1276
            PreparedStatement pstmt = dbConn.prepareStatement("SELECT DISTINCT n.docid, "
1277
                                            + "n.nodedata, n.nodedatanumerical,"
1278
                                            + " n.parentnodeid, i.path FROM"
1279
                                            + " xml_nodes n, xml_index i WHERE"
1280
                                            + " (" + pathList
1281
                                            + " ) AND n.parentnodeid=i.nodeid AND"
1282
                                            + " n.nodetype LIKE 'TEXT' and n.docid = ?");
1257 1283

  
1258
              try {
1259
                  while (rs.next()) {
1260
                      String docid = rs.getString(1);
1261
                      String nodedata = rs.getString(2);
1262
                      float nodedatanumerical = rs.getFloat(3);
1263
                      int parentnodeid = rs.getInt(4);
1284
            pstmt.setString(1, doc.getIdentifier());
1264 1285

  
1265
                      if (!nodedata.trim().equals("")) {
1266
                          pstmt1 = dbConn.prepareStatement("INSERT INTO "
1267
                                      + "xml_path_index (docid, path, nodedata, "
1268
                                      + "nodedatanumerical, parentnodeid)"
1269
                                      + " VALUES (?, ?, ?, ?, ?)");
1286
            pstmt.execute();
1287
            rs = pstmt.getResultSet();
1270 1288

  
1271
                          pstmt1.setString(1, docid);
1272
                          pstmt1.setString(2, (String) MetaCatUtil.
1273
                                           pathsForIndexing.elementAt(i));
1274
                          pstmt1.setString(3, nodedata);
1275
                          pstmt1.setFloat(4, nodedatanumerical);
1276
                          pstmt1.setFloat(5, parentnodeid);
1289
            int count = 0;
1277 1290

  
1278
                          pstmt1.execute();
1279
                          pstmt1.close();
1291
            while (rs.next()) {
1292
                String docid = rs.getString(1);
1293
                String nodedata = rs.getString(2);
1294
                float nodedatanumerical = rs.getFloat(3);
1295
                int parentnodeid = rs.getInt(4);
1296
                String path = rs.getString(5);
1280 1297

  
1281
                          count++;
1298
                if (!nodedata.trim().equals("")) {
1299
                    pstmt1 = dbConn.prepareStatement("INSERT INTO "
1300
                                   + "xml_path_index (docid, path, nodedata, "
1301
                                   + "nodedatanumerical, parentnodeid)"
1302
                                   + " VALUES (?, ?, ?, ?, ?)");
1282 1303

  
1283
                      }
1284
                  }
1285
              }
1286
              catch (Exception e) {
1287
                  System.out.println("Exception:" + e.getMessage());
1288
                  e.printStackTrace();
1289
              }
1304
                    pstmt1.setString(1, docid);
1305
                    pstmt1.setString(2, path);
1306
                    pstmt1.setString(3, nodedata);
1307
                    pstmt1.setFloat(4, nodedatanumerical);
1308
                    pstmt1.setFloat(5, parentnodeid);
1290 1309

  
1291
              rs.close();
1292
              pstmt.close();
1293
              dbConn.increaseUsageCount(1);
1310
                    pstmt1.execute();
1311
                    pstmt1.close();
1294 1312

  
1295
              MetaCatUtil.formattedDebugMessage("indexed " + count
1296
                                       + " records from xml_nodes",
1297
                                       20, true, false);
1298
          }
1299
          dbConn.commit();
1300
      } catch (SQLException e) {
1313
                    count++;
1314
                }
1315
            }
1316

  
1317
            rs.close();
1318
            pstmt.close();
1319
            dbConn.increaseUsageCount(1);
1320

  
1321
            MetaCatUtil.debugMessage("Indexed " + count
1322
                                              + " records from xml_nodes",
1323
                                              20);
1324

  
1325
            dbConn.commit();
1326
        } catch (SQLException e) {
1301 1327
          MetaCatUtil.debugMessage("SQL Exception while inserting path index "
1302 1328
                                   + "in DocumentImpl.buildIndex for "
1303 1329
                                   + "document " + docid, 10);
1304 1330
          MetaCatUtil.debugMessage(e.getMessage(), 10);
1305
          e.printStackTrace();
1306 1331
          try {
1307 1332
              dbConn.rollback();
1308 1333
          } catch (SQLException sqle) {

Also available in: Unified diff