001/*
002 * (C) Copyright 2015-2019 Nuxeo (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 *     Tiry
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.transientstore.api;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.common.xmap.XMap;
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeMap;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.runtime.model.Descriptor;
030
031/**
032 * {@link XMap} descriptor for representing the Configuration of a {@link TransientStore}
033 *
034 * @since 7.2
035 */
036@XObject("store")
037public class TransientStoreConfig implements Descriptor {
038
039    public static final int DEFAULT_TARGET_MAX_SIZE_MB = -1;
040
041    public static final int DEFAULT_ABSOLUTE_MAX_SIZE_MB = -1;
042
043    public static final int DEFAULT_FIRST_LEVEL_TTL = 60 * 2;
044
045    public static final int DEFAULT_SECOND_LEVEL_TTL = 10;
046
047    @XNode("@name")
048    public String name;
049
050    @XNode("@path")
051    protected String path;
052
053    // target size that ideally should never be exceeded
054    @XNode("targetMaxSizeMB")
055    protected Integer targetMaxSizeMB;
056
057    // size that must never be exceeded
058    @XNode("absoluteMaxSizeMB")
059    protected Integer absoluteMaxSizeMB;
060
061    @XNode("firstLevelTTL")
062    protected Integer firstLevelTTL;
063
064    @XNode("secondLevelTTL")
065    protected Integer secondLevelTTL;
066
067    @XNode("@class")
068    public Class<? extends TransientStoreProvider> implClass;
069
070    @XNodeMap(value = "property", key = "@name", type = HashMap.class, componentType = String.class, nullByDefault = true)
071    protected Map<String, String> properties  = new HashMap<>();
072
073    public TransientStoreConfig() {
074    }
075
076    public TransientStoreConfig(String name) {
077        this.name = name;
078    }
079
080    /**
081     * Copy constructor.
082     *
083     * @since 10.10
084     */
085    public TransientStoreConfig(TransientStoreConfig other) {
086        name = other.name;
087        path = other.path;
088        targetMaxSizeMB = other.targetMaxSizeMB;
089        absoluteMaxSizeMB = other.absoluteMaxSizeMB;
090        firstLevelTTL = other.firstLevelTTL;
091        secondLevelTTL = other.secondLevelTTL;
092        implClass = other.implClass;
093        properties.putAll(other.properties);
094    }
095
096    @Override
097    public TransientStoreConfig merge(Descriptor o) {
098        TransientStoreConfig other = (TransientStoreConfig) o;
099        TransientStoreConfig merged = new TransientStoreConfig();
100        merged.name = other.name;
101        merged.path = defaultValue(other.path, path);
102        merged.targetMaxSizeMB = defaultValue(other.targetMaxSizeMB, targetMaxSizeMB);
103        merged.absoluteMaxSizeMB = defaultValue(other.absoluteMaxSizeMB, absoluteMaxSizeMB);
104        merged.firstLevelTTL = defaultValue(other.firstLevelTTL, firstLevelTTL);
105        merged.secondLevelTTL = defaultValue(other.secondLevelTTL, secondLevelTTL);
106        merged.implClass = defaultValue(other.implClass, implClass);
107        merged.properties.putAll(properties);
108        merged.properties.putAll(other.properties);
109        return merged;
110    }
111
112    protected static <T> T defaultValue(T value, T defaultValue) {
113        return value == null ? defaultValue : value;
114    }
115
116    @Override
117    public String getId() {
118        return name;
119    }
120
121    public String getName() {
122        return name;
123    }
124
125    public int getTargetMaxSizeMB() {
126        return targetMaxSizeMB == null ? DEFAULT_TARGET_MAX_SIZE_MB : targetMaxSizeMB.intValue();
127    }
128
129    /** @deprecated since 10.10, unused */
130    @Deprecated
131    public void setTargetMaxSizeMB(int targetMaxSizeMB) {
132        this.targetMaxSizeMB = Integer.valueOf(targetMaxSizeMB);
133    }
134
135    public int getAbsoluteMaxSizeMB() {
136        return absoluteMaxSizeMB == null ? DEFAULT_ABSOLUTE_MAX_SIZE_MB : absoluteMaxSizeMB.intValue();
137    }
138
139    /** @deprecated since 10.10, unused */
140    @Deprecated
141    public void setAbsoluteMaxSizeMB(int absoluteMaxSizeMB) {
142        this.absoluteMaxSizeMB = Integer.valueOf(absoluteMaxSizeMB);
143    }
144
145    public int getFirstLevelTTL() {
146        return firstLevelTTL == null ? DEFAULT_FIRST_LEVEL_TTL : firstLevelTTL.intValue();
147    }
148
149    /** @deprecated since 10.10, unused */
150    @Deprecated
151    public void setFirstLevelTTL(int firstLevelTTL) {
152        this.firstLevelTTL = Integer.valueOf(firstLevelTTL);
153    }
154
155    public int getSecondLevelTTL() {
156        return secondLevelTTL == null ? DEFAULT_SECOND_LEVEL_TTL : secondLevelTTL.intValue();
157    }
158
159    /** @deprecated since 10.10, unused */
160    @Deprecated
161    public void setSecondLevelTTL(int secondLevelTTL) {
162        this.secondLevelTTL = Integer.valueOf(secondLevelTTL);
163    }
164
165    /**
166     * Returns the directory where blobs will be stored.
167     *
168     * @since 9.1
169     */
170    public String getDataDir() {
171        return path;
172    }
173
174    /**
175     * Returns properties.
176     *
177     * @since 10.1
178     */
179    public Map<String, String> getProperties() {
180        return properties;
181    }
182
183    /**
184     * Returns the implementation class, or {@code null} if not defined.
185     *
186     * @since 10.10
187     */
188    public Class<? extends TransientStoreProvider> getKlass() {
189        return implClass;
190    }
191
192}