From 86374588a6103114b1f0f335a1b65da1ad0817a5 Mon Sep 17 00:00:00 2001 From: "ning.xi" Date: Thu, 26 Dec 2019 21:37:52 +0800 Subject: update Db provider to support query number Issue-ID: POLICY-1629 Signed-off-by: ning.xi Change-Id: I42c9542adc44d52b760a70e710ab4cc6fa8a1b3b --- .../main/java/org/onap/policy/models/dao/PfDao.java | 8 +++++--- .../org/onap/policy/models/dao/impl/DefaultPfDao.java | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'models-dao/src/main/java') diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java index 692aa08bf..062ec4662 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -162,11 +162,13 @@ public interface PfDao { * <= endTime. null for ignore start time. * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <= * endTime. null for ignore end time - * @filterMap Map store extra key/value used to filter from database, can be null. + * @param filterMap Map store extra key/value used to filter from database, can be null. + * @param sortOrder sortOrder to query database + * @param getRecordNum Total query count from database * @return the objects that was retrieved from the database */ List getFiltered(Class someClass, String name, String version, Date startTime, - Date endTime, Map filterMap); + Date endTime, Map filterMap, String sortOrder, int getRecordNum); /** * Get an object from the database, referred to by concept key. diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java index e98266706..b3d72a3fd 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -66,9 +66,11 @@ public class DefaultPfDao implements PfDao { private static final String WHERE = " WHERE "; private static final String AND = " AND "; + private static final String ORDER = " ORDER BY "; private static final String NAME_FILTER = "c.key.name = :name"; private static final String VERSION_FILTER = "c.key.version = :version"; + private static final String TIMESTAMP_FILTER = "c.key.timeStamp = :timeStamp"; private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime"; private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime"; private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; @@ -80,6 +82,9 @@ public class DefaultPfDao implements PfDao { private static final String DELETE_BY_CONCEPT_KEY = DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; + private static final String DELETE_BY_TIMESTAMP_KEY = + DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER + AND + TIMESTAMP_FILTER; + private static final String DELETE_BY_REFERENCE_KEY = DELETE_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER; @@ -221,7 +226,7 @@ public class DefaultPfDao implements PfDao { try { // @formatter:off mg.getTransaction().begin(); - mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass) + mg.createQuery(setQueryTable(DELETE_BY_TIMESTAMP_KEY, someClass), someClass) .setParameter(NAME, key.getName()) .setParameter(VERSION, key.getVersion()) .setParameter(TIMESTAMP, key.getTimeStamp()) @@ -347,7 +352,8 @@ public class DefaultPfDao implements PfDao { @Override public List getFiltered(final Class someClass, final String name, final String version, - final Date startTime, final Date endTime, final Map filterMap) { + final Date startTime, final Date endTime, final Map filterMap, final String sortOrder, + final int getRecordNum) { final EntityManager mg = getEntityManager(); String filterQueryString = SELECT_FROM_TABLE + WHERE; @@ -361,6 +367,9 @@ public class DefaultPfDao implements PfDao { filterQueryString = bld.toString(); } filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); + if (getRecordNum > 0) { + filterQueryString += ORDER + " c.key.timeStamp " + sortOrder; + } TypedQuery query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); if (filterMap != null) { @@ -383,6 +392,9 @@ public class DefaultPfDao implements PfDao { query.setParameter("endTime", endTime); } } + if (getRecordNum > 0) { + query.setMaxResults(getRecordNum); + } LOGGER.error("filterQueryString is \"{}\"", filterQueryString); return query.getResultList(); -- cgit 1.2.3-korg