-
Notifications
You must be signed in to change notification settings - Fork 336
Storing LocalDate in off-heap IntBuffer
#14652
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
Merged
+154
−44
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8313f2e
Removing unnecessary debris in LongStorageTest and DataStorageTest
JaroslavTulach 645a68e
Arrow storage for Time_Of_Day
JaroslavTulach fbf272e
Generic ValidityBuffer renamed
JaroslavTulach d68f00d
Storing Date in off-heap IntBuffer
JaroslavTulach c9aacd8
Resolving merge conflicts
JaroslavTulach 1ae21bf
Limit the number of data to copy
JaroslavTulach 7626b71
Use _
JaroslavTulach 9e23b5e
Eliminating needless if statement
JaroslavTulach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,56 @@ | ||
| package org.enso.table.data.column.builder; | ||
|
|
||
| import java.lang.foreign.MemorySegment; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
| import java.nio.IntBuffer; | ||
| import java.time.LocalDate; | ||
| import java.util.BitSet; | ||
| import java.util.Objects; | ||
| import org.enso.table.data.column.storage.ColumnStorage; | ||
| import org.enso.table.data.column.storage.DateStorage; | ||
| import org.enso.table.data.column.storage.Storage; | ||
| import org.enso.table.data.column.storage.TypedStorage; | ||
| import org.enso.table.data.column.storage.type.DateTimeType; | ||
| import org.enso.table.data.column.storage.type.DateType; | ||
| import org.enso.table.data.column.storage.type.StorageType; | ||
| import org.enso.table.error.ValueTypeMismatchException; | ||
|
|
||
| /** A builder for LocalDate columns. */ | ||
| final class DateBuilder extends TypedBuilder<LocalDate> { | ||
| final class DateBuilder extends ValidityBuilder | ||
| implements BuilderForType<LocalDate>, BuilderWithRetyping { | ||
| private final boolean allowDateToDateTimeConversion; | ||
| private IntBuffer data; | ||
|
|
||
| DateBuilder(int size, boolean allowDateToDateTimeConversion) { | ||
| super(DateType.INSTANCE, new LocalDate[size]); | ||
| this(size, 0, 0, allowDateToDateTimeConversion); | ||
| } | ||
|
|
||
| private DateBuilder(int size, long data, long validity, boolean allowDateToDateTimeConversion) { | ||
| super(size, validity); | ||
| this.data = allocBuffer(size, data); | ||
| this.allowDateToDateTimeConversion = allowDateToDateTimeConversion; | ||
| } | ||
|
|
||
| static DateBuilder fromAddress(int size, long data, long validity) { | ||
| var validityBuffer = | ||
| MemorySegment.ofAddress(validity).reinterpret((size + 7) / 8).asByteBuffer(); | ||
| var bits = BitSet.valueOf(validityBuffer); | ||
| var buf = | ||
| MemorySegment.ofAddress(data) | ||
| .reinterpret(Integer.BYTES * size) | ||
| .asByteBuffer() | ||
| .order(ByteOrder.LITTLE_ENDIAN); | ||
|
|
||
| var b = new DateBuilder(size, false); | ||
| for (var i = 0; i < size; i++) { | ||
| var day = buf.getInt(); | ||
| if (bits.get(i)) { | ||
| b.append(LocalDate.ofEpochDay(day)); | ||
| } else { | ||
| b.appendNulls(1); | ||
| } | ||
| private static IntBuffer allocBuffer(int initialSize, long data) { | ||
| var wholeDataSize = Long.BYTES * initialSize; | ||
| ByteBuffer buf; | ||
| if (data == 0L) { | ||
| buf = ByteBuffer.allocateDirect(wholeDataSize).order(ByteOrder.LITTLE_ENDIAN); | ||
| } else { | ||
| var seg = MemorySegment.ofAddress(data).reinterpret(wholeDataSize); | ||
| buf = seg.asByteBuffer().order(ByteOrder.LITTLE_ENDIAN); | ||
| } | ||
| return b; | ||
| assert buf.capacity() == wholeDataSize; | ||
| assert buf.order() == ByteOrder.LITTLE_ENDIAN; | ||
| return buf.asIntBuffer(); | ||
| } | ||
|
|
||
| static DateBuilder fromAddress(int size, long data, long validity) { | ||
| return new DateBuilder(size, data, validity, false); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean accepts(Object o) { | ||
| return o instanceof LocalDate; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -51,7 +60,9 @@ public DateBuilder append(Object o) { | |
| appendNulls(1); | ||
| } else { | ||
| try { | ||
| data[currentSize++] = (LocalDate) o; | ||
| var local = (LocalDate) o; | ||
| this.setValid(currentSize); | ||
| data.put(currentSize++, Math.toIntExact(local.toEpochDay())); | ||
| } catch (ClassCastException e) { | ||
| throw new ValueTypeMismatchException(getType(), o); | ||
| } | ||
|
|
@@ -60,36 +71,81 @@ public DateBuilder append(Object o) { | |
| } | ||
|
|
||
| @Override | ||
| public boolean accepts(Object o) { | ||
| return o instanceof LocalDate; | ||
| public DateBuilder appendNulls(int count) { | ||
| doAppendNulls(count); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| protected ColumnStorage<LocalDate> doSeal() { | ||
| return seal(null, DateType.INSTANCE); | ||
| } | ||
|
|
||
| final Storage<LocalDate> seal(ColumnStorage<?> otherStorage, DateType type) { | ||
| return new TypedStorage<>(type, data, otherStorage); | ||
| public void appendBulkStorage(ColumnStorage<?> storage) { | ||
| var size = storage.getSize(); | ||
| for (var i = 0L; i < size; i++) { | ||
| var item = storage.getItemBoxed(i); | ||
| append(item); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canRetypeTo(StorageType<?> type) { | ||
| if (allowDateToDateTimeConversion && Objects.equals(type, DateTimeType.INSTANCE)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
JaroslavTulach marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return super.canRetypeTo(type); | ||
| } | ||
|
|
||
| @Override | ||
| public Builder retypeTo(StorageType<?> type) { | ||
| if (allowDateToDateTimeConversion && Objects.equals(type, DateTimeType.INSTANCE)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit but would invert the if and throw with the body not surrounded anymore
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| var res = new DateTimeBuilder(data.length, true); | ||
| var res = new DateTimeBuilder(data.capacity(), true); | ||
| for (int i = 0; i < currentSize; i++) { | ||
| res.append(data[i]); | ||
| res.append(getData(i)); | ||
| } | ||
| return res; | ||
| } else { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| return super.retypeTo(type); | ||
| } | ||
|
|
||
| @Override | ||
| protected int getDataSize() { | ||
| return this.data.capacity(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void resize(int desiredCapacity) { | ||
| var newData = allocBuffer(desiredCapacity, 0); | ||
| int toCopy = Math.min(currentSize, data.capacity()); | ||
| newData.put(0, this.data, 0, toCopy); | ||
| this.data = newData; | ||
| } | ||
|
|
||
| @Override | ||
| public ColumnStorage<LocalDate> seal() { | ||
| return seal(null); | ||
| } | ||
|
|
||
| final Storage<LocalDate> seal(ColumnStorage<?> otherStorage) { | ||
| ensureFreeSpaceFor(0); | ||
| var buf = data.asReadOnlyBuffer().position(0).limit(currentSize); | ||
| var validity = this.validityMap(); | ||
|
|
||
| return new DateStorage(buf, validity, otherStorage); | ||
| } | ||
|
|
||
| @Override | ||
| public StorageType<LocalDate> getType() { | ||
| return DateType.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public void copyDataTo(Object[] items) { | ||
| for (var i = 0; i < items.length && i < currentSize; i++) { | ||
| items[i] = getData(i); | ||
| } | ||
| } | ||
JaroslavTulach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final LocalDate getData(int i) { | ||
| return LocalDate.ofEpochDay(data.get(i)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
std-bits/table/src/main/java/org/enso/table/data/column/storage/DateStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package org.enso.table.data.column.storage; | ||
|
|
||
| import java.lang.foreign.MemorySegment; | ||
| import java.nio.IntBuffer; | ||
| import java.time.LocalDate; | ||
| import org.enso.table.data.column.storage.type.DateType; | ||
| import org.enso.table.util.ImmutableBitSet; | ||
|
|
||
| /** A column containing local dates */ | ||
| public final class DateStorage extends Storage<LocalDate> { | ||
|
|
||
| private final IntBuffer data; | ||
| private final ImmutableBitSet validityMap; | ||
|
|
||
| /** original proxy storage to keep from being garbage collected */ | ||
| private final ColumnStorage<?> proxy; | ||
|
|
||
| /** | ||
| * @param data the underlying data | ||
| * @param validityMap a bit set denoting at index {@code i} whether there is a real value at that | ||
| * index. | ||
| * @param otherStorage reference to proxy storage to prevent it from being GCed while this storage | ||
| * is used | ||
| */ | ||
| public DateStorage(IntBuffer data, ImmutableBitSet validityMap, ColumnStorage<?> otherStorage) { | ||
| super(DateType.INSTANCE); | ||
| this.data = data; | ||
| this.validityMap = validityMap; | ||
| this.proxy = otherStorage; | ||
| } | ||
|
|
||
| @Override | ||
| public long getSize() { | ||
| return data.limit(); | ||
| } | ||
|
|
||
| @Override | ||
| public LocalDate getItemBoxed(long index) { | ||
| var at = Math.toIntExact(index); | ||
| if (validityMap.get(at)) { | ||
| var local = data.get(at); | ||
| return LocalDate.ofEpochDay(local); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long addressOfData() { | ||
| return MemorySegment.ofBuffer(data).address(); | ||
| } | ||
|
|
||
| @Override | ||
| public long addressOfValidity() { | ||
| return MemorySegment.ofBuffer(validityMap.rawData()).address(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.