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 12, 2017
1 parent cdac9c8 commit 777dcb1
Show file tree
Hide file tree
Showing 25 changed files with 951 additions and 193 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* 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;
}

}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* A family of attributes with the same storage.
*/
public class AttributeFamilyDescriptor<W extends AttributeWriterBase> {
public class AttributeFamilyDescriptor {

public static final class Builder {

Expand Down Expand Up @@ -82,13 +82,13 @@ public static final class Builder {

private Builder() { }

public Builder addAttribute(AttributeDescriptor desc) {
public Builder addAttribute(AttributeDescriptor<?> desc) {
attributes.add(desc);
return this;
}

public AttributeFamilyDescriptor<AttributeWriterBase> build() {
return new AttributeFamilyDescriptor<>(
public AttributeFamilyDescriptor build() {
return new AttributeFamilyDescriptor(
name, type, attributes, writer, commitLog, batchObservable,
randomAccess, partitionedView, access, filter);
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public static Builder newBuilder() {
* Writer associated with this attribute family.
*/
@Nullable
private final W writer;
private final AttributeWriterBase writer;

@Nullable
private final CommitLogReader commitLogReader;
Expand All @@ -133,10 +133,10 @@ public static Builder newBuilder() {
@Nullable
private final PartitionedView partitionedView;

private AttributeFamilyDescriptor(String name,
AttributeFamilyDescriptor(String name,
StorageType type,
List<AttributeDescriptor<?>> attributes,
@Nullable W writer,
@Nullable AttributeWriterBase writer,
@Nullable CommitLogReader commitLogReader,
@Nullable BatchLogObservable batchObservable,
@Nullable RandomAccessReader randomAccess,
Expand Down Expand Up @@ -183,7 +183,7 @@ public int hashCode() {
* Retrieve writer for this family.
* Empty if this family is not writable
*/
public Optional<W> getWriter() {
public Optional<AttributeWriterBase> getWriter() {
if (!access.isReadonly()) {
return Optional.of(Objects.requireNonNull(writer));
}
Expand Down
Loading

0 comments on commit 777dcb1

Please sign in to comment.