001/*
002 * (C) Copyright 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 *     Florent Guillaume
018 */
019package org.nuxeo.runtime.kv;
020
021import java.util.Collection;
022import java.util.Map;
023
024/**
025 * Key/Value Store.
026 * <p>
027 * This is the interface for a Key/Value store, which stores simple values associated to keys.
028 * <p>
029 * A Key/Value store is thread-safe.
030 *
031 * @since 9.1
032 */
033public interface KeyValueStore {
034
035    /**
036     * Sets the value associated to the key.
037     *
038     * @param key the key
039     * @param value the value, which may be {@code null}
040     */
041    void put(String key, byte[] value);
042
043    /**
044     * Sets the value associated to the key.
045     *
046     * @param key the key
047     * @param value the value, which may be {@code null}
048     * @since 9.3
049     */
050    void put(String key, String value);
051
052    /**
053     * Sets the value associated to the key, and a TTL.
054     *
055     * @param key the key
056     * @param value the value, which may be {@code null}
057     * @param ttl the TTL, in seconds (0 for infinite)
058     * @since 9.3
059     */
060    void put(String key, byte[] value, long ttl);
061
062    /**
063     * Sets the value associated to the key, and a TTL.
064     *
065     * @param key the key
066     * @param value the value, which may be {@code null}
067     * @param ttl the TTL, in seconds (0 for infinite)
068     * @since 9.3
069     */
070    void put(String key, String value, long ttl);
071
072    /**
073     * Sets the TTL for an existing key.
074     *
075     * @param key the key
076     * @param ttl the TTL, in seconds (0 for infinite)
077     * @return {@code true} if the TTL has been set, or {@code false} if the key does not exist
078     * @since 9.3
079     */
080    boolean setTTL(String key, long ttl);
081
082    /**
083     * Retrieves the value associated to the key.
084     *
085     * @param key the key
086     * @return the value, or {@code null} if there is no value
087     */
088    byte[] get(String key);
089
090    /**
091     * Retrieves the value associated to the key.
092     *
093     * @param key the key
094     * @return the value, or {@code null} if there is no value
095     * @throws IllegalArgumentException if the value cannot be returned as a {@link String}
096     * @since 9.3
097     */
098    String getString(String key);
099
100    /**
101     * Retrieves the key/value map associated with the keys.
102     *
103     * @param keys the keys
104     * @return the key/value map
105     * @since 9.10
106     */
107    Map<String, byte[]> get(Collection<String> keys);
108
109    /**
110     * Retrieves the key/value map associated with the  keys.
111     *
112     * @param keys the keys
113     * @return the key/value map
114     * @throws IllegalArgumentException if one of the values cannot be returned as a {@link String}
115     * @since 9.10
116     */
117    Map<String, String> getStrings(Collection<String> keys);
118
119    /**
120     * Atomically sets the value associated to the key to the given value if the current value is the expected value.
121     * <p>
122     * Note value comparison is done by value and not by reference.
123     *
124     * @param key the key
125     * @param expected the expected value, which may be {@code null}
126     * @param value the updated value, which may be {@code null}
127     * @return {@code true} if the value was updated, or {@code false} if not (the expected value was not found)
128     */
129    boolean compareAndSet(String key, byte[] expected, byte[] value);
130
131    /**
132     * Atomically sets the value associated to the key to the given value, with the given TTL, if the current value is
133     * the expected value.
134     * <p>
135     * Note value comparison is done by value and not by reference.
136     *
137     * @param key the key
138     * @param expected the expected value, which may be {@code null}
139     * @param value the updated value, which may be {@code null}
140     * @param ttl the TTL, in seconds (0 for infinite)
141     * @return {@code true} if the value was updated, or {@code false} if not (the expected value was not found)
142     * @since 9.3
143     */
144    boolean compareAndSet(String key, byte[] expected, byte[] value, long ttl);
145
146    /**
147     * Atomically sets the value associated to the key to the given value if the current value is the expected value.
148     * <p>
149     * Note value comparison is done by value and not by reference.
150     *
151     * @param key the key
152     * @param expected the expected value, which may be {@code null}
153     * @param value the updated value, which may be {@code null}
154     * @return {@code true} if the value was updated, or {@code false} if not (the expected value was not found)
155     * @since 9.3
156     */
157    boolean compareAndSet(String key, String expected, String value);
158
159    /**
160     * Atomically sets the value associated to the key to the given value, with the given TTL, if the current value is
161     * the expected value.
162     * <p>
163     * Note value comparison is done by value and not by reference.
164     *
165     * @param key the key
166     * @param expected the expected value, which may be {@code null}
167     * @param value the updated value, which may be {@code null}
168     * @param ttl the TTL, in seconds (0 for infinite)
169     * @return {@code true} if the value was updated, or {@code false} if not (the expected value was not found)
170     * @since 9.3
171     */
172    boolean compareAndSet(String key, String expected, String value, long ttl);
173
174}