6aee42b4de93c318b5706df61d96d1fdbd5e5719
[vxquery.git] / vxquery-core / src / main / java / org / apache / vxquery / datamodel / accessors / nodes / PINodePointable.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.vxquery.datamodel.accessors.nodes;
18
19 import org.apache.hyracks.api.dataflow.value.ITypeTraits;
20 import org.apache.hyracks.data.std.api.IPointable;
21 import org.apache.hyracks.data.std.api.IPointableFactory;
22 import org.apache.hyracks.data.std.primitive.VoidPointable;
23 import org.apache.hyracks.util.string.UTF8StringUtil;
24
25 /*
26 * PI {
27 * NodeId nodeId?;
28 * String target
29 * String content;
30 * }
31 */
32 public class PINodePointable extends AbstractNodePointable {
33 private static final int LOCAL_NODE_ID_SIZE = 4;
34 public static final IPointableFactory FACTORY = new IPointableFactory() {
35 private static final long serialVersionUID = 1L;
36
37 @Override
38 public ITypeTraits getTypeTraits() {
39 return VoidPointable.TYPE_TRAITS;
40 }
41
42 @Override
43 public IPointable createPointable() {
44 return new PINodePointable();
45 }
46 };
47
48 public void getTarget(NodeTreePointable nodeTree, IPointable target) {
49 target.set(bytes, getTargetOffset(nodeTree), getTargetSize(nodeTree));
50 }
51
52 public void getContent(NodeTreePointable nodeTree, IPointable content) {
53 content.set(bytes, getContentOffset(nodeTree), getContentSize(nodeTree));
54 }
55
56 protected int getLocalNodeIdOffset(NodeTreePointable nodeTree) {
57 return start;
58 }
59
60 private int getLocalNodeIdSize(NodeTreePointable nodeTree) {
61 return nodeTree.nodeIdExists() ? LOCAL_NODE_ID_SIZE : 0;
62 }
63
64 private int getTargetOffset(NodeTreePointable nodeTree) {
65 return getLocalNodeIdOffset(nodeTree) + getLocalNodeIdSize(nodeTree);
66 }
67
68 private int getTargetSize(NodeTreePointable nodeTree) {
69 int utfLength = UTF8StringUtil.getUTFLength(bytes, getTargetOffset(nodeTree));
70 return utfLength + UTF8StringUtil.getNumBytesToStoreLength(utfLength);
71 }
72
73 private int getContentOffset(NodeTreePointable nodeTree) {
74 return getTargetOffset(nodeTree) + getTargetSize(nodeTree);
75 }
76
77 private int getContentSize(NodeTreePointable nodeTree) {
78 int utfLength = UTF8StringUtil.getUTFLength(bytes, getContentOffset(nodeTree));
79 return utfLength + UTF8StringUtil.getNumBytesToStoreLength(utfLength);
80 }
81 }