Skip to content

Commit

Permalink
[proxima-core] #30 add proxy attributes to entities
Browse files Browse the repository at this point in the history
  • Loading branch information
je-ik committed Dec 13, 2017
1 parent 41561e1 commit ca6dc50
Show file tree
Hide file tree
Showing 26 changed files with 2,249 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.datastax.driver.core.Statement;
import com.typesafe.config.ConfigFactory;
import cz.o2.proxima.repository.AttributeDescriptor;
import cz.o2.proxima.repository.AttributeDescriptorBase;
import cz.o2.proxima.repository.EntityDescriptor;
import cz.o2.proxima.repository.Repository;
import cz.o2.proxima.storage.Partition;
Expand Down Expand Up @@ -201,8 +202,8 @@ public BoundStatement scanPartition(


Repository repo = Repository.Builder.ofTest(ConfigFactory.defaultApplication()).build();
AttributeDescriptor attr;
AttributeDescriptor attrWildcard;
AttributeDescriptorBase<?> attr;
AttributeDescriptorBase<?> attrWildcard;
EntityDescriptor entity;

public CassandraDBAccessorTest() throws URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import cz.o2.proxima.repository.AttributeDescriptor;
import cz.o2.proxima.repository.AttributeDescriptorBase;
import cz.o2.proxima.repository.EntityDescriptor;
import cz.o2.proxima.repository.Repository;
import cz.o2.proxima.storage.StreamElement;
Expand All @@ -47,8 +48,8 @@ public class DefaultCQLFactoryTest {

final Config cfg = ConfigFactory.defaultApplication();
final Repository repo = Repository.Builder.ofTest(cfg).build();
final AttributeDescriptor attr;
final AttributeDescriptor attrWildcard;
final AttributeDescriptorBase<?> attr;
final AttributeDescriptorBase<?> attrWildcard;
final EntityDescriptor entity;
final PreparedStatement statement = mock(PreparedStatement.class);
final Session session = mock(Session.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.datastax.driver.core.Session;
import com.typesafe.config.ConfigFactory;
import cz.o2.proxima.repository.AttributeDescriptor;
import cz.o2.proxima.repository.AttributeDescriptorBase;
import cz.o2.proxima.repository.EntityDescriptor;
import cz.o2.proxima.repository.Repository;
import cz.o2.proxima.storage.StreamElement;
Expand All @@ -44,7 +45,7 @@
public class TransformingCQLFactoryTest {

Repository repo = Repository.Builder.ofTest(ConfigFactory.defaultApplication()).build();
AttributeDescriptor attr;
AttributeDescriptorBase<?> attr;
EntityDescriptor entity;

final List<String> statements = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
*/
public interface AttributeDescriptor<T> extends Serializable {

static class NoOpTransform implements ProxyTransform {

@Override
public String fromProxy(String access) {
return access;
}

@Override
public String toProxy(String raw) {
return raw;
}

}


class Builder {

Expand All @@ -52,7 +66,7 @@ private Builder(Repository repo) {
private URI schemeURI;

@SuppressWarnings("unchecked")
public <T> AttributeDescriptor<T> build() {
public <T> AttributeDescriptorImpl<T> build() {
Objects.requireNonNull(name, "Please specify name");
Objects.requireNonNull(entity, "Please specify entity");
Objects.requireNonNull(schemeURI, "Please specify scheme URI");
Expand All @@ -66,6 +80,14 @@ static Builder newBuilder(Repository repo) {
return new Builder(repo);
}

static <T> AttributeDescriptorBase<T> newProxy(
String name,
AttributeDescriptorBase<T> target,
ProxyTransform transform) {

return new AttributeProxyDescriptorImpl<>(name, target, transform);
}

/** Retrieve name of the attribute. */
String getName();

Expand Down Expand Up @@ -101,4 +123,10 @@ default String toAttributePrefix() {
*/
ValueSerializer<T> getValueSerializer();

/**
* Marker if this is a public attribute.
* @return {@code true} it this is public attribute
*/
boolean isPublic();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright 2017 O2 Czech Republic, a.s.
*
* 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 cz.o2.proxima.repository;

import cz.o2.proxima.scheme.ValueSerializer;
import cz.o2.proxima.storage.OnlineAttributeWriter;
import java.net.URI;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;

/**
* Base class for {@link AttributeDescriptorImpl} and {@link AttributeProxyDescriptorImpl}.
*/
public abstract class AttributeDescriptorBase<T> implements AttributeDescriptor<T> {

@Getter
protected final String entity;

@Getter
protected final String name;

@Getter
protected final URI schemeURI;

@Getter
protected final boolean proxy;

@Getter
protected final boolean wildcard;

@Getter
protected final ValueSerializer<T> valueSerializer;

@Getter
@Setter
protected OnlineAttributeWriter writer = null;

public AttributeDescriptorBase(
String name, String entity, URI schemeURI,
ValueSerializer<T> valueSerializer) {

this.name = Objects.requireNonNull(name);
this.entity = Objects.requireNonNull(entity);
this.schemeURI = Objects.requireNonNull(schemeURI);
this.wildcard = this.name.endsWith(".*");
this.proxy = false;
this.valueSerializer = Objects.requireNonNull(valueSerializer);
if (this.wildcard) {
if (name.length() < 3
|| name.substring(0, name.length() - 1).contains("*")
|| name.charAt(name.length() - 2) != '.') {

throw new IllegalArgumentException(
"Please specify wildcard attributes only in the format `<name>.*; for now. "
+ "That is - wildcard attributes can contain only single asterisk "
+ "right after a dot at the end of the attribute name. "
+ "This is implementation constraint for now.");
}
}
}

public AttributeDescriptorBase(String name, AttributeDescriptorBase<T> target) {
this.name = Objects.requireNonNull(name);
this.entity = target.getEntity();
this.schemeURI = target.getSchemeURI();
this.proxy = true;
this.wildcard = target.isWildcard();
this.valueSerializer = target.getValueSerializer();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof AttributeDescriptor) {
AttributeDescriptor other = (AttributeDescriptor) obj;
return Objects.equals(other.getEntity(), entity) && Objects.equals(other.getName(), name);
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(entity, name);
}

/**
* Retrieve name of the attribute if not wildcard, otherwise
* retrieve the prefix without the last asterisk.
*/
@Override
public String toAttributePrefix(boolean includeLastDot) {
if (isWildcard()) {
return name.substring(0, name.length() - (includeLastDot ? 1 : 2));
}
return name;
}

@Override
public boolean isPublic() {
return !name.startsWith("_");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,91 +17,26 @@
package cz.o2.proxima.repository;

import java.net.URI;
import java.util.Objects;
import lombok.Getter;
import cz.o2.proxima.storage.OnlineAttributeWriter;
import cz.o2.proxima.scheme.ValueSerializer;

/**
* Descriptor of attribute of entity.
*/
public class AttributeDescriptorImpl<T> implements AttributeDescriptor<T> {

@Getter
private final String name;

@Getter
private final boolean isWildcard;

@Getter
private final URI schemeURI;

@Getter
private final ValueSerializer<T> valueSerializer;

@Getter
private final String entity;

@Getter
private OnlineAttributeWriter writer;
public class AttributeDescriptorImpl<T>
extends AttributeDescriptorBase<T> {

AttributeDescriptorImpl(
String name, String entity,
URI schemeURI, ValueSerializer<T> parser) {

this.name = Objects.requireNonNull(name);
this.schemeURI = Objects.requireNonNull(schemeURI);
this.valueSerializer = Objects.requireNonNull(parser);
this.entity = Objects.requireNonNull(entity);
this.isWildcard = this.name.contains("*");
if (this.isWildcard) {
if (name.length() < 3
|| name.substring(0, name.length() - 1).contains("*")
|| name.charAt(name.length() - 2) != '.') {

throw new IllegalArgumentException(
"Please specify wildcard attributes only in the format `<name>.*; for now. "
+ "That is - wildcard attributes can contain only single asterisk "
+ "right after a dot at the end of the attribute name. "
+ "This is implementation constraint for now.");
}
}
}

public void setWriter(OnlineAttributeWriter writer) {
this.writer = writer;
}
URI schemeURI, ValueSerializer<T> serializer) {

@Override
public boolean equals(Object obj) {
if (obj instanceof AttributeDescriptor) {
AttributeDescriptor other = (AttributeDescriptor) obj;
return Objects.equals(other.getEntity(), entity)
&& Objects.equals(other.getName(), name);
}
return false;
super(name, entity, schemeURI, serializer);
}

@Override
public int hashCode() {
return Objects.hash(entity, name);
}

@Override
public String toString() {
return "AttributeDescriptor(entity=" + entity + ", name=" + name + ")";
}

/**
* Retrieve name of the attribute if not wildcard, otherwise
* retrieve the prefix without the last asterisk.
*/
@Override
public String toAttributePrefix(boolean includeLastDot) {
if (isWildcard()) {
return name.substring(0, name.length() - (includeLastDot ? 1 : 2));
}
return name;
}

}

0 comments on commit ca6dc50

Please sign in to comment.