From f9e3b4d55b1bdcfdbc62ae94d61b6b2543a9808f Mon Sep 17 00:00:00 2001 From: Liviu Enache Date: Wed, 7 Jun 2023 17:52:35 +0300 Subject: [PATCH] Updated PagingIterator mechanism and tests Signed-off-by: Liviu Enache --- .../connectors/properties/PagingIterator.java | 73 ++++++++++--------- .../properties/SchemaAttributes.java | 68 ----------------- .../properties/TestSchemaAttributes.java | 32 ++++---- 3 files changed, 55 insertions(+), 118 deletions(-) diff --git a/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/PagingIterator.java b/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/PagingIterator.java index 3344708b8ca..f900b025733 100644 --- a/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/PagingIterator.java +++ b/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/PagingIterator.java @@ -170,7 +170,41 @@ public PagingIterator(PropertyIteratorBase iterator, @Override public boolean hasNext() { - return (cachedElementStart < totalElementCount); + if (cachedElementList == null) + { + return false; + } + + if (cachedElementPointer == cachedElementList.size()) + { + try + { + cachedElementList = iterator.getCachedList(cachedElementStart, maxCacheSize); + if (cachedElementList == null || cachedElementList.isEmpty()) + { + /* + * Prevents the pointer from being equal to the size of the list, signifying that the list is empty. + */ + cachedElementPointer = -1; + return false; + } + + cachedElementPointer = 0; + } + catch (PropertyServerException error) + { + /* + * Problem retrieving next cache. The exception includes a detailed error message, + */ + throw new OCFRuntimeException(OCFErrorCode.PROPERTIES_NOT_AVAILABLE.getMessageDefinition(error.getReportedErrorMessage(), + this.toString()), + this.getClass().getName(), + "next", + error); + } + } + + return true; } @@ -184,38 +218,9 @@ public boolean hasNext() @Override public ElementBase next() { - /* - * Check more elements available - */ if (this.hasNext()) { - ElementBase retrievedElement; - - /* - * If the pointer is at the end of the cache then retrieve more content from the property (metadata) - * server. - */ - if (cachedElementPointer == cachedElementList.size()) - { - try - { - cachedElementList = iterator.getCachedList(cachedElementStart, maxCacheSize); - cachedElementPointer = 0; - } - catch (PropertyServerException error) - { - /* - * Problem retrieving next cache. The exception includes a detailed error message, - */ - throw new OCFRuntimeException(OCFErrorCode.PROPERTIES_NOT_AVAILABLE.getMessageDefinition(error.getReportedErrorMessage(), - this.toString()), - this.getClass().getName(), - "next", - error); - } - } - - retrievedElement = iterator.cloneElement(cachedElementList.get(cachedElementPointer)); + ElementBase retrievedElement = iterator.cloneElement(cachedElementList.get(cachedElementPointer)); cachedElementPointer++; cachedElementStart++; @@ -232,9 +237,9 @@ public ElementBase next() /* * Throw runtime exception to show the caller they are not using the list correctly. */ - throw new OCFRuntimeException(OCFErrorCode.NO_MORE_ELEMENTS.getMessageDefinition(this.getClass().getSimpleName()), - this.getClass().getName(), - "next"); + throw new OCFRuntimeException(OCFErrorCode.NO_MORE_ELEMENTS.getMessageDefinition(this.getClass().getName()), + this.getClass().getName(), + "next"); } } diff --git a/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/SchemaAttributes.java b/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/SchemaAttributes.java index 5d1830db1a5..09712a51fc5 100644 --- a/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/SchemaAttributes.java +++ b/open-metadata-implementation/frameworks/open-connector-framework/src/main/java/org/odpi/openmetadata/frameworks/connectors/properties/SchemaAttributes.java @@ -31,74 +31,6 @@ public SchemaAttributes(int totalElementCount, int maxCacheSize) { super(totalElementCount, maxCacheSize); - // Anonymous class for temporary changing the behaviour of the iterator in the case of SchemaAttributes - // as to not use the "totalElementCount" parameter because its value is always 0 - super.pagingIterator = new PagingIterator(this, totalElementCount, maxCacheSize) - { - @Override - public ElementBase next() - { - if (this.hasNext()) - { - ElementBase retrievedElement = iterator.cloneElement(cachedElementList.get(cachedElementPointer)); - cachedElementPointer++; - cachedElementStart++; - - log.debug("Returning next element:"); - log.debug("==> totalElementCount: " + totalElementCount); - log.debug("==> cachedElementPointer: " + cachedElementPointer); - log.debug("==> cachedElementStart:" + cachedElementStart); - log.debug("==> maxCacheSize:" + maxCacheSize); - - return retrievedElement; - } - else - { - /* - * Throw runtime exception to show the caller they are not using the list correctly. - */ - throw new OCFRuntimeException(OCFErrorCode.NO_MORE_ELEMENTS.getMessageDefinition(this.getClass().getName()), - this.getClass().getName(), - "next"); - } - } - - @Override - public boolean hasNext() - { - if (cachedElementList == null) - { - return false; - } - - if (cachedElementPointer == cachedElementList.size()) - { - try - { - cachedElementList = iterator.getCachedList(cachedElementStart, maxCacheSize); - if (cachedElementList == null) - { - return false; - } - - cachedElementPointer = 0; - } - catch (PropertyServerException error) - { - /* - * Problem retrieving next cache. The exception includes a detailed error message, - */ - throw new OCFRuntimeException(OCFErrorCode.PROPERTIES_NOT_AVAILABLE.getMessageDefinition(error.getReportedErrorMessage(), - this.toString()), - this.getClass().getName(), - "next", - error); - } - } - - return true; - } - }; } diff --git a/open-metadata-implementation/frameworks/open-connector-framework/src/test/java/org/odpi/openmetadata/frameworks/connectors/properties/TestSchemaAttributes.java b/open-metadata-implementation/frameworks/open-connector-framework/src/test/java/org/odpi/openmetadata/frameworks/connectors/properties/TestSchemaAttributes.java index 012ed9fe755..3581c6155f6 100644 --- a/open-metadata-implementation/frameworks/open-connector-framework/src/test/java/org/odpi/openmetadata/frameworks/connectors/properties/TestSchemaAttributes.java +++ b/open-metadata-implementation/frameworks/open-connector-framework/src/test/java/org/odpi/openmetadata/frameworks/connectors/properties/TestSchemaAttributes.java @@ -118,23 +118,23 @@ private void validatePropertyIterator(int totalElementCount, int maxCache } - /** - * Validate that element count is set. + /* + * This test is no longer relevant for SchemaAttributes as in real scenarios the count is no longer calculated. */ - @Test public void testElementCount() - { - SchemaAttributes propertyIterator = getPropertyIterator(30, 10); - - assertTrue(propertyIterator.getElementCount() == 30); - - SchemaAttributes clonedPropertyIterator = new MockSchemaAttributes( propertyIterator); - - assertTrue(clonedPropertyIterator.getElementCount() == 30); - - clonedPropertyIterator = new MockSchemaAttributes( null); - - assertTrue(clonedPropertyIterator.getElementCount() == 0); - } +// @Test public void testElementCount() +// { +// SchemaAttributes propertyIterator = getPropertyIterator(30, 10); +// +// assertTrue(propertyIterator.getElementCount() == 30); +// +// SchemaAttributes clonedPropertyIterator = new MockSchemaAttributes( propertyIterator); +// +// assertTrue(clonedPropertyIterator.getElementCount() == 30); +// +// clonedPropertyIterator = new MockSchemaAttributes( null); +// +// assertTrue(clonedPropertyIterator.getElementCount() == 0); +// } /**