|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.host.dao; |
| 18 | + |
| 19 | +import java.util.Date; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.annotation.PostConstruct; |
| 23 | + |
| 24 | +import org.apache.logging.log4j.LogManager; |
| 25 | +import org.apache.logging.log4j.Logger; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | + |
| 28 | +import com.cloud.host.HostStatsVO; |
| 29 | +import com.cloud.utils.db.GenericDaoBase; |
| 30 | +import com.cloud.utils.db.SearchBuilder; |
| 31 | +import com.cloud.utils.db.SearchCriteria; |
| 32 | +import com.cloud.utils.db.SearchCriteria.Op; |
| 33 | + |
| 34 | +/** DAO for the host_stats table. */ |
| 35 | +@Component |
| 36 | +public class HostStatsDaoImpl extends GenericDaoBase<HostStatsVO, Long> implements HostStatsDao { |
| 37 | + |
| 38 | + protected Logger logger = LogManager.getLogger(getClass()); |
| 39 | + |
| 40 | + protected SearchBuilder<HostStatsVO> hostIdSearch; |
| 41 | + protected SearchBuilder<HostStatsVO> hostIdTimestampGreaterThanEqualSearch; |
| 42 | + protected SearchBuilder<HostStatsVO> hostIdTimestampLessThanEqualSearch; |
| 43 | + protected SearchBuilder<HostStatsVO> hostIdTimestampBetweenSearch; |
| 44 | + protected SearchBuilder<HostStatsVO> timestampSearch; |
| 45 | + |
| 46 | + @PostConstruct |
| 47 | + protected void init() { |
| 48 | + hostIdSearch = createSearchBuilder(); |
| 49 | + hostIdSearch.and("hostId", hostIdSearch.entity().getHostId(), Op.EQ); |
| 50 | + hostIdSearch.done(); |
| 51 | + |
| 52 | + hostIdTimestampGreaterThanEqualSearch = createSearchBuilder(); |
| 53 | + hostIdTimestampGreaterThanEqualSearch.and("hostId", hostIdTimestampGreaterThanEqualSearch.entity().getHostId(), Op.EQ); |
| 54 | + hostIdTimestampGreaterThanEqualSearch.and("timestamp", hostIdTimestampGreaterThanEqualSearch.entity().getTimestamp(), Op.GTEQ); |
| 55 | + hostIdTimestampGreaterThanEqualSearch.done(); |
| 56 | + |
| 57 | + hostIdTimestampLessThanEqualSearch = createSearchBuilder(); |
| 58 | + hostIdTimestampLessThanEqualSearch.and("hostId", hostIdTimestampLessThanEqualSearch.entity().getHostId(), Op.EQ); |
| 59 | + hostIdTimestampLessThanEqualSearch.and("timestamp", hostIdTimestampLessThanEqualSearch.entity().getTimestamp(), Op.LTEQ); |
| 60 | + hostIdTimestampLessThanEqualSearch.done(); |
| 61 | + |
| 62 | + hostIdTimestampBetweenSearch = createSearchBuilder(); |
| 63 | + hostIdTimestampBetweenSearch.and("hostId", hostIdTimestampBetweenSearch.entity().getHostId(), Op.EQ); |
| 64 | + hostIdTimestampBetweenSearch.and("timestamp", hostIdTimestampBetweenSearch.entity().getTimestamp(), Op.BETWEEN); |
| 65 | + hostIdTimestampBetweenSearch.done(); |
| 66 | + |
| 67 | + timestampSearch = createSearchBuilder(); |
| 68 | + timestampSearch.and("timestamp", timestampSearch.entity().getTimestamp(), Op.LT); |
| 69 | + timestampSearch.done(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public List<HostStatsVO> findByHostId(long hostId) { |
| 74 | + SearchCriteria<HostStatsVO> sc = hostIdSearch.create(); |
| 75 | + sc.setParameters("hostId", hostId); |
| 76 | + return listBy(sc); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public List<HostStatsVO> findByHostIdAndTimestampGreaterThanEqual(long hostId, Date time) { |
| 81 | + SearchCriteria<HostStatsVO> sc = hostIdTimestampGreaterThanEqualSearch.create(); |
| 82 | + sc.setParameters("hostId", hostId); |
| 83 | + sc.setParameters("timestamp", time); |
| 84 | + return listBy(sc); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public List<HostStatsVO> findByHostIdAndTimestampLessThanEqual(long hostId, Date time) { |
| 89 | + SearchCriteria<HostStatsVO> sc = hostIdTimestampLessThanEqualSearch.create(); |
| 90 | + sc.setParameters("hostId", hostId); |
| 91 | + sc.setParameters("timestamp", time); |
| 92 | + return listBy(sc); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public List<HostStatsVO> findByHostIdAndTimestampBetween(long hostId, Date startTime, Date endTime) { |
| 97 | + SearchCriteria<HostStatsVO> sc = hostIdTimestampBetweenSearch.create(); |
| 98 | + sc.setParameters("hostId", hostId); |
| 99 | + sc.setParameters("timestamp", startTime, endTime); |
| 100 | + return listBy(sc); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void removeAllByTimestampLessThan(Date limitDate, long limitPerQuery) { |
| 105 | + SearchCriteria<HostStatsVO> sc = timestampSearch.create(); |
| 106 | + sc.setParameters("timestamp", limitDate); |
| 107 | + |
| 108 | + logger.debug(String.format("Starting to remove all host_stats rows older than [%s].", limitDate)); |
| 109 | + |
| 110 | + long totalRemoved = batchExpunge(sc, limitPerQuery); |
| 111 | + |
| 112 | + logger.info(String.format("Removed a total of [%s] host_stats rows older than [%s].", totalRemoved, limitDate)); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments