Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve modularization #852

Merged
merged 19 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions baremaps-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ limitations under the License.
<groupId>net.ripe.ipresource</groupId>
<artifactId>ipresource</artifactId>
</dependency>
<dependency>
<groupId>org.apache.baremaps</groupId>
<artifactId>baremaps-openstreetmap</artifactId>
</dependency>
<dependency>
<groupId>org.apache.baremaps</groupId>
<artifactId>baremaps-testing</artifactId>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class BinarySearch {
* @param <E> the type of the elements in the list
*/
public static <E> Long binarySearch(DataList<E> list, E value, Comparator<E> comparator) {
return binarySearch(list, value, comparator, 0, list.sizeAsLong() - 1l);
return binarySearch(list, value, comparator, 0, list.size() - 1l);
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public static <E, A> E binarySearchAttribute(
A value,
Comparator<A> comparator) {
long lo = 0;
long hi = list.sizeAsLong() - 1l;
long hi = list.size() - 1l;
while (lo <= hi) {
long mi = (lo + hi) >>> 1;
E e = list.get(mi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static <T> long mergeSortedBatches(
new PriorityQueue<>(batches.size(), (i, j) -> comparator.compare(i.peek(), j.peek()));

for (DataList<T> input : batches) {
if (input.sizeAsLong() == 0) {
if (input.size() == 0) {
continue;
}
DataStack stack = new DataStack(input);
Expand Down Expand Up @@ -221,7 +221,7 @@ public DataStack(DataList<T> list) {
}

public boolean empty() {
return this.index > list.sizeAsLong();
return this.index > list.size();
}

public T peek() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package org.apache.baremaps.database.algorithm;

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.apache.baremaps.database.collection.DataList;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.TopologyException;
import org.locationtech.jts.index.hprtree.HPRtree;
Expand All @@ -35,14 +36,14 @@ public class UnionStream {

private static final Logger logger = LoggerFactory.getLogger(UnionStream.class);

private final List<Geometry> list;
private final DataList<Geometry> list;

/**
* Creates a new union stream.
*
* @param list the list of geometries to union.
*/
public UnionStream(List<Geometry> list) {
public UnionStream(DataList<Geometry> list) {
this.list = list;
}

Expand All @@ -63,7 +64,7 @@ public Stream<Geometry> union() {
tree.build();

// Create a stream of geometry unions that are spatially connected
var stream = IntStream.range(0, list.size()).mapToObj(i -> {
var stream = LongStream.range(0, list.size()).mapToObj(i -> {
// Skip the geometries that have already been visited
if (visited.contains(i)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.baremaps.database.calcite;

import java.util.Collection;
import org.apache.baremaps.database.collection.DataCollectionAdapter;
import org.apache.baremaps.database.collection.DataCollection;
import org.apache.baremaps.database.collection.DataCollectionMapper;
import org.apache.baremaps.database.schema.DataColumn;
import org.apache.baremaps.database.schema.DataTable;
import org.apache.calcite.DataContext;
Expand All @@ -44,8 +44,8 @@ public SqlDataTable(DataTable table) {

@Override
public Enumerable<Object[]> scan(final DataContext root) {
Collection<Object[]> collection =
new DataCollectionAdapter<>(table, row -> row.values().toArray());
DataCollection<Object[]> collection =
new DataCollectionMapper<>(table, row -> row.values().toArray());
return Linq4j.asEnumerable(collection);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,38 @@
import org.apache.baremaps.database.type.DataType;

/**
* A buffer of elements backed by a {@link DataType} and a {@link Memory}. Elements are appended to
* the buffer and can be accessed by their position in the {@link Memory}. Appending elements to the
* buffer is thread-safe.
* A log of records backed by a {@link DataType} and a {@link Memory}. Elements are appended to the
* log and can be accessed by their position in the {@link Memory}. Appending elements to the log is
* thread-safe.
*
* @param <E> The type of the data.
*/
public class AppendOnlyBuffer<E> extends AbstractDataCollection<E> {
public class AppendOnlyLog<E> implements DataCollection<E> {

private final DataType<E> dataType;
private final Memory memory;
private final Memory<?> memory;
private final long segmentSize;
private long offset;
private long size;

private Lock lock = new ReentrantLock();

/**
* Constructs an append only buffer.
* Constructs an {@link AppendOnlyLog}.
*
* @param dataType the data type
*/
public AppendOnlyBuffer(DataType<E> dataType) {
public AppendOnlyLog(DataType<E> dataType) {
this(dataType, new OffHeapMemory());
}

/**
* Constructs an append only buffer.
* Constructs an append only log.
*
* @param dataType the data type
* @param memory the memory
*/
public AppendOnlyBuffer(DataType<E> dataType, Memory memory) {
public AppendOnlyLog(DataType<E> dataType, Memory<?> memory) {
this.dataType = dataType;
this.memory = memory;
this.segmentSize = memory.segmentSize();
Expand All @@ -70,7 +70,7 @@
}

/**
* Appends the value to the buffer and returns its position in the memory.
* Appends the value to the log and returns its position in the memory.
*
* @param value the value
* @return the position of the value in the memory.
Expand Down Expand Up @@ -101,39 +101,32 @@
return position;
}

/**
* {@inheritDoc}
*/
@Override
public boolean add(E value) {
addPositioned(value);
return true;
}

/**
* Returns a values at the specified position in the memory.
*
* @param position the position of the value
* @return the value
*/
public E read(long position) {
public E getPositioned(long position) {
long segmentIndex = position / segmentSize;
long segmentOffset = position % segmentSize;
ByteBuffer buffer = memory.segment((int) segmentIndex);
return dataType.read(buffer, (int) segmentOffset);
}

/** {@inheritDoc} */
public long sizeAsLong() {
return size;
@Override
public boolean add(E e) {
addPositioned(e);
return true;
}

public void close() {
memory.segment(0).putLong(0, size);
/** {@inheritDoc} */
public long size() {
Fixed Show fixed Hide fixed
return size;
}

/** {@inheritDoc} */
@Override
public void clear() {
try {
memory.clear();
Expand All @@ -143,22 +136,30 @@
}

/**
* {@inheritDoc}
* Returns an iterator over the values of the log, starting at the beginning of the log. The
* iterator allows to get the current position in the memory.
*
* @return an iterator over the values of the log
*/
@Override
public BufferIterator iterator() {
final long size = sizeAsLong();
return new BufferIterator(size);
public AppendOnlyLogIterator iterator() {
final long size = size();
return new AppendOnlyLogIterator(size);
}

public class BufferIterator implements Iterator<E> {
/**
* An iterator over the values of the log that can be used to iterate over the values of the log
* and to get the current position in the memory.
*/
public class AppendOnlyLogIterator implements Iterator<E> {

private final long size;

private long index;

private long position;

public BufferIterator(long size) {
private AppendOnlyLogIterator(long size) {
this.size = size;
index = 0;
position = Long.BYTES;
Expand Down Expand Up @@ -202,5 +203,6 @@
public long getPosition() {
return position;
}

}
}
Loading
Loading