001/*
002 * (C) Copyright 2015 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 *     Delbosc Benoit
018 */
019package org.nuxeo.ecm.core.redis.contribs;
020
021import org.nuxeo.ecm.core.storage.dbs.DBSInvalidations;
022
023/**
024 * invalidations and nodeId serializer/unserializer.
025 *
026 * @since 8.10
027 */
028public class RedisDBSInvalidations {
029
030    private static final String MESSAGE_SEPARATOR = ":";
031
032    private static final String ID_SEPARATOR = "/";
033
034    private static final String ALL_DOCUMENTS = "ALL";
035
036    private String nodeId;
037
038    private DBSInvalidations invalidations;
039
040    public RedisDBSInvalidations(String nodeId, DBSInvalidations invals) {
041        assert nodeId != null : "nodeId required";
042        assert invals != null : "invals required";
043        this.nodeId = nodeId;
044        this.invalidations = invals;
045    }
046
047    public RedisDBSInvalidations(String receiverNodeId, String message) {
048        assert receiverNodeId != null : "receiverNodeId required";
049        if (message == null || !message.contains(":")) {
050            throw new IllegalArgumentException("Invalid message: " + message);
051        }
052        String[] parts = message.split(MESSAGE_SEPARATOR, 2);
053        nodeId = parts[0];
054        if (!receiverNodeId.equals(nodeId)) {
055            // only decode if it is a remote node
056            invalidations = deserializeInvalidations(parts[1]);
057        }
058    }
059
060    private DBSInvalidations deserializeInvalidations(String invalsStr) {
061        if (ALL_DOCUMENTS.equals(invalsStr)) {
062            return new DBSInvalidations(true);
063        }
064        DBSInvalidations invals = new DBSInvalidations();
065        for (String id : invalsStr.split(ID_SEPARATOR)) {
066            invals.add(id);
067        }
068        return invals;
069    }
070
071    public DBSInvalidations getInvalidations() {
072        return invalidations;
073    }
074
075    public String serialize() {
076        // message:
077        // - nodeId:id1/id2/...
078        // - nodeId:ALL
079        return nodeId + MESSAGE_SEPARATOR + serializeInvalidations(invalidations);
080    }
081
082    private String serializeInvalidations(DBSInvalidations invals) {
083        if (invals.all) {
084            return ALL_DOCUMENTS;
085        }
086        return String.join(ID_SEPARATOR, invals.ids);
087    }
088
089    @Override
090    public String toString() {
091        if (invalidations == null) {
092            return "RedisDBSInvalidationsInvalidations(local, discarded)";
093        }
094        return "RedisDBSInvalidationsInvalidations(fromNode=" + nodeId + ", " + invalidations.toString() + ")";
095    }
096
097}