You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2023/01/06 14:01:33 UTC

[couchdb] 02/07: introduce our own abstraction over lucene fields

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

rnewson pushed a commit to branch import-nouveau-reorg
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit a1fc85901e2ac5d13bcac2c7cee4f1e1b8559778
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu Jan 5 23:52:58 2023 +0000

    introduce our own abstraction over lucene fields
---
 .../couchdb/nouveau/api/DocumentUpdateRequest.java | 10 ++--
 .../couchdb/nouveau/api/document/DocField.java     | 40 ++++++++++++++++
 .../nouveau/api/document/DoublePointDocField.java  | 44 +++++++++++++++++
 .../nouveau/api/document/StringDocField.java       | 56 ++++++++++++++++++++++
 .../couchdb/nouveau/api/document/TextDocField.java | 56 ++++++++++++++++++++++
 .../couchdb/nouveau/core/lucene9/Lucene9Index.java | 27 +++++++++--
 .../apache/couchdb/nouveau/IntegrationTest.java    | 10 ++--
 .../nouveau/api/DocumentUpdateRequestTest.java     | 17 ++++---
 8 files changed, 237 insertions(+), 23 deletions(-)

diff --git a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequest.java b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequest.java
index 92a8e2076..098c8ed7b 100644
--- a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequest.java
+++ b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequest.java
@@ -19,9 +19,9 @@ import javax.validation.constraints.Min;
 import javax.validation.constraints.NotEmpty;
 import javax.validation.constraints.NotNull;
 
-import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.couchdb.nouveau.api.document.DocField;
 
-import org.apache.couchdb.nouveau.l9x.lucene.index.IndexableField;
+import com.fasterxml.jackson.annotation.JsonProperty;
 
 import io.dropwizard.jackson.JsonSnakeCase;
 
