Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,13 @@ public T lockOneRandomRow(final SearchCriteria<T> sc, final boolean exclusive) {

@DB()
protected List<T> search(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache) {
if (_removed != null) {
if (sc == null) {
sc = createSearchCriteria();
}
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
}
sc = checkAndSetRemovedIsNull(sc);
return searchIncludingRemoved(sc, filter, lock, cache);
}

@DB()
protected List<T> search(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache, final boolean enableQueryCache) {
if (_removed != null) {
if (sc == null) {
sc = createSearchCriteria();
}
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
}
sc = checkAndSetRemovedIsNull(sc);
return searchIncludingRemoved(sc, filter, lock, cache, enableQueryCache);
}

Expand Down Expand Up @@ -519,7 +509,6 @@ public <M> List<M> customSearch(SearchCriteria<M> sc, final Filter filter) {
if (_removed != null) {
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use checkAndSetRemovedIsNull() here ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes it a bit difficult since it is of a different type M not T

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return customSearchIncludingRemoved(sc, filter);
}

Expand Down Expand Up @@ -911,26 +900,20 @@ protected T findOneIncludingRemovedBy(final SearchCriteria<T> sc) {

@Override
@DB()
public T findOneBy(final SearchCriteria<T> sc) {
if (_removed != null) {
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
}
public T findOneBy(SearchCriteria<T> sc) {
sc = checkAndSetRemovedIsNull(sc);
return findOneIncludingRemovedBy(sc);
}

@DB()
protected List<T> listBy(final SearchCriteria<T> sc, final Filter filter) {
if (_removed != null) {
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
}
protected List<T> listBy(SearchCriteria<T> sc, final Filter filter) {
sc = checkAndSetRemovedIsNull(sc);
return listIncludingRemovedBy(sc, filter);
}

@DB()
protected List<T> listBy(final SearchCriteria<T> sc, final Filter filter, final boolean enableQueryCache) {
if (_removed != null) {
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
}
protected List<T> listBy(SearchCriteria<T> sc, final Filter filter, final boolean enableQueryCache) {
sc = checkAndSetRemovedIsNull(sc);
return listIncludingRemovedBy(sc, filter, enableQueryCache);
}

Expand Down Expand Up @@ -1329,7 +1312,7 @@ public List<T> search(final SearchCriteria<T> sc, final Filter filter) {
@DB()
public Pair<List<T>, Integer> searchAndCount(final SearchCriteria<T> sc, final Filter filter) {
List<T> objects = search(sc, filter, null, false);
Integer count = getCount(sc);
Integer count = getCount(sc, false);
// Count cannot be less than the result set but can be higher due to pagination, see CLOUDSTACK-10320
if (count < objects.size()) {
count = objects.size();
Expand All @@ -1341,7 +1324,7 @@ public Pair<List<T>, Integer> searchAndCount(final SearchCriteria<T> sc, final F
@DB()
public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, final Filter filter) {
List<T> objects = search(sc, filter, null, false);
Integer count = getDistinctCount(sc);
Integer count = getDistinctCount(sc, false);
// Count cannot be 0 if there is at least a result in the list, see CLOUDSTACK-10320
if (count == 0 && !objects.isEmpty()) {
// Cannot assume if it's more than one since the count is distinct vs search
Expand All @@ -1354,7 +1337,7 @@ public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc,
@DB()
public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, final Filter filter, final String[] distinctColumns) {
List<T> objects = search(sc, filter, null, false);
Integer count = getDistinctCount(sc, distinctColumns);
Integer count = getDistinctCount(sc, distinctColumns, false);
// Count cannot be 0 if there is at least a result in the list, see CLOUDSTACK-10320
if (count == 0 && !objects.isEmpty()) {
// Cannot assume if it's more than one since the count is distinct vs search
Expand Down Expand Up @@ -1935,6 +1918,23 @@ public SearchCriteria<T> createSearchCriteria() {
return builder.create();
}

private SearchCriteria<T> checkAndSetRemovedIsNull(SearchCriteria<T> sc) {
if (_removed != null) {
if (sc == null) {
sc = createSearchCriteria();
}
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
}
return sc;
}

public Integer getDistinctCount(SearchCriteria<T> sc, boolean removed) {
if (!removed) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed & _removed seems to be redundant here & wherever used in similar way, can you directly set / unset _removed as per its usage ? If the purpose is to include / exclude removed records, you can use methods with naming *IncludingRemovedBy() [for ex: getDistinctCountIncludingRemovedBy()] to be in sync with the other methods.

@davidjumani davidjumani Oct 6, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now getDistinctCount returns including removed so changing it would require changing where it's been referenced, would it be better to create getDistinctCountExcludingRemoved ?
Edit : Nevermind. Isn't used much elsewhere. Will make the changes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDistinctCountExcludingRemoved is not in line with other methods, so I do not recommend.

@sureshanaparti sureshanaparti Oct 6, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default method should not include removed records, explicitly mentioned methods "*IncludingRemovedBy()" should. so, may be you can change current method to "*IncludingRemovedBy()" and add other method with current name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added! Thanks @sureshanaparti

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, changes LGTM.

sc = checkAndSetRemovedIsNull(sc);
}
return getDistinctCount(sc);
}

public Integer getDistinctCount(SearchCriteria<T> sc) {
String clause = sc != null ? sc.getWhereClause() : null;
if (clause != null && clause.length() == 0) {
Expand Down Expand Up @@ -1993,6 +1993,13 @@ public Integer getDistinctCount(SearchCriteria<T> sc) {
}
}

public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns, boolean removed) {
if (!removed) {
sc = checkAndSetRemovedIsNull(sc);
}
return getDistinctCount(sc, distinctColumns);
}

public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns) {
String clause = sc != null ? sc.getWhereClause() : null;
if (Strings.isNullOrEmpty(clause)) {
Expand Down Expand Up @@ -2041,9 +2048,13 @@ public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns)

public Integer countAll() {
SearchCriteria<T> sc = null;
if (_removed != null) {
sc = createSearchCriteria();
sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL);
sc = checkAndSetRemovedIsNull(sc);
return getCount(sc);
}

public Integer getCount(SearchCriteria<T> sc, boolean removed) {
if (!removed) {
sc = checkAndSetRemovedIsNull(sc);
}
return getCount(sc);
}
Expand Down