Skip to content

Commit b163bc8

Browse files
authored
Merge pull request #674 from apache/ffm_phase6
FFM Phase 6: KLL Sketches
2 parents bc9bfd7 + 7293060 commit b163bc8

68 files changed

Lines changed: 4338 additions & 3944 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 java.lang.foreign.MemorySegment;
23+
24+
/**
25+
* This is a callback interface to provide a means to request a new MemorySegment of a specified size.
26+
*
27+
* @author Lee Rhodes
28+
*/
29+
public interface MemorySegmentRequest {
30+
31+
/**
32+
* Request a new MemorySegment with the given <i>newByteSize</i>.
33+
* Because we do not have a reference to an Arena, the default here is to
34+
* allocate a new MemorySegment on the heap. It is up to the user to override this as appropriate.
35+
* @param prevSeg the previous MemorySegment to be possibly closed here or by using the separate
36+
* {@link #requestClose requestClose} method. This is included for convenience, it may be null.
37+
* @param newByteSize The new <i>byteSize</i> being requested.
38+
* @return new MemorySegment with the requested <i>byteSize</i>.
39+
*/
40+
default MemorySegment request(final MemorySegment prevSeg, final long newByteSize) {
41+
if (newByteSize > Integer.MAX_VALUE) {
42+
throw new SketchesArgumentException("Requested size in bytes exceeds Integer.MAX_VALUE.");
43+
}
44+
return MemorySegment.ofArray(new byte[(int)newByteSize]);
45+
}
46+
47+
/**
48+
* Request to close the given MemorySegment.
49+
* Because we do not have a reference to an Arena, the default here is to do nothing.
50+
* It is up to the user to override this as appropriate.
51+
* @param prevSeg the previous MemorySegment to be closed.
52+
*/
53+
default void requestClose(final MemorySegment prevSeg) { }
54+
55+
/**
56+
* This class implements the defaults
57+
*/
58+
public static class Default implements MemorySegmentRequest { }
59+
60+
/**
61+
* Create Default as static member.
62+
*/
63+
Default DEFAULT = new Default();
64+
65+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 java.lang.foreign.Arena;
23+
import java.lang.foreign.MemorySegment;
24+
import java.util.Enumeration;
25+
import java.util.Hashtable;
26+
27+
/**
28+
* This is just an example of a possible extension of the MemorySegmentRequest interface.
29+
* You may want to enable the println statements to track the state of the Hashtable.
30+
*/
31+
public final class MemorySegmentRequestExtension implements MemorySegmentRequest {
32+
private final Hashtable<MemorySegment, Arena> table = new Hashtable<>();
33+
34+
@Override
35+
public synchronized MemorySegment request(final MemorySegment prevSeg, final long newByteSize) {
36+
if (prevSeg.isNative()) {
37+
final Arena arena = Arena.ofConfined();
38+
final MemorySegment seg = arena.allocate(newByteSize);
39+
table.put(seg, arena); //println("Add");
40+
return seg;
41+
} else {
42+
if (newByteSize > Integer.MAX_VALUE) {
43+
throw new SketchesArgumentException("Requested byteSize is greater than Integer.MAX_VALUE.");
44+
}
45+
return MemorySegment.ofArray(new byte[(int)newByteSize]);
46+
}
47+
}
48+
49+
@Override
50+
public synchronized void requestClose(final MemorySegment prevSeg) {
51+
final Arena arena = table.get(prevSeg);
52+
if ((arena != null) && arena.scope().isAlive()) {
53+
arena.close();
54+
table.remove(prevSeg); //println("Remove");
55+
} //else ignore
56+
}
57+
58+
/**
59+
* This cleans up any unclosed off-heap MemorySegments.
60+
*/
61+
public synchronized void cleanup() {
62+
for (final Enumeration<Arena> e = table.elements(); e.hasMoreElements();) {
63+
final Arena arena = e.nextElement();
64+
if (arena.scope().isAlive()) {
65+
arena.close(); //println("Closed remaining Arenas in the Hashtable");
66+
}
67+
}
68+
}
69+
70+
// /**
71+
// * Println Object o
72+
// * @param o object to print
73+
// */
74+
// private static void println(final Object o) {
75+
// System.out.println(o.toString());
76+
// }
77+
78+
}

0 commit comments

Comments
 (0)