View Javadoc
1   package com.kodexa.client.store;
2   
3   import com.fasterxml.jackson.core.type.TypeReference;
4   import com.fasterxml.jackson.databind.ObjectMapper;
5   import com.kodexa.client.Document;
6   import com.kodexa.client.KodexaException;
7   import com.kodexa.client.connectors.Connector;
8   import lombok.extern.slf4j.Slf4j;
9   import org.apache.commons.io.FileUtils;
10  
11  import java.io.File;
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  @Slf4j
18  public abstract class AbstractFileSystemDocumentStore implements Connector, DocumentStore {
19      protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
20  
21      protected final String path;
22  
23      protected final boolean forceInitialize;
24  
25      protected final File storeFolder;
26  
27      protected final File indexFile;
28  
29      protected List<String> index = new ArrayList<>();
30  
31      private int position = 0;
32  
33      protected abstract String getExtension();
34  
35      public AbstractFileSystemDocumentStore(String path, boolean forceInitialize) {
36          this.path = path;
37          this.forceInitialize = forceInitialize;
38  
39          this.storeFolder = new File(path);
40          this.indexFile = new File(path + File.separator + "index.json");
41  
42          if (forceInitialize || !storeFolder.exists()) {
43              log.info("Initialize message pack store [" + storeFolder + "]");
44              try {
45                  if (storeFolder.exists()) {
46                      FileUtils.deleteDirectory(storeFolder);
47                  }
48                  FileUtils.forceMkdir(storeFolder);
49  
50                  saveIndex();
51              }
52              catch (IOException e) {
53                  throw new KodexaException("Unable to delete store folder [" + path + "]", e);
54              }
55          }
56          else {
57              readIndex();
58          }
59      }
60  
61      public void readIndex() {
62          try {
63              index = AbstractFileSystemDocumentStore.OBJECT_MAPPER.readValue(indexFile, new TypeReference<List<String>>() {
64              });
65          }
66          catch (IOException e) {
67              throw new KodexaException("Unable to read index.json for store [" + indexFile.getAbsolutePath() + "]", e);
68          }
69      }
70  
71      public void saveIndex() {
72          try {
73              AbstractFileSystemDocumentStore.OBJECT_MAPPER.writeValue(indexFile, index);
74          }
75          catch (IOException e) {
76              throw new KodexaException("Unable to write index.json for store [" + indexFile.getAbsolutePath() + "]", e);
77          }
78      }
79  
80      @Override
81      public InputStream getSource(Document document) {
82          throw new UnsupportedOperationException("Unable to get source content from the message pack store");
83      }
84  
85      protected File getFile(String uuid) {
86          return new File(storeFolder.getAbsolutePath() + File.separator + uuid + "." + getExtension());
87      }
88  
89      @Override
90      public String getName() {
91          return "Message Pack Document Store";
92      }
93  
94      public void resetConnector() {
95          this.position = 0;
96      }
97  
98      @Override
99      public boolean hasNext() {
100         return position < index.size();
101     }
102 
103     @Override
104     public Document next() {
105         Document document = getDocument(position);
106         position++;
107         return document;
108     }
109 
110     public abstract Document getDocument(int position);
111 }