1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.storage;
18  
19  import java.time.Instant;
20  import java.util.Map;
21  import java.util.function.Supplier;
22  
23  import com.linecorp.centraldogma.common.Author;
24  import com.linecorp.centraldogma.common.CentralDogmaException;
25  
26  /**
27   * An abstract interface to define operations performed on the storage.
28   *
29   * @param <T> type of the managed element
30   */
31  public interface StorageManager<T> {
32      /**
33       * Closes this manager.
34       *
35       * @param failureCauseSupplier the supplier which provides a reason why it is closed
36       */
37      void close(Supplier<CentralDogmaException> failureCauseSupplier);
38  
39      /**
40       * Returns {@code true} if an element with the specified {@code name} exists.
41       *
42       * @param name the name of an element
43       */
44      boolean exists(String name);
45  
46      /**
47       * Returns an element with the specified {@code name}.
48       *
49       * @param name the name of an element
50       * @throws CentralDogmaException if no element with the specified {@code name} exists
51       */
52      T get(String name);
53  
54      /**
55       * Returns a newly-created element with the specified {@code name} by the specified {@link Author}.
56       *
57       * @param name the name of an element which is supposed to be created
58       * @param author the author who is creating the new element
59       */
60      default T create(String name, Author author) {
61          return create(name, System.currentTimeMillis(), author);
62      }
63  
64      /**
65       * Returns a newly-created element with the specified {@code name} and the specified
66       * {@code creationTimeMillis} by the specified {@link Author}.
67       *
68       * @param name the name of an element which is supposed to be created
69       * @param creationTimeMillis the creation time in milliseconds
70       * @param author the author who is creating the new element
71       */
72      T create(String name, long creationTimeMillis, Author author);
73  
74      /**
75       * Returns all elements as a {@link Map} of the name and the element.
76       */
77      Map<String, T> list();
78  
79      /**
80       * Returns all removed elements as a {@link Map} of the name and the removal timestamp.
81       */
82      Map<String, Instant> listRemoved();
83  
84      /**
85       * Removes an element with the specified {@code name}.
86       *
87       * @param name the name of an element which is supposed to be removed
88       */
89      void remove(String name);
90  
91      /**
92       * Restores an element with the specified {@code name}.
93       *
94       * @param name the name of an element which is supposed to be restored
95       */
96      T unremove(String name);
97  
98      /**
99       * Purges a set of names for the elements which have been marked for purge.
100      */
101     void purgeMarked();
102 
103     /**
104      * Marks the specified {@code name} element for purge.
105      *
106      * @param name the name of an element which is supposed to be purged
107      */
108     void markForPurge(String name);
109 
110     /**
111      * Ensures that this manager is open.
112      *
113      * @throws IllegalStateException if this manager is not initialized yet
114      * @throws CentralDogmaException if this manager has already been closed with a cause.
115      *                               See {@link #close(Supplier)} method.
116      */
117     void ensureOpen();
118 }