Author: jukka Date: Mon Jun 25 22:50:24 2012 New Revision: 1353771 URL: http://svn.apache.org/viewvc?rev=1353771&view=rev Log: OAK-152: More efficient CoreValues Add type-specific in-memory value classes Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BooleanValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DecimalValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DoubleValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/LongValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValueFactory.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringValue.java Removed: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueImpl.java Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/BinaryValue.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryImpl.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/PropertyStateImpl.java Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/BinaryValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/BinaryValue.java?rev=1353771&r1=1353770&r2=1353771&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/BinaryValue.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/BinaryValue.java Mon Jun 25 22:50:24 2012 @@ -18,34 +18,47 @@ package org.apache.jackrabbit.oak.kernel import org.apache.jackrabbit.mk.api.MicroKernel; import org.apache.jackrabbit.mk.util.MicroKernelInputStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.jackrabbit.oak.plugins.memory.MemoryValue; import java.io.InputStream; +import javax.jcr.PropertyType; + /** * BinaryValue... TODO: review name (BlobValue? BlobCoreValue? BinaryCoreValue?) */ -class BinaryValue { - - /** - * logger instance - */ - private static final Logger log = LoggerFactory.getLogger(BinaryValue.class); +class BinaryValue extends MemoryValue { private final String binaryID; private final MicroKernel mk; - BinaryValue(String binaryID, MicroKernel mk) { + public BinaryValue(String binaryID, MicroKernel mk) { this.binaryID = binaryID; this.mk = mk; } - InputStream getStream() { + @Override + public int getType() { + return PropertyType.BINARY; + } + + @Override + public boolean getBoolean() { + return Boolean.parseBoolean(getString()); + } + + @Override + public InputStream getNewStream() { return new MicroKernelInputStream(mk, binaryID); } - long length() { + @Override + public String getString() { + return binaryID; + } + + @Override + public long length() { return mk.getLength(binaryID); } @@ -71,8 +84,4 @@ class BinaryValue { return false; } - @Override - public String toString() { - return binaryID; - } } \ No newline at end of file Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryImpl.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryImpl.java?rev=1353771&r1=1353770&r2=1353771&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryImpl.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryImpl.java Mon Jun 25 22:50:24 2012 @@ -16,19 +16,19 @@ */ package org.apache.jackrabbit.oak.kernel; -import org.apache.jackrabbit.mk.api.MicroKernel; -import org.apache.jackrabbit.oak.api.CoreValue; -import org.apache.jackrabbit.oak.api.CoreValueFactory; +import java.io.InputStream; import javax.jcr.PropertyType; -import java.io.InputStream; -import java.math.BigDecimal; + +import org.apache.jackrabbit.mk.api.MicroKernel; +import org.apache.jackrabbit.oak.api.CoreValue; +import org.apache.jackrabbit.oak.plugins.memory.MemoryValueFactory; /** * {@code CoreValueFactoryImpl} is the default implementation of the * {@code CoreValueFactory} interface. */ -public class CoreValueFactoryImpl implements CoreValueFactory { +public class CoreValueFactoryImpl extends MemoryValueFactory { private final MicroKernel mk; @@ -38,45 +38,21 @@ public class CoreValueFactoryImpl implem } //---------------------------------------------------< CoreValueFactory >--- - @Override - public CoreValue createValue(String value) { - return new CoreValueImpl(value, PropertyType.STRING); - } - - @Override - public CoreValue createValue(double value) { - return new CoreValueImpl(value); - } - - @Override - public CoreValue createValue(long value) { - return new CoreValueImpl(value); - } - - @Override - public CoreValue createValue(boolean value) { - return new CoreValueImpl(value); - } - - @Override - public CoreValue createValue(BigDecimal value) { - return new CoreValueImpl(value); - } @Override public CoreValue createValue(InputStream value) { // TODO: mk.write throws MicrokernelException ... deal with this here. String binaryID = mk.write(value); - return new CoreValueImpl(new BinaryValue(binaryID, mk)); + return new BinaryValue(binaryID, mk); } @Override public CoreValue createValue(String value, int type) { if (type == PropertyType.BINARY) { - BinaryValue bv = new BinaryValue(value, mk); - return new CoreValueImpl(bv); + return new BinaryValue(value, mk); } else { - return new CoreValueImpl(value, type); + return super.createValue(value, type); } } + } \ No newline at end of file Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/PropertyStateImpl.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/PropertyStateImpl.java?rev=1353771&r1=1353770&r2=1353771&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/PropertyStateImpl.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/PropertyStateImpl.java Mon Jun 25 22:50:24 2012 @@ -20,13 +20,12 @@ package org.apache.jackrabbit.oak.kernel import org.apache.jackrabbit.oak.api.CoreValue; import org.apache.jackrabbit.oak.api.PropertyState; +import org.apache.jackrabbit.oak.plugins.memory.MemoryValueFactory; import java.util.Collections; import java.util.Iterator; import java.util.List; -import javax.jcr.PropertyType; - public class PropertyStateImpl implements PropertyState { private final String name; @@ -50,7 +49,7 @@ public class PropertyStateImpl implement } public PropertyStateImpl(String name, String value) { - this(name, new CoreValueImpl(value, PropertyType.STRING)); + this(name, MemoryValueFactory.INSTANCE.createValue(value)); } @Override Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import javax.jcr.PropertyType; + +final class BinaryValue extends MemoryValue { + + private final byte[] value; + + public BinaryValue(byte[] value) { + this.value = value; + } + + @Override + public int getType() { + return PropertyType.BINARY; + } + + @Override + public boolean getBoolean() { + return Boolean.parseBoolean(getString()); + } + + @Override + public InputStream getNewStream() { + return new ByteArrayInputStream(value); + } + + @Override + public String getString() { + return ""; + } + + @Override + public long length() { + return value.length; + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BooleanValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BooleanValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BooleanValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BooleanValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import javax.jcr.PropertyType; + +abstract class BooleanValue extends MemoryValue { + + public static final BooleanValue TRUE = new BooleanValue() { + @Override + public boolean getBoolean() { + return true; + } + }; + + public static final BooleanValue FALSE = new BooleanValue() { + @Override + public boolean getBoolean() { + return false; + } + }; + + @Override + public int getType() { + return PropertyType.BOOLEAN; + } + + @Override + public String getString() { + return Boolean.toString(getBoolean()); + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DecimalValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DecimalValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DecimalValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DecimalValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import java.math.BigDecimal; + +import javax.jcr.PropertyType; + +final class DecimalValue extends MemoryValue { + + private final BigDecimal value; + + public DecimalValue(BigDecimal value) { + this.value = value; + } + + @Override + public int getType() { + return PropertyType.DECIMAL; + } + + @Override + public BigDecimal getDecimal() { + return value; + } + + @Override + public double getDouble() { + return value.doubleValue(); + } + + @Override + public long getLong() { + return value.longValue(); + } + + @Override + public String getString() { + return value.toString(); + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DoubleValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DoubleValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DoubleValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/DoubleValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import java.math.BigDecimal; + +import javax.jcr.PropertyType; + +final class DoubleValue extends MemoryValue { + + private final double value; + + public DoubleValue(double value) { + this.value = value; + } + + @Override + public int getType() { + return PropertyType.DOUBLE; + } + + @Override + public BigDecimal getDecimal() { + return new BigDecimal(value); + } + + @Override + public double getDouble() { + return value; + } + + @Override + public long getLong() { + return (long) value; + } + + @Override + public String getString() { + return String.valueOf(value); + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +final class GenericValue extends MemoryValue { + + private final int type; + + private final String value; + + public GenericValue(int type, String value) { + this.type = type; + this.value = value; + } + + @Override + public int getType() { + return type; + } + + @Override + public String getString() { + return value; + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/LongValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/LongValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/LongValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/LongValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import java.math.BigDecimal; + +import javax.jcr.PropertyType; + +final class LongValue extends MemoryValue { + + private final long value; + + public LongValue(long value) { + this.value = value; + } + + @Override + public int getType() { + return PropertyType.LONG; + } + + @Override + public BigDecimal getDecimal() { + return new BigDecimal(value); + } + + @Override + public double getDouble() { + return value; + } + + @Override + public long getLong() { + return value; + } + + @Override + public String getString() { + return String.valueOf(value); + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.math.BigDecimal; +import java.util.Calendar; + +import javax.jcr.PropertyType; + +import org.apache.jackrabbit.oak.api.CoreValue; +import org.apache.jackrabbit.util.ISO8601; + +public abstract class MemoryValue implements CoreValue { + + @Override + public long getLong() { + return Long.parseLong(getString()); + } + + @Override + public double getDouble() { + return Double.parseDouble(getString()); + } + + @Override + public boolean getBoolean() { + throw new UnsupportedOperationException("Unsupported conversion."); + } + + @Override + public BigDecimal getDecimal() { + return new BigDecimal(getString()); + } + + @Override + public InputStream getNewStream() { + try { + return new ByteArrayInputStream(getString().getBytes("UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException("UTF-8 is not supported", e); + } + } + + @Override + public long length() { + return getString().length(); + } + + //----------------------------------------------------------< Comparable > + + @Override + public int compareTo(CoreValue o) { + if (this == o) { + return 0; + } + + int type = getType(); + if (type != o.getType()) { + return type - o.getType(); + } else if (type == PropertyType.LONG) { + return Long.signum(getLong() - o.getLong()); + } else if (type == PropertyType.DOUBLE) { + return Double.compare(getDouble(), o.getDouble()); + } else if (type == PropertyType.DECIMAL) { + return getDecimal().compareTo(o.getDecimal()); + } else if (type == PropertyType.BOOLEAN) { + return (getBoolean() ? 1 : 0) - (o.getBoolean() ? 1 : 0); + } else if (type == PropertyType.DATE) { + Calendar a = ISO8601.parse(getString()); + Calendar b = ISO8601.parse(o.getString()); + if (a != null && b != null) { + return a.compareTo(b); + } else { + return getString().compareTo(o.getString()); + } + } else { + return getString().compareTo(o.getString()); + } + } + + //--------------------------------------------------------------< Object > + + @Override + public int hashCode() { + return getType() ^ getString().hashCode(); + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (o instanceof CoreValue) { + return compareTo((CoreValue) o) == 0; + } else { + return false; + } + } + + @Override + public String toString() { + return getString(); + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValueFactory.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValueFactory.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValueFactory.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryValueFactory.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.math.BigDecimal; + +import javax.jcr.PropertyType; + +import org.apache.jackrabbit.oak.api.CoreValue; +import org.apache.jackrabbit.oak.api.CoreValueFactory; + +public class MemoryValueFactory implements CoreValueFactory { + + public static final CoreValueFactory INSTANCE = new MemoryValueFactory(); + + @Override + public CoreValue createValue(String value) { + return new StringValue(value); + } + + @Override + public CoreValue createValue(double value) { + return new DoubleValue(value); + } + + @Override + public CoreValue createValue(long value) { + return new LongValue(value); + } + + @Override + public CoreValue createValue(boolean value) { + if (value) { + return BooleanValue.TRUE; + } else { + return BooleanValue.FALSE; + } + } + + @Override + public CoreValue createValue(BigDecimal value) { + return new DecimalValue(value); + } + + @Override + public CoreValue createValue(InputStream value) throws IOException { + try { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + byte[] b = new byte[4096]; + int n = value.read(b); + while (n != -1) { + buffer.write(b, 0, n); + n = value.read(b); + } + return new BinaryValue(buffer.toByteArray()); + } finally { + value.close(); + } + } + + @Override + public CoreValue createValue(String value, final int type) { + if (type == PropertyType.BINARY) { + try { + return new BinaryValue(value.getBytes("UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException("UTF-8 is not supported", e); + } + } else if (type == PropertyType.DECIMAL) { + return createValue(createValue(value).getDecimal()); + } else if (type == PropertyType.DECIMAL) { + return createValue(createValue(value).getDecimal()); + } else if (type == PropertyType.DOUBLE) { + return createValue(createValue(value).getDouble()); + } else if (type == PropertyType.LONG) { + return createValue(createValue(value).getLong()); + } else if (type == PropertyType.STRING) { + return createValue(value); + } else { + return new GenericValue(type, value); + } + } + +} Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringValue.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringValue.java?rev=1353771&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringValue.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringValue.java Mon Jun 25 22:50:24 2012 @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.oak.plugins.memory; + +import javax.jcr.PropertyType; + +final class StringValue extends MemoryValue { + + private final String value; + + public StringValue(String value) { + this.value = value; + } + + @Override + public int getType() { + return PropertyType.STRING; + } + + @Override + public boolean getBoolean() { + return Boolean.parseBoolean(value); + } + + @Override + public String getString() { + return value; + } + +}