Skip to content

Commit 3ee1750

Browse files
authored
Merge pull request #589 from apache/add_MemoryStatus_to_theta
Add new MemoryStatus interface to Theta.
2 parents 4197148 + 83d44cb commit 3ee1750

16 files changed

Lines changed: 99 additions & 105 deletions
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.common;
21+
22+
import org.apache.datasketches.memory.Memory;
23+
24+
/**
25+
* Methods for inquiring the status of a backing Memory object.
26+
*/
27+
public interface MemoryStatus {
28+
29+
/**
30+
* Returns true if this object's internal data is backed by a Memory object,
31+
* which may be on-heap or off-heap.
32+
* @return true if this object's internal data is backed by a Memory object.
33+
*/
34+
default boolean hasMemory() { return false; }
35+
36+
/**
37+
* Returns true if this object's internal data is backed by direct (off-heap) Memory.
38+
* @return true if this object's internal data is backed by direct (off-heap) Memory.
39+
*/
40+
default boolean isDirect() { return false; }
41+
42+
/**
43+
* Returns true if the backing resource of <i>this</i> is identical with the backing resource
44+
* of <i>that</i>. The capacities must be the same. If <i>this</i> is a region,
45+
* the region offset must also be the same.
46+
*
47+
* @param that A different non-null and alive Memory object.
48+
* @return true if the backing resource of <i>this</i> is identical with the backing resource
49+
* of <i>that</i>.
50+
* @throws SketchesArgumentException if <i>that</i> is not alive (already closed).
51+
*/
52+
default boolean isSameResource(final Memory that) { return false; }
53+
54+
}

src/main/java/org/apache/datasketches/theta/AnotBimpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Arrays;
2727

2828
import org.apache.datasketches.common.SketchesArgumentException;
29-
import org.apache.datasketches.memory.Memory;
3029
import org.apache.datasketches.memory.WritableMemory;
3130
import org.apache.datasketches.thetacommon.ThetaUtil;
3231

@@ -148,11 +147,6 @@ int getRetainedEntries() {
148147
return curCount_;
149148
}
150149

151-
@Override
152-
public boolean isSameResource(final Memory that) {
153-
return false;
154-
}
155-
156150
//restricted
157151

