001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.log.chronicle;
020
021import static net.openhft.chronicle.queue.RollCycles.DAILY;
022import static net.openhft.chronicle.queue.RollCycles.HOURLY;
023import static net.openhft.chronicle.queue.RollCycles.MINUTELY;
024import static net.openhft.chronicle.queue.RollCycles.TEST_SECONDLY;
025
026import net.openhft.chronicle.queue.RollCycle;
027
028/**
029 * @since 9.3
030 */
031public class ChronicleRetentionDuration {
032
033    protected static final String SECOND_ROLLING_PERIOD = "s";
034
035    protected static final String MINUTE_ROLLING_PERIOD = "m";
036
037    protected static final String HOUR_ROLLING_PERIOD = "h";
038
039    protected static final String DAY_ROLLING_PERIOD = "d";
040
041    protected final RollCycle rollCycle;
042
043    protected final int retentionCycles;
044
045    protected final String retention;
046
047    public static final ChronicleRetentionDuration NONE = new ChronicleRetentionDuration("0d");
048
049    public ChronicleRetentionDuration(String retention) {
050        this.retention = decodeRetention(retention);
051        this.rollCycle = decodeRollCycle(this.retention);
052        this.retentionCycles = decodeRetentionCycles(this.retention);
053    }
054
055    protected String decodeRetention(String retention) {
056        if (retention == null || retention.isEmpty()) {
057            return "0d";
058        }
059        return retention;
060    }
061
062    @Override
063    public String toString() {
064        return retention;
065    }
066
067    public boolean disable() {
068        return retentionCycles <= 0;
069    }
070
071    public RollCycle getRollCycle() {
072        return rollCycle;
073    }
074
075    @SuppressWarnings("unused")
076    public String getRetention() {
077        return retention;
078    }
079
080    public int getRetentionCycles() {
081        return retentionCycles;
082    }
083
084    protected RollCycle decodeRollCycle(String retentionDuration) {
085        if (retentionDuration == null || retentionDuration.isEmpty()) {
086            return DAILY;
087        }
088        String rollingPeriod = retentionDuration.substring(retentionDuration.length() - 1);
089        switch (rollingPeriod) {
090        case SECOND_ROLLING_PERIOD:
091            return TEST_SECONDLY;
092        case MINUTE_ROLLING_PERIOD:
093            return MINUTELY;
094        case HOUR_ROLLING_PERIOD:
095            return HOURLY;
096        case DAY_ROLLING_PERIOD:
097            return DAILY;
098        default:
099            throw new IllegalArgumentException("Unknown rolling period: " + rollingPeriod);
100        }
101    }
102
103    protected int decodeRetentionCycles(String retentionDuration) {
104        if (retentionDuration != null) {
105            return Integer.parseInt(retentionDuration.substring(0, retentionDuration.length() - 1));
106        }
107        return 0;
108    }
109
110    protected static String encodeRollCycle(RollCycle rollCycle) {
111        if (rollCycle.equals(TEST_SECONDLY)) {
112            return SECOND_ROLLING_PERIOD;
113        }
114        if (rollCycle.equals(MINUTELY)) {
115            return MINUTE_ROLLING_PERIOD;
116        }
117        if (rollCycle.equals(HOURLY)) {
118            return HOUR_ROLLING_PERIOD;
119        }
120        if (rollCycle.equals(DAILY)) {
121            return DAY_ROLLING_PERIOD;
122        }
123        throw new IllegalArgumentException("Unknown rolling cycle: " + rollCycle);
124    }
125
126    public static ChronicleRetentionDuration disableOf(ChronicleRetentionDuration retention) {
127        return new ChronicleRetentionDuration("0" + encodeRollCycle(retention.getRollCycle()));
128    }
129}