http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaURIResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaURIResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaURIResolver.java new file mode 100644 index 0000000..daa3721 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaURIResolver.java @@ -0,0 +1,43 @@ +package mvm.rya.api.resolver.impl; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java new file mode 100644 index 0000000..bc886b4 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ServiceBackedRyaTypeResolverMappings.java @@ -0,0 +1,44 @@ +package mvm.rya.api.resolver.impl; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ShortRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ShortRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ShortRyaTypeResolver.java new file mode 100644 index 0000000..ff859c9 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ShortRyaTypeResolver.java @@ -0,0 +1,64 @@ +package mvm.rya.api.resolver.impl; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRow.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRow.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRow.java new file mode 100644 index 0000000..ee731c4 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRow.java @@ -0,0 +1,106 @@ +package mvm.rya.api.resolver.triple; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowRegex.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowRegex.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowRegex.java new file mode 100644 index 0000000..353191d --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowRegex.java @@ -0,0 +1,83 @@ +package mvm.rya.api.resolver.triple; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolver.java new file mode 100644 index 0000000..53250af --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolver.java @@ -0,0 +1,42 @@ +package mvm.rya.api.resolver.triple; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolverException.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolverException.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolverException.java new file mode 100644 index 0000000..7c286d2 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/TripleRowResolverException.java @@ -0,0 +1,42 @@ +package mvm.rya.api.resolver.triple; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +/** + * 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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java new file mode 100644 index 0000000..61e1330 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowHashedTripleResolver.java @@ -0,0 +1,153 @@ +package mvm.rya.api.resolver.triple.impl; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +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 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(subjHashBytes, 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(predHashBytes, 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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowTripleResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowTripleResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowTripleResolver.java new file mode 100644 index 0000000..c637fce --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/triple/impl/WholeRowTripleResolver.java @@ -0,0 +1,138 @@ +package mvm.rya.api.resolver.triple.impl; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/security/SecurityProvider.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/security/SecurityProvider.java b/common/rya.api/src/main/java/mvm/rya/api/security/SecurityProvider.java new file mode 100644 index 0000000..314ed62 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/security/SecurityProvider.java @@ -0,0 +1,8 @@ +package mvm.rya.api.security; + +import javax.servlet.http.HttpServletRequest; + +public interface SecurityProvider { + + public String[] getUserAuths(HttpServletRequest incRequest); +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/CloseableIterableIteration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/CloseableIterableIteration.java b/common/rya.api/src/main/java/mvm/rya/api/utils/CloseableIterableIteration.java new file mode 100644 index 0000000..f947c94 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/CloseableIterableIteration.java @@ -0,0 +1,75 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/EnumerationWrapper.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/EnumerationWrapper.java b/common/rya.api/src/main/java/mvm/rya/api/utils/EnumerationWrapper.java new file mode 100644 index 0000000..9baa619 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/EnumerationWrapper.java @@ -0,0 +1,57 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/IteratorWrapper.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/IteratorWrapper.java b/common/rya.api/src/main/java/mvm/rya/api/utils/IteratorWrapper.java new file mode 100644 index 0000000..8860d99 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/IteratorWrapper.java @@ -0,0 +1,57 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/NullableStatementImpl.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/NullableStatementImpl.java b/common/rya.api/src/main/java/mvm/rya/api/utils/NullableStatementImpl.java new file mode 100644 index 0000000..c5f7622 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/NullableStatementImpl.java @@ -0,0 +1,104 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/PeekingCloseableIteration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/PeekingCloseableIteration.java b/common/rya.api/src/main/java/mvm/rya/api/utils/PeekingCloseableIteration.java new file mode 100644 index 0000000..70b89d0 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/PeekingCloseableIteration.java @@ -0,0 +1,73 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementAddBindingSetFunction.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementAddBindingSetFunction.java b/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementAddBindingSetFunction.java new file mode 100644 index 0000000..0bb9420 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementAddBindingSetFunction.java @@ -0,0 +1,39 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java b/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java new file mode 100644 index 0000000..0bd3ca1 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/utils/RyaStatementRemoveBindingSetCloseableIteration.java @@ -0,0 +1,60 @@ +package mvm.rya.api.utils; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/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 new file mode 100644 index 0000000..66d6134 --- /dev/null +++ b/common/rya.api/src/test/java/mvm/rya/api/domain/RyaURIPrefixTest.java @@ -0,0 +1,36 @@ +package mvm.rya.api.domain; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +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/92ddfa59/common/rya.api/src/test/java/mvm/rya/api/persist/query/RyaQueryTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/persist/query/RyaQueryTest.java b/common/rya.api/src/test/java/mvm/rya/api/persist/query/RyaQueryTest.java new file mode 100644 index 0000000..6eae3d0 --- /dev/null +++ b/common/rya.api/src/test/java/mvm/rya/api/persist/query/RyaQueryTest.java @@ -0,0 +1,62 @@ +package mvm.rya.api.persist.query; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaURI; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + */ +public class RyaQueryTest { + + @Test + public void testBuildQueryWithOptions() { + RyaURI subj = new RyaURI("urn:test#1234"); + RyaURI pred = new RyaURI("urn:test#pred"); + RyaURI obj = new RyaURI("urn:test#obj"); + RyaStatement ryaStatement = new RyaStatement(subj, pred, obj); + String[] auths = {"U,FOUO"}; + long currentTime = System.currentTimeMillis(); + RyaQuery ryaQuery = RyaQuery.builder(ryaStatement).setAuths(auths).setNumQueryThreads(4).setRegexObject("regexObj") + .setRegexPredicate("regexPred").setRegexSubject("regexSubj").setTtl(100l).setBatchSize(10). + setCurrentTime(currentTime).setMaxResults(1000l) + .build(); + + assertNotNull(ryaQuery); + assertEquals(ryaStatement, ryaQuery.getQuery()); + assertEquals(4, (int) ryaQuery.getNumQueryThreads()); + assertEquals("regexObj", ryaQuery.getRegexObject()); + assertEquals("regexPred", ryaQuery.getRegexPredicate()); + assertEquals("regexSubj", ryaQuery.getRegexSubject()); + assertEquals(100l, (long) ryaQuery.getTtl()); + assertEquals(10, (int) ryaQuery.getBatchSize()); + assertEquals(currentTime, (long) ryaQuery.getCurrentTime()); + assertEquals(1000l, (long) ryaQuery.getMaxResults()); + assertTrue(Arrays.equals(auths, ryaQuery.getAuths())); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/test/java/mvm/rya/api/query/strategy/AbstractTriplePatternStrategyTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/query/strategy/AbstractTriplePatternStrategyTest.java b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/AbstractTriplePatternStrategyTest.java new file mode 100644 index 0000000..be234a3 --- /dev/null +++ b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/AbstractTriplePatternStrategyTest.java @@ -0,0 +1,191 @@ +package mvm.rya.api.query.strategy; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +import static mvm.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT.OSP; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import junit.framework.TestCase; +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.RdfCloudTripleStoreConstants; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.query.strategy.wholerow.OspWholeRowTriplePatternStrategy; +import mvm.rya.api.query.strategy.wholerow.PoWholeRowTriplePatternStrategy; +import mvm.rya.api.query.strategy.wholerow.SpoWholeRowTriplePatternStrategy; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTripleContext; +import mvm.rya.api.resolver.triple.TripleRow; +import mvm.rya.api.resolver.triple.TripleRowRegex; +import mvm.rya.api.resolver.triple.impl.WholeRowTripleResolver; + +import org.openrdf.model.vocabulary.XMLSchema; + +/** + * Date: 7/25/12 + * Time: 11:41 AM + */ +public class AbstractTriplePatternStrategyTest extends TestCase { + public class MockRdfConfiguration extends RdfCloudTripleStoreConfiguration { + + @Override + public RdfCloudTripleStoreConfiguration clone() { + return new MockRdfConfiguration(); + } + + } + + public void testRegex() throws Exception { + RyaURI subj = new RyaURI("urn:test#1234"); + RyaURI pred = new RyaURI("urn:test#pred"); + RyaURI obj = new RyaURI("urn:test#obj"); + RyaStatement ryaStatement = new RyaStatement(subj, pred, obj); + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = new WholeRowTripleResolver().serialize(ryaStatement); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + String row = new String(tripleRow.getRow()); + TriplePatternStrategy spoStrategy = new SpoWholeRowTriplePatternStrategy(); + TriplePatternStrategy poStrategy = new PoWholeRowTriplePatternStrategy(); + TriplePatternStrategy ospStrategy = new OspWholeRowTriplePatternStrategy(); + //pred + TripleRowRegex tripleRowRegex = spoStrategy.buildRegex(null, pred.getData(), null, null, null); + Pattern p = Pattern.compile(tripleRowRegex.getRow()); + Matcher matcher = p.matcher(row); + assertTrue(matcher.matches()); + //subj + tripleRowRegex = spoStrategy.buildRegex(subj.getData(), null, null, null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertTrue(matcher.matches()); + //obj + tripleRowRegex = spoStrategy.buildRegex(null, null, obj.getData(), null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertTrue(matcher.matches()); + + //po table + row = new String(serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO).getRow()); + tripleRowRegex = poStrategy.buildRegex(null, pred.getData(), null, null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertTrue(matcher.matches()); + + tripleRowRegex = poStrategy.buildRegex(null, pred.getData(), obj.getData(), null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertTrue(matcher.matches()); + + tripleRowRegex = poStrategy.buildRegex(subj.getData(), pred.getData(), obj.getData(), null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertTrue(matcher.matches()); + + //various regex + tripleRowRegex = poStrategy.buildRegex(null, "urn:test#pr[e|d]{2}", null, null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertTrue(matcher.matches()); + + //does not match + tripleRowRegex = poStrategy.buildRegex(null, "hello", null, null, null); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertFalse(matcher.matches()); + } + + public void testObjectTypeInfo() throws Exception { + RyaURI subj = new RyaURI("urn:test#1234"); + RyaURI pred = new RyaURI("urn:test#pred"); + RyaType obj = new RyaType(XMLSchema.LONG, "10"); + RyaStatement ryaStatement = new RyaStatement(subj, pred, obj); + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = RyaTripleContext.getInstance(new MockRdfConfiguration()).serializeTriple(ryaStatement); + TripleRow tripleRow = serialize.get(SPO); + + String row = new String(tripleRow.getRow()); + TriplePatternStrategy spoStrategy = new SpoWholeRowTriplePatternStrategy(); + //obj + byte[][] bytes = RyaContext.getInstance().serializeType(obj); + String objStr = new String(bytes[0]); + byte[] objectTypeInfo = bytes[1]; + TripleRowRegex tripleRowRegex = spoStrategy.buildRegex(null, null, + objStr + , null, objectTypeInfo); + Pattern p = Pattern.compile(tripleRowRegex.getRow()); + Matcher matcher = p.matcher(row); + assertTrue(matcher.matches()); + + //build row with same object str data + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> dupTriple_str = RyaTripleContext.getInstance(new MockRdfConfiguration()).serializeTriple( + new RyaStatement(subj, pred, new RyaType(XMLSchema.STRING, objStr)) + ); + TripleRow tripleRow_dup_str = dupTriple_str.get(SPO); + + row = new String(tripleRow_dup_str.getRow()); + spoStrategy = new SpoWholeRowTriplePatternStrategy(); + + tripleRowRegex = spoStrategy.buildRegex(null, null, + objStr + , null, objectTypeInfo); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(row); + assertFalse(matcher.matches()); + + //po table + TriplePatternStrategy poStrategy = new PoWholeRowTriplePatternStrategy(); + tripleRowRegex = poStrategy.buildRegex(null, null, + objStr + , null, objectTypeInfo); + p = Pattern.compile(tripleRowRegex.getRow()); + String po_row = new String(serialize.get(PO).getRow()); + matcher = p.matcher(po_row); + assertTrue(matcher.matches()); + + tripleRowRegex = poStrategy.buildRegex(null, null, + objStr + , null, objectTypeInfo); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(new String(dupTriple_str.get(PO).getRow())); + assertFalse(matcher.matches()); + + //osp table + TriplePatternStrategy ospStrategy = new OspWholeRowTriplePatternStrategy(); + tripleRowRegex = ospStrategy.buildRegex(null, null, + objStr + , null, objectTypeInfo); + p = Pattern.compile(tripleRowRegex.getRow()); + String osp_row = new String(serialize.get(OSP).getRow()); + matcher = p.matcher(osp_row); + assertTrue(matcher.matches()); + + tripleRowRegex = ospStrategy.buildRegex(null, null, + objStr + , null, objectTypeInfo); + p = Pattern.compile(tripleRowRegex.getRow()); + matcher = p.matcher(new String(dupTriple_str.get(OSP).getRow())); + assertFalse(matcher.matches()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategyTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategyTest.java b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategyTest.java new file mode 100644 index 0000000..0ecec9b --- /dev/null +++ b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategyTest.java @@ -0,0 +1,174 @@ +package mvm.rya.api.query.strategy.wholerow; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ + +import junit.framework.TestCase; +import mvm.rya.api.RdfCloudTripleStoreConstants; +import mvm.rya.api.domain.*; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTripleContext; +import mvm.rya.api.resolver.triple.TripleRow; + +import org.apache.hadoop.io.Text; +import org.junit.Before; +import org.openrdf.model.impl.URIImpl; + +import java.util.Map; + +/** + * Date: 7/14/12 + * Time: 11:46 AM + */ +public class HashedPoWholeRowTriplePatternStrategyTest extends TestCase { + + RyaURI uri = new RyaURI("urn:test#1234"); + RyaURI uri2 = new RyaURI("urn:test#1235"); + RyaURIRange rangeURI = new RyaURIRange(uri, uri2); + RyaURIRange rangeURI2 = new RyaURIRange(new RyaURI("urn:test#1235"), new RyaURI("urn:test#1236")); + HashedPoWholeRowTriplePatternStrategy strategy = new HashedPoWholeRowTriplePatternStrategy(); + RyaContext ryaContext = RyaContext.getInstance(); + RyaTripleContext ryaTripleContext; + + RyaType customType1 = new RyaType(new URIImpl("urn:custom#type"), "1234"); + RyaType customType2 = new RyaType(new URIImpl("urn:custom#type"), "1235"); + RyaType customType3 = new RyaType(new URIImpl("urn:custom#type"), "1236"); + RyaTypeRange customTypeRange1 = new RyaTypeRange(customType1, customType2); + RyaTypeRange customTypeRange2 = new RyaTypeRange(customType2, customType3); + + @Before + public void setUp() { + MockRdfCloudConfiguration config = new MockRdfCloudConfiguration(); + config.set(MockRdfCloudConfiguration.CONF_PREFIX_ROW_WITH_HASH, Boolean.TRUE.toString()); + ryaTripleContext = RyaTripleContext.getInstance(config); + } + + public void testPoRange() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(null, uri, rangeURI, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(null, uri, rangeURI2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + private void assertContains(ByteRange value, byte[] row) { + Text rowText = new Text(row); + Text startText = new Text(value.getStart()); + Text endText = new Text(value.getEnd()); + assertTrue((startText.compareTo(rowText) <= 0) &&(endText.compareTo(rowText) >= 0)) ; + } + + private void assertContainsFalse(ByteRange value, byte[] row) { + Text rowText = new Text(row); + Text startText = new Text(value.getStart()); + Text endText = new Text(value.getEnd()); + assertFalse((startText.compareTo(rowText) <= 0) &&(endText.compareTo(rowText) >= 0)) ; + } + + public void testPoRangeCustomType() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, customType1, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(null, uri, customTypeRange1, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(null, uri, customTypeRange2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testPo() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(null, uri, uri, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(null, uri, uri2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testPoCustomType() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, customType1, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(null, uri, customType1, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(null, uri, customType2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testPosRange() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(rangeURI, uri, uri, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(rangeURI2, uri, uri, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testPRange() throws Exception { + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(null, rangeURI, null, null, null); + assertNull(entry); + } + + public void testP() throws Exception { + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(null, uri, null, null, null); + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO); + assertContains(entry.getValue(), tripleRow.getRow()); + } + + public void testHandles() throws Exception { + //po(ng) + assertTrue(strategy.handles(null, uri, uri, null)); + assertTrue(strategy.handles(null, uri, uri, uri)); + //po_r(s)(ng) + assertTrue(strategy.handles(rangeURI, uri, uri, null)); + assertTrue(strategy.handles(rangeURI, uri, uri, uri)); + //p(ng) + assertTrue(strategy.handles(null, uri, null, null)); + assertTrue(strategy.handles(null, uri, null, uri)); + //p_r(o)(ng) + assertTrue(strategy.handles(null, uri, rangeURI, null)); + assertTrue(strategy.handles(null, uri, rangeURI, uri)); + //r(p)(ng) + assertFalse(strategy.handles(null, rangeURI, null, null)); + assertFalse(strategy.handles(null, rangeURI, null, uri)); + + //false cases + //sp.. + assertFalse(strategy.handles(uri, uri, null, null)); + //r(s)_p + assertFalse(strategy.handles(rangeURI, uri, null, null)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategyTest.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategyTest.java b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategyTest.java new file mode 100644 index 0000000..f726333 --- /dev/null +++ b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategyTest.java @@ -0,0 +1,198 @@ +package mvm.rya.api.query.strategy.wholerow; + +/* + * #%L + * mvm.rya.rya.api + * %% + * Copyright (C) 2014 Rya + * %% + * 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. + * #L% + */ +// +import java.util.Map; + +import junit.framework.TestCase; +import mvm.rya.api.RdfCloudTripleStoreConstants; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaTypeRange; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.domain.RyaURIRange; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTripleContext; +import mvm.rya.api.resolver.triple.TripleRow; + +import org.apache.hadoop.io.Text; +import org.junit.Before; +import org.openrdf.model.impl.URIImpl; + +/** + * Date: 7/14/12 + * Time: 7:47 AM + */ +public class HashedSpoWholeRowTriplePatternStrategyTest extends TestCase { + + RyaURI uri = new RyaURI("urn:test#1234"); + RyaURI uri2 = new RyaURI("urn:test#1235"); + RyaURIRange rangeURI = new RyaURIRange(uri, uri2); + RyaURIRange rangeURI2 = new RyaURIRange(new RyaURI("urn:test#1235"), new RyaURI("urn:test#1236")); + HashedSpoWholeRowTriplePatternStrategy strategy = new HashedSpoWholeRowTriplePatternStrategy(); + RyaContext ryaContext = RyaContext.getInstance(); + RyaTripleContext ryaTripleContext; + + RyaType customType1 = new RyaType(new URIImpl("urn:custom#type"), "1234"); + RyaType customType2 = new RyaType(new URIImpl("urn:custom#type"), "1235"); + RyaType customType3 = new RyaType(new URIImpl("urn:custom#type"), "1236"); + RyaTypeRange customTypeRange1 = new RyaTypeRange(customType1, customType2); + RyaTypeRange customTypeRange2 = new RyaTypeRange(customType2, customType3); + + @Before + public void setUp() { + MockRdfCloudConfiguration config = new MockRdfCloudConfiguration(); + config.set(MockRdfCloudConfiguration.CONF_PREFIX_ROW_WITH_HASH, Boolean.TRUE.toString()); + ryaTripleContext = RyaTripleContext.getInstance(config); + } + + public void testSpo() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, uri, uri, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + + entry = strategy.defineRange(uri, uri, uri2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + private void assertContains(ByteRange value, byte[] row) { + Text rowText = new Text(row); + Text startText = new Text(value.getStart()); + Text endText = new Text(value.getEnd()); + assertTrue((startText.compareTo(rowText) <= 0) &&(endText.compareTo(rowText) >= 0)) ; + } + + private void assertContainsFalse(ByteRange value, byte[] row) { + Text rowText = new Text(row); + Text startText = new Text(value.getStart()); + Text endText = new Text(value.getEnd()); + assertFalse((startText.compareTo(rowText) <= 0) &&(endText.compareTo(rowText) >= 0)) ; + } + + public void testSpoCustomType() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, customType1, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, uri, customType1, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(uri, uri, customType2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testSpoRange() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, uri, rangeURI, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(uri, uri, rangeURI2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testSpoRangeCustomType() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, customType1, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, uri, customTypeRange1, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(uri, uri, customTypeRange2, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testSp() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, uri, null, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + entry = strategy.defineRange(uri, uri2, null, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testSpRange() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, rangeURI, null, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + entry = strategy.defineRange(uri, rangeURI2, null, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testS() throws Exception { + Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serialize = ryaTripleContext.serializeTriple( + new RyaStatement(uri, uri, uri, null)); + TripleRow tripleRow = serialize.get(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO); + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(uri, null, null, null, null); + assertContains(entry.getValue(), tripleRow.getRow()); + + entry = strategy.defineRange(uri2, null, null, null, null); + assertContainsFalse(entry.getValue(), tripleRow.getRow()); + } + + public void testSRange() throws Exception { + + Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(rangeURI, null, null, null, null); + assertNull(entry); + } + + public void testHandles() throws Exception { + //spo(ng) + assertTrue(strategy.handles(uri, uri, uri, null)); + assertTrue(strategy.handles(uri, uri, uri, uri)); + //sp(ng) + assertTrue(strategy.handles(uri, uri, null, null)); + assertTrue(strategy.handles(uri, uri, null, uri)); + //s(ng) + assertTrue(strategy.handles(uri, null, null, null)); + assertTrue(strategy.handles(uri, null, null, uri)); + //sp_r(o)(ng) + assertTrue(strategy.handles(uri, uri, rangeURI, null)); + assertTrue(strategy.handles(uri, uri, rangeURI, uri)); + //s_r(p)(ng) + assertTrue(strategy.handles(uri, rangeURI, null, null)); + assertTrue(strategy.handles(uri, rangeURI, null, uri)); + + //fail + //s_r(p)_r(o) + assertFalse(strategy.handles(uri, rangeURI, rangeURI, null)); + + //s==null + assertFalse(strategy.handles(null, uri, uri, null)); + + //s_r(o) + assertFalse(strategy.handles(uri, null, rangeURI, null)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/MockRdfCloudConfiguration.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/MockRdfCloudConfiguration.java b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/MockRdfCloudConfiguration.java new file mode 100644 index 0000000..5b91c88 --- /dev/null +++ b/common/rya.api/src/test/java/mvm/rya/api/query/strategy/wholerow/MockRdfCloudConfiguration.java @@ -0,0 +1,12 @@ +package mvm.rya.api.query.strategy.wholerow; + +import mvm.rya.api.RdfCloudTripleStoreConfiguration; + +public class MockRdfCloudConfiguration extends RdfCloudTripleStoreConfiguration { + + @Override + public RdfCloudTripleStoreConfiguration clone() { + return this; + } + +}
