001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.dbs;
020
021import java.util.Collection;
022import java.util.HashSet;
023import java.util.Set;
024
025/**
026 * A set of invalidations for a given repository.
027 * <p>
028 * Records both modified and deleted fragments, as well as "parents modified" fragments.
029 *
030 * @since 8.10
031 */
032public class DBSInvalidations {
033
034    /**
035     * Maximum number of invalidations kept, after which only {@link #all} is set. This avoids accumulating too many
036     * invalidations in memory, at the expense of more coarse-grained invalidations.
037     */
038    public static final int MAX_SIZE = 10000;
039
040    /**
041     * Used locally when invalidating everything, or when too many invalidations have been received.
042     */
043    public boolean all;
044
045    /** null when empty */
046    public Set<String> ids;
047
048    public DBSInvalidations() {
049    }
050
051    public DBSInvalidations(boolean all) {
052        this.all = all;
053    }
054
055    public boolean isEmpty() {
056        return ids == null && !all;
057    }
058
059    public void clear() {
060        all = false;
061        ids = null;
062    }
063
064    protected void setAll() {
065        all = true;
066        ids = null;
067    }
068
069    protected void checkMaxSize() {
070        if (ids != null && ids.size() > MAX_SIZE) {
071            setAll();
072        }
073    }
074
075    public void add(DBSInvalidations other) {
076        if (other == null) {
077            return;
078        }
079        if (all) {
080            return;
081        }
082        if (other.all) {
083            setAll();
084            return;
085        }
086        if (other.ids != null) {
087            if (ids == null) {
088                ids = new HashSet<>();
089            }
090            ids.addAll(other.ids);
091        }
092        checkMaxSize();
093    }
094
095    public void add(String id) {
096        if (all) {
097            return;
098        }
099        if (ids == null) {
100            ids = new HashSet<>();
101        }
102        ids.add(id);
103        checkMaxSize();
104    }
105
106    public void addAll(Collection<String> idsToAdd) {
107        if (all) {
108            return;
109        }
110        if (ids == null) {
111            ids = new HashSet<>(idsToAdd);
112        } else {
113            ids.addAll(idsToAdd);
114        }
115        checkMaxSize();
116    }
117
118    @Override
119    public String toString() {
120        StringBuilder sb = new StringBuilder(this.getClass().getSimpleName() + '(');
121        if (all) {
122            sb.append("all=true");
123        }
124        if (ids != null) {
125            sb.append("ids=");
126            sb.append(ids);
127        }
128        sb.append(')');
129        return sb.toString();
130    }
131
132}