• 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

98.4
/src/main/java/org/apache/datasketches/hll/ToByteArrayImpl.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.hll;
21

22
import static org.apache.datasketches.hll.AbstractCoupons.find;
23
import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS;
24
import static org.apache.datasketches.hll.PreambleUtil.AUX_COUNT_INT;
25
import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START;
26
import static org.apache.datasketches.hll.PreambleUtil.insertAuxCount;
27
import static org.apache.datasketches.hll.PreambleUtil.insertCompactFlag;
28
import static org.apache.datasketches.hll.PreambleUtil.insertCurMin;
29
import static org.apache.datasketches.hll.PreambleUtil.insertCurMode;
30
import static org.apache.datasketches.hll.PreambleUtil.insertEmptyFlag;
31
import static org.apache.datasketches.hll.PreambleUtil.insertFamilyId;
32
import static org.apache.datasketches.hll.PreambleUtil.insertHashSetCount;
33
import static org.apache.datasketches.hll.PreambleUtil.insertHipAccum;
34
import static org.apache.datasketches.hll.PreambleUtil.insertInt;
35
import static org.apache.datasketches.hll.PreambleUtil.insertKxQ0;
36
import static org.apache.datasketches.hll.PreambleUtil.insertKxQ1;
37
import static org.apache.datasketches.hll.PreambleUtil.insertLgArr;
38
import static org.apache.datasketches.hll.PreambleUtil.insertLgK;
39
import static org.apache.datasketches.hll.PreambleUtil.insertListCount;
40
import static org.apache.datasketches.hll.PreambleUtil.insertNumAtCurMin;
41
import static org.apache.datasketches.hll.PreambleUtil.insertOooFlag;
42
import static org.apache.datasketches.hll.PreambleUtil.insertPreInts;
43
import static org.apache.datasketches.hll.PreambleUtil.insertRebuildCurMinNumKxQFlag;
44
import static org.apache.datasketches.hll.PreambleUtil.insertSerVer;
45
import static org.apache.datasketches.hll.PreambleUtil.insertTgtHllType;
46

47
import org.apache.datasketches.common.SketchesStateException;
48
import org.apache.datasketches.memory.Memory;
49
import org.apache.datasketches.memory.WritableMemory;
50

51
/**
52
 * @author Lee Rhodes
53
 */
54
class ToByteArrayImpl {
1✔
55

56
  // To byte array used by the heap HLL modes.
57
  static final byte[] toHllByteArray(final AbstractHllArray impl, final boolean compact) {
58
    int auxBytes = 0;
1✔
59
    if (impl.tgtHllType == TgtHllType.HLL_4) {
1✔
60
      final AuxHashMap auxHashMap = impl.getAuxHashMap();
1✔
61
      if (auxHashMap != null) {
1✔
62
        auxBytes = (compact)
1✔
63
            ? auxHashMap.getCompactSizeBytes()
1✔
64
            : auxHashMap.getUpdatableSizeBytes();
1✔
65
      } else {
66
        auxBytes = (compact) ? 0 : 4 << LG_AUX_ARR_INTS[impl.lgConfigK];
1✔
67
      }
68
    }
69
    final int totBytes = HLL_BYTE_ARR_START + impl.getHllByteArrBytes() + auxBytes;
1✔
70
    final byte[] byteArr = new byte[totBytes];
1✔
71
    final WritableMemory wmem = WritableMemory.writableWrap(byteArr);
1✔
72
    insertHll(impl, wmem, compact);
1✔
73
    return byteArr;
1✔
74
  }
75

76
  private static final void insertHll(final AbstractHllArray impl, final WritableMemory wmem,
77
      final boolean compact) {
78
    insertCommonHll(impl, wmem, compact);
1✔
79
    final byte[] hllByteArr = ((HllArray)impl).hllByteArr;
1✔
80
    wmem.putByteArray(HLL_BYTE_ARR_START, hllByteArr, 0, hllByteArr.length);
1✔
81
    if (impl.getAuxHashMap() != null) {
1✔
82
      insertAux(impl, wmem, compact);
1✔
83
    } else {
84
      wmem.putInt(AUX_COUNT_INT, 0);
1✔
85
    }
86
  }
1✔
87

88
  private static final void insertCommonHll(final AbstractHllArray srcImpl,
89
      final WritableMemory tgtWmem, final boolean compact) {
90
    insertPreInts(tgtWmem, srcImpl.getPreInts());
1✔
91
    insertSerVer(tgtWmem);
1✔
92
    insertFamilyId(tgtWmem);
1✔
93
    insertLgK(tgtWmem, srcImpl.getLgConfigK());
1✔
94
    insertEmptyFlag(tgtWmem, srcImpl.isEmpty());
1✔
95
    insertCompactFlag(tgtWmem, compact);
1✔
96
    insertOooFlag(tgtWmem, srcImpl.isOutOfOrder());
1✔
97
    insertCurMin(tgtWmem, srcImpl.getCurMin());
1✔
98
    insertCurMode(tgtWmem, srcImpl.getCurMode());
1✔
99
    insertTgtHllType(tgtWmem, srcImpl.getTgtHllType());
1✔
100
    insertHipAccum(tgtWmem, srcImpl.getHipAccum());
1✔
101
    insertKxQ0(tgtWmem, srcImpl.getKxQ0());
1✔
102
    insertKxQ1(tgtWmem, srcImpl.getKxQ1());
1✔
103
    insertNumAtCurMin(tgtWmem, srcImpl.getNumAtCurMin());
1✔
104
    insertRebuildCurMinNumKxQFlag(tgtWmem, srcImpl.isRebuildCurMinNumKxQFlag());
1✔
105
  }
1✔
106

107
  private static final void insertAux(final AbstractHllArray srcImpl, final WritableMemory tgtWmem,
108
      final boolean tgtCompact) {
109
    final AuxHashMap auxHashMap = srcImpl.getAuxHashMap();
1✔
110
    final int auxCount = auxHashMap.getAuxCount();
1✔
111
    insertAuxCount(tgtWmem, auxCount);
1✔
112
    insertLgArr(tgtWmem, auxHashMap.getLgAuxArrInts()); //only used for direct HLL
1✔
113
    final long auxStart = srcImpl.auxStart;
1✔
114
    if (tgtCompact) {
1✔
115
      final PairIterator itr = auxHashMap.getIterator();
1✔
116
      int cnt = 0;
1✔
117
      while (itr.nextValid()) { //works whether src has compact memory or not
1✔
118
        insertInt(tgtWmem, auxStart + (cnt++ << 2), itr.getPair());
1✔
119
      }
120
      assert cnt == auxCount;
1✔
121
    } else { //updatable
1✔
122
      final int auxInts = 1 << auxHashMap.getLgAuxArrInts();
1✔
123
      final int[] auxArr = auxHashMap.getAuxIntArr();
1✔
124
      tgtWmem.putIntArray(auxStart, auxArr, 0, auxInts);
1✔
125
    }
126
  }
1✔
127

128
  //To byte array for coupons
129
  static final byte[] toCouponByteArray(final AbstractCoupons impl, final boolean dstCompact) {
130
    final int srcCouponCount = impl.getCouponCount();
1✔
131
    final int srcLgCouponArrInts = impl.getLgCouponArrInts();
1✔
132
    final int srcCouponArrInts = 1 << srcLgCouponArrInts;
1✔
133
    final byte[] byteArrOut;
134
    final boolean list = impl.getCurMode() == CurMode.LIST;
1✔
135
    //prepare switch
136
    final int sw = (impl.isMemory() ? 0 : 4) | (impl.isCompact() ? 0 : 2) | (dstCompact ? 0 : 1);
1✔
137
    switch (sw) {
1✔
138
      case 0: { //Src Memory, Src Compact, Dst Compact
139
        final Memory srcMem = impl.getMemory();
1✔
140
        final int bytesOut = impl.getMemDataStart() + (srcCouponCount << 2);
1✔
141
        byteArrOut = new byte[bytesOut];
1✔
142
        srcMem.getByteArray(0, byteArrOut, 0, bytesOut);
1✔
143
        break;
1✔
144
      }
145
      case 1: { //Src Memory, Src Compact, Dst Updatable
146
        final int dataStart = impl.getMemDataStart();
1✔
147
        final int bytesOut = dataStart + (srcCouponArrInts << 2);
1✔
148
        byteArrOut = new byte[bytesOut];
1✔
149
        final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
1✔
150
        copyCommonListAndSet(impl, memOut);
1✔
151
        insertCompactFlag(memOut, dstCompact);
1✔
152

153
        final int[] tgtCouponIntArr = new int[srcCouponArrInts];
1✔
154
        final PairIterator itr = impl.iterator();
1✔
155
        while (itr.nextValid()) {
1✔
156
          final int pair = itr.getPair();
1✔
157
          final int idx = find(tgtCouponIntArr, srcLgCouponArrInts, pair);
1✔
158
          if (idx < 0) { //found EMPTY
1✔
159
            tgtCouponIntArr[~idx] = pair; //insert
1✔
160
            continue;
1✔
161
          }
162
          throw new SketchesStateException("Error: found duplicate.");
×
163
        }
164
        memOut.putIntArray(dataStart, tgtCouponIntArr, 0, srcCouponArrInts);
1✔
165

166
        if (list) {
1✔
167
          insertListCount(memOut, srcCouponCount);
1✔
168
        } else {
169
          insertHashSetCount(memOut, srcCouponCount);
1✔
170
        }
171
        break;
1✔
172
      }
173

174
      case 6:   //Src Heap,   Src Updatable, Dst Compact
175
      case 2: { //Src Memory, Src Updatable, Dst Compact
176
        final int dataStart = impl.getMemDataStart();
1✔
177
        final int bytesOut = dataStart + (srcCouponCount << 2);
1✔
178
        byteArrOut = new byte[bytesOut];
1✔
179
        final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
1✔
180
        copyCommonListAndSet(impl, memOut);
1✔
181
        insertCompactFlag(memOut, dstCompact);
1✔
182

183
        final PairIterator itr = impl.iterator();
1✔
184
        int cnt = 0;
1✔
185
        while (itr.nextValid()) {
1✔
186
          insertInt(memOut, dataStart + (cnt++ << 2), itr.getPair());
1✔
187
        }
188
        if (list) {
1✔
189
          insertListCount(memOut, srcCouponCount);
1✔
190
        } else {
191
          insertHashSetCount(memOut, srcCouponCount);
1✔
192
        }
193
        break;
1✔
194
      }
195
      case 3: { //Src Memory, Src Updatable, Dst Updatable
196
        final Memory srcMem = impl.getMemory();
1✔
197
        final int bytesOut = impl.getMemDataStart() + (srcCouponArrInts << 2);
1✔
198
        byteArrOut = new byte[bytesOut];
1✔
199
        srcMem.getByteArray(0, byteArrOut, 0, bytesOut);
1✔
200
        break;
1✔
201
      }
202
      case 7: { //Src Heap, Src Updatable, Dst Updatable
203
        final int dataStart = impl.getMemDataStart();
1✔
204
        final int bytesOut = dataStart + (srcCouponArrInts << 2);
1✔
205
        byteArrOut = new byte[bytesOut];
1✔
206
        final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut);
1✔
207
        copyCommonListAndSet(impl, memOut);
1✔
208

209
        memOut.putIntArray(dataStart, impl.getCouponIntArr(), 0, srcCouponArrInts);
1✔
210
        if (list) {
1✔
211
          insertListCount(memOut, srcCouponCount);
1✔
212
        } else {
213
          insertHashSetCount(memOut, srcCouponCount);
1✔
214
        }
215
        break;
1✔
216
      }
217
      default: throw new SketchesStateException("Corruption, should not happen: " + sw);
×
218
    }
219
    return byteArrOut;
1✔
220
  }
221

222
  private static final void copyCommonListAndSet(final AbstractCoupons impl,
223
      final WritableMemory wmem) {
224
    insertPreInts(wmem, impl.getPreInts());
1✔
225
    insertSerVer(wmem);
1✔
226
    insertFamilyId(wmem);
1✔
227
    insertLgK(wmem, impl.getLgConfigK());
1✔
228
    insertLgArr(wmem, impl.getLgCouponArrInts());
1✔
229
    insertEmptyFlag(wmem, impl.isEmpty());
1✔
230
    insertOooFlag(wmem, impl.isOutOfOrder());
1✔
231
    insertCurMode(wmem, impl.getCurMode());
1✔
232
    insertTgtHllType(wmem, impl.getTgtHllType());
1✔
233
  }
1✔
234

235
}
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