http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaTypeResolverImpl.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaTypeResolverImpl.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaTypeResolverImpl.java new file mode 100644 index 0000000..3f4d6b8 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaTypeResolverImpl.java @@ -0,0 +1,124 @@ +package mvm.rya.api.resolver.impl; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import com.google.common.primitives.Bytes; +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.resolver.RyaTypeResolver; +import mvm.rya.api.resolver.RyaTypeResolverException; +import org.calrissian.mango.types.LexiTypeEncoders; +import org.calrissian.mango.types.TypeEncoder; +import org.openrdf.model.URI; +import org.openrdf.model.vocabulary.XMLSchema; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTE; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTES; + +/** + * Date: 7/16/12 + * Time: 12:42 PM + */ +public class RyaTypeResolverImpl implements RyaTypeResolver { + public static final int PLAIN_LITERAL_MARKER = 3; + public static final TypeEncoder<String, String> STRING_TYPE_ENCODER = LexiTypeEncoders + .stringEncoder(); + + protected byte markerByte; + protected URI dataType; + protected byte[] markerBytes; + + public RyaTypeResolverImpl() { + this((byte) PLAIN_LITERAL_MARKER, XMLSchema.STRING); + } + + public RyaTypeResolverImpl(byte markerByte, URI dataType) { + setMarkerByte(markerByte); + setRyaDataType(dataType); + } + + public void setMarkerByte(byte markerByte) { + this.markerByte = markerByte; + this.markerBytes = new byte[]{markerByte}; + } + + @Override + public byte getMarkerByte() { + return markerByte; + } + + @Override + public RyaRange transformRange(RyaRange ryaRange) throws RyaTypeResolverException { + return ryaRange; + } + + @Override + public byte[] serialize(RyaType ryaType) throws RyaTypeResolverException { + byte[][] bytes = serializeType(ryaType); + return Bytes.concat(bytes[0], bytes[1]); + } + + @Override + public byte[][] serializeType(RyaType ryaType) throws RyaTypeResolverException { + byte[] bytes = serializeData(ryaType.getData()).getBytes(); + return new byte[][]{bytes, Bytes.concat(TYPE_DELIM_BYTES, markerBytes)}; + } + + @Override + public URI getRyaDataType() { + return dataType; + } + + public void setRyaDataType(URI dataType) { + this.dataType = dataType; + } + + @Override + public RyaType newInstance() { + return new RyaType(); + } + + @Override + public boolean deserializable(byte[] bytes) { + return bytes != null && bytes.length >= 2 && bytes[bytes.length - 1] == getMarkerByte() && bytes[bytes.length - 2] == TYPE_DELIM_BYTE; + } + + protected String serializeData(String data) throws RyaTypeResolverException { + return STRING_TYPE_ENCODER.encode(data); + } + + @Override + public RyaType deserialize(byte[] bytes) throws RyaTypeResolverException { + if (!deserializable(bytes)) { + throw new RyaTypeResolverException("Bytes not deserializable"); + } + RyaType rt = newInstance(); + rt.setDataType(getRyaDataType()); + String data = new String(bytes, 0, bytes.length - 2); + rt.setData(deserializeData(data)); + return rt; + } + + protected String deserializeData(String data) throws RyaTypeResolverException { + return STRING_TYPE_ENCODER.decode(data); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaURIResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaURIResolver.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaURIResolver.java new file mode 100644 index 0000000..8f8bf00 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/RyaURIResolver.java @@ -0,0 +1,44 @@ +package mvm.rya.api.resolver.impl; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import org.openrdf.model.vocabulary.XMLSchema; + +/** + * Date: 7/16/12 + * Time: 12:41 PM + */ +public class RyaURIResolver extends RyaTypeResolverImpl { + + public static final int URI_MARKER = 2; + + public RyaURIResolver() { + super((byte) URI_MARKER, XMLSchema.ANYURI); + } + + @Override + public RyaType newInstance() { + return new RyaURI(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java new file mode 100644 index 0000000..ce3f05b --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java @@ -0,0 +1,45 @@ +package mvm.rya.api.resolver.impl; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import mvm.rya.api.resolver.RyaTypeResolver; +import mvm.rya.api.resolver.RyaTypeResolverMapping; + +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; + +/** + * Date: 8/29/12 + * Time: 2:04 PM + */ +public class ServiceBackedRyaTypeResolverMappings { + + public List<RyaTypeResolverMapping> getResolvers() { + ServiceLoader<RyaTypeResolver> loader = ServiceLoader.load(RyaTypeResolver.class); + List<RyaTypeResolverMapping> resolvers = new ArrayList<RyaTypeResolverMapping>(); + for (RyaTypeResolver aLoader : loader) { + resolvers.add(new RyaTypeResolverMapping(aLoader)); + } + return resolvers; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ShortRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ShortRyaTypeResolver.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ShortRyaTypeResolver.java new file mode 100644 index 0000000..dba9773 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/impl/ShortRyaTypeResolver.java @@ -0,0 +1,65 @@ +package mvm.rya.api.resolver.impl; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import mvm.rya.api.resolver.RyaTypeResolverException; +import org.calrissian.mango.types.LexiTypeEncoders; +import org.calrissian.mango.types.TypeEncoder; +import org.calrissian.mango.types.exception.TypeDecodingException; +import org.calrissian.mango.types.exception.TypeEncodingException; +import org.openrdf.model.vocabulary.XMLSchema; + +/** + */ +public class ShortRyaTypeResolver extends RyaTypeResolverImpl { + public static final int INTEGER_LITERAL_MARKER = 12; + public static final TypeEncoder<Integer, String> INTEGER_STRING_TYPE_ENCODER = LexiTypeEncoders + .integerEncoder(); + + public ShortRyaTypeResolver() { + super((byte) INTEGER_LITERAL_MARKER, XMLSchema.SHORT); + } + + @Override + protected String serializeData(String data) throws + RyaTypeResolverException { + try { + return INTEGER_STRING_TYPE_ENCODER.encode(Integer.parseInt(data)); + } catch (NumberFormatException e) { + throw new RyaTypeResolverException( + "Exception occurred serializing data[" + data + "]", e); + } catch (TypeEncodingException e) { + throw new RyaTypeResolverException( + "Exception occurred serializing data[" + data + "]", e); + } + } + + @Override + protected String deserializeData(String value) throws RyaTypeResolverException { + try { + return INTEGER_STRING_TYPE_ENCODER.decode(value).toString(); + } catch (TypeDecodingException e) { + throw new RyaTypeResolverException( + "Exception occurred deserializing data[" + value + "]", e); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRow.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRow.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRow.java new file mode 100644 index 0000000..f825e86 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRow.java @@ -0,0 +1,107 @@ +package mvm.rya.api.resolver.triple; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import java.util.Arrays; + +/** + * Date: 7/13/12 + * Time: 8:54 AM + */ +public class TripleRow { + private byte[] row, columnFamily, columnQualifier, columnVisibility, value; + private Long timestamp; + + public TripleRow(byte[] row, byte[] columnFamily, byte[] columnQualifier) { + this(row, columnFamily, columnQualifier, null, null, null); + } + public TripleRow(byte[] row, byte[] columnFamily, byte[] columnQualifier, Long timestamp, + byte[] columnVisibility, byte[] value) { + this.row = row; + this.columnFamily = columnFamily; + this.columnQualifier = columnQualifier; + //Default TS to current time to ensure the timestamps on all the tables are the same for the same triple + this.timestamp = timestamp != null ? timestamp : System.currentTimeMillis(); + this.columnVisibility = columnVisibility; + this.value = value; + } + + public byte[] getRow() { + return row; + } + + public byte[] getColumnFamily() { + return columnFamily; + } + + public byte[] getColumnQualifier() { + return columnQualifier; + } + + public byte[] getColumnVisibility() { + return columnVisibility; + } + + public byte[] getValue() { + return value; + } + + public Long getTimestamp() { + return timestamp; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TripleRow tripleRow = (TripleRow) o; + + if (!Arrays.equals(columnFamily, tripleRow.columnFamily)) return false; + if (!Arrays.equals(columnQualifier, tripleRow.columnQualifier)) return false; + if (!Arrays.equals(row, tripleRow.row)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = row != null ? Arrays.hashCode(row) : 0; + result = 31 * result + (columnFamily != null ? Arrays.hashCode(columnFamily) : 0); + result = 31 * result + (columnQualifier != null ? Arrays.hashCode(columnQualifier) : 0); + result = 31 * result + (columnVisibility != null ? Arrays.hashCode(columnVisibility) : 0); + result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "TripleRow{" + + "row=" + row + + ", columnFamily=" + columnFamily + + ", columnQualifier=" + columnQualifier + + ", columnVisibility=" + columnVisibility + + ", value=" + value + + ", timestamp=" + timestamp + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowRegex.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowRegex.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowRegex.java new file mode 100644 index 0000000..36d23df --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowRegex.java @@ -0,0 +1,84 @@ +package mvm.rya.api.resolver.triple; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import java.util.Arrays; + +/** + * Date: 7/13/12 + * Time: 8:54 AM + */ +public class TripleRowRegex { + private String row, columnFamily, columnQualifier; + + public TripleRowRegex(String row, String columnFamily, String columnQualifier) { + this.row = row; + this.columnFamily = columnFamily; + this.columnQualifier = columnQualifier; + } + + public String getRow() { + return row; + } + + public String getColumnFamily() { + return columnFamily; + } + + public String getColumnQualifier() { + return columnQualifier; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TripleRowRegex that = (TripleRowRegex) o; + + if (columnFamily != null ? !columnFamily.equals(that.columnFamily) : that.columnFamily != null) return false; + if (columnQualifier != null ? !columnQualifier.equals(that.columnQualifier) : that.columnQualifier != null) + return false; + if (row != null ? !row.equals(that.row) : that.row != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = row != null ? row.hashCode() : 0; + result = 31 * result + (columnFamily != null ? columnFamily.hashCode() : 0); + result = 31 * result + (columnQualifier != null ? columnQualifier.hashCode() : 0); + return result; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(); + sb.append("TripleRowRegex"); + sb.append("{row='").append(row).append('\''); + sb.append(", columnFamily='").append(columnFamily).append('\''); + sb.append(", columnQualifier='").append(columnQualifier).append('\''); + sb.append('}'); + return sb.toString(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolver.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolver.java new file mode 100644 index 0000000..2ccc986 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolver.java @@ -0,0 +1,43 @@ +package mvm.rya.api.resolver.triple; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import mvm.rya.api.RdfCloudTripleStoreConstants; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; + +import java.util.Map; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT; + +/** + * Date: 7/17/12 + * Time: 7:33 AM + */ +public interface TripleRowResolver { + + public Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize(RyaStatement statement) throws TripleRowResolverException; + + public RyaStatement deserialize(TABLE_LAYOUT table_layout, TripleRow tripleRow) throws TripleRowResolverException; + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolverException.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolverException.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolverException.java new file mode 100644 index 0000000..d1824b1 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/TripleRowResolverException.java @@ -0,0 +1,43 @@ +package mvm.rya.api.resolver.triple; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +/** + * Date: 7/17/12 + * Time: 7:35 AM + */ +public class TripleRowResolverException extends Exception { + public TripleRowResolverException() { + } + + public TripleRowResolverException(String s) { + super(s); + } + + public TripleRowResolverException(String s, Throwable throwable) { + super(s, throwable); + } + + public TripleRowResolverException(Throwable throwable) { + super(throwable); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java new file mode 100644 index 0000000..cbfc30f --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java @@ -0,0 +1,160 @@ +package mvm.rya.api.resolver.triple.impl; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTE; +import static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTES; +import static mvm.rya.api.RdfCloudTripleStoreConstants.EMPTY_BYTES; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTE; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.codec.binary.Hex; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import com.google.common.primitives.Bytes; + +import mvm.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; +import mvm.rya.api.resolver.triple.TripleRow; +import mvm.rya.api.resolver.triple.TripleRowResolver; +import mvm.rya.api.resolver.triple.TripleRowResolverException; + +/** + * Will store triple in spo, po, osp. Storing everything in the whole row. + * Date: 7/13/12 + * Time: 8:51 AM + */ +public class WholeRowHashedTripleResolver implements TripleRowResolver { + + @Override + public Map<TABLE_LAYOUT, TripleRow> serialize(RyaStatement stmt) throws TripleRowResolverException { + try { + RyaURI subject = stmt.getSubject(); + RyaURI predicate = stmt.getPredicate(); + RyaType object = stmt.getObject(); + RyaURI context = stmt.getContext(); + Long timestamp = stmt.getTimestamp(); + byte[] columnVisibility = stmt.getColumnVisibility(); + String qualifer = stmt.getQualifer(); + byte[] qualBytes = qualifer == null ? EMPTY_BYTES : qualifer.getBytes(); + byte[] value = stmt.getValue(); + assert subject != null && predicate != null && object != null; + byte[] cf = (context == null) ? EMPTY_BYTES : context.getData().getBytes(); + Map<TABLE_LAYOUT, TripleRow> tripleRowMap = new HashMap<TABLE_LAYOUT, TripleRow>(); + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] subjBytes = subject.getData().getBytes(); + byte[] subjHashBytes = md.digest(subjBytes); + byte[] predBytes = predicate.getData().getBytes(); + byte[] predHashBytes = md.digest(predBytes); + byte[][] objBytes = RyaContext.getInstance().serializeType(object); + tripleRowMap.put(TABLE_LAYOUT.SPO, + new TripleRow(Bytes.concat(Hex.encodeHexString(subjHashBytes).getBytes(), DELIM_BYTES, subjBytes, DELIM_BYTES, + predBytes, DELIM_BYTES, + objBytes[0], objBytes[1]), cf, qualBytes, + timestamp, columnVisibility, value)); + tripleRowMap.put(TABLE_LAYOUT.PO, + new TripleRow(Bytes.concat(Hex.encodeHexString(predHashBytes).getBytes(), DELIM_BYTES, predBytes, DELIM_BYTES, + objBytes[0], DELIM_BYTES, + subjBytes, objBytes[1]), cf, qualBytes, + timestamp, columnVisibility, value)); + tripleRowMap.put(TABLE_LAYOUT.OSP, + new TripleRow(Bytes.concat(objBytes[0], DELIM_BYTES, + subjBytes, DELIM_BYTES, + predBytes, objBytes[1]), cf, qualBytes, + timestamp, columnVisibility, value)); + return tripleRowMap; + } catch (RyaTypeResolverException e) { + throw new TripleRowResolverException(e); + } catch (NoSuchAlgorithmException e) { + throw new TripleRowResolverException(e); + } + } + + @Override + public RyaStatement deserialize(TABLE_LAYOUT table_layout, TripleRow tripleRow) throws TripleRowResolverException { + try { + assert tripleRow != null && table_layout != null; + byte[] row = tripleRow.getRow(); + + // if it is a hashed row, ony keep the row after the hash + if ((table_layout == TABLE_LAYOUT.SPO) || (table_layout == TABLE_LAYOUT.PO)) { + int hashStart = Bytes.indexOf(row, DELIM_BYTE); + row = Arrays.copyOfRange(row, hashStart + 1, row.length); + } + + int firstIndex = Bytes.indexOf(row, DELIM_BYTE); + byte[] first= Arrays.copyOf(row, firstIndex); + int secondIndex = Bytes.lastIndexOf(row, DELIM_BYTE); + int typeIndex = Bytes.indexOf(row, TYPE_DELIM_BYTE); + byte[] second = Arrays.copyOfRange(row, firstIndex + 1, secondIndex); + byte[] third = Arrays.copyOfRange(row, secondIndex + 1, typeIndex); + byte[] type = Arrays.copyOfRange(row, typeIndex, row.length); + byte[] columnFamily = tripleRow.getColumnFamily(); + boolean contextExists = columnFamily != null && columnFamily.length > 0; + RyaURI context = (contextExists) ? (new RyaURI(new String(columnFamily))) : null; + byte[] columnQualifier = tripleRow.getColumnQualifier(); + String qualifier = columnQualifier != null && columnQualifier.length > 0 ? new String(columnQualifier) : null; + Long timestamp = tripleRow.getTimestamp(); + byte[] columnVisibility = tripleRow.getColumnVisibility(); + byte[] value = tripleRow.getValue(); + + switch (table_layout) { + case SPO: { + byte[] obj = Bytes.concat(third, type); + return new RyaStatement( + new RyaURI(new String(first)), + new RyaURI(new String(second)), + RyaContext.getInstance().deserialize(obj), + context, qualifier, columnVisibility, value, timestamp); + } + case PO: { + byte[] obj = Bytes.concat(second, type); + return new RyaStatement( + new RyaURI(new String(third)), + new RyaURI(new String(first)), + RyaContext.getInstance().deserialize(obj), + context, qualifier, columnVisibility, value, timestamp); + } + case OSP: { + byte[] obj = Bytes.concat(first, type); + return new RyaStatement( + new RyaURI(new String(second)), + new RyaURI(new String(third)), + RyaContext.getInstance().deserialize(obj), + context, qualifier, columnVisibility, value, timestamp); + } + } + } catch (RyaTypeResolverException e) { + throw new TripleRowResolverException(e); + } + throw new TripleRowResolverException("TripleRow[" + tripleRow + "] with Table layout[" + table_layout + "] is not deserializable"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowTripleResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowTripleResolver.java b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowTripleResolver.java new file mode 100644 index 0000000..dc0695b --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/resolver/triple/impl/WholeRowTripleResolver.java @@ -0,0 +1,139 @@ +package mvm.rya.api.resolver.triple.impl; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import com.google.common.primitives.Bytes; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; +import mvm.rya.api.resolver.triple.TripleRow; +import mvm.rya.api.resolver.triple.TripleRowResolver; +import mvm.rya.api.resolver.triple.TripleRowResolverException; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.*; + +/** + * Will store triple in spo, po, osp. Storing everything in the whole row. + * Date: 7/13/12 + * Time: 8:51 AM + */ +public class WholeRowTripleResolver implements TripleRowResolver { + + @Override + public Map<TABLE_LAYOUT, TripleRow> serialize(RyaStatement stmt) throws TripleRowResolverException { + try { + RyaURI subject = stmt.getSubject(); + RyaURI predicate = stmt.getPredicate(); + RyaType object = stmt.getObject(); + RyaURI context = stmt.getContext(); + Long timestamp = stmt.getTimestamp(); + byte[] columnVisibility = stmt.getColumnVisibility(); + String qualifer = stmt.getQualifer(); + byte[] qualBytes = qualifer == null ? EMPTY_BYTES : qualifer.getBytes(); + byte[] value = stmt.getValue(); + assert subject != null && predicate != null && object != null; + byte[] cf = (context == null) ? EMPTY_BYTES : context.getData().getBytes(); + Map<TABLE_LAYOUT, TripleRow> tripleRowMap = new HashMap<TABLE_LAYOUT, TripleRow>(); + byte[] subjBytes = subject.getData().getBytes(); + byte[] predBytes = predicate.getData().getBytes(); + byte[][] objBytes = RyaContext.getInstance().serializeType(object); + tripleRowMap.put(TABLE_LAYOUT.SPO, + new TripleRow(Bytes.concat(subjBytes, DELIM_BYTES, + predBytes, DELIM_BYTES, + objBytes[0], objBytes[1]), cf, qualBytes, + timestamp, columnVisibility, value)); + tripleRowMap.put(TABLE_LAYOUT.PO, + new TripleRow(Bytes.concat(predBytes, DELIM_BYTES, + objBytes[0], DELIM_BYTES, + subjBytes, objBytes[1]), cf, qualBytes, + timestamp, columnVisibility, value)); + tripleRowMap.put(TABLE_LAYOUT.OSP, + new TripleRow(Bytes.concat(objBytes[0], DELIM_BYTES, + subjBytes, DELIM_BYTES, + predBytes, objBytes[1]), cf, qualBytes, + timestamp, columnVisibility, value)); + return tripleRowMap; + } catch (RyaTypeResolverException e) { + throw new TripleRowResolverException(e); + } + } + + @Override + public RyaStatement deserialize(TABLE_LAYOUT table_layout, TripleRow tripleRow) throws TripleRowResolverException { + try { + assert tripleRow != null && table_layout != null; + byte[] row = tripleRow.getRow(); + int firstIndex = Bytes.indexOf(row, DELIM_BYTE); + int secondIndex = Bytes.lastIndexOf(row, DELIM_BYTE); + int typeIndex = Bytes.indexOf(row, TYPE_DELIM_BYTE); + byte[] first = Arrays.copyOf(row, firstIndex); + byte[] second = Arrays.copyOfRange(row, firstIndex + 1, secondIndex); + byte[] third = Arrays.copyOfRange(row, secondIndex + 1, typeIndex); + byte[] type = Arrays.copyOfRange(row, typeIndex, row.length); + byte[] columnFamily = tripleRow.getColumnFamily(); + boolean contextExists = columnFamily != null && columnFamily.length > 0; + RyaURI context = (contextExists) ? (new RyaURI(new String(columnFamily))) : null; + byte[] columnQualifier = tripleRow.getColumnQualifier(); + String qualifier = columnQualifier != null && columnQualifier.length > 0 ? new String(columnQualifier) : null; + Long timestamp = tripleRow.getTimestamp(); + byte[] columnVisibility = tripleRow.getColumnVisibility(); + byte[] value = tripleRow.getValue(); + + switch (table_layout) { + case SPO: { + byte[] obj = Bytes.concat(third, type); + return new RyaStatement( + new RyaURI(new String(first)), + new RyaURI(new String(second)), + RyaContext.getInstance().deserialize(obj), + context, qualifier, columnVisibility, value, timestamp); + } + case PO: { + byte[] obj = Bytes.concat(second, type); + return new RyaStatement( + new RyaURI(new String(third)), + new RyaURI(new String(first)), + RyaContext.getInstance().deserialize(obj), + context, qualifier, columnVisibility, value, timestamp); + } + case OSP: { + byte[] obj = Bytes.concat(first, type); + return new RyaStatement( + new RyaURI(new String(second)), + new RyaURI(new String(third)), + RyaContext.getInstance().deserialize(obj), + context, qualifier, columnVisibility, value, timestamp); + } + } + } catch (RyaTypeResolverException e) { + throw new TripleRowResolverException(e); + } + throw new TripleRowResolverException("TripleRow[" + tripleRow + "] with Table layout[" + table_layout + "] is not deserializable"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/security/SecurityProvider.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/security/SecurityProvider.java b/common/rya.api/src/main/java/org/apache/rya/api/security/SecurityProvider.java new file mode 100644 index 0000000..61b14d9 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/security/SecurityProvider.java @@ -0,0 +1,28 @@ +package mvm.rya.api.security; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + +import javax.servlet.http.HttpServletRequest; + +public interface SecurityProvider { + + public String[] getUserAuths(HttpServletRequest incRequest); +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/CloseableIterableIteration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/CloseableIterableIteration.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/CloseableIterableIteration.java new file mode 100644 index 0000000..f3e5479 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/CloseableIterableIteration.java @@ -0,0 +1,76 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import info.aduna.iteration.CloseableIteration; + +import java.io.IOException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.calrissian.mango.collect.CloseableIterable; + +/** + * Date: 1/30/13 + * Time: 2:21 PM + */ +public class CloseableIterableIteration<T, X extends Exception> implements CloseableIteration<T, X> { + + private CloseableIterable<T> closeableIterable; + private final Iterator<T> iterator; + + private boolean isClosed = false; + + public CloseableIterableIteration(CloseableIterable<T> closeableIterable) { + this.closeableIterable = closeableIterable; + iterator = closeableIterable.iterator(); + } + + @Override + public void close() throws X { + try { + isClosed = true; + closeableIterable.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean hasNext() throws X { + return iterator.hasNext(); + } + + @Override + public T next() throws X { + if (!hasNext() || isClosed) { + throw new NoSuchElementException(); + } + + return iterator.next(); + } + + @Override + public void remove() throws X { + iterator.remove(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/EnumerationWrapper.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/EnumerationWrapper.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/EnumerationWrapper.java new file mode 100644 index 0000000..b098e52 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/EnumerationWrapper.java @@ -0,0 +1,58 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import info.aduna.iteration.CloseableIteration; + +import java.util.Enumeration; + +/** + * Date: 7/26/12 + * Time: 9:12 AM + */ +public class EnumerationWrapper<E, X extends Exception> implements CloseableIteration<E, X> { + private Enumeration<E> enumeration; + + public EnumerationWrapper(Enumeration<E> enumeration) { + this.enumeration = enumeration; + } + + @Override + public void close() throws X { + //nothing + } + + @Override + public boolean hasNext() throws X { + return enumeration.hasMoreElements(); + } + + @Override + public E next() throws X { + return enumeration.nextElement(); + } + + @Override + public void remove() throws X { + enumeration.nextElement(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/IteratorWrapper.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/IteratorWrapper.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/IteratorWrapper.java new file mode 100644 index 0000000..86748f9 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/IteratorWrapper.java @@ -0,0 +1,58 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import info.aduna.iteration.CloseableIteration; + +import java.util.Iterator; + +/** + * Date: 7/26/12 + * Time: 9:12 AM + */ +public class IteratorWrapper<E, X extends Exception> implements CloseableIteration<E, X> { + private Iterator<E> iterator; + + public IteratorWrapper(Iterator<E> iterator) { + this.iterator = iterator; + } + + @Override + public void close() throws X { + //nothing + } + + @Override + public boolean hasNext() throws X { + return iterator.hasNext(); + } + + @Override + public E next() throws X { + return iterator.next(); + } + + @Override + public void remove() throws X { + iterator.remove(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/NullableStatementImpl.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/NullableStatementImpl.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/NullableStatementImpl.java new file mode 100644 index 0000000..dfa17e8 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/NullableStatementImpl.java @@ -0,0 +1,105 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import org.openrdf.model.Resource; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; + +/** + * Class NullableStatementImpl + * Date: Feb 23, 2011 + * Time: 10:37:34 AM + */ +public class NullableStatementImpl implements Statement { + + private Resource subject; + private URI predicate; + private Value object; + private Resource[] contexts; + + public NullableStatementImpl(Resource subject, URI predicate, Value object, Resource... contexts) { + this.subject = subject; + this.predicate = predicate; + this.object = object; + this.contexts = contexts; + } + + @Override + public int hashCode() { + return 961 * ((this.getSubject() == null) ? (0) : (this.getSubject().hashCode())) + + 31 * ((this.getPredicate() == null) ? (0) : (this.getPredicate().hashCode())) + + ((this.getObject() == null) ? (0) : (this.getObject().hashCode())); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(256); + sb.append("("); + sb.append(getSubject()); + sb.append(", "); + sb.append(getPredicate()); + sb.append(", "); + sb.append(getObject()); + sb.append(")"); + return sb.toString(); + } + + @Override + public boolean equals(Object other) { + if (this == other) + return true; + if (other instanceof Statement) { + Statement otherSt = (Statement) other; + return this.hashCode() == otherSt.hashCode(); + } else { + return false; + } + } + + public Value getObject() { + return object; + } + + public URI getPredicate() { + return predicate; + } + + public Resource getSubject() { + return subject; + } + + public Resource getContext() { + if (contexts == null || contexts.length == 0) + return null; + else return contexts[0]; + } + + public Resource[] getContexts() { + return contexts; + } + + public void setContexts(Resource[] contexts) { + this.contexts = contexts; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/PeekingCloseableIteration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/PeekingCloseableIteration.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/PeekingCloseableIteration.java new file mode 100644 index 0000000..297c950 --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/PeekingCloseableIteration.java @@ -0,0 +1,74 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import com.google.common.base.Preconditions; +import info.aduna.iteration.CloseableIteration; + +/** + * Date: 7/24/12 + * Time: 4:40 PM + */ +public class PeekingCloseableIteration<E, X extends java.lang.Exception> implements CloseableIteration<E, X> { + + private final CloseableIteration<E, X> iteration; + private boolean hasPeeked; + private E peekedElement; + + public PeekingCloseableIteration(CloseableIteration<E, X> iteration) { + this.iteration = Preconditions.checkNotNull(iteration); + } + + @Override + public void close() throws X { + iteration.close(); + } + + public boolean hasNext() throws X { + return hasPeeked || iteration.hasNext(); + } + + public E next() throws X { + if (!hasPeeked) { + return iteration.next(); + } else { + E result = peekedElement; + hasPeeked = false; + peekedElement = null; + return result; + } + } + + public void remove() throws X { + Preconditions.checkState(!hasPeeked, "Can't remove after you've peeked at next"); + iteration.remove(); + } + + public E peek() throws X { + if (!hasPeeked) { + peekedElement = iteration.next(); + hasPeeked = true; + } + return peekedElement; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementAddBindingSetFunction.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementAddBindingSetFunction.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementAddBindingSetFunction.java new file mode 100644 index 0000000..0fc2a7f --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementAddBindingSetFunction.java @@ -0,0 +1,40 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import com.google.common.base.Function; +import mvm.rya.api.RdfCloudTripleStoreUtils; +import mvm.rya.api.domain.RyaStatement; +import org.openrdf.query.BindingSet; + +import java.util.Map; + +/** + * Date: 1/18/13 + * Time: 1:25 PM + */ +public class RyaStatementAddBindingSetFunction implements Function<RyaStatement, Map.Entry<RyaStatement, BindingSet>> { + @Override + public Map.Entry<RyaStatement, BindingSet> apply(RyaStatement ryaStatement) { + return new RdfCloudTripleStoreUtils.CustomEntry<mvm.rya.api.domain.RyaStatement, org.openrdf.query.BindingSet>(ryaStatement, null); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java b/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java new file mode 100644 index 0000000..b39fafe --- /dev/null +++ b/common/rya.api/src/main/java/org/apache/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java @@ -0,0 +1,61 @@ +package mvm.rya.api.utils; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + + + +import info.aduna.iteration.CloseableIteration; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.persist.RyaDAOException; +import org.openrdf.query.BindingSet; + +import java.util.Map; + +/** + * Date: 1/18/13 + * Time: 1:22 PM + */ +public class RyaStatementRemoveBindingSetCloseableIteration implements CloseableIteration<RyaStatement, RyaDAOException>{ + + private CloseableIteration<? extends Map.Entry<RyaStatement, BindingSet>, RyaDAOException> iter; + + public RyaStatementRemoveBindingSetCloseableIteration(CloseableIteration<? extends Map.Entry<RyaStatement, BindingSet>, RyaDAOException> iter) { + this.iter = iter; + } + + @Override + public void close() throws RyaDAOException { + iter.close(); + } + + @Override + public boolean hasNext() throws RyaDAOException { + return iter.hasNext(); + } + + @Override + public RyaStatement next() throws RyaDAOException { + return iter.next().getKey(); + } + + @Override + public void remove() throws RyaDAOException { + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/test/java/mvm/rya/api/domain/RyaTypeTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/domain/RyaTypeTest.java b/common/rya.api/src/test/java/mvm/rya/api/domain/RyaTypeTest.java deleted file mode 100644 index 6db6053..0000000 --- a/common/rya.api/src/test/java/mvm/rya/api/domain/RyaTypeTest.java +++ /dev/null @@ -1,119 +0,0 @@ -package mvm.rya.api.domain; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import org.junit.Assert; -import org.junit.Test; -import org.openrdf.model.vocabulary.XMLSchema; - -public class RyaTypeTest { - static RyaType a = new RyaType(XMLSchema.STRING, "http://www.example.com/Alice"); - static RyaType b = new RyaType(XMLSchema.STRING, "http://www.example.com/Bob"); - static RyaType c = new RyaType(XMLSchema.STRING, "http://www.example.com/Carol"); - static RyaType aUri = new RyaType(XMLSchema.ANYURI, "http://www.example.com/Alice"); - static RyaType bUri = new RyaType(XMLSchema.ANYURI, "http://www.example.com/Bob"); - RyaType nullData = new RyaType(XMLSchema.STRING, null); - RyaType nullType = new RyaType(null, "http://www.example.com/Alice"); - RyaType nullBoth = new RyaType(null, null); - RyaType same = new RyaType(XMLSchema.STRING, "http://www.example.com/Alice"); - - @Test - public void testCompareTo() throws Exception { - Assert.assertEquals("compareTo(self) should return zero.", 0, aUri.compareTo(aUri)); - Assert.assertFalse("compareTo should return nonzero for different data and type.", aUri.compareTo(b) == 0); - Assert.assertFalse("compareTo should return nonzero for same data and different datatypes.", a.compareTo(aUri) == 0); - Assert.assertFalse("compareTo should return nonzero for same datatype and different data.", bUri.compareTo(aUri) == 0); - Assert.assertEquals("compareTo should return zero for different objects with matching data and datatype.", - 0, a.compareTo(same)); - } - - @Test - public void testCompareToNullFields() throws Exception { - Assert.assertEquals("[has no nulls].compareTo([has null data]) should return -1", -1, a.compareTo(nullData)); - Assert.assertEquals("[has no nulls].compareTo([has null type]) should return -1 if data is equal", - -1, a.compareTo(nullType)); - Assert.assertEquals("[has null data].compareTo([has no nulls]) should return 1", 1, nullData.compareTo(a)); - Assert.assertEquals("[has null type].compareTo([has no nulls]) should return 1 if data is equal", - 1, nullType.compareTo(a)); - Assert.assertEquals("[has null type].compareTo([has null data]) should return -1", -1, nullType.compareTo(nullData)); - } - - @Test - public void testCompareToSymmetry() throws Exception { - int forward = Integer.signum(a.compareTo(b)); - int backward = Integer.signum(b.compareTo(a)); - Assert.assertEquals("Comparison of different values with same type should yield opposite signs.", forward, backward * -1); - forward = Integer.signum(bUri.compareTo(b)); - backward = Integer.signum(b.compareTo(bUri)); - Assert.assertEquals("Comparison of same values with different types should yield opposite signs.", forward, backward*-1); - forward = Integer.signum(aUri.compareTo(b)); - backward = Integer.signum(b.compareTo(aUri)); - Assert.assertEquals("Comparison of different values with different types should yield opposite signs.", - forward, backward * -1); - } - - @Test - public void testCompareToTransitive() throws Exception { - int sign = Integer.signum(a.compareTo(b)); - Assert.assertEquals("compareTo(a,b) and compareTo(b,c) should have the same sign.", - sign, Integer.signum(b.compareTo(c))); - Assert.assertEquals("if a > b > c, compareTo(a,c) should be consistent.", sign, Integer.signum(a.compareTo(c))); - } - - @Test - public void testEquals() throws Exception { - Assert.assertTrue("Same data and datatype should be equal.", a.equals(same)); - Assert.assertFalse("Same data, different datatype should be unequal.", a.equals(aUri)); - Assert.assertFalse("Same datatype, different data should be unequal.", a.equals(b)); - } - - @Test - public void testEqualsNullFields() throws Exception { - Assert.assertFalse("equals(null) should return false.", a.equals(null)); - Assert.assertFalse("Same data, one null datatype should be unequal.", a.equals(nullType)); - Assert.assertFalse("Same datatype, one null data should be unequal.", a.equals(nullData)); - RyaType sameNull = new RyaType(null, null); - Assert.assertTrue("Matching null fields should be equal.", sameNull.equals(nullBoth)); - } - - @Test - public void testEqualsCompareToConsistency() throws Exception { - Assert.assertEquals("equals and compareTo inconsistent for matching values and types.", - a.equals(same), a.compareTo(same) == 0); - Assert.assertEquals("equals and compareTo inconsistent for different values with same types.", - a.equals(b), a.compareTo(b) == 0); - Assert.assertEquals("equals and compareTo inconsistent for same values having different types.", - a.equals(aUri), a.compareTo(aUri) == 0); - Assert.assertEquals("equals and compareTo inconsistent for different values and different types.", - a.equals(bUri), a.compareTo(bUri) == 0); - } - - @Test - public void testHashCodeEquals() throws Exception { - Assert.assertEquals("Same data and same type should yield same hash code.", - a.hashCode(), same.hashCode()); - Assert.assertEquals("Same type and both null data should yield same hash code.", - nullData.hashCode(), new RyaType(XMLSchema.STRING, null).hashCode()); - Assert.assertEquals("Same data and both null type should yield same hash code.", - nullType.hashCode(), new RyaType(null, "http://www.example.com/Alice").hashCode()); - Assert.assertEquals("Null type and null data should yield same hash code.", - nullBoth.hashCode(), new RyaType(null, null).hashCode()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/test/java/mvm/rya/api/domain/RyaURIPrefixTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/domain/RyaURIPrefixTest.java b/common/rya.api/src/test/java/mvm/rya/api/domain/RyaURIPrefixTest.java deleted file mode 100644 index 3966679..0000000 --- a/common/rya.api/src/test/java/mvm/rya/api/domain/RyaURIPrefixTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package mvm.rya.api.domain; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - - -import junit.framework.TestCase; - -/** - * Date: 7/24/12 - * Time: 3:30 PM - */ -public class RyaURIPrefixTest extends TestCase { - - public void testPrefix() throws Exception { - String prefix = "urn:test#"; - RyaURIPrefix uriPrefix = new RyaURIPrefix(prefix); - assertEquals(prefix, uriPrefix.getPrefix()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsTest.java b/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsTest.java deleted file mode 100644 index c613fee..0000000 --- a/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsTest.java +++ /dev/null @@ -1,140 +0,0 @@ -package mvm.rya.api.instance; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import static org.junit.Assert.assertEquals; - -import java.util.Date; - -import org.junit.Test; - -import com.google.common.base.Optional; - -import mvm.rya.api.instance.RyaDetails.EntityCentricIndexDetails; -import mvm.rya.api.instance.RyaDetails.FreeTextIndexDetails; -import mvm.rya.api.instance.RyaDetails.GeoIndexDetails; -import mvm.rya.api.instance.RyaDetails.JoinSelectivityDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails.PCJUpdateStrategy; -import mvm.rya.api.instance.RyaDetails.ProspectorDetails; -import mvm.rya.api.instance.RyaDetails.TemporalIndexDetails; - -/** - * Tests the methods of {@link RyaDetails}. - */ -public class RyaDetailsTest { - - @Test - public void equals() { - final RyaDetails.Builder builder = RyaDetails.builder(); - - builder.setRyaInstanceName("test_instance") - .setRyaVersion("1.2.3.4") - .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) - .setGeoIndexDetails( new GeoIndexDetails(true) ) - .setTemporalIndexDetails( new TemporalIndexDetails(true) ) - .setFreeTextDetails( new FreeTextIndexDetails(true) ) - .setPCJIndexDetails( - PCJIndexDetails.builder() - .setEnabled(true) - .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 1") - .setUpdateStrategy(PCJUpdateStrategy.BATCH) - .setLastUpdateTime( new Date() )) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 2") - .setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL))) - .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) - .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ); - - final RyaDetails details1 = builder.build(); - final RyaDetails details2 = builder.build(); - assertEquals(details1, details2); - } - - @Test - public void hashcode() { - final RyaDetails.Builder builder = RyaDetails.builder(); - - builder.setRyaInstanceName("test_instance") - .setRyaVersion("1.2.3.4") - .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) - .setGeoIndexDetails( new GeoIndexDetails(true) ) - .setTemporalIndexDetails( new TemporalIndexDetails(true) ) - .setFreeTextDetails( new FreeTextIndexDetails(true) ) - .setPCJIndexDetails( - PCJIndexDetails.builder() - .setEnabled(true) - .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 1") - .setUpdateStrategy(PCJUpdateStrategy.BATCH) - .setLastUpdateTime( new Date() )) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 2") - .setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL))) - .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) - .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ); - - final RyaDetails details1 = builder.build(); - final RyaDetails details2 = builder.build(); - assertEquals(details1.hashCode(), details2.hashCode()); - } - - @Test - public void constructor() { - final RyaDetails originalDetails = RyaDetails.builder() - .setRyaInstanceName("test_instance") - .setRyaVersion("1.2.3.4") - .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) - .setGeoIndexDetails( new GeoIndexDetails(true) ) - .setTemporalIndexDetails( new TemporalIndexDetails(true) ) - .setFreeTextDetails( new FreeTextIndexDetails(true) ) - .setPCJIndexDetails( - PCJIndexDetails.builder() - .setEnabled(true) - .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 1") - .setUpdateStrategy(PCJUpdateStrategy.BATCH) - .setLastUpdateTime( new Date() )) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 2") - .setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL))) - .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) - .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ) - .build(); - - // Create a new Builder using another RyaDetails object. - final RyaDetails.Builder builder = new RyaDetails.Builder( originalDetails ); - - // Show it builds the object that was passed into it. - assertEquals(originalDetails, builder.build()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsToConfigurationTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsToConfigurationTest.java b/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsToConfigurationTest.java deleted file mode 100644 index 960e8a9..0000000 --- a/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsToConfigurationTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package mvm.rya.api.instance; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import static mvm.rya.api.instance.ConfigurationFields.USE_ENTITY; -import static mvm.rya.api.instance.ConfigurationFields.USE_FREETEXT; -import static mvm.rya.api.instance.ConfigurationFields.USE_GEO; -import static mvm.rya.api.instance.ConfigurationFields.USE_PCJ; -import static mvm.rya.api.instance.ConfigurationFields.USE_TEMPORAL; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.Date; - -import org.apache.hadoop.conf.Configuration; -import org.junit.Test; - -import com.google.common.base.Optional; - -import mvm.rya.api.instance.RyaDetails.EntityCentricIndexDetails; -import mvm.rya.api.instance.RyaDetails.FreeTextIndexDetails; -import mvm.rya.api.instance.RyaDetails.GeoIndexDetails; -import mvm.rya.api.instance.RyaDetails.JoinSelectivityDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails.PCJUpdateStrategy; -import mvm.rya.api.instance.RyaDetails.ProspectorDetails; -import mvm.rya.api.instance.RyaDetails.TemporalIndexDetails; - -public class RyaDetailsToConfigurationTest { - @Test - public void populateConfigTest() { - final RyaDetails.Builder builder = RyaDetails.builder(); - - builder.setRyaInstanceName("test_instance") - .setRyaVersion("1.2.3.4") - .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) - .setGeoIndexDetails( new GeoIndexDetails(true) ) - .setTemporalIndexDetails( new TemporalIndexDetails(true) ) - .setFreeTextDetails( new FreeTextIndexDetails(false) ) - .setPCJIndexDetails( - PCJIndexDetails.builder() - .setEnabled(true) - .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 1") - .setUpdateStrategy(PCJUpdateStrategy.BATCH) - .setLastUpdateTime( new Date() )) - .addPCJDetails( - PCJDetails.builder() - .setId("pcj 2") - .setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL))) - .setProspectorDetails( new ProspectorDetails(Optional.of(new Date())) ) - .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date())) ); - final Configuration conf = new Configuration(); - RyaDetailsToConfiguration.addRyaDetailsToConfiguration(builder.build(), conf); - - //defaults are set to cause the assert to fail - assertTrue(conf.getBoolean(USE_ENTITY, false)); - assertFalse(conf.getBoolean(USE_FREETEXT, true)); - assertTrue(conf.getBoolean(USE_GEO, false)); - assertTrue(conf.getBoolean(USE_TEMPORAL, false)); - assertTrue(conf.getBoolean(USE_PCJ, false)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/44a2dcf0/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsUpdaterTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsUpdaterTest.java b/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsUpdaterTest.java deleted file mode 100644 index 596cc7b..0000000 --- a/common/rya.api/src/test/java/mvm/rya/api/instance/RyaDetailsUpdaterTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 mvm.rya.api.instance; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Date; - -import org.junit.Test; - -import com.google.common.base.Optional; - -import mvm.rya.api.instance.RyaDetails.EntityCentricIndexDetails; -import mvm.rya.api.instance.RyaDetails.FreeTextIndexDetails; -import mvm.rya.api.instance.RyaDetails.GeoIndexDetails; -import mvm.rya.api.instance.RyaDetails.JoinSelectivityDetails; -import mvm.rya.api.instance.RyaDetails.PCJIndexDetails; -import mvm.rya.api.instance.RyaDetails.ProspectorDetails; -import mvm.rya.api.instance.RyaDetails.TemporalIndexDetails; -import mvm.rya.api.instance.RyaDetailsRepository.ConcurrentUpdateException; -import mvm.rya.api.instance.RyaDetailsRepository.NotInitializedException; -import mvm.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException; -import mvm.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator; -import mvm.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator.CouldNotApplyMutationException; - -/** - * Tests the methods of {@link RyaDetailsUpdater}. - */ -public class RyaDetailsUpdaterTest { - - @Test - public void update() throws RyaDetailsRepositoryException, CouldNotApplyMutationException { - // Setup initial details and mock a repository that returns them. - final RyaDetails originalDetails = RyaDetails.builder() - .setRyaInstanceName("instanceName") - .setRyaVersion("0.0.0.0") - .setFreeTextDetails( new FreeTextIndexDetails(true) ) - .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) - .setGeoIndexDetails( new GeoIndexDetails(true) ) - .setTemporalIndexDetails( new TemporalIndexDetails(true) ) - .setPCJIndexDetails( - PCJIndexDetails.builder() - .setEnabled(true) ) - .setJoinSelectivityDetails( new JoinSelectivityDetails( Optional.<Date>absent() ) ) - .setProspectorDetails( new ProspectorDetails( Optional.<Date>absent() )) - .build(); - - final RyaDetailsRepository detailsRepo = mock( RyaDetailsRepository.class ); - when( detailsRepo.getRyaInstanceDetails() ).thenReturn( originalDetails ); - - // Use an updater to change the Rya version number. - new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() { - @Override - public RyaDetails mutate(final RyaDetails old) { - return RyaDetails.builder(old) - .setRyaVersion("1.1.1.1") - .build(); - } - }); - - // Verify the repository was asked to update the details. - final RyaDetails mutatedDetails = RyaDetails.builder(originalDetails) - .setRyaVersion("1.1.1.1") - .build(); - verify(detailsRepo, times(1)).update( eq(originalDetails), eq(mutatedDetails) ); - } - - @Test - public void update_concurrentUpdateEncountered() throws NotInitializedException, RyaDetailsRepositoryException, CouldNotApplyMutationException { - // Setup initial details and mock a repository that returns them. - final RyaDetails originalDetails = RyaDetails.builder() - .setRyaInstanceName("instanceName") - .setRyaVersion("0.0.0.0") - .setFreeTextDetails( new FreeTextIndexDetails(true) ) - .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) - .setGeoIndexDetails( new GeoIndexDetails(true) ) - .setTemporalIndexDetails( new TemporalIndexDetails(true) ) - .setPCJIndexDetails( - PCJIndexDetails.builder() - .setEnabled(true) ) - .setJoinSelectivityDetails( new JoinSelectivityDetails( Optional.<Date>absent() ) ) - .setProspectorDetails( new ProspectorDetails( Optional.<Date>absent() )) - .build(); - - final RyaDetails otherUsersUpdate = RyaDetails.builder(originalDetails) - .setRyaVersion("1.1.1.1") - .build(); - - // The first time get detail is called, we get the original state for the details. - // When the mutator tries to update using those, it throws an exception. - // The second iteration, the other user's state is updated. - // When the mutator tries to update again, it succeeds. - final RyaDetailsRepository detailsRepo = mock( RyaDetailsRepository.class ); - - when( detailsRepo.getRyaInstanceDetails() ) - .thenReturn( originalDetails ) - .thenReturn( otherUsersUpdate ); - - doThrow( ConcurrentUpdateException.class ).when( detailsRepo ).update( eq(originalDetails), any(RyaDetails.class) ); - - // Run the test. - new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() { - @Override - public RyaDetails mutate(final RyaDetails old) { - return RyaDetails.builder(old) - .setTemporalIndexDetails( new TemporalIndexDetails(false) ) - .build(); - } - }); - - // Verify the intended mutation eventually gets committed. - final RyaDetails finalDetails = RyaDetails.builder(originalDetails) - .setRyaVersion("1.1.1.1") - .setTemporalIndexDetails( new TemporalIndexDetails(false) ) - .build(); - - verify(detailsRepo, times(1)).update( eq(otherUsersUpdate), eq(finalDetails) ); - } -} \ No newline at end of file