158152
private static long[] getHashArrA(final Sketch skA) { //returns a new array

src/main/java/org/apache/datasketches/theta/ConcurrentHeapThetaBuffer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.concurrent.atomic.AtomicBoolean;
2727

2828
import org.apache.datasketches.common.ResizeFactor;
29+
import org.apache.datasketches.memory.Memory;
2930
import org.apache.datasketches.thetacommon.HashOperations;
3031

3132
/**
@@ -167,6 +168,11 @@ public boolean isEstimationMode() {
167168
return shared.isEstimationMode();
168169
}
169170

171+
@Override
172+
public boolean isSameResource(final Memory that) {
173+
return shared.isSameResource(that);
174+
}
175+
170176
//End of proxies
171177

172178
@Override

src/main/java/org/apache/datasketches/theta/ConcurrentSharedThetaSketch.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.concurrent.atomic.AtomicBoolean;
2323

24+
import org.apache.datasketches.common.MemoryStatus;
2425
import org.apache.datasketches.memory.WritableMemory;
2526

2627
/**
@@ -30,7 +31,7 @@
3031
*
3132
* @author eshcar
3233
*/
33-
interface ConcurrentSharedThetaSketch {
34+
interface ConcurrentSharedThetaSketch extends MemoryStatus {
3435

3536
long NOT_SINGLE_HASH = -1L;
3637
double MIN_ERROR = 0.0000001;
@@ -127,7 +128,7 @@ boolean propagate(final AtomicBoolean localPropagationInProgress, final Sketch s
127128
//attempt to access these methods from the local buffer will be diverted to the shared
128129
//sketch.
129130

130-
//From Sketch
131+
//From Sketch and MemoryStatus
131132

132133
int getCompactBytes();
133134

@@ -139,10 +140,6 @@ boolean propagate(final AtomicBoolean localPropagationInProgress, final Sketch s
139140

140141
double getUpperBound(int numStdDev);
141142

142-
boolean hasMemory();
143-
144-
boolean isDirect();
145-
146143
boolean isEmpty();
147144

148145
boolean isEstimationMode();

src/main/java/org/apache/datasketches/theta/DirectCompactSketch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public long getThetaLong() {
109109

110110
@Override
111111
public boolean hasMemory() {
112-
return true;
112+
return mem_ != null;
113113
}
114114

115115
@Override
116116
public boolean isDirect() {
117-
return mem_.isDirect();
117+
return hasMemory() ? mem_.isDirect() : false;
118118
}
119119

120120
@Override
@@ -132,7 +132,7 @@ public boolean isOrdered() {
132132

133133
@Override
134134
public boolean isSameResource(final Memory that) {
135-
return mem_.isSameResource(that);
135+
return hasMemory() ? mem_.isSameResource(that) : false;
136136
}
137137

138138
@Override

src/main/java/org/apache/datasketches/theta/DirectQuickSelectSketchR.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ public long getThetaLong() {
144144

145145
@Override
146146
public boolean hasMemory() {
147-
return true;
147+
return wmem_ != null;
148148
}
149149

150150
@Override
151151
public boolean isDirect() {
152-
return wmem_.isDirect();
152+
return hasMemory() ? wmem_.isDirect() : false;
153153
}
154154

155155
@Override
@@ -159,7 +159,7 @@ public boolean isEmpty() {
159159

160160
@Override
161161
public boolean isSameResource(final Memory that) {
162-
return wmem_.isSameResource(that);
162+
return hasMemory() ? wmem_.isSameResource(that) : false;
163163
}
164164

165165
@Override

src/main/java/org/apache/datasketches/theta/EmptyCompactSketch.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ public long getThetaLong() {
9191
return Long.MAX_VALUE;
9292
}
9393

94-
@Override
95-
public boolean hasMemory() {
96-
return false;
97-
}
98-
99-
@Override
100-
public boolean isDirect() {
101-
return false;
102-
}
103-
10494
@Override
10595
public boolean isEmpty() {
10696
return true;

src/main/java/org/apache/datasketches/theta/HeapCompactSketch.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,6 @@ public long getThetaLong() {
102102
return thetaLong_;
103103
}
104104

105-
@Override
106-
public boolean hasMemory() {
107-
return false;
108-
}
109-
110-
@Override
111-
public boolean isDirect() {
112-
return false;
113-
}
114-
115105
@Override
116106
public boolean isEmpty() {
117107
return empty_;

src/main/java/org/apache/datasketches/theta/HeapUpdateSketch.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ public int getCurrentBytes() {
6666
return (preLongs + dataLongs) << 3;
6767
}
6868

69-
@Override
70-
public boolean isDirect() {
71-
return false;
72-
}
73-
74-
@Override
75-
public boolean hasMemory() {
76-
return false;
77-
}
78-
7969
//UpdateSketch
8070

8171
@Override

src/main/java/org/apache/datasketches/theta/IntersectionImpl.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,24 @@ public CompactSketch getResult(final boolean dstOrdered, final WritableMemory ds
336336
dstMem, compactCache);
337337
}
338338

339+
@Override
340+
public boolean hasMemory() {
341+
return wmem_ != null;
342+
}
343+
339344
@Override
340345
public boolean hasResult() {
341-
return wmem_ != null ? wmem_.getInt(RETAINED_ENTRIES_INT) >= 0 : curCount_ >= 0;
346+
return hasMemory() ? wmem_.getInt(RETAINED_ENTRIES_INT) >= 0 : curCount_ >= 0;
347+
}
348+
349+
@Override
350+
public boolean isDirect() {
351+
return hasMemory() ? wmem_.isDirect() : false;
342352
}
343353

344354
@Override
345355
public boolean isSameResource(final Memory that) {
346-
return wmem_ != null ? wmem_.isSameResource(that) : false;
356+
return hasMemory() ? wmem_.isSameResource(that) : false;
347357
}
348358

349359
@Override

0 commit comments

Comments
 (0)