Project

General

Profile

« Previous | Next » 

Revision 706

Added by bojilova over 23 years ago

included flag in DBQuery to run pathqueries with or without the index,
but for the command line only.

View differences:

src/edu/ucsb/nceas/metacat/QuerySpecification.java
134 134
         QuerySpecification qspec = 
135 135
                 new QuerySpecification(xml, util.getOption("saxparser"),
136 136
                                        util.getOption("accNumberSeparator"));
137
         System.out.println(qspec.printSQL());
137
         boolean useXMLIndex = false;
138
         System.out.println(qspec.printSQL(useXMLIndex));
138 139

  
139 140
       } catch (IOException e) {
140 141
         System.err.println(e.getMessage());
......
289 290
  /**
290 291
   * create a SQL serialization of the query that this instance represents
291 292
   */
292
  public String printSQL() {
293
  public String printSQL(boolean useXMLIndex) {
293 294
    StringBuffer self = new StringBuffer();
294 295

  
295 296
    self.append("SELECT docid,docname,doctype,");
......
297 298
    self.append("FROM xml_documents WHERE docid IN (");
298 299

  
299 300
    // This determines the documents that meet the query conditions
300
    self.append(query.printSQL());
301
    self.append(query.printSQL(useXMLIndex));
301 302

  
302 303
    self.append(") ");
303 304
 
......
556 557
    /**
557 558
     * create a SQL serialization of the query that this instance represents
558 559
     */
559
    public String printSQL() {
560
    public String printSQL(boolean useXMLIndex) {
560 561
      StringBuffer self = new StringBuffer();
561 562
      boolean first = true;
562 563

  
......
572 573
        }
573 574
        if (qobject instanceof QueryGroup) {
574 575
          QueryGroup qg = (QueryGroup)qobject;
575
          self.append(qg.printSQL());
576
          self.append(qg.printSQL(useXMLIndex));
576 577
        } else if (qobject instanceof QueryTerm) {
577 578
          QueryTerm qt = (QueryTerm)qobject;
578
          self.append(qt.printSQL());
579
          self.append(qt.printSQL(useXMLIndex));
579 580
        } else {
580 581
          System.err.println("qobject wrong type: fatal error");
581 582
        }
......
664 665
    /**
665 666
     * create a SQL serialization of the query that this instance represents
666 667
     */
667
    public String printSQL() {
668
    public String printSQL(boolean useXMLIndex) {
668 669
      StringBuffer self = new StringBuffer();
669 670

  
670 671
      // Uppercase the search string if case match is not important
......
696 697
      if (pathexpr != null) {
697 698
        self.append(nodedataterm + " LIKE " + "'" + searchvalue + "' ");
698 699
        self.append("AND parentnodeid IN ");
699
        self.append("(SELECT nodeid FROM xml_index WHERE path LIKE " + 
700
                    "'" +  pathexpr + "') " );
700
        // use XML Index
701
        if ( useXMLIndex ) {
702
          self.append("(SELECT nodeid FROM xml_index WHERE path LIKE " + 
703
                      "'" +  pathexpr + "') " );
704
        // without using XML Index; using nested statements instead
705
        } else {
706
          self.append(useNestedStatements(pathexpr));
707
        }
701 708
      } else {
702 709
        self.append(nodedataterm + " LIKE " + "'" + searchvalue + "' ");
703 710
      }
......
705 712
      return self.toString();
706 713
    }
707 714

  
715
    /* 
716
     * Constraint the query with @pathexp without using the XML Index,
717
     * but nested SQL statements instead. The query migth be slower.
718
     */
719
    private String useNestedStatements(String pathexpr)
720
    {
721
      StringBuffer nestedStmts = new StringBuffer();
722
      Vector nodes = new Vector();
723
      String path = pathexpr;
724
      int inx = 0;
725

  
726
      do {
727
        inx = path.lastIndexOf("/");
728
//System.out.println(path.substring(inx+1));
729
        nodes.addElement(path.substring(inx+1));
730
        path = path.substring(0, Math.abs(inx));
731
      } while ( inx > 0 );
732
      
733
      // nested statements
734
      int i = 0;
735
      for (i = 0; i < nodes.size()-1; i++) {
736
        nestedStmts.append("(SELECT nodeid FROM xml_nodes" + 
737
                           " WHERE nodename LIKE '" +
738
                             (String)nodes.elementAt(i) + "'" +
739
                           " AND parentnodeid IN ");
740
      }
741
      // for the last statement: it is without " AND parentnodeid IN "
742
      nestedStmts.append("(SELECT nodeid FROM xml_nodes" + 
743
                         " WHERE nodename LIKE '" +
744
                         (String)nodes.elementAt(i) + "'" );
745
      // node.size() number of closing brackets
746
      for (i = 0; i < nodes.size(); i++) {
747
        nestedStmts.append(")");
748
      }
749

  
750

  
751
//System.out.println(nestedStmts.toString());
752
      return nestedStmts.toString();
753
    }
754

  
708 755
    /**
709 756
     * create a String description of the query that this instance represents.
710 757
     * This should become a way to get the XML serialization of the query.
711 758
     */
712
    public String toString() {
759
    public String toString(boolean useXMLIndex) {
713 760

  
714
      return this.printSQL();
761
      return this.printSQL(useXMLIndex);
715 762
    }
716 763
  }
717 764
}
src/edu/ucsb/nceas/metacat/DBQuery.java
39 39
import java.util.Stack;
40 40
import java.util.Hashtable;
41 41
import java.util.Enumeration;
42
import java.io.File;
43
import java.io.FileWriter;
44
import java.io.BufferedWriter;
42 45

  
43 46
/** 
44 47
 * A Class that searches a relational DB for elements and 
......
68 71
     if (args.length < 1)
69 72
     {
70 73
        System.err.println("Wrong number of arguments!!!");
71
        System.err.println("USAGE: java DBQuery <xmlfile>");
74
        System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
72 75
        return;
73 76
     } else {
74 77
        try {
75
                    
76
          String xmlfile  = args[0];
77 78

  
79
          int i = 0;
80
          boolean showRuntime = false;
81
          boolean useXMLIndex = false;
82
          if ( args[i].equals( "-t" ) ) {
83
            showRuntime = true;
84
            i++;
85
          }
86
          if ( args[i].equals( "-index" ) ) {
87
            useXMLIndex = true;
88
            i++;
89
          } 
90
          String xmlfile  = args[i];
91

  
92
          // Time the request if asked for
93
          double startTime = System.currentTimeMillis();
94

  
78 95
          // Open a connection to the database
79 96
          MetaCatUtil   util = new MetaCatUtil();
80
          double startTime = System.currentTimeMillis();
81 97
          Connection dbconn = util.openDBConnection();
98

  
82 99
          double connTime = System.currentTimeMillis();
100

  
83 101
          // Execute the query
84 102
          DBQuery queryobj = new DBQuery(dbconn, util.getOption("saxparser"));
85 103
          FileReader xml = new FileReader(new File(xmlfile));
86 104
          Hashtable nodelist = null;
87
          nodelist = queryobj.findDocuments(xml, null, null);
88
          double stopTime = System.currentTimeMillis();
105
          nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
106

  
89 107
          // Print the reulting document listing
90 108
          StringBuffer result = new StringBuffer();
91 109
          String document = null;
92 110
          String docid = null;
93 111
          result.append("<?xml version=\"1.0\"?>\n");
94 112
          result.append("<resultset>\n"); 
95
          // following line removed by Dan Higgins to avoid insertion of 
96
          // query XML inside returned XML doc
97
          //result.append("  <query>" + xmlfile + "</query>\n");
113
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
114
  //        result.append("  <query>" + xmlfile + "</query>\n");
98 115
          Enumeration doclist = nodelist.keys(); 
99 116
          while (doclist.hasMoreElements()) {
100 117
            docid = (String)doclist.nextElement();
......
104 121
          }
105 122
          result.append("</resultset>\n");
106 123

  
107
          System.out.println(result);
108

  
124
          // Time the request if asked for
125
          double stopTime = System.currentTimeMillis();
109 126
          double dbOpenTime = (connTime - startTime)/1000;
110
          double insertTime = (stopTime - connTime)/1000;
127
          double readTime = (stopTime - connTime)/1000;
111 128
          double executionTime = (stopTime - startTime)/1000;
112
          System.out.println("\n\nTotal Execution time was: " + 
113
                             executionTime + " seconds.");
114
          System.out.println("Time to open DB connection was: " + dbOpenTime + 
115
                             " seconds.");
116
          System.out.println("Time to perform query was: " + insertTime +
117
                             " seconds.");
118
                    
129
          if (showRuntime) {
130
            System.out.println("\n\nTotal Execution time was: " + 
131
                               executionTime + " seconds.");
132
            System.out.println("Time to open DB connection was: " + dbOpenTime + 
133
                               " seconds.");
134
            System.out.println("Time to read was: " + readTime +
135
                               " seconds.");
136
          }
137
          //System.out.println(result);
138
          //write into a file "result.txt"
139
          File f = new File("./result.txt");
140
          FileWriter fw = new FileWriter(f);
141
          BufferedWriter out = new BufferedWriter(fw);
142
          out.write(result.toString());          
143
    	    out.flush();
144
	        out.close();
145
	        fw.close();
146

  
119 147
        } catch (Exception e) {
120 148
          System.err.println("Error in DBQuery.main");
121 149
          System.err.println(e.getMessage());
......
142 170
    this.parserName = parserName;
143 171
  }
144 172
  
145
  public Hashtable findDocuments(Reader xmlquery, String user, String group)
173
  public Hashtable findDocuments(Reader xmlquery, String user, String group,
174
                                 boolean useXMLIndex)
146 175
  {
147
    return findDocuments(xmlquery, user, group, null);
176
    return findDocuments(xmlquery, user, group, null, useXMLIndex);
148 177
  }
149 178
  
179
  public Hashtable findDocuments(Reader xmlquery, String user, String group,
180
                                 String[] returndoc)
181
  {
182
    return findDocuments(xmlquery, user, group, returndoc, true);
183
  }
184

  
150 185
  /** 
151 186
   * routine to search the elements and attributes looking to match query
152 187
   *
......
156 191
   * @param returndoc an array of document types to backtrack against.
157 192
   */
158 193
  public Hashtable findDocuments(Reader xmlquery, String user, String group,
159
                                 String[] returndoc)
194
                                 String[] returndoc, boolean useXMLIndex)
160 195
  {
161 196
    //System.out.println("in finddocuments");
162 197
      Hashtable   docListResult = new Hashtable();
......
191 226
                                   parserName, 
192 227
                                   util.getOption("accNumSeparator"));
193 228
       // System.out.println(qspec.printSQL());
194
        pstmt = dbconn.prepareStatement( qspec.printSQL() );
229
        pstmt = dbconn.prepareStatement( qspec.printSQL(useXMLIndex) );
195 230

  
196 231
        // Execute the SQL query using the JDBC connection
197 232
        pstmt.execute();

Also available in: Unified diff