001/*
002 * (C) Copyright 2015-2017 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 */
019package org.nuxeo.ecm.core.transientstore.api;
020
021import org.nuxeo.common.xmap.XMap;
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.transientstore.SimpleTransientStore;
026
027/**
028 * {@link XMap} descriptor for representing the Configuration of a {@link TransientStore}
029 *
030 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
031 * @since 7.2
032 */
033@XObject("store")
034public class TransientStoreConfig {
035
036    @XNode("@name")
037    protected String name;
038
039    @XNode("@path")
040    protected String path;
041
042    // target size that ideally should never be exceeded
043    @XNode("targetMaxSizeMB")
044    protected int targetMaxSizeMB = -1;
045
046    // size that must never be exceeded
047    @XNode("absoluteMaxSizeMB")
048    protected int absoluteMaxSizeMB = -1;
049
050    @XNode("firstLevelTTL")
051    protected int firstLevelTTL = 60 * 2;
052
053    @XNode("secondLevelTTL")
054    protected int secondLevelTTL = 10;
055
056    @XNode("minimalRetention")
057    protected int minimalRetention = 10;
058
059    @XNode("@class")
060    protected Class<? extends TransientStore> implClass = SimpleTransientStore.class;
061
062    protected TransientStore store;
063
064    public TransientStoreConfig() {
065    }
066
067    public TransientStoreConfig(String name) {
068        this.name = name;
069    }
070
071    public String getName() {
072        return name;
073    }
074
075    public int getTargetMaxSizeMB() {
076        return targetMaxSizeMB;
077    }
078
079    public void setTargetMaxSizeMB(int targetMaxSizeMB) {
080        this.targetMaxSizeMB = targetMaxSizeMB;
081    }
082
083    public int getAbsoluteMaxSizeMB() {
084        return absoluteMaxSizeMB;
085    }
086
087    public void setAbsoluteMaxSizeMB(int absoluteMaxSizeMB) {
088        this.absoluteMaxSizeMB = absoluteMaxSizeMB;
089    }
090
091    public int getFirstLevelTTL() {
092        return firstLevelTTL;
093    }
094
095    public void setFirstLevelTTL(int firstLevelTTL) {
096        this.firstLevelTTL = firstLevelTTL;
097    }
098
099    public int getSecondLevelTTL() {
100        return secondLevelTTL;
101    }
102
103    public void setSecondLevelTTL(int secondLevelTTL) {
104        this.secondLevelTTL = secondLevelTTL;
105    }
106
107    public TransientStore getStore() {
108        if (store == null) {
109            try {
110                store = implClass.newInstance();
111                store.init(this);
112            } catch (InstantiationException | IllegalAccessException e) {
113                throw new NuxeoException(e);
114            }
115        }
116        return store;
117    }
118
119    /**
120     * Flush the cached store if any
121     *
122     * @since 9.2
123     */
124    public void flush() {
125        store = null;
126    }
127
128    /**
129     * Returns the directory where blobs will be stored.
130     *
131     * @since 9.1
132     */
133    public String getDataDir() {
134        return path;
135    }
136
137}