]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/IndexQueries.java
Added new field TypeId to dependency index for exact type searching
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / genericrelation / IndexQueries.java
index 135c5ebc2a107b02d20923db0cdd0eda52f082a8..ee6296fdd4ecb7a1fbc02e4934119f456aabf0f1 100644 (file)
@@ -1,5 +1,9 @@
 package org.simantics.db.layer0.genericrelation;
 
+import java.util.Collection;
+
+import org.simantics.datatypes.literal.GUID;
+import org.simantics.db.Resource;
 
 /**
  * This class contains utilities related to queries made into Lucene indexes,
@@ -52,7 +56,7 @@ public class IndexQueries {
                        // These characters are part of the query syntax and must be escaped
                        if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
                                        || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
-                                       || c == '|' || c == '&' || c == '/' || (escapeWildcards && (c == '*' || c == '?'))) {
+                                       || c == '|' || c == '&' || c == '/' || c == ' ' || (escapeWildcards && (c == '*' || c == '?'))) {
                                sb.append('\\');
                                sb.append(c);
                                lastWhitespace = false;
@@ -95,7 +99,7 @@ public class IndexQueries {
                        // These characters are part of the query syntax and must be escaped
                        if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
                                        || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
-                                       || c == '|' || c == '&' || c == '/' || (escapeWildcards && (c == '*' || c == '?'))) {
+                                       || c == '|' || c == '&' || c == '/' || c == ' ' || (escapeWildcards && (c == '*' || c == '?'))) {
                                return true;
                        } else if (Character.isWhitespace(c)) {
                                lastWhitespace = true;
@@ -113,8 +117,9 @@ public class IndexQueries {
        }
 
        private static final String[] RESERVED_WORDS = {
-               "AND", "and",
-               "OR", "or"
+               "AND", "\\AND",
+               "OR", "\\OR",
+               "NOT", "\\NOT",
        };
 
        /**
@@ -158,8 +163,97 @@ public class IndexQueries {
        }
 
        public static String escapeTerm(String field, String term, boolean escapeWildcards) {
+               return escapeTerm(field, term, escapeWildcards, new StringBuilder()).toString();
+       }
+
+       public static StringBuilder quoteTerm(String field, String term, StringBuilder result) {
+               if (field != null)
+                       result.append(field).append(':');
+               result.append("\"");
+               result.append(term.replaceAll("(\"|\\\\)", "\\\\$0"));
+               result.append("\"");
+               return result;
+       }
+
+       public static String quoteTerm(String term) {
+               return quoteTerm(null, term, new StringBuilder(term.length()*2)).toString();
+       }
+
+       public static String quoteTerm(String field, String term) {
+               return quoteTerm(field, term,
+                               new StringBuilder(
+                                               term.length()*2
+                                               + (field != null ? field.length() + 1 : 0))
+                               ).toString();
+       }
+
+       public static StringBuilder appendLongTerm(StringBuilder sb, String field, long term) {
+               return sb.append(field).append(':').append(term);
+       }
+
+       public static String longTerm(String field, long term) {
+               return appendLongTerm(new StringBuilder(), field, term).toString();
+       }
+
+       public static StringBuilder appendResourceIdTerm(StringBuilder sb, String field, Resource term) {
+               return appendLongTerm(sb, field, term.getResourceId());
+       }
+
+       public static String resourceIdTerm(String field, Resource term) {
+               return appendLongTerm(new StringBuilder(), field, term.getResourceId()).toString();
+       }
+
+       private static String join(String withString, String... exps) {
+               if (exps.length == 0)
+                       return "";
+               StringBuilder sb = new StringBuilder(128);
+               for (int i = 0; i < exps.length - 1; ++i) {
+                       sb.append(exps[i]).append(withString);
+               }
+               sb.append(exps[exps.length - 1]);
+               return sb.toString();
+       }
+
+       public static String and(String exp1, String exp2) {
+               return exp1 + " AND " + exp2;
+       }
+
+       public static String and(String... exps) {
+               return join(" AND ", exps);
+       }
+
+       public static String or(String exp1, String exp2) {
+               return exp1 + " OR " + exp2;
+       }
+
+       public static String or(String... exps) {
+               return join(" OR ", exps);
+       }
+
+       public static String idFromGUID(GUID guid) {
+               return guid != null ? guid.indexString() : "";
+       }
+
+       public static String toResourceIdString(Resource r, Collection<Resource> rs) {
+               StringBuilder sb = new StringBuilder();
+               sb.append(r.getResourceId());
+               for (Resource rr : rs)
+                       sb.append(' ').append(rr.getResourceId());
+               return sb.toString();
+       }
+
+       public static String toResourceIdString(Collection<Resource> rs) {
+               if (rs.isEmpty())
+                       return "";
                StringBuilder sb = new StringBuilder();
-               return escapeTerm(field, term, escapeWildcards, sb).toString();
+               boolean first = true;
+               for (Resource rr : rs) {
+                       if (!first)
+                               sb.append(' ');
+                       first = false;
+                       sb.append(rr.getResourceId());
+               }
+               return sb.toString();
        }
 
 //     public static void main(String[] args) {