virajjasani commented on code in PR #1780: URL: https://github.com/apache/phoenix/pull/1780#discussion_r1581856136
########## phoenix-core-client/src/main/java/org/apache/phoenix/util/json/BsonDataFormat.java: ########## @@ -0,0 +1,201 @@ +/* + * 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.phoenix.util.json; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; +import com.jayway.jsonpath.PathNotFoundException; +import org.apache.hadoop.hbase.util.Bytes; +import org.bson.BsonBinaryReader; +import org.bson.BsonDocument; +import org.bson.BsonDocumentReader; +import org.bson.BsonValue; +import org.bson.RawBsonDocument; +import org.bson.codecs.BsonDocumentCodec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.RawBsonDocumentCodec; +import org.bson.io.ByteBufferBsonInput; + +import java.nio.ByteBuffer; +import java.sql.Types; +import java.util.List; +import java.util.stream.Collectors; + +public class BsonDataFormat implements JsonDataFormat { + @Override + public byte[] toBytes(Object object) { + return Bytes.toBytes(((RawBsonDocument) object).getByteBuffer().asNIO()); + } + + @Override + public Object toObject(String value) { + return RawBsonDocument.parse(value); + } + + @Override + public Object toObject(byte[] bytes, int offset, int length) { + return new RawBsonDocument(bytes, offset, length); + } + + @Override + public int estimateByteSize(Object o) { + RawBsonDocument rawBSON = (RawBsonDocument) o; + return rawBSON.size(); + } + + @Override + public int getValueType(Object obj, String jsonPathExprStr) { + BsonValue value = getBsonValue(jsonPathExprStr, (RawBsonDocument) obj); + return getSqlType(value); + } + + @Override + public Object getValue(Object obj, String jsonPathExprStr) { + BsonValue value = getBsonValue(jsonPathExprStr, (RawBsonDocument) obj); + return getValue(value); + } + + private Object getValue(BsonValue value) { + if (value != null) { + switch (value.getBsonType()) { + case INT32: + return value.asInt32().getValue(); + case INT64: + return value.asInt64().getValue(); + case STRING: + case SYMBOL: + return value.asString().getValue(); + case DECIMAL128: + return value.asDecimal128().doubleValue(); + case DOUBLE: + return value.asDouble().getValue(); + case BOOLEAN: + return value.asBoolean().getValue(); + case BINARY: + return value.asBinary().getData(); + case DATE_TIME: + return value.asDateTime().getValue(); + case DOCUMENT: + return value.asDocument().toJson(); + case ARRAY: + return readArray(value).toString(); + default: + return null; + } + } + return null; + } + + @Override + public ByteBuffer updateValue(Object top, String jsonPathExprStr, String newVal) { + Configuration conf = Configuration.builder().jsonProvider(new BsonJsonProvider()).build(); + BsonValue newValue = JsonPath.using(conf).parse(newVal).json(); + BsonDocument root = fromRaw((RawBsonDocument) top); Review Comment: The problem here is that, `asDocument()` just does the typecast, so even if we use it, the underlying object is still of Raw type, meaning we will not be able to update the document. Hence, we need to create updatable `BsonDocument` object using `BsonBinaryReader`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
