Skip to content

Commit 70ada6e

Browse files
committed
Add fillWith() to FloatArray
1 parent cd6e672 commit 70ada6e

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

server/src/main/java/org/elasticsearch/common/util/BigArrays.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,19 @@ public void set(long index, byte[] buf, int offset, int len) {
426426
assert index >= 0 && index < size();
427427
System.arraycopy(buf, offset << 2, array, (int) index << 2, len << 2);
428428
}
429+
430+
@Override
431+
public void fillWith(StreamInput in) throws IOException {
432+
int numBytes = in.readVInt();
433+
in.readBytes(array, 0, numBytes);
434+
}
435+
436+
@Override
437+
public void writeTo(StreamOutput out) throws IOException {
438+
int size = (int) size();
439+
out.writeVInt(size * 8);
440+
out.write(array, 0, size * Double.BYTES);
441+
}
429442
}
430443

431444
private static class ObjectArrayWrapper<T> extends AbstractArrayWrapper implements ObjectArray<T> {

server/src/main/java/org/elasticsearch/common/util/BigFloatArray.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99

1010
package org.elasticsearch.common.util;
1111

12+
import org.elasticsearch.common.io.stream.StreamInput;
13+
import org.elasticsearch.common.io.stream.StreamOutput;
14+
15+
import java.io.IOException;
1216
import java.lang.invoke.MethodHandles;
1317
import java.lang.invoke.VarHandle;
1418
import java.nio.ByteOrder;
1519

20+
import static org.elasticsearch.common.util.BigLongArray.writePages;
1621
import static org.elasticsearch.common.util.PageCacheRecycler.FLOAT_PAGE_SIZE;
1722

1823
/**
@@ -94,4 +99,14 @@ private static int pageIdx(long index) {
9499
private static int idxInPage(long index) {
95100
return (int) (index & FLOAT_PAGE_SIZE - 1);
96101
}
102+
103+
@Override
104+
public void fillWith(StreamInput in) throws IOException {
105+
readPages(in);
106+
}
107+
108+
@Override
109+
public void writeTo(StreamOutput out) throws IOException {
110+
writePages(out, size, pages, Float.BYTES);
111+
}
97112
}

server/src/main/java/org/elasticsearch/common/util/FloatArray.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99

1010
package org.elasticsearch.common.util;
1111

12+
import org.elasticsearch.common.io.stream.StreamInput;
13+
import org.elasticsearch.common.io.stream.Writeable;
14+
15+
import java.io.IOException;
16+
1217
/**
1318
* Abstraction of an array of double values.
1419
*/
15-
public interface FloatArray extends BigArray {
16-
20+
public interface FloatArray extends BigArray, Writeable {
1721
/**
1822
* Get an element given its index.
1923
*/
2024
float get(long index);
2125

2226
/**
23-
* Set a value at the given index.
27+
* Set a value at the given index
2428
*/
2529
void set(long index, float value);
2630

@@ -33,4 +37,6 @@ public interface FloatArray extends BigArray {
3337
* Bulk set.
3438
*/
3539
void set(long index, byte[] buf, int offset, int len);
40+
41+
void fillWith(StreamInput in) throws IOException;
3642
}

server/src/main/java/org/elasticsearch/index/mapper/BlockLoader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ interface IntBuilder extends Builder {
554554
IntBuilder appendInt(int value);
555555
}
556556

557+
interface DenseVectorBuilder extends Builder {
558+
/**
559+
* Appends a double to the current entry.
560+
*/
561+
DenseVectorBuilder appendDenseVector(float[] value);
562+
}
563+
557564
/**
558565
* Specialized builder for collecting dense arrays of long values.
559566
*/

test/framework/src/main/java/org/elasticsearch/common/util/MockBigArrays.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ public void fill(long fromIndex, long toIndex, float value) {
604604
in.fill(fromIndex, toIndex, value);
605605
}
606606

607+
@Override
608+
public void fillWith(StreamInput streamInput) throws IOException {
609+
in.fillWith(streamInput);
610+
}
611+
607612
@Override
608613
public void set(long index, byte[] buf, int offset, int len) {
609614
in.set(index, buf, offset, len);
@@ -613,6 +618,11 @@ public void set(long index, byte[] buf, int offset, int len) {
613618
public Collection<Accountable> getChildResources() {
614619
return Collections.singleton(Accountables.namedAccountable("delegate", in));
615620
}
621+
622+
@Override
623+
public void writeTo(StreamOutput out) throws IOException {
624+
in.writeTo(out);
625+
}
616626
}
617627

618628
private class DoubleArrayWrapper extends AbstractArrayWrapper implements DoubleArray {

0 commit comments

Comments
 (0)