You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2022/10/27 07:26:26 UTC

[jackrabbit-oak] branch OAK-9977b created (now 5e5b4af5a0)

This is an automated email from the ASF dual-hosted git repository.

thomasm pushed a change to branch OAK-9977b
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


      at 5e5b4af5a0 OAK-9977 The query recorder lists the same query many times

This branch includes the following new commits:

     new 5e5b4af5a0 OAK-9977 The query recorder lists the same query many times

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[jackrabbit-oak] 01/01: OAK-9977 The query recorder lists the same query many times

Posted by th...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

thomasm pushed a commit to branch OAK-9977b
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit 5e5b4af5a066e336baedd54fd48d349b6d0c4b52
Author: Thomas Mueller <th...@apache.org>
AuthorDate: Thu Oct 27 09:26:12 2022 +0200

    OAK-9977 The query recorder lists the same query many times
---
 .../jackrabbit/oak/query/stats/QueryRecorder.java  | 35 +++++++++++++++++++--
 .../oak/query/stats/QueryRecorderTest.java         | 36 ++++++++++++++++++++++
 2 files changed, 68 insertions(+), 3 deletions(-)

diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/query/stats/QueryRecorder.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/query/stats/QueryRecorder.java
index 2117addab8..40d3f59ba6 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/query/stats/QueryRecorder.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/query/stats/QueryRecorder.java
@@ -80,10 +80,39 @@ public class QueryRecorder {
     }
     
     public static String simplify(String query) {
+
+        // numbers
+        query = query.replaceAll("[0-9]+", "1");
+
+        // single quoted literals
         query = query.replaceAll("'[^']*'", "'x'");
-        query = query.replaceAll("ISDESCENDANTNODE\\(\\[/[^]]*\\]\\)", "ISDESCENDANTNODE('x')");
-        int pathIndex = query.indexOf("/jcr:root/");
-        if (pathIndex >= 0) {
+        query = query.replaceAll("('x')+", "'x'");
+
+        // doubly quoted literals
+        query = query.replaceAll("\"[^\"]*\"", "\"x\"");
+        query = query.replaceAll("(\"x\")+", "\"x\"");
+
+        // replace "in('x', 'x')" with "in('x')"
+        query = query.replaceAll("'x', ", "'x',");
+        query = query.replaceAll("('x','x')+", "'x'");
+
+        // usage of [/path] inside ISDESCENDANTNODE and so on
+        // (case insensitive)
+        query = query.replaceAll(
+                "(?i)(ISDESCENDANTNODE|ISCHILDNODE|ISSAMENODE)\\s*\\(.*,\\s*\\[/[^]]*\\]\\)",
+                "$1('x')");
+        query = query.replaceAll(
+                "(?i)(ISDESCENDANTNODE|ISCHILDNODE|ISSAMENODE)\\s*\\(\\[/[^]]*\\]\\)",
+                "$1('x')");
+
+        // the beginning of xpath queries, including xpath union
+        int startIndex = 0;
+        while (true) {
+            int pathIndex = query.indexOf("/jcr:root/", startIndex);
+            if (pathIndex < 0) {
+                break;
+            }
+            startIndex = pathIndex + 1;
             int start = pathIndex + "/jcr:root/".length();
             int end = getFirstOccurance(query, start,
                     " ", "/element(", "/text(", "/*", "/(", "/jcr:deref(");
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/query/stats/QueryRecorderTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/query/stats/QueryRecorderTest.java
index 2412d0b61a..f8a49ced1a 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/query/stats/QueryRecorderTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/query/stats/QueryRecorderTest.java
@@ -18,12 +18,48 @@ package org.apache.jackrabbit.oak.query.stats;
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.TreeSet;
+
 import org.junit.Test;
 
+import com.google.common.io.LineReader;
+
 public class QueryRecorderTest {
     
+    public static void main(String... args) throws IOException {
+        LineReader r = new LineReader(new FileReader("/Users/mueller/temp/queryRecorder.txt"));
+        TreeSet<String> set = new TreeSet<>();
+        while(true) {
+            String l = r.readLine();
+            if (l == null) {
+                break;
+            }
+            String s = QueryRecorder.simplify(l);
+            set.add(s);
+        }
+        for (String x : set) {
+            System.out.println(x);
+        }
+    }
+
     @Test
     public void simplify() {
+        // special cases
+        assertEquals("SELECT * FROM [nt:unstructured] AS node WHERE [id]='x'",
+                QueryRecorder.simplify("SELECT * FROM [nt:unstructured] AS node WHERE [id]='Joes''s'"));
+        assertEquals("SELECT * FROM [nt:unstructured] AS node WHERE [id]=1",
+                QueryRecorder.simplify("SELECT * FROM [nt:unstructured] AS node WHERE [id]=1002877501"));
+        assertEquals("SELECT p.* FROM [cq:Page] AS p WHERE isdescendantnode('x')", QueryRecorder
+                .simplify("SELECT p.* FROM [cq:Page] AS p WHERE isdescendantnode(p,[/content/kaufland/hr/hr/home])"));
+        assertEquals("SELECT p.* FROM [cq:Page] AS p WHERE ISDESCENDANTNODE('x')", QueryRecorder
+                .simplify("SELECT p.* FROM [cq:Page] AS p WHERE ISDESCENDANTNODE(p,[/content/kaufland/hr/hr/home])"));
+        assertEquals("SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE(\"x\") AND [cq:template] like \"x\"", QueryRecorder
+                .simplify("SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE(\"/test\") AND [cq:template] like \"abc\""));
+        assertEquals("(/jcr:root/a/b/.../* | /jcr:root/d/e/.../*)",
+                QueryRecorder.simplify("(/jcr:root/a/b/c/d//* | /jcr:root/d/e/f/g//*)"));
+
         // SQL-2
         // dummy
         assertEquals("SELECT sling:alias FROM nt:base WHERE sling:alias IS NOT NULL",