001/*
002 * (C) Copyright 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob;
020
021import java.io.OutputStream;
022
023/**
024 * Decides how a key is computed from a blob.
025 * <p>
026 * Implementations of this class must have a proper {@link Object#equals} method.
027 *
028 * @since 11.1
029 */
030public interface KeyStrategy {
031
032    /**
033     * Separator between object key and version id in the returned key.
034     *
035     * @since 11.2
036     */
037    char VER_SEP = '@';
038
039    /**
040     * Checks whether this key strategy uses de-duplication. When de-duplication is used, two blobs with identical
041     * contents have identical keys.
042     */
043    boolean useDeDuplication();
044
045    /**
046     * Gets, if possible, a digest from the key. This is not possible if the key is not derived from a digest.
047     *
048     * @param key the key
049     * @return a digest, or {@code null}
050     */
051    String getDigestFromKey(String key);
052
053    /**
054     * Observer of the writes to an {@link OutputStream}.
055     */
056    interface WriteObserver {
057
058        /**
059         * Wraps the given stream to observe it.
060         */
061        OutputStream wrap(OutputStream out);
062
063        /**
064         * Must be called when writes to the wrapped stream are done, to complete observation.
065         */
066        void done();
067    }
068
069    /**
070     * Gets the write context for the given blob.
071     */
072    BlobWriteContext getBlobWriteContext(BlobContext blobContext);
073
074}