Skip to content

Commit 9fd008f

Browse files
authored
Replace EntityListIterator.hasNext() with next()-based iteration for performance (#1163)
Improved: Updated iteration logic to avoid using EntityListIterator.hasNext(), which is inefficient due to JDBC ResultSet behavior. - Replaced hasNext() checks with next() != null pattern - Aligned with recommended usage in EntityListIterator JavaDoc - Prevented unnecessary ResultSet cursor operations and warning logs - Improved iteration performance and reduced overhead Example: while ((value = iterator.next()) != null) { ... }
1 parent 67056db commit 9fd008f

6 files changed

Lines changed: 9 additions & 18 deletions

File tree

applications/accounting/src/main/groovy/org/apache/ofbiz/accounting/reports/SalesInvoiceByProductCategorySummary.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ for (int currentDay = 0; currentDay <= daysInMonth; currentDay++) {
120120
productResultListIterator = select('productId', 'quantityTotal', 'amountTotal')
121121
.from('InvoiceItemProductSummary').where(productAndExprs).cursorScrollInsensitive().cache(true).queryIterator()
122122
productResultMap = [:]
123-
while (productResultListIterator.hasNext()) {
124-
productResult = productResultListIterator.next()
123+
while ((productResult = productResultListIterator.next()) != null) {
125124
productResultMap[productResult.productId] = productResult
126125
monthProductResult = UtilMisc.getMapFromMap(monthProductResultMap, productResult.productId)
127126
UtilMisc.addToBigDecimalInMap(monthProductResult, 'quantityTotal', productResult.getBigDecimal('quantityTotal'))
@@ -139,8 +138,7 @@ for (int currentDay = 0; currentDay <= daysInMonth; currentDay++) {
139138
categoryResultListIterator = select('productCategoryId', 'quantityTotal', 'amountTotal')
140139
.from('InvoiceItemCategorySummary').where(categoryAndExprs).cursorScrollInsensitive().cache(true).queryIterator()
141140
categoryResultMap = [:]
142-
while (categoryResultListIterator.hasNext()) {
143-
categoryResult = categoryResultListIterator.next()
141+
while ((categoryResult = categoryResultListIterator.next()) != null) {
144142
categoryResultMap[categoryResult.productCategoryId] = categoryResult
145143
monthCategoryResult = UtilMisc.getMapFromMap(monthCategoryResultMap, categoryResult.productCategoryId)
146144
UtilMisc.addToBigDecimalInMap(monthCategoryResult, 'quantityTotal', categoryResult.getBigDecimal('quantityTotal'))

applications/order/src/main/groovy/org/apache/ofbiz/order/entry/StorePaymentOptions.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ productStore = ProductStoreWorker.getProductStore(request)
2525
productStorePaymentMethodTypeIdMap = [:]
2626
productStorePaymentSettingList = productStore.getRelated('ProductStorePaymentSetting', null, null, true)
2727
productStorePaymentSettingIter = productStorePaymentSettingList.iterator()
28-
while (productStorePaymentSettingIter.hasNext()) {
29-
productStorePaymentSetting = productStorePaymentSettingIter.next()
28+
while ((productStorePaymentSetting = productStorePaymentSettingIter.next()) != null) {
3029
productStorePaymentMethodTypeIdMap.put(productStorePaymentSetting.get('paymentMethodTypeId'), true)
3130
}
3231
context.put('productStorePaymentMethodTypeIdMap', productStorePaymentMethodTypeIdMap)

applications/product/src/main/groovy/org/apache/ofbiz/product/catalog/FastLoadCache.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ messageList.add(ctimer.timerString('Before load all categories into cache'))
3131

3232
category = null
3333
long numCategories = 0
34-
while (categories.hasNext()) {
35-
category = (GenericValue) categories.next()
34+
while ((category = (GenericValue) categories.next()) != null) {
3635
delegator.putInPrimaryKeyCache(category.getPrimaryKey(), category)
3736
numCategories++
3837
}
@@ -50,8 +49,7 @@ products = from('Product').queryIterator()
5049
messageList.add(ptimer.timerString('Before load all products into cache'))
5150
product = null
5251
long numProducts = 0
53-
while (products.hasNext()) {
54-
product = (GenericValue) products.next()
52+
while ((product = (GenericValue) products.next()) != null) {
5553
delegator.putInPrimaryKeyCache(product.getPrimaryKey(), product)
5654
numProducts++
5755
}

applications/product/src/main/groovy/org/apache/ofbiz/product/facility/facility/CountFacilityInventoryByProduct.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ if (action) {
322322
// attempt to get the full size
323323
if (hasOffsetQOH || hasOffsetATP) {
324324
rowProcessed = 0
325-
while (prodsEli.hasNext()) {
326-
nextValue = prodsEli.next()
325+
while ((nextValue = prodsEli.next()) != null) {
327326
offsetQOHQtyAvailable = nextValue.getDouble('offsetQOHQtyAvailable')
328327
offsetATPQtyAvailable = nextValue.getDouble('offsetATPQtyAvailable')
329328
if (hasOffsetATP) {

applications/product/src/main/groovy/org/apache/ofbiz/product/facility/shipment/PackingSlip.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ previousShipmentIter = from('Shipment')
5959
EntityOperator.AND))
6060
.queryIterator()
6161

62-
while (previousShipmentIter.hasNext()) {
63-
previousShipmentItem = previousShipmentIter.next()
62+
while ((previousShipmentItem = previousShipmentIter.next()) != null) {
6463
if (previousShipmentItem.shipmentId != shipment.shipmentId) {
6564
previousShipmentItems = previousShipmentItem.getRelated('ShipmentItem', null, null, false)
6665
previousShipmentItems.each { shipmentItem ->

framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/entity/XmlDsDump.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ if (passedEntityNames) {
220220
}
221221

222222
curNumberWritten = 0
223-
while (values.hasNext()) {
224-
value = values.next()
223+
while ((value = values.next()) != null) {
225224
value.writeXmlText(writer, '')
226225
numberWritten++
227226
curNumberWritten++
@@ -277,8 +276,7 @@ if (passedEntityNames) {
277276
isFirst = true
278277
writer = null
279278
fileSplitNumber = 1
280-
while (values.hasNext()) {
281-
value = values.next()
279+
while ((value = values.next()) != null) {
282280
//Don't bother writing the file if there's nothing
283281
//to put into it
284282
if (isFirst) {

0 commit comments

Comments
 (0)