• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / datasketches-java / #306

30 Apr 2024 10:01PM UTC coverage: 97.645% (-0.5%) from 98.139%
#306

push

web-flow
Merge pull request #555 from apache/fix_pom_xml_header

Fix pom xml header

26865 of 27513 relevant lines covered (97.64%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.37
/src/main/java/org/apache/datasketches/quantiles/DoublesSketchAccessor.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.datasketches.quantiles;
21

22
import static org.apache.datasketches.quantiles.PreambleUtil.COMBINED_BUFFER;
23

24
import org.apache.datasketches.common.Family;
25
import org.apache.datasketches.common.SketchesArgumentException;
26

27
/**
28
 * This allows access to package-private levels and data in whatever quantiles sketch you give
29
 * it: on-heap, off-heap; compact and non-compact
30
 * @author Jon Malkin
31
 */
32
abstract class DoublesSketchAccessor extends DoublesBufferAccessor {
33
  static final int BB_LVL_IDX = -1;
34

35
  final DoublesSketch ds_;
36
  final boolean forceSize_;
37

38
  long n_;
39
  int currLvl_;
40
  int numItems_;
41
  int offset_;
42

43
  DoublesSketchAccessor(
44
    final DoublesSketch ds,
45
    final boolean forceSize,
46
    final int level) {
47
    this(checkLvl(level), ds, forceSize, level);
1✔
48
    //SpotBugs CT_CONSTRUCTOR_THROW is false positive.
49
    //this construction scheme is compliant with SEI CERT Oracle Coding Standard for Java / OBJ11-J
50
  }
1✔
51

52
  private DoublesSketchAccessor(
53
      final boolean secure, //required part of Finalizer Attack prevention
54
      final DoublesSketch ds,
55
      final boolean forceSize,
56
      final int level) {
1✔
57
    ds_ = ds;
1✔
58
    forceSize_ = forceSize;
1✔
59
    setLevel(level);
1✔
60
  }
1✔
61

62
  private static final boolean checkLvl(final int level) {
63
    if (level != BB_LVL_IDX && level < 0) {
1✔
64
      throw new SketchesArgumentException("Parameter level is < 0.");
×
65
    }
66
    return true;
1✔
67
  }
68

69
  static DoublesSketchAccessor wrap(final DoublesSketch ds) {
70
    return wrap(ds, false);
1✔
71
  }
72

73
  static DoublesSketchAccessor wrap(final DoublesSketch ds, final boolean forceSize) {
74

75
    if (ds.hasMemory()) {
1✔
76
      return new DirectDoublesSketchAccessor(ds, forceSize, BB_LVL_IDX);
1✔
77
    }
78
    return new HeapDoublesSketchAccessor(ds, forceSize, BB_LVL_IDX);
1✔
79
  }
80

81
  abstract DoublesSketchAccessor copyAndSetLevel(final int level);
82

83
  DoublesSketchAccessor setLevel(final int lvl) {
84
    currLvl_ = lvl;
1✔
85
    if (lvl == BB_LVL_IDX) {
1✔
86
      numItems_ = (forceSize_ ? ds_.getK() * 2 : ds_.getBaseBufferCount());
1✔
87
      offset_ = (ds_.hasMemory() ? COMBINED_BUFFER : 0);
1✔
88
    } else {
89
      if (((ds_.getBitPattern() & (1L << lvl)) > 0) || forceSize_) {
1✔
90
        numItems_ = ds_.getK();
1✔
91
      } else {
92
        numItems_ = 0;
1✔
93
      }
94

95
      // determine offset in two parts
96
      // 1. index into combined buffer (compact vs update)
97
      // 2. adjust if byte offset (direct) instead of array index (heap)
98
      final int levelStart;
99
      if (ds_.isCompact()) {
1✔
100
        levelStart = ds_.getBaseBufferCount() + (countValidLevelsBelow(lvl) * ds_.getK());
1✔
101
      } else {
102
        levelStart = (2 + currLvl_) * ds_.getK();
1✔
103
      }
104

105
      if (ds_.hasMemory()) {
1✔
106
        final int preLongsAndExtra = Family.QUANTILES.getMaxPreLongs() + 2; // +2 for min, max vals
1✔
107
        offset_ = (preLongsAndExtra + levelStart) << 3;
1✔
108
      } else {
1✔
109
        offset_ = levelStart;
1✔
110
      }
111
    }
112

113
    n_ = ds_.getN();
1✔
114

115
    return this;
1✔
116
  }
117

118
  // getters/queries
119

120
  @Override
121
  int numItems() {
122
    return numItems_;
1✔
123
  }
124

125
  @Override
126
  abstract double get(final int index);
127

128
  @Override
129
  abstract double[] getArray(final int fromIdx, final int numItems);
130

131
  // setters/modifying methods
132

133
  @Override
134
  abstract double set(final int index, final double quantile);
135

136
  @Override
137
  abstract void putArray(final double[] srcArray, final int srcIndex,
138
                         final int dstIndex, final int numItems);
139

140
  abstract void sort();
141

142
  /**
143
   * Counts number of full levels in the sketch below tgtLvl. Useful for computing the level
144
   * offset in a compact sketch.
145
   * @param tgtLvl Target level in the sketch
146
   * @return Number of full levels in the sketch below tgtLvl
147
   */
148
  private int countValidLevelsBelow(final int tgtLvl) {
149
    int count = 0;
1✔
150
    long bitPattern = ds_.getBitPattern();
1✔
151
    for (int i = 0; (i < tgtLvl) && (bitPattern > 0); ++i, bitPattern >>>= 1) {
1✔
152
      if ((bitPattern & 1L) > 0L) {
1✔
153
        ++count;
1✔
154
      }
155
    }
156
    return count;
1✔
157

158
    // shorter implementation, testing suggests a tiny bit slower
159
    //final long mask = (1 << tgtLvl) - 1;
160
    //return Long.bitCount(ds_.getBitPattern() & mask);
161
  }
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc