-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathDirectCompactSketch.java
More file actions
177 lines (153 loc) · 5.74 KB
/
DirectCompactSketch.java
File metadata and controls
177 lines (153 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* 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.datasketches.theta;
import static org.apache.datasketches.theta.CompactOperations.checkIllegalCurCountAndEmpty;
import static org.apache.datasketches.theta.CompactOperations.memoryToCompact;
import static org.apache.datasketches.theta.PreambleUtil.ORDERED_FLAG_MASK;
import static org.apache.datasketches.theta.PreambleUtil.extractCurCount;
import static org.apache.datasketches.theta.PreambleUtil.extractFlags;
import static org.apache.datasketches.theta.PreambleUtil.extractPreLongs;
import static org.apache.datasketches.theta.PreambleUtil.extractSeedHash;
import static org.apache.datasketches.theta.PreambleUtil.extractThetaLong;
import static org.apache.datasketches.theta.SingleItemSketch.otherCheckForSingleItem;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.WritableMemory;
import org.apache.datasketches.thetacommon.ThetaUtil;
/**
* An off-heap (Direct), compact, read-only sketch. The internal hash array can be either ordered
* or unordered. It is not empty, not a single item.
*
* <p>This sketch can only be associated with a Serialization Version 3 format binary image.</p>
*
* <p>This implementation uses data in a given Memory that is owned and managed by the caller.
* This Memory can be off-heap, which if managed properly will greatly reduce the need for
* the JVM to perform garbage collection.</p>
*
* @author Lee Rhodes
*/
class DirectCompactSketch extends CompactSketch {
final Memory mem_;
/**
* Construct this sketch with the given memory.
* @param mem Read-only Memory object with the order bit properly set.
*/
DirectCompactSketch(final Memory mem) {
mem_ = mem;
}
/**
* Wraps the given Memory, which must be a SerVer 3, CompactSketch image.
* Must check the validity of the Memory before calling. The order bit must be set properly.
* @param srcMem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
* @param seedHash The update seedHash.
* <a href="{@docRoot}/resources/dictionary.html#seedHash">See Seed Hash</a>.
* @return this sketch
*/
static DirectCompactSketch wrapInstance(final Memory srcMem, final short seedHash) {
ThetaUtil.checkSeedHashes((short) extractSeedHash(srcMem), seedHash);
return new DirectCompactSketch(srcMem);
}
//Sketch Overrides
@Override
public CompactSketch compact(final boolean dstOrdered, final WritableMemory dstMem) {
return memoryToCompact(mem_, dstOrdered, dstMem);
}
@Override
public int getCurrentBytes() {
if (otherCheckForSingleItem(mem_)) { return 16; }
final int preLongs = extractPreLongs(mem_);
final int curCount = (preLongs == 1) ? 0 : extractCurCount(mem_);
return (preLongs + curCount) << 3;
}
@Override
public int getRetainedEntries(final boolean valid) { //compact is always valid
if (otherCheckForSingleItem(mem_)) { return 1; }
final int preLongs = extractPreLongs(mem_);
final int curCount = (preLongs == 1) ? 0 : extractCurCount(mem_);
return curCount;
}
@Override
public long getThetaLong() {
final int preLongs = extractPreLongs(mem_);
return (preLongs > 2) ? extractThetaLong(mem_) : Long.MAX_VALUE;
}
@Override
public boolean hasMemory() {
return mem_ != null;
}
@Override
public boolean isDirect() {
return hasMemory() ? mem_.isDirect() : false;
}
@Override
public boolean isEmpty() {
final boolean emptyFlag = PreambleUtil.isEmptyFlag(mem_);
final long thetaLong = getThetaLong();
final int curCount = getRetainedEntries(true);
return emptyFlag || ((curCount == 0) && (thetaLong == Long.MAX_VALUE));
}
@Override
public boolean isOrdered() {
return (extractFlags(mem_) & ORDERED_FLAG_MASK) > 0;
}
@Override
public boolean isSameResource(final Memory that) {
return hasMemory() ? mem_.isSameResource(that) : false;
}
@Override
public HashIterator iterator() {
return new MemoryHashIterator(mem_, getRetainedEntries(true), getThetaLong());
}
@Override
public byte[] toByteArray() {
checkIllegalCurCountAndEmpty(isEmpty(), getRetainedEntries());
final int outBytes = getCurrentBytes();
final byte[] byteArrOut = new byte[outBytes];
mem_.getByteArray(0, byteArrOut, 0, outBytes);
return byteArrOut;
}
//restricted methods
@Override
long[] getCache() {
if (otherCheckForSingleItem(mem_)) { return new long[] { mem_.getLong(8) }; }
final int preLongs = extractPreLongs(mem_);
final int curCount = (preLongs == 1) ? 0 : extractCurCount(mem_);
if (curCount > 0) {
final long[] cache = new long[curCount];
mem_.getLongArray(preLongs << 3, cache, 0, curCount);
return cache;
}
return new long[0];
}
@Override
int getCompactPreambleLongs() {
return extractPreLongs(mem_);
}
@Override
int getCurrentPreambleLongs() {
return extractPreLongs(mem_);
}
@Override
Memory getMemory() {
return mem_;
}
@Override
short getSeedHash() {
return (short) extractSeedHash(mem_);
}
}