20
20
21
21
import sleeper .core .range .Range ;
22
22
import sleeper .core .range .Region ;
23
+ import sleeper .core .schema .Schema ;
23
24
import sleeper .foreign .bridge .FFIArray ;
24
25
26
+ import java .util .List ;
25
27
import java .util .Objects ;
26
28
27
29
/**
@@ -46,50 +48,55 @@ public class FFISleeperRegion extends Struct {
46
48
public final FFIArray <java .lang .Boolean > maxs_inclusive = new FFIArray <>(this );
47
49
48
50
public FFISleeperRegion (jnr .ffi .Runtime runtime ) {
49
- this (runtime , null );
51
+ this (runtime , null , null );
50
52
}
51
53
52
54
/**
53
55
* Creates and validates Sleeper region into an FFI compatible form.
54
56
*
55
57
* @param runtime FFI runtime
58
+ * @param schema the table schema
56
59
* @param region the Sleeper partition region
57
60
* @throws IllegalStateException when the region data is invalid
58
61
*/
59
- public FFISleeperRegion (jnr .ffi .Runtime runtime , Region region ) {
62
+ public FFISleeperRegion (jnr .ffi .Runtime runtime , Schema schema , Region region ) {
60
63
super (runtime );
61
64
if (region != null ) {
62
- populateRegion (region );
65
+ populateRegion (schema , region );
63
66
}
64
67
}
65
68
66
69
/**
67
70
* Set the region data in this FFI object.
68
71
*
72
+ * @param schema the table schema
69
73
* @param region region data to copy into FFI object
70
74
* @throws NullPointerException if {@code region} is {@code null}
71
75
*/
72
76
@ SuppressWarnings (value = "checkstyle:avoidNestedBlocks" )
73
- public void populateRegion (Region region ) {
77
+ public void populateRegion (Schema schema , Region region ) {
78
+ Objects .requireNonNull (schema , "schema" );
74
79
Objects .requireNonNull (region , "region" );
80
+ List <java .lang .String > rowKeysOrdered = schema .getRowKeyFieldNames ();
81
+ List <Range > orderedRanges = rowKeysOrdered .stream ().map (rowKeyName -> region .getRange (rowKeyName )).toList ();
75
82
// Extra braces: Make sure wrong array isn't populated to wrong pointers
76
83
{
77
84
// This array can't contain nulls
78
- Object [] regionMins = region . getRanges () .stream ().map (Range ::getMin ).toArray ();
85
+ Object [] regionMins = orderedRanges .stream ().map (Range ::getMin ).toArray ();
79
86
this .mins .populate (regionMins , false );
80
87
}
81
88
{
82
- java .lang .Boolean [] regionMinInclusives = region . getRanges () .stream ().map (Range ::isMinInclusive )
89
+ java .lang .Boolean [] regionMinInclusives = orderedRanges .stream ().map (Range ::isMinInclusive )
83
90
.toArray (java .lang .Boolean []::new );
84
91
this .mins_inclusive .populate (regionMinInclusives , false );
85
92
}
86
93
{
87
94
// This array can contain nulls
88
- Object [] regionMaxs = region . getRanges () .stream ().map (Range ::getMax ).toArray ();
95
+ Object [] regionMaxs = orderedRanges .stream ().map (Range ::getMax ).toArray ();
89
96
this .maxs .populate (regionMaxs , true );
90
97
}
91
98
{
92
- java .lang .Boolean [] regionMaxInclusives = region . getRanges () .stream ().map (Range ::isMaxInclusive )
99
+ java .lang .Boolean [] regionMaxInclusives = orderedRanges .stream ().map (Range ::isMaxInclusive )
93
100
.toArray (java .lang .Boolean []::new );
94
101
this .maxs_inclusive .populate (regionMaxInclusives , false );
95
102
}
0 commit comments