1414import java .util .HashMap ;
1515import java .util .IdentityHashMap ;
1616import java .util .Map ;
17+ import java .util .function .BiConsumer ;
1718
19+ import org .eclipse .rdf4j .model .Value ;
1820import org .eclipse .rdf4j .query .BindingSet ;
1921import org .eclipse .rdf4j .query .MutableBindingSet ;
2022import org .eclipse .rdf4j .query .algebra .evaluation .impl .QueryEvaluationContext ;
@@ -32,6 +34,8 @@ public final class LmdbLftjBindingState {
3234 private final long [] assignedValues ;
3335 private final boolean [] assignedPresent ;
3436 private final IdentityHashMap <LmdbLftjPatternPlan .TermRef , Long > constantIds = new IdentityHashMap <>();
37+ private BiConsumer <Value , MutableBindingSet >[] bindingSetters ;
38+ private QueryEvaluationContext bindingSettersContext ;
3539
3640 private TxnManager .Txn txn ;
3741
@@ -145,10 +149,10 @@ long fixedId(LmdbLftjPatternPlan.TermRef term) {
145149
146150 BindingSet materialize (QueryEvaluationContext context ) {
147151 MutableBindingSet result = context .createBindingSet (inputBindings );
152+ BiConsumer <Value , MutableBindingSet >[] setters = bindingSetters (context );
148153 for (int slot = 0 ; slot < variableNames .length ; slot ++) {
149- String variableName = variableNames [slot ];
150- if (!result .hasBinding (variableName ) && isBound (slot )) {
151- context .setBinding (variableName ).accept (queryAccess .resolveValue (value (slot )), result );
154+ if (assignedPresent [slot ]) {
155+ setters [slot ].accept (queryAccess .resolveValue (assignedValues [slot ]), result );
152156 }
153157 }
154158 return result ;
@@ -169,6 +173,20 @@ void close() {
169173 }
170174 }
171175
176+ @ SuppressWarnings ("unchecked" )
177+ private BiConsumer <Value , MutableBindingSet >[] bindingSetters (QueryEvaluationContext context ) {
178+ if (bindingSetters != null && bindingSettersContext == context ) {
179+ return bindingSetters ;
180+ }
181+ BiConsumer <Value , MutableBindingSet >[] setters = new BiConsumer [variableNames .length ];
182+ for (int slot = 0 ; slot < variableNames .length ; slot ++) {
183+ setters [slot ] = context .setBinding (variableNames [slot ]);
184+ }
185+ bindingSetters = setters ;
186+ bindingSettersContext = context ;
187+ return setters ;
188+ }
189+
172190 private int slot (String variableName ) {
173191 Integer slot = variableSlots .get (variableName );
174192 if (slot == null ) {
0 commit comments