http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategy.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategy.java b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategy.java new file mode 100644 index 0000000..655d7da --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedPoWholeRowTriplePatternStrategy.java @@ -0,0 +1,134 @@ +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 static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTES; +import static mvm.rya.api.RdfCloudTripleStoreConstants.LAST_BYTES; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTES; + +import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; + +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.RdfCloudTripleStoreConstants; +import mvm.rya.api.RdfCloudTripleStoreUtils; +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.query.strategy.AbstractTriplePatternStrategy; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; + +import com.google.common.primitives.Bytes; + +/** + * Date: 7/14/12 + * Time: 7:35 AM + */ +public class HashedPoWholeRowTriplePatternStrategy extends AbstractTriplePatternStrategy { + + @Override + public RdfCloudTripleStoreConstants.TABLE_LAYOUT getLayout() { + return RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO; + } + + @Override + public Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, + ByteRange> defineRange(RyaURI subject, RyaURI predicate, RyaType object, + RyaURI context, RdfCloudTripleStoreConfiguration conf) throws IOException { + try { + //po(ng) + //po_r(s)(ng) + //p(ng) + //p_r(o)(ng) + //r(p)(ng) + if (!handles(subject, predicate, object, context)) return null; + + RyaContext ryaContext = RyaContext.getInstance(); + MessageDigest md = MessageDigest.getInstance("MD5"); + + + RdfCloudTripleStoreConstants.TABLE_LAYOUT table_layout = RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO; + byte[] start, stop; + if (object != null) { + if (object instanceof RyaRange) { + //p_r(o) + RyaRange rv = (RyaRange) object; + rv = ryaContext.transformRange(rv); + byte[] objStartBytes = ryaContext.serializeType(rv.getStart())[0]; + byte[] objEndBytes = ryaContext.serializeType(rv.getStop())[0]; + byte[] predBytes = predicate.getData().getBytes(); + byte[] predHash = md.digest(predBytes); + start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objStartBytes); + stop = Bytes.concat(predHash, DELIM_BYTES, predBytes,DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES); + } else { + if (subject != null && subject instanceof RyaRange) { + //po_r(s) + RyaRange ru = (RyaRange) subject; + ru = ryaContext.transformRange(ru); + byte[] subjStartBytes = ru.getStart().getData().getBytes(); + byte[] subjStopBytes = ru.getStop().getData().getBytes(); + byte[] predBytes = predicate.getData().getBytes(); + byte[] predHash = md.digest(predBytes); + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStartBytes); + stop = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStopBytes, TYPE_DELIM_BYTES, LAST_BYTES); + } else { + //po + //TODO: There must be a better way than creating multiple byte[] + byte[] predBytes = predicate.getData().getBytes(); + byte[] predHash = md.digest(predBytes); + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes, DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } + } else { + //p + byte[] predBytes = predicate.getData().getBytes(); + byte[] predHash = md.digest(predBytes); + start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + return new RdfCloudTripleStoreUtils.CustomEntry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, + ByteRange>(table_layout, new ByteRange(start, stop)); + } catch (RyaTypeResolverException e) { + throw new IOException(e); + } catch (NoSuchAlgorithmException e) { + throw new IOException(e); + } + } + + @Override + public boolean handles(RyaURI subject, RyaURI predicate, RyaType object, RyaURI context) { + //po(ng) + //p_r(o)(ng) + //po_r(s)(ng) + //p(ng) + //r(p)(ng) + if ((predicate == null) || (predicate instanceof RyaRange)) return false; + if (subject != null && !(subject instanceof RyaRange)) return false; + return subject == null || object != null; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategy.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategy.java b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategy.java new file mode 100644 index 0000000..aa1df79 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/HashedSpoWholeRowTriplePatternStrategy.java @@ -0,0 +1,137 @@ +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 com.google.common.primitives.Bytes; + +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.RdfCloudTripleStoreUtils; +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.domain.RyaURIRange; +import mvm.rya.api.query.strategy.AbstractTriplePatternStrategy; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; + +import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.*; + +/** + * Date: 7/14/12 + * Time: 7:35 AM + */ +public class HashedSpoWholeRowTriplePatternStrategy extends AbstractTriplePatternStrategy { + + + @Override + public TABLE_LAYOUT getLayout() { + return TABLE_LAYOUT.SPO; + } + + @Override + public Map.Entry<TABLE_LAYOUT, ByteRange> defineRange(RyaURI subject, RyaURI predicate, RyaType object, + RyaURI context, RdfCloudTripleStoreConfiguration conf) throws IOException { + try { + //spo(ng) + //sp(ng) + //s(ng) + //sp_r(o)(ng) + //s_r(p)(ng) + if (!handles(subject, predicate, object, context)) return null; + MessageDigest md = MessageDigest.getInstance("MD5"); + + RyaContext ryaContext = RyaContext.getInstance(); + + TABLE_LAYOUT table_layout = TABLE_LAYOUT.SPO; + byte[] start; + byte[] stop; + if (predicate != null) { + if (object != null) { + if (object instanceof RyaRange) { + //sp_r(o) + //range = sp_r(o.s)->sp_r(o.e) (remove last byte to remove type info) + RyaRange rv = (RyaRange) object; + rv = ryaContext.transformRange(rv); + byte[] objStartBytes = ryaContext.serializeType(rv.getStart())[0]; + byte[] objEndBytes = ryaContext.serializeType(rv.getStop())[0]; + byte[] subjBytes = subject.getData().getBytes(); + byte[] hashSubj = md.digest(subjBytes); + byte[] predBytes = predicate.getData().getBytes(); + start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objStartBytes); + stop = Bytes.concat(hashSubj, DELIM_BYTES,subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES); + } else { + //spo + //range = spo->spo (remove last byte to remove type info) + //TODO: There must be a better way than creating multiple byte[] + byte[] subjBytes = subject.getData().getBytes(); + byte[] hashSubj = md.digest(subjBytes); + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predicate.getData().getBytes(), DELIM_BYTES, objBytes, TYPE_DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } else if (predicate instanceof RyaRange) { + //s_r(p) + //range = s_r(p.s)->s_r(p.e) + RyaRange rv = (RyaRange) predicate; + rv = ryaContext.transformRange(rv); + byte[] subjBytes = subject.getData().getBytes(); + byte[] hashSubj = md.digest(subjBytes); + byte[] predStartBytes = rv.getStart().getData().getBytes(); + byte[] predStopBytes = rv.getStop().getData().getBytes(); + start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predStartBytes); + stop = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predStopBytes, DELIM_BYTES, LAST_BYTES); + } else { + //sp + //range = sp + byte[] subjBytes = subject.getData().getBytes(); + byte[] hashSubj = md.digest(subjBytes); + start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predicate.getData().getBytes(), DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } else { + //s + //range = s + byte[] subjBytes = subject.getData().getBytes(); + byte[] hashSubj = md.digest(subjBytes); + start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + return new RdfCloudTripleStoreUtils.CustomEntry<TABLE_LAYOUT, ByteRange>(table_layout, + new ByteRange(start, stop)); + } catch (RyaTypeResolverException e) { + throw new IOException(e); + } catch (NoSuchAlgorithmException e) { + throw new IOException(e); + } + } + + @Override + public boolean handles(RyaURI subject, RyaURI predicate, RyaType object, RyaURI context) { + //if subject is not null and not a range (if predicate is null then object must be null) + return (subject != null && !(subject instanceof RyaURIRange)) && !((predicate == null || predicate instanceof RyaURIRange) && (object != null)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/OspWholeRowTriplePatternStrategy.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/OspWholeRowTriplePatternStrategy.java b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/OspWholeRowTriplePatternStrategy.java new file mode 100644 index 0000000..5f58898 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/OspWholeRowTriplePatternStrategy.java @@ -0,0 +1,112 @@ +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 static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTES; +import static mvm.rya.api.RdfCloudTripleStoreConstants.LAST_BYTES; + +import java.io.IOException; +import java.util.Map; + +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT; +import mvm.rya.api.RdfCloudTripleStoreUtils; +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.query.strategy.AbstractTriplePatternStrategy; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; + +import com.google.common.primitives.Bytes; + +/** + * Date: 7/14/12 + * Time: 7:35 AM + */ +public class OspWholeRowTriplePatternStrategy extends AbstractTriplePatternStrategy { + + @Override + public TABLE_LAYOUT getLayout() { + return TABLE_LAYOUT.OSP; + } + + @Override + public Map.Entry<TABLE_LAYOUT, + ByteRange> defineRange(RyaURI subject, RyaURI predicate, RyaType object, + RyaURI context, RdfCloudTripleStoreConfiguration conf) throws IOException { + try { + //os(ng) + //o_r(s)(ng) + //o(ng) + //r(o) + if (!handles(subject, predicate, object, context)) return null; + + RyaContext ryaContext = RyaContext.getInstance(); + + TABLE_LAYOUT table_layout = TABLE_LAYOUT.OSP; + byte[] start, stop; + if (subject != null) { + if (subject instanceof RyaRange) { + //o_r(s) + RyaRange ru = (RyaRange) subject; + ru = ryaContext.transformRange(ru); + byte[] subjStartBytes = ru.getStart().getData().getBytes(); + byte[] subjEndBytes = ru.getStop().getData().getBytes(); + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(objBytes, DELIM_BYTES, subjStartBytes); + stop = Bytes.concat(objBytes, DELIM_BYTES, subjEndBytes, DELIM_BYTES, LAST_BYTES); + } else { + //os + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(objBytes, DELIM_BYTES, subject.getData().getBytes(), DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } else { + if (object instanceof RyaRange) { + //r(o) + RyaRange rv = (RyaRange) object; + rv = ryaContext.transformRange(rv); + start = ryaContext.serializeType(rv.getStart())[0]; + stop = Bytes.concat(ryaContext.serializeType(rv.getStop())[0], DELIM_BYTES, LAST_BYTES); + } else { + //o + start = Bytes.concat(ryaContext.serializeType(object)[0], DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } + return new RdfCloudTripleStoreUtils.CustomEntry<TABLE_LAYOUT, + ByteRange>(table_layout, new ByteRange(start, stop)); + } catch (RyaTypeResolverException e) { + throw new IOException(e); + } + } + + @Override + public boolean handles(RyaURI subject, RyaURI predicate, RyaType object, RyaURI context) { + //os(ng) + //o_r(s)(ng) + //o(ng) + //r(o) + return object != null && (!(object instanceof RyaRange) || predicate == null && subject == null); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/PoWholeRowTriplePatternStrategy.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/PoWholeRowTriplePatternStrategy.java b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/PoWholeRowTriplePatternStrategy.java new file mode 100644 index 0000000..087c996 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/PoWholeRowTriplePatternStrategy.java @@ -0,0 +1,127 @@ +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 com.google.common.primitives.Bytes; +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.RdfCloudTripleStoreConstants; +import mvm.rya.api.RdfCloudTripleStoreUtils; +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.query.strategy.AbstractTriplePatternStrategy; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; + +import java.io.IOException; +import java.util.Map; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.*; + +/** + * Date: 7/14/12 + * Time: 7:35 AM + */ +public class PoWholeRowTriplePatternStrategy extends AbstractTriplePatternStrategy { + + @Override + public RdfCloudTripleStoreConstants.TABLE_LAYOUT getLayout() { + return RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO; + } + + @Override + public Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, + ByteRange> defineRange(RyaURI subject, RyaURI predicate, RyaType object, + RyaURI context, RdfCloudTripleStoreConfiguration conf) throws IOException { + try { + //po(ng) + //po_r(s)(ng) + //p(ng) + //p_r(o)(ng) + //r(p)(ng) + if (!handles(subject, predicate, object, context)) return null; + + RyaContext ryaContext = RyaContext.getInstance(); + + RdfCloudTripleStoreConstants.TABLE_LAYOUT table_layout = RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO; + byte[] start, stop; + if (object != null) { + if (object instanceof RyaRange) { + //p_r(o) + RyaRange rv = (RyaRange) object; + rv = ryaContext.transformRange(rv); + byte[] objStartBytes = ryaContext.serializeType(rv.getStart())[0]; + byte[] objEndBytes = ryaContext.serializeType(rv.getStop())[0]; + byte[] predBytes = predicate.getData().getBytes(); + start = Bytes.concat(predBytes, DELIM_BYTES, objStartBytes); + stop = Bytes.concat(predBytes, DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES); + } else { + if (subject != null && subject instanceof RyaRange) { + //po_r(s) + RyaRange ru = (RyaRange) subject; + ru = ryaContext.transformRange(ru); + byte[] subjStartBytes = ru.getStart().getData().getBytes(); + byte[] subjStopBytes = ru.getStop().getData().getBytes(); + byte[] predBytes = predicate.getData().getBytes(); + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStartBytes); + stop = Bytes.concat(predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStopBytes, TYPE_DELIM_BYTES, LAST_BYTES); + } else { + //po + //TODO: There must be a better way than creating multiple byte[] + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(predicate.getData().getBytes(), DELIM_BYTES, objBytes, DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } + } else if (predicate instanceof RyaRange) { + //r(p) + RyaRange rv = (RyaRange) predicate; + rv = ryaContext.transformRange(rv); + start = rv.getStart().getData().getBytes(); + stop = Bytes.concat(rv.getStop().getData().getBytes(), DELIM_BYTES, LAST_BYTES); + } else { + //p + start = Bytes.concat(predicate.getData().getBytes(), DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + return new RdfCloudTripleStoreUtils.CustomEntry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, + ByteRange>(table_layout, new ByteRange(start, stop)); + } catch (RyaTypeResolverException e) { + throw new IOException(e); + } + } + + @Override + public boolean handles(RyaURI subject, RyaURI predicate, RyaType object, RyaURI context) { + //po(ng) + //p_r(o)(ng) + //po_r(s)(ng) + //p(ng) + //r(p)(ng) + if (predicate == null) return false; + if (subject != null && !(subject instanceof RyaRange)) return false; + if (predicate instanceof RyaRange) + return object == null && subject == null; + return subject == null || object != null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/SpoWholeRowTriplePatternStrategy.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/SpoWholeRowTriplePatternStrategy.java b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/SpoWholeRowTriplePatternStrategy.java new file mode 100644 index 0000000..6f41d5e --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/query/strategy/wholerow/SpoWholeRowTriplePatternStrategy.java @@ -0,0 +1,129 @@ +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 com.google.common.primitives.Bytes; +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.RdfCloudTripleStoreUtils; +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.domain.RyaURIRange; +import mvm.rya.api.query.strategy.AbstractTriplePatternStrategy; +import mvm.rya.api.query.strategy.ByteRange; +import mvm.rya.api.resolver.RyaContext; +import mvm.rya.api.resolver.RyaTypeResolverException; + +import java.io.IOException; +import java.util.Map; + +import static mvm.rya.api.RdfCloudTripleStoreConstants.*; + +/** + * Date: 7/14/12 + * Time: 7:35 AM + */ +public class SpoWholeRowTriplePatternStrategy extends AbstractTriplePatternStrategy { + + @Override + public TABLE_LAYOUT getLayout() { + return TABLE_LAYOUT.SPO; + } + + @Override + public Map.Entry<TABLE_LAYOUT, ByteRange> defineRange(RyaURI subject, RyaURI predicate, RyaType object, + RyaURI context, RdfCloudTripleStoreConfiguration conf) throws IOException { + try { + //spo(ng) + //sp(ng) + //s(ng) + //sp_r(o)(ng) + //s_r(p)(ng) + if (!handles(subject, predicate, object, context)) return null; + + RyaContext ryaContext = RyaContext.getInstance(); + + TABLE_LAYOUT table_layout = TABLE_LAYOUT.SPO; + byte[] start; + byte[] stop; + if (predicate != null) { + if (object != null) { + if (object instanceof RyaRange) { + //sp_r(o) + //range = sp_r(o.s)->sp_r(o.e) (remove last byte to remove type info) + RyaRange rv = (RyaRange) object; + rv = ryaContext.transformRange(rv); + byte[] objStartBytes = ryaContext.serializeType(rv.getStart())[0]; + byte[] objEndBytes = ryaContext.serializeType(rv.getStop())[0]; + byte[] subjBytes = subject.getData().getBytes(); + byte[] predBytes = predicate.getData().getBytes(); + start = Bytes.concat(subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objStartBytes); + stop = Bytes.concat(subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES); + } else { + //spo + //range = spo->spo (remove last byte to remove type info) + //TODO: There must be a better way than creating multiple byte[] + byte[] objBytes = ryaContext.serializeType(object)[0]; + start = Bytes.concat(subject.getData().getBytes(), DELIM_BYTES, predicate.getData().getBytes(), DELIM_BYTES, objBytes, TYPE_DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } else if (predicate instanceof RyaRange) { + //s_r(p) + //range = s_r(p.s)->s_r(p.e) + RyaRange rv = (RyaRange) predicate; + rv = ryaContext.transformRange(rv); + byte[] subjBytes = subject.getData().getBytes(); + byte[] predStartBytes = rv.getStart().getData().getBytes(); + byte[] predStopBytes = rv.getStop().getData().getBytes(); + start = Bytes.concat(subjBytes, DELIM_BYTES, predStartBytes); + stop = Bytes.concat(subjBytes, DELIM_BYTES, predStopBytes, DELIM_BYTES, LAST_BYTES); + } else { + //sp + //range = sp + start = Bytes.concat(subject.getData().getBytes(), DELIM_BYTES, predicate.getData().getBytes(), DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + } else if (subject instanceof RyaRange) { + //r(s) + //range = r(s.s) -> r(s.e) + RyaRange ru = (RyaRange) subject; + ru = ryaContext.transformRange(ru); + start = ru.getStart().getData().getBytes(); + stop = Bytes.concat(ru.getStop().getData().getBytes(), DELIM_BYTES, LAST_BYTES); + } else { + //s + //range = s + start = Bytes.concat(subject.getData().getBytes(), DELIM_BYTES); + stop = Bytes.concat(start, LAST_BYTES); + } + return new RdfCloudTripleStoreUtils.CustomEntry<TABLE_LAYOUT, ByteRange>(table_layout, + new ByteRange(start, stop)); + } catch (RyaTypeResolverException e) { + throw new IOException(e); + } + } + + @Override + public boolean handles(RyaURI subject, RyaURI predicate, RyaType object, RyaURI context) { + //if subject is not null and (if predicate is null then object must be null) + return (subject != null && !(subject instanceof RyaURIRange && predicate != null)) && !((predicate == null || predicate instanceof RyaURIRange) && (object != null)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/CustomRyaTypeResolverMapping.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/CustomRyaTypeResolverMapping.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/CustomRyaTypeResolverMapping.java new file mode 100644 index 0000000..80d7cba --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/CustomRyaTypeResolverMapping.java @@ -0,0 +1,56 @@ +package mvm.rya.api.resolver; + +/* + * #%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.URI; + +/** + * Date: 7/16/12 + * Time: 12:25 PM + */ +public class CustomRyaTypeResolverMapping extends RyaTypeResolverMapping { + + protected URI ryaDataType; + protected byte markerByte; + + public CustomRyaTypeResolverMapping() { + } + + public CustomRyaTypeResolverMapping(URI ryaDataType, byte markerByte) { + this(null, ryaDataType, markerByte); + } + + public CustomRyaTypeResolverMapping(RyaTypeResolver ryaTypeResolver, URI ryaDataType, byte markerByte) { + super(ryaTypeResolver); + this.ryaDataType = ryaDataType; + this.markerByte = markerByte; + } + + @Override + public URI getRyaDataType() { + return ryaDataType; + } + + @Override + byte getMarkerByte() { + return markerByte; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/RdfToRyaConversions.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RdfToRyaConversions.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RdfToRyaConversions.java new file mode 100644 index 0000000..bd5a236 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RdfToRyaConversions.java @@ -0,0 +1,92 @@ +package mvm.rya.api.resolver; + +/* + * #%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.*; +import org.openrdf.model.*; + +/** + * Date: 7/17/12 + * Time: 8:34 AM + */ +public class RdfToRyaConversions { + + public static RyaURI convertURI(URI uri) { + if (uri == null) return null; + if (uri instanceof RangeURI) { + RangeURI ruri = (RangeURI) uri; + return new RyaURIRange(convertURI(ruri.getStart()), convertURI(ruri.getEnd())); + } + return new RyaURI(uri.stringValue()); + } + + public static RyaType convertLiteral(Literal literal) { + if (literal == null) return null; + if (literal.getDatatype() != null) { + return new RyaType(literal.getDatatype(), literal.stringValue()); + } + //no language literal conversion yet + return new RyaType(literal.stringValue()); + } + + public static RyaType convertValue(Value value) { + if (value == null) return null; + //assuming either uri or Literal here + if(value instanceof Resource) { + return convertResource((Resource) value); + } + if (value instanceof Literal) { + return convertLiteral((Literal) value); + } + if (value instanceof RangeValue) { + RangeValue rv = (RangeValue) value; + if (rv.getStart() instanceof URI) { + return new RyaURIRange(convertURI((URI) rv.getStart()), convertURI((URI) rv.getEnd())); + } else { + //literal + return new RyaTypeRange(convertLiteral((Literal) rv.getStart()), convertLiteral((Literal) rv.getEnd())); + } + } + return null; + } + + public static RyaURI convertResource(Resource subject) { + if(subject == null) return null; + if (subject instanceof BNode) { + return new RyaURI(RyaSchema.BNODE_NAMESPACE + ((BNode) subject).getID()); + } + return convertURI((URI) subject); + } + + public static RyaStatement convertStatement(Statement statement) { + if (statement == null) return null; + Resource subject = statement.getSubject(); + URI predicate = statement.getPredicate(); + Value object = statement.getObject(); + Resource context = statement.getContext(); + return new RyaStatement( + convertResource(subject), + convertURI(predicate), + convertValue(object), + convertResource(context)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaContext.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaContext.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaContext.java new file mode 100644 index 0000000..7398114 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaContext.java @@ -0,0 +1,191 @@ +package mvm.rya.api.resolver; + +/* + * #%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.HashMap; +import java.util.List; +import java.util.Map; + +import mvm.rya.api.domain.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.resolver.impl.BooleanRyaTypeResolver; +import mvm.rya.api.resolver.impl.ByteRyaTypeResolver; +import mvm.rya.api.resolver.impl.CustomDatatypeResolver; +import mvm.rya.api.resolver.impl.DateTimeRyaTypeResolver; +import mvm.rya.api.resolver.impl.DoubleRyaTypeResolver; +import mvm.rya.api.resolver.impl.FloatRyaTypeResolver; +import mvm.rya.api.resolver.impl.IntegerRyaTypeResolver; +import mvm.rya.api.resolver.impl.LongRyaTypeResolver; +import mvm.rya.api.resolver.impl.RyaTypeResolverImpl; +import mvm.rya.api.resolver.impl.RyaURIResolver; +import mvm.rya.api.resolver.impl.ServiceBackedRyaTypeResolverMappings; +import mvm.rya.api.resolver.impl.ShortRyaTypeResolver; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openrdf.model.URI; +import org.openrdf.model.vocabulary.XMLSchema; + +/** + * Date: 7/16/12 + * Time: 12:04 PM + */ +public class RyaContext { + + public Log logger = LogFactory.getLog(RyaContext.class); + + private Map<URI, RyaTypeResolver> uriToResolver = new HashMap<URI, RyaTypeResolver>(); + private Map<Byte, RyaTypeResolver> byteToResolver = new HashMap<Byte, RyaTypeResolver>(); + private RyaTypeResolver defaultResolver = new CustomDatatypeResolver(); + + private RyaContext() { + //add default + addDefaultMappings(); + } + + protected void addDefaultMappings() { + if (logger.isDebugEnabled()) { + logger.debug("Adding default mappings"); + } + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new RyaTypeResolverImpl())); // plain string + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new RyaURIResolver())); // uri + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new DateTimeRyaTypeResolver())); // dateTime + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new DoubleRyaTypeResolver())); // double + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new FloatRyaTypeResolver())); // float + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new IntegerRyaTypeResolver())); // integer + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new ShortRyaTypeResolver())); // short + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new LongRyaTypeResolver())); // long + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new BooleanRyaTypeResolver())); // boolean + addRyaTypeResolverMapping(new RyaTypeResolverMapping(new ByteRyaTypeResolver())); // byte + + //int is integer + uriToResolver.put(XMLSchema.INT, new IntegerRyaTypeResolver()); + + //add service loaded mappings + addRyaTypeResolverMappings(new ServiceBackedRyaTypeResolverMappings().getResolvers()); + } + + private static class RyaContextHolder { + public static final RyaContext INSTANCE = new RyaContext(); + } + + public synchronized static RyaContext getInstance() { + return RyaContextHolder.INSTANCE; + } + + + //need to go from datatype->resolver + public RyaTypeResolver retrieveResolver(URI datatype) { + RyaTypeResolver ryaTypeResolver = uriToResolver.get(datatype); + if (ryaTypeResolver == null) return defaultResolver; + return ryaTypeResolver; + } + + //need to go from byte->resolver + public RyaTypeResolver retrieveResolver(byte markerByte) { + RyaTypeResolver ryaTypeResolver = byteToResolver.get(markerByte); + if (ryaTypeResolver == null) return defaultResolver; + return ryaTypeResolver; + } + + public byte[] serialize(RyaType ryaType) throws RyaTypeResolverException { + RyaTypeResolver ryaTypeResolver = retrieveResolver(ryaType.getDataType()); + if (ryaTypeResolver != null) { + return ryaTypeResolver.serialize(ryaType); + } + return null; + } + + public byte[][] serializeType(RyaType ryaType) throws RyaTypeResolverException { + RyaTypeResolver ryaTypeResolver = retrieveResolver(ryaType.getDataType()); + if (ryaTypeResolver != null) { + return ryaTypeResolver.serializeType(ryaType); + } + return null; + } + + public RyaType deserialize(byte[] bytes) throws RyaTypeResolverException { + RyaTypeResolver ryaTypeResolver = retrieveResolver(bytes[bytes.length - 1]); + if (ryaTypeResolver != null) { + return ryaTypeResolver.deserialize(bytes); + } + return null; + } + + public void addRyaTypeResolverMapping(RyaTypeResolverMapping mapping) { + if (!uriToResolver.containsKey(mapping.getRyaDataType())) { + if (logger.isDebugEnabled()) { + logger.debug("addRyaTypeResolverMapping uri:[" + mapping.getRyaDataType() + "] byte:[" + mapping.getMarkerByte() + "] for mapping[" + mapping + "]"); + } + uriToResolver.put(mapping.getRyaDataType(), mapping.getRyaTypeResolver()); + byteToResolver.put(mapping.getMarkerByte(), mapping.getRyaTypeResolver()); + } else { + logger.warn("Could not add ryaType mapping because one already exists. uri:[" + mapping.getRyaDataType() + "] byte:[" + mapping.getMarkerByte() + "] for mapping[" + mapping + "]"); + } + } + + public void addRyaTypeResolverMappings(List<RyaTypeResolverMapping> mappings) { + for (RyaTypeResolverMapping mapping : mappings) { + addRyaTypeResolverMapping(mapping); + } + } + + public RyaTypeResolver removeRyaTypeResolver(URI dataType) { + RyaTypeResolver ryaTypeResolver = uriToResolver.remove(dataType); + if (ryaTypeResolver != null) { + if (logger.isDebugEnabled()) { + logger.debug("Removing ryaType Resolver uri[" + dataType + "] + [" + ryaTypeResolver + "]"); + } + byteToResolver.remove(ryaTypeResolver.getMarkerByte()); + return ryaTypeResolver; + } + return null; + } + + public RyaTypeResolver removeRyaTypeResolver(byte markerByte) { + RyaTypeResolver ryaTypeResolver = byteToResolver.remove(markerByte); + if (ryaTypeResolver != null) { + if (logger.isDebugEnabled()) { + logger.debug("Removing ryaType Resolver byte[" + markerByte + "] + [" + ryaTypeResolver + "]"); + } + uriToResolver.remove(ryaTypeResolver.getRyaDataType()); + return ryaTypeResolver; + } + return null; + } + + //transform range + public RyaRange transformRange(RyaRange range) throws RyaTypeResolverException { + RyaTypeResolver ryaTypeResolver = retrieveResolver(range.getStart().getDataType()); + if (ryaTypeResolver != null) { + return ryaTypeResolver.transformRange(range); + } + return range; + } + + public RyaTypeResolver getDefaultResolver() { + return defaultResolver; + } + + public void setDefaultResolver(RyaTypeResolver defaultResolver) { + this.defaultResolver = defaultResolver; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaToRdfConversions.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaToRdfConversions.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaToRdfConversions.java new file mode 100644 index 0000000..b746aa5 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaToRdfConversions.java @@ -0,0 +1,74 @@ +package mvm.rya.api.resolver; + +/* + * #%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.RyaType; +import mvm.rya.api.domain.RyaURI; +import org.openrdf.model.Literal; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; +import org.openrdf.model.impl.ContextStatementImpl; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.StatementImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.model.vocabulary.XMLSchema; + +/** + * Date: 7/17/12 + * Time: 8:34 AM + */ +public class RyaToRdfConversions { + + public static URI convertURI(RyaURI uri) { + return new URIImpl(uri.getData()); + } + + public static Literal convertLiteral(RyaType literal) { + if (XMLSchema.STRING.equals(literal.getDataType())) { + return new LiteralImpl(literal.getData()); + } else { + return new LiteralImpl(literal.getData(), literal.getDataType()); + } + //TODO: No Language support yet + } + + public static Value convertValue(RyaType value) { + //assuming either uri or Literal here + return (value instanceof RyaURI) ? convertURI((RyaURI) value) : convertLiteral(value); + } + + public static Statement convertStatement(RyaStatement statement) { + assert statement != null; + if (statement.getContext() != null) { + return new ContextStatementImpl(convertURI(statement.getSubject()), + convertURI(statement.getPredicate()), + convertValue(statement.getObject()), + convertURI(statement.getContext())); + } else { + return new StatementImpl(convertURI(statement.getSubject()), + convertURI(statement.getPredicate()), + convertValue(statement.getObject())); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTripleContext.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTripleContext.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTripleContext.java new file mode 100644 index 0000000..64360f3 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTripleContext.java @@ -0,0 +1,122 @@ +package mvm.rya.api.resolver; + +/* + * #%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.ArrayList; +import java.util.List; +import java.util.Map; + +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.TriplePatternStrategy; +import mvm.rya.api.query.strategy.wholerow.HashedPoWholeRowTriplePatternStrategy; +import mvm.rya.api.query.strategy.wholerow.HashedSpoWholeRowTriplePatternStrategy; +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.triple.TripleRow; +import mvm.rya.api.resolver.triple.TripleRowResolver; +import mvm.rya.api.resolver.triple.TripleRowResolverException; +import mvm.rya.api.resolver.triple.impl.WholeRowHashedTripleResolver; +import mvm.rya.api.resolver.triple.impl.WholeRowTripleResolver; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Date: 7/16/12 + * Time: 12:04 PM + */ +public class RyaTripleContext { + + public Log logger = LogFactory.getLog(RyaTripleContext.class); + private TripleRowResolver tripleResolver; + private List<TriplePatternStrategy> triplePatternStrategyList = new ArrayList<TriplePatternStrategy>(); + + private RyaTripleContext(boolean addPrefixHash) { + addDefaultTriplePatternStrategies(addPrefixHash); + if (addPrefixHash){ + tripleResolver = new WholeRowHashedTripleResolver(); + } + else { + tripleResolver = new WholeRowTripleResolver(); + } + } + + + private static class RyaTripleContextHolder { + // TODO want to be able to support more variability in configuration here + public static final RyaTripleContext INSTANCE = new RyaTripleContext(false); + public static final RyaTripleContext HASHED_INSTANCE = new RyaTripleContext(true); + } + + public synchronized static RyaTripleContext getInstance(RdfCloudTripleStoreConfiguration conf) { + if (conf.isPrefixRowsWithHash()){ + return RyaTripleContextHolder.HASHED_INSTANCE; + } + return RyaTripleContextHolder.INSTANCE; + } + + + public Map<RdfCloudTripleStoreConstants.TABLE_LAYOUT, TripleRow> serializeTriple(RyaStatement statement) throws TripleRowResolverException { + return getTripleResolver().serialize(statement); + } + + public RyaStatement deserializeTriple(RdfCloudTripleStoreConstants.TABLE_LAYOUT table_layout, TripleRow tripleRow) throws TripleRowResolverException { + return getTripleResolver().deserialize(table_layout, tripleRow); + } + + protected void addDefaultTriplePatternStrategies(boolean addPrefixHash) { + if (addPrefixHash){ + triplePatternStrategyList.add(new HashedSpoWholeRowTriplePatternStrategy()); + triplePatternStrategyList.add(new HashedPoWholeRowTriplePatternStrategy()); + } + else { + triplePatternStrategyList.add(new SpoWholeRowTriplePatternStrategy()); + triplePatternStrategyList.add(new PoWholeRowTriplePatternStrategy()); + } + triplePatternStrategyList.add(new OspWholeRowTriplePatternStrategy()); + } + + //retrieve triple pattern strategy + public TriplePatternStrategy retrieveStrategy(RyaURI subject, RyaURI predicate, RyaType object, RyaURI context) { + for (TriplePatternStrategy strategy : triplePatternStrategyList) { + if (strategy.handles(subject, predicate, object, context)) + return strategy; + } + return null; + } + + public TriplePatternStrategy retrieveStrategy(RyaStatement stmt) { + return retrieveStrategy(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), stmt.getContext()); + } + + public TripleRowResolver getTripleResolver() { + return tripleResolver; + } + + public void setTripleResolver(TripleRowResolver tripleResolver) { + this.tripleResolver = tripleResolver; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolver.java new file mode 100644 index 0000000..53fbce8 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolver.java @@ -0,0 +1,59 @@ +package mvm.rya.api.resolver; + +/* + * #%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.RyaRange; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaTypeRange; +import org.openrdf.model.URI; + +/** + * Date: 7/16/12 + * Time: 12:08 PM + */ +public interface RyaTypeResolver { + public byte[] serialize(RyaType ryaType) throws RyaTypeResolverException; + public byte[][] serializeType(RyaType ryaType) throws RyaTypeResolverException; + + public RyaType deserialize(byte[] bytes) throws RyaTypeResolverException; + + public RyaType newInstance(); + + /** + * @param bytes + * @return true if this byte[] is deserializable by this resolver + */ + public boolean deserializable(byte[] bytes); + + public URI getRyaDataType(); + + byte getMarkerByte(); + + /** + * This will allow a resolver to modify a range. For example, a date time resolver, with a reverse index, + * might want to reverse the start and stop + * + * @return + * @throws RyaTypeResolverException + */ + public RyaRange transformRange(RyaRange ryaRange) throws RyaTypeResolverException; + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverException.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverException.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverException.java new file mode 100644 index 0000000..0bc7147 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverException.java @@ -0,0 +1,42 @@ +package mvm.rya.api.resolver; + +/* + * #%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/16/12 + * Time: 12:09 PM + */ +public class RyaTypeResolverException extends Exception { + public RyaTypeResolverException() { + } + + public RyaTypeResolverException(String s) { + super(s); + } + + public RyaTypeResolverException(String s, Throwable throwable) { + super(s, throwable); + } + + public RyaTypeResolverException(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/RyaTypeResolverMapping.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverMapping.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverMapping.java new file mode 100644 index 0000000..1cf7ea2 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/RyaTypeResolverMapping.java @@ -0,0 +1,56 @@ +package mvm.rya.api.resolver; + +/* + * #%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.URI; + +/** + * Date: 7/16/12 + * Time: 12:11 PM + */ +public class RyaTypeResolverMapping { + + protected RyaTypeResolver ryaTypeResolver; + + public RyaTypeResolverMapping() { + } + + public RyaTypeResolverMapping(RyaTypeResolver ryaTypeResolver) { + this.ryaTypeResolver = ryaTypeResolver; + } + + public void setRyaTypeResolver(RyaTypeResolver ryaTypeResolver) { + this.ryaTypeResolver = ryaTypeResolver; + } + + public RyaTypeResolver getRyaTypeResolver() { + return ryaTypeResolver; + } + + public URI getRyaDataType() { + return ryaTypeResolver.getRyaDataType(); + } + + byte getMarkerByte() { + return ryaTypeResolver.getMarkerByte(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/BooleanRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/BooleanRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/BooleanRyaTypeResolver.java new file mode 100644 index 0000000..30c43d9 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/BooleanRyaTypeResolver.java @@ -0,0 +1,60 @@ +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 BooleanRyaTypeResolver extends RyaTypeResolverImpl { + public static final int BOOLEAN_LITERAL_MARKER = 10; + public static final TypeEncoder<Boolean, String> BOOLEAN_TYPE_ENCODER = LexiTypeEncoders + .booleanEncoder(); + + public BooleanRyaTypeResolver() { + super((byte) BOOLEAN_LITERAL_MARKER, XMLSchema.BOOLEAN); + } + + @Override + protected String serializeData(String data) throws + RyaTypeResolverException { + try { + boolean value = Boolean.parseBoolean(data); + return BOOLEAN_TYPE_ENCODER.encode(value); + } catch (TypeEncodingException e) { + throw new RyaTypeResolverException( + "Exception occurred serializing data[" + data + "]", e); + } + } + + @Override + protected String deserializeData(String value) throws RyaTypeResolverException { + try { + return BOOLEAN_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/impl/ByteRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ByteRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ByteRyaTypeResolver.java new file mode 100644 index 0000000..e530ed4 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/ByteRyaTypeResolver.java @@ -0,0 +1,62 @@ +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 ByteRyaTypeResolver extends RyaTypeResolverImpl { + public static final int LITERAL_MARKER = 9; + public static final TypeEncoder<Byte, String> BYTE_STRING_TYPE_ENCODER = LexiTypeEncoders + .byteEncoder(); + + public ByteRyaTypeResolver() { + super((byte) LITERAL_MARKER, XMLSchema.BYTE); + } + + @Override + protected String serializeData(String data) throws RyaTypeResolverException { + try { + Byte value = Byte.parseByte(data); + return BYTE_STRING_TYPE_ENCODER.encode(value); + } 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 BYTE_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/impl/CustomDatatypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/CustomDatatypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/CustomDatatypeResolver.java new file mode 100644 index 0000000..6393cbb --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/CustomDatatypeResolver.java @@ -0,0 +1,69 @@ +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 com.google.common.primitives.Bytes; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.resolver.RyaTypeResolverException; +import org.openrdf.model.impl.URIImpl; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTE; +import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTES; + +/** + * Date: 7/16/12 + * Time: 1:12 PM + */ +public class CustomDatatypeResolver extends RyaTypeResolverImpl { + public static final int DT_LITERAL_MARKER = 8; + + public CustomDatatypeResolver() { + super((byte) DT_LITERAL_MARKER, null); + } + + @Override + public byte[][] serializeType(RyaType ryaType) throws RyaTypeResolverException { + byte[] bytes = serializeData(ryaType.getData()).getBytes(); + return new byte[][]{bytes, Bytes.concat(TYPE_DELIM_BYTES, ryaType.getDataType().stringValue().getBytes(), TYPE_DELIM_BYTES, markerBytes)}; + } + + @Override + public byte[] serialize(RyaType ryaType) throws RyaTypeResolverException { + byte[][] bytes = serializeType(ryaType); + return Bytes.concat(bytes[0], bytes[1]); + } + + @Override + public RyaType deserialize(byte[] bytes) throws RyaTypeResolverException { + if (!deserializable(bytes)) { + throw new RyaTypeResolverException("Bytes not deserializable"); + } + RyaType rt = newInstance(); + int length = bytes.length; + int indexOfType = Bytes.indexOf(bytes, TYPE_DELIM_BYTE); + if (indexOfType < 1) { + throw new RyaTypeResolverException("Not a datatype literal"); + } + String label = deserializeData(new String(bytes, 0, indexOfType)); + rt.setDataType(new URIImpl(new String(bytes, indexOfType + 1, (length - indexOfType) - 3))); + rt.setData(label); + return rt; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/92ddfa59/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DateTimeRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DateTimeRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DateTimeRyaTypeResolver.java new file mode 100644 index 0000000..a93652c --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DateTimeRyaTypeResolver.java @@ -0,0 +1,75 @@ +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.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; +import org.openrdf.model.vocabulary.XMLSchema; + +import java.util.Date; + +/** + * Reverse index xml datetime strings + * <p/> + * Date: 7/13/12 + * Time: 7:33 AM + */ +public class DateTimeRyaTypeResolver extends RyaTypeResolverImpl { + public static final int DATETIME_LITERAL_MARKER = 7; + public static final TypeEncoder<Date, String> DATE_STRING_TYPE_ENCODER = LexiTypeEncoders.dateEncoder(); + public static final DateTimeFormatter XMLDATETIME_PARSER = org.joda.time.format.ISODateTimeFormat.dateTimeParser(); + public static final DateTimeFormatter UTC_XMLDATETIME_FORMATTER = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); + + + public DateTimeRyaTypeResolver() { + super((byte) DATETIME_LITERAL_MARKER, XMLSchema.DATETIME); + } + + @Override + protected String serializeData(String data) throws RyaTypeResolverException { + try { + DateTime dateTime = DateTime.parse(data, XMLDATETIME_PARSER); + Date value = dateTime.toDate(); + return DATE_STRING_TYPE_ENCODER.encode(value); + } catch (TypeEncodingException e) { + throw new RyaTypeResolverException( + "Exception occurred serializing data[" + data + "]", e); + } + } + + @Override + protected String deserializeData(String value) throws RyaTypeResolverException { + try { + Date date = DATE_STRING_TYPE_ENCODER.decode(value); + return UTC_XMLDATETIME_FORMATTER.print(date.getTime()); + } 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/impl/DoubleRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DoubleRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DoubleRyaTypeResolver.java new file mode 100644 index 0000000..ba85a0b --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/DoubleRyaTypeResolver.java @@ -0,0 +1,67 @@ +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; + +import java.text.DecimalFormat; + +/** + * Date: 7/20/12 + * Time: 9:33 AM + */ +public class DoubleRyaTypeResolver extends RyaTypeResolverImpl { + public static final int DOUBLE_LITERAL_MARKER = 6; + public static final TypeEncoder<Double, String> DOUBLE_TYPE_ENCODER = LexiTypeEncoders.doubleEncoder(); + + public DoubleRyaTypeResolver() { + super((byte) DOUBLE_LITERAL_MARKER, XMLSchema.DOUBLE); + } + + @Override + protected String serializeData(String data) throws RyaTypeResolverException { + try { + double value = Double.parseDouble(data); + return DOUBLE_TYPE_ENCODER.encode(value); + } 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 DOUBLE_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/impl/FloatRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/FloatRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/FloatRyaTypeResolver.java new file mode 100644 index 0000000..46c3ef7 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/FloatRyaTypeResolver.java @@ -0,0 +1,63 @@ +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 FloatRyaTypeResolver extends RyaTypeResolverImpl { + public static final int FLOAT_LITERAL_MARKER = 11; + public static final TypeEncoder<Float, String> FLOAT_TYPE_ENCODER = LexiTypeEncoders.floatEncoder(); + + public FloatRyaTypeResolver() { + super((byte) FLOAT_LITERAL_MARKER, XMLSchema.FLOAT); + } + + @Override + protected String serializeData(String data) throws RyaTypeResolverException { + try { + float value = Float.parseFloat(data); + return FLOAT_TYPE_ENCODER.encode(value); + } 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 FLOAT_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/impl/IntegerRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/IntegerRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/IntegerRyaTypeResolver.java new file mode 100644 index 0000000..24f0028 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/IntegerRyaTypeResolver.java @@ -0,0 +1,66 @@ +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; + +/** + * Date: 7/20/12 + * Time: 10:13 AM + */ +public class IntegerRyaTypeResolver extends RyaTypeResolverImpl { + public static final int INTEGER_LITERAL_MARKER = 5; + public static final TypeEncoder<Integer, String> INTEGER_STRING_TYPE_ENCODER = LexiTypeEncoders + .integerEncoder(); + + public IntegerRyaTypeResolver() { + super((byte) INTEGER_LITERAL_MARKER, XMLSchema.INTEGER); + } + + @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/impl/LongRyaTypeResolver.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/LongRyaTypeResolver.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/LongRyaTypeResolver.java new file mode 100644 index 0000000..cf017fa --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/LongRyaTypeResolver.java @@ -0,0 +1,67 @@ +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; + +/** + * Date: 7/20/12 + * Time: 10:13 AM + */ +public class LongRyaTypeResolver extends RyaTypeResolverImpl { + public static final int LONG_LITERAL_MARKER = 4; + public static final TypeEncoder<Long, String> LONG_STRING_TYPE_ENCODER = LexiTypeEncoders + .longEncoder(); + + public LongRyaTypeResolver() { + super((byte) LONG_LITERAL_MARKER, XMLSchema.LONG); + } + + @Override + protected String serializeData(String data) throws + RyaTypeResolverException { + try { + return LONG_STRING_TYPE_ENCODER.encode(Long.parseLong(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 LONG_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/impl/RyaTypeResolverImpl.java ---------------------------------------------------------------------- diff --git a/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaTypeResolverImpl.java b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaTypeResolverImpl.java new file mode 100644 index 0000000..22743f6 --- /dev/null +++ b/common/rya.api/src/main/java/mvm/rya/api/resolver/impl/RyaTypeResolverImpl.java @@ -0,0 +1,123 @@ +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 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); + } +}
