Skip to content

DATACOUCH-407 - Use meta prefix for metadata properties in N1ql predi… #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@ public void testDeleteQuery() {
assertTrue(partyList.size() == 1);
}

@Test
public void testN1qlMetaPropertyConstructionInPartTree() {
partyRepository.save(new Party("testN1qlMetaPropertyConstructionInPartTree", "", "", null, 0, null));
List<Party> parties = partyRepository.findByKeyLike("%Meta%");
assertTrue("Party ids contain substring party", parties.size() >= 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ public interface PartyRepository extends CouchbaseRepository<Party, String> {
List<Party> findByDescriptionOrName(String description, String name);

List<Party> removeByDescriptionOrName(String description, String name);

List<Party> findByKeyLike(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.Assert.*;

import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -123,4 +124,11 @@ public void testPartTreeQuery() {
long count = partyRepository.countAllByDescriptionNotNull().block();
assertEquals("Test N1QL part tree based query", 15, count);
}

@Test
public void testN1qlMetaPropertyConstructionInPartTree() {
partyRepository.save(new Party("reactiveTestN1qlMetaPropertyConstructionInPartTree", "", "", null, 0, null)).block();
List<Party> parties = partyRepository.findByKeyLike("%Meta%").collectList().block();
assertTrue("Party ids contain substring party", parties.size() >= 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ public interface ReactivePartyRepository extends ReactiveCouchbaseRepository<Par

Flux<Party> findByDescriptionOrName(String description, String name);

Flux<Party> findByKeyLike(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class BasicCouchbasePersistentProperty
implements CouchbasePersistentProperty {

private final FieldNamingStrategy fieldNamingStrategy;
private final String ID_FIELD_NAME = "id";
private final String VERSION_FIELD_NAME = "cas";

/**
* Create a new instance of the BasicCouchbasePersistentProperty class.
Expand Down Expand Up @@ -75,6 +77,10 @@ protected Association<CouchbasePersistentProperty> createAssociation() {
*/
@Override
public String getFieldName() {

if (isIdProperty() && getOwner().getIdProperty().equals(this)) return ID_FIELD_NAME;
if (isVersionProperty()) return VERSION_FIELD_NAME;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style NIT: would be good to use braces, even for single-statement blocks.


com.couchbase.client.java.repository.annotation.Field annotation = getField().
getAnnotation(com.couchbase.client.java.repository.annotation.Field.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import static org.springframework.data.couchbase.core.support.TemplateUtils.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
Expand Down Expand Up @@ -60,18 +63,15 @@
* @author Mark Paluch
*/
public class N1qlUtils {
//As per https://docs.couchbase.com/server/5.5/n1ql/n1ql-language-reference/indexing-meta-info.html
final static Set<String> META_PROPERTIES = new HashSet<>(Arrays.asList("id", "expiration", "cas"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be private?


/**
* A converter that can be used to extract the {@link CouchbasePersistentProperty#getFieldName() fieldName},
* eg. when one wants a path from {@link PersistentPropertyPath#toDotPath(Converter)} made of escaped field names.
*/
public static final Converter<? super CouchbasePersistentProperty,String> FIELD_NAME_ESCAPED =
new Converter<CouchbasePersistentProperty, String>() {
@Override
public String convert(CouchbasePersistentProperty source) {
return "`" + source.getFieldName() + "`";
}
};
(source) -> META_PROPERTIES.contains(source.getFieldName()) ? "META()." + source.getFieldName() : "`" + source.getFieldName() + "`";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the document has a normal property called "id", "cas", or "expiration" -- can the user still reference it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes good point, will change the check to use the annotations instead and add the prefix.


/**
* Escape the given bucketName and produce an {@link Expression}.
Expand Down