View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.oai;
20  
21  import java.time.Instant;
22  import java.util.Optional;
23  import java.util.Random;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.mycore.oai.pmh.Header;
28  import org.mycore.oai.pmh.MetadataFormat;
29  import org.mycore.oai.set.MCRSet;
30  
31  /**
32   * <p>Base class to query different types of data in the mycore system.
33   * Implementations has to implement the query and the earliest time stamp
34   * methods. The result of a query is a {@link MCROAIResult}.</p>
35   * 
36   * <p>Searchers have a unique id and a expiration time which increases
37   * every time a query is fired.</p>
38   * 
39   * @author Matthias Eichner
40   */
41  public abstract class MCROAISearcher {
42  
43      protected static final Logger LOGGER = LogManager.getLogger(MCROAISearcher.class);
44  
45      protected MCROAIIdentify identify;
46  
47      protected MetadataFormat metadataFormat;
48  
49      /** The unique ID of this result set */
50      protected final String id;
51  
52      protected long expire;
53  
54      /**
55       * Increase every time a {@link #query(String)} is called
56       */
57      protected long runningExpirationTimer;
58  
59      protected int partitionSize;
60  
61      protected MCROAISetManager setManager;
62  
63      private MCROAIObjectManager objectManager;
64  
65      public MCROAISearcher() {
66          Random random = new Random(System.currentTimeMillis());
67          this.id = Long.toString(random.nextLong(), 36) + Long.toString(System.currentTimeMillis(), 36);
68      }
69  
70      public void init(MCROAIIdentify identify, MetadataFormat format, long expire, int partitionSize,
71          MCROAISetManager setManager, MCROAIObjectManager objectManager) {
72          this.identify = identify;
73          this.metadataFormat = format;
74          this.expire = expire;
75          this.partitionSize = partitionSize;
76          this.setManager = setManager;
77          this.objectManager = objectManager;
78          updateRunningExpirationTimer();
79      }
80  
81      public abstract Optional<Header> getHeader(String mcrId);
82  
83      public abstract MCROAIResult query(String cursor);
84  
85      public abstract MCROAIResult query(MCRSet set, Instant from, Instant until);
86  
87      /**
88       * Returns the earliest created/modified record time stamp. If the earliest time stamp cannot be retrieved an
89       * empty optional is returned.
90       *
91       * @return the earliest created/modified time stamp
92       */
93      public abstract Optional<Instant> getEarliestTimestamp();
94  
95      public boolean isExpired() {
96          return System.currentTimeMillis() > runningExpirationTimer;
97      }
98  
99      public Instant getExpirationTime() {
100         return Instant.ofEpochMilli(runningExpirationTimer);
101     }
102 
103     public MetadataFormat getMetadataFormat() {
104         return metadataFormat;
105     }
106 
107     public String getID() {
108         return id;
109     }
110 
111     public MCROAISetManager getSetManager() {
112         return setManager;
113     }
114 
115     public MCROAIObjectManager getObjectManager() {
116         return objectManager;
117     }
118 
119     protected String getConfigPrefix() {
120         return this.identify.getConfigPrefix();
121     }
122 
123     /**
124      * Updates the running expiration timer.
125      */
126     protected void updateRunningExpirationTimer() {
127         this.runningExpirationTimer = System.currentTimeMillis() + this.expire;
128     }
129 
130     public int getPartitionSize() {
131         return partitionSize;
132     }
133 
134 }