You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2012/11/08 15:35:12 UTC
svn commit: r1407100 - in
/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak:
kernel/JsopDiff.java kernel/TypeCodes.java
plugins/index/old/PrefixContentIndex.java plugins/memory/PropertyStates.java
Author: mduerig
Date: Thu Nov 8 14:35:11 2012
New Revision: 1407100
URL: http://svn.apache.org/viewvc?rev=1407100&view=rev
Log:
OAK-417: Refactor/reimplement TreeImpl.getStatus and TreeImpl.getPropertyStatus to take advantage of the underlying NodeBuilder
refactor TypeCodes
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TypeCodes.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/old/PrefixContentIndex.java
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java?rev=1407100&r1=1407099&r2=1407100&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java Thu Nov 8 14:35:11 2012
@@ -154,15 +154,15 @@ class JsopDiff implements NodeStateDiff
case PropertyType.BINARY:
for (Blob value : property.getValue(BINARIES)) {
InputStream is = value.getNewStream();
- String binId = TypeCodes.getCodeForType(type) + ':' + kernel.write(is);
+ String binId = TypeCodes.encode(type, kernel.write(is));
close(is);
jsop.value(binId);
}
break;
default:
for (String value : property.getValue(STRINGS)) {
- if (PropertyType.STRING != type || TypeCodes.startsWithCode(value)) {
- value = TypeCodes.getCodeForType(type) + ':' + value;
+ if (PropertyType.STRING != type || TypeCodes.split(value) != -1) {
+ value = TypeCodes.encode(type, value);
}
jsop.value(value);
}
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TypeCodes.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TypeCodes.java?rev=1407100&r1=1407099&r2=1407100&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TypeCodes.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/TypeCodes.java Thu Nov 8 14:35:11 2012
@@ -22,6 +22,8 @@ import java.util.Map;
import javax.jcr.PropertyType;
+import static com.google.common.base.Preconditions.checkNotNull;
+
/**
* TypeCodes maps between {@code Type} and the code used to prefix
* its json serialisation.
@@ -41,34 +43,63 @@ public class TypeCodes {
private TypeCodes() { }
/**
- * Returns {@code true} if the specified JSON String represents a value
- * serialization that is prefixed with a type code.
- *
- * @param jsonString The JSON String representation of the value of a {@code PropertyState}
- * @return {@code true} if the {@code jsonString} starts with a type
- * code; {@code false} otherwise.
+ * Encodes the given {@code propertyName} of the given {@code propertyType} into
+ * a json string, which is prefixed with a type code.
+ * @param propertyType type of the property
+ * @param propertyName name of the property
+ * @return type code prefixed json string
*/
- public static boolean startsWithCode(String jsonString) {
- return jsonString.length() >= 4 && jsonString.charAt(3) == ':';
+ public static String encode(int propertyType, String propertyName) {
+ String typeCode = checkNotNull(TYPE2CODE.get(propertyType));
+ return typeCode + ':' + propertyName;
}
/**
- * Get the type code for the given property type.
- *
- * @param propertyType the property type
- * @return the type code
+ * Splits a {@code jsonString}, which is prefixed with a type code
+ * at the location where the prefix ends.
+ * @param jsonString json string to split
+ * @return the location where the prefix ends or -1 if no prefix is present
*/
- public static String getCodeForType(int propertyType) {
- return TYPE2CODE.get(propertyType);
+ public static int split(String jsonString) {
+ if (jsonString.length() >= 4 && jsonString.charAt(3) == ':') {
+ return 3;
+ }
+ else {
+ return -1;
+ }
}
/**
- * Get the property type for the given type code.
- * @param code the type code
- * @return the property type.
+ * Decode the type encoded into {@code jsonString} given its split.
+ * @param split split of the json string
+ * @param jsonString json string
+ * @return decoded type. {@code PropertyType.UNDEFINED} if none or split is not within {@code jsonString}.
*/
- public static int getTypeForCode(String code) {
- return CODE2TYPE.get(code);
+ public static int decodeType(int split, String jsonString) {
+ if (split == -1 || split > jsonString.length()) {
+ return PropertyType.UNDEFINED;
+ }
+ else {
+ Integer type = CODE2TYPE.get(jsonString.substring(0, split));
+ return type == null
+ ? PropertyType.UNDEFINED
+ : type;
+ }
+ }
+
+ /**
+ * Decode the property name encoded into a {@code jsonString} given its split.
+ * @param split split of the json string
+ * @param jsonString json string
+ * @return decoded property name. Or {@code jsonString} if split is not with {@code jsonString}.
+ */
+ public static String decodeName(int split, String jsonString) {
+ if (split == -1 || split >= jsonString.length()) {
+ return jsonString;
+ }
+ else {
+ return jsonString.substring(split + 1);
+ }
}
}
\ No newline at end of file
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/old/PrefixContentIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/old/PrefixContentIndex.java?rev=1407100&r1=1407099&r2=1407100&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/old/PrefixContentIndex.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/old/PrefixContentIndex.java Thu Nov 8 14:35:11 2012
@@ -65,8 +65,7 @@ public class PrefixContentIndex implemen
if (restriction.propertyType == PropertyType.UNDEFINED) {
continue;
}
- String code = TypeCodes.getCodeForType(restriction.propertyType);
- String prefix = code + ":";
+ String prefix = TypeCodes.encode(restriction.propertyType, "");
if (prefix.equals(index.getPrefix())) {
return restriction;
}
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java?rev=1407100&r1=1407099&r2=1407100&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java Thu Nov 8 14:35:11 2012
@@ -288,9 +288,10 @@ public final class PropertyStates {
return BooleanPropertyState.booleanProperty(name, false);
} else if (reader.matches(JsopReader.STRING)) {
String jsonString = reader.getToken();
- if (TypeCodes.startsWithCode(jsonString)) {
- int type = TypeCodes.getTypeForCode(jsonString.substring(0, 3));
- String value = jsonString.substring(4);
+ int split = TypeCodes.split(jsonString);
+ if (split != -1) {
+ int type = TypeCodes.decodeType(split, jsonString);
+ String value = TypeCodes.decodeName(split, jsonString);
if (type == PropertyType.BINARY) {
return BinaryPropertyState.binaryProperty(name, new KernelBlob(value, kernel));
} else {
@@ -327,9 +328,10 @@ public final class PropertyStates {
values.add(false);
} else if (reader.matches(JsopReader.STRING)) {
String jsonString = reader.getToken();
- if (TypeCodes.startsWithCode(jsonString)) {
- type = TypeCodes.getTypeForCode(jsonString.substring(0, 3));
- String value = jsonString.substring(4);
+ int split = TypeCodes.split(jsonString);
+ if (split != -1) {
+ type = TypeCodes.decodeType(split, jsonString);
+ String value = TypeCodes.decodeName(split, jsonString);
if (type == PropertyType.BINARY) {
values.add(new KernelBlob(value, kernel));
} else if(type == PropertyType.DOUBLE) {