@@ -34,13 +34,13 @@ public class DocumentUpdateRequest {
     private String partition;
 
     @NotEmpty
-    private Collection<@NotNull IndexableField> fields;
+    private Collection<org.apache.couchdb.nouveau.api.document.DocField> fields;
 
     public DocumentUpdateRequest() {
         // Jackson deserialization
     }
 
-    public DocumentUpdateRequest(long seq, String partition, Collection<IndexableField> fields) {
+    public DocumentUpdateRequest(long seq, String partition, Collection<DocField> fields) {
         this.seq = seq;
         this.partition = partition;
         this.fields = fields;
@@ -61,7 +61,7 @@ public class DocumentUpdateRequest {
     }
 
     @JsonProperty
-    public Collection<IndexableField> getFields() {
+    public Collection<DocField> getFields() {
         return fields;
     }
 
diff --git a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/DocField.java b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/DocField.java
new file mode 100644
index 000000000..54cd244a8
--- /dev/null
+++ b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/DocField.java
@@ -0,0 +1,40 @@
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.couchdb.nouveau.api.document;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "@type")
+@JsonSubTypes({
+        @JsonSubTypes.Type(value = StringDocField.class, name = "string"),
+        @JsonSubTypes.Type(value = TextDocField.class, name = "text"),
+        @JsonSubTypes.Type(value = DoublePointDocField.class, name = "double_point")
+})
+public abstract class DocField {
+
+    protected String name;
+
+    @JsonProperty
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}
diff --git a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/DoublePointDocField.java b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/DoublePointDocField.java
new file mode 100644
index 000000000..c36149f2d
--- /dev/null
+++ b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/DoublePointDocField.java
@@ -0,0 +1,44 @@
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.couchdb.nouveau.api.document;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class DoublePointDocField extends DocField {
+
+    private double value;
+
+    public DoublePointDocField() {
+    }
+
+    public DoublePointDocField(String name, double value) {
+        this.name = name;
+        this.value = value;
+    }
+
+    @JsonProperty
+    public double getValue() {
+        return value;
+    }
+
+    public void setValue(double value) {
+        this.value = value;
+    }
+
+    @Override
+    public String toString() {
+        return "DoublePointDocField [name=" + name + ", value=" + value + "]";
+    }
+
+}
diff --git a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/StringDocField.java b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/StringDocField.java
new file mode 100644
index 000000000..6675bc50c
--- /dev/null
+++ b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/StringDocField.java
@@ -0,0 +1,56 @@
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.couchdb.nouveau.api.document;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public final class StringDocField extends DocField {
+
+    private String value;
+
+    private boolean stored;
+
+    public StringDocField() {
+    }
+
+    public StringDocField(final String name, String value, boolean stored) {
+        this.name = name;
+        this.value = value;
+        this.stored = stored;
+    }
+
+    @JsonProperty
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    @JsonProperty
+    public boolean isStored() {
+        return stored;
+    }
+
+    public void setStored(boolean stored) {
+        this.stored = stored;
+    }
+
+    @Override
+    public String toString() {
+        return "StringDocField [name=" + name + ", value=" + value + ", stored=" + stored + "]";
+    }
+
+}
diff --git a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/TextDocField.java b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/TextDocField.java
new file mode 100644
index 000000000..bf5fa4404
--- /dev/null
+++ b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/api/document/TextDocField.java
@@ -0,0 +1,56 @@
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.couchdb.nouveau.api.document;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public final class TextDocField extends DocField {
+
+    private String value;
+
+    private boolean stored;
+
+    public TextDocField() {
+    }
+
+    public TextDocField(final String name, String value, boolean stored) {
+        this.name = name;
+        this.value = value;
+        this.stored = stored;
+    }
+
+    @JsonProperty
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    @JsonProperty
+    public boolean isStored() {
+        return stored;
+    }
+
+    public void setStored(boolean stored) {
+        this.stored = stored;
+    }
+
+    @Override
+    public String toString() {
+        return "TextDocField [name=" + name + ", value=" + value + ", stored=" + stored + "]";
+    }
+
+}
diff --git a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/core/lucene9/Lucene9Index.java b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/core/lucene9/Lucene9Index.java
index 6223fa143..c1ad64db5 100644
--- a/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/core/lucene9/Lucene9Index.java
+++ b/java/nouveau/server/src/main/java/org/apache/couchdb/nouveau/core/lucene9/Lucene9Index.java
@@ -32,11 +32,16 @@ import org.apache.couchdb.nouveau.api.DocumentUpdateRequest;
 import org.apache.couchdb.nouveau.api.SearchHit;
 import org.apache.couchdb.nouveau.api.SearchRequest;
 import org.apache.couchdb.nouveau.api.SearchResults;
+import org.apache.couchdb.nouveau.api.document.DocField;
+import org.apache.couchdb.nouveau.api.document.DoublePointDocField;
+import org.apache.couchdb.nouveau.api.document.StringDocField;
+import org.apache.couchdb.nouveau.api.document.TextDocField;
 import org.apache.couchdb.nouveau.core.Index;
 import org.apache.couchdb.nouveau.core.QueryParser;
 import org.apache.couchdb.nouveau.core.QueryParserException;
 import org.apache.couchdb.nouveau.l9x.lucene.analysis.Analyzer;
 import org.apache.couchdb.nouveau.l9x.lucene.document.Document;
+import org.apache.couchdb.nouveau.l9x.lucene.document.DoublePoint;
 import org.apache.couchdb.nouveau.l9x.lucene.document.SortedDocValuesField;
 import org.apache.couchdb.nouveau.l9x.lucene.document.StringField;
 import org.apache.couchdb.nouveau.l9x.lucene.document.Field.Store;
@@ -305,17 +310,33 @@ class Lucene9Index extends Index {
             result.add(new StringField("_partition", request.getPartition(), Store.NO));
         }
 
-        for (IndexableField field : request.getFields()) {
+        for (DocField field : request.getFields()) {
             // Underscore-prefix is reserved.
-            if (field.name().startsWith("_")) {
+            if (field.getName().startsWith("_")) {
                 continue;
             }
-            result.add(field);
+            result.add(convertField(field));
         }
 
         return result;
     }
 
+    private static IndexableField convertField(final DocField docField) {
+        if (docField instanceof StringDocField) {
+            final StringDocField field = (StringDocField) docField;
+            return new StringField(field.getName(), field.getValue(), field.isStored() ? Store.YES : Store.NO);
+        }
+        if (docField instanceof TextDocField) {
+            final TextDocField field = (TextDocField) docField;
+            return new StringField(field.getName(), field.getValue(), field.isStored() ? Store.YES : Store.NO);
+        }
+        if (docField instanceof DoublePointDocField) {
+            final DoublePointDocField field = (DoublePointDocField) docField;
+            return new DoublePoint(field.getName(), field.getValue());
+        }
+        throw new IllegalArgumentException(docField.getClass() + " not valid");
+    }
+
     private static Query docIdQuery(final String docId) {
         return new TermQuery(docIdTerm(docId));
     }
diff --git a/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/IntegrationTest.java b/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/IntegrationTest.java
index 4b9a851df..81ac17e56 100644
--- a/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/IntegrationTest.java
+++ b/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/IntegrationTest.java
@@ -28,9 +28,8 @@ import org.apache.couchdb.nouveau.api.DocumentUpdateRequest;
 import org.apache.couchdb.nouveau.api.IndexDefinition;
 import org.apache.couchdb.nouveau.api.SearchRequest;
 import org.apache.couchdb.nouveau.api.SearchResults;
-import org.apache.couchdb.nouveau.l9x.lucene.document.DoubleDocValuesField;
-import org.apache.couchdb.nouveau.l9x.lucene.document.DoublePoint;
-import org.apache.couchdb.nouveau.l9x.lucene.document.SortedSetDocValuesField;
+import org.apache.couchdb.nouveau.api.document.StringDocField;
+import org.apache.couchdb.nouveau.api.document.TextDocField;
 import org.apache.couchdb.nouveau.l9x.lucene.facet.range.DoubleRange;
 import org.apache.couchdb.nouveau.l9x.lucene.search.FieldDoc;
 import org.apache.couchdb.nouveau.l9x.lucene.search.TotalHits;
@@ -78,9 +77,8 @@ public class IntegrationTest {
         for (int i = 0; i < 10; i++) {
             final DocumentUpdateRequest docUpdate = new DocumentUpdateRequest(i + 1, null,
                 List.of(
-                    new DoublePoint("foo", i), 
-                    new DoubleDocValuesField("baz", i),
-                    new SortedSetDocValuesField("bar", new BytesRef("baz"))));
+                    new StringDocField("foo", "bar", true),
+                    new TextDocField("bar", "baz", true)));
             response = 
                 APP.client().target(String.format("%s/index/%s/doc/doc%d", url, indexName, i))
                 .request()
diff --git a/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequestTest.java b/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequestTest.java
index 60470c5d1..d146ad4fc 100644
--- a/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequestTest.java
+++ b/java/nouveau/server/src/test/java/org/apache/couchdb/nouveau/api/DocumentUpdateRequestTest.java
@@ -21,12 +21,11 @@ import java.util.List;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 
+import org.apache.couchdb.nouveau.api.document.DocField;
+import org.apache.couchdb.nouveau.api.document.DoublePointDocField;
+import org.apache.couchdb.nouveau.api.document.StringDocField;
+import org.apache.couchdb.nouveau.api.document.TextDocField;
 import org.apache.couchdb.nouveau.core.lucene9.Lucene9Module;
-import org.apache.couchdb.nouveau.l9x.lucene.document.DoublePoint;
-import org.apache.couchdb.nouveau.l9x.lucene.document.Field.Store;
-import org.apache.couchdb.nouveau.l9x.lucene.document.StringField;
-import org.apache.couchdb.nouveau.l9x.lucene.document.TextField;
-import org.apache.couchdb.nouveau.l9x.lucene.index.IndexableField;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
@@ -56,10 +55,10 @@ public class DocumentUpdateRequestTest {
     }
 
     private DocumentUpdateRequest asObject() {
-        final List<IndexableField> fields = new ArrayList<IndexableField>();
-        fields.add(new StringField("stringfoo", "bar", Store.YES));
-        fields.add(new TextField("textfoo", "hello there", Store.YES));
-        fields.add(new DoublePoint("doublefoo", 12));
+        final List<DocField> fields = new ArrayList<DocField>();
+        fields.add(new StringDocField("stringfoo", "bar", true));
+        fields.add(new TextDocField("textfoo", "hello there", true));
+        fields.add(new DoublePointDocField("doublefoo", 12));
         return new DocumentUpdateRequest(12, null, fields);
     }