Project

General

Profile

Download (6.42 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package org.cybertaxonomy.utis.store;
11

    
12
import java.io.File;
13
import java.io.FileWriter;
14
import java.io.IOException;
15
import java.util.Date;
16

    
17
import org.apache.commons.httpclient.util.DateParseException;
18
import org.apache.commons.httpclient.util.DateUtil;
19
import org.apache.commons.io.FileUtils;
20
import org.apache.lucene.index.CorruptIndexException;
21
import org.apache.lucene.index.IndexReader;
22
import org.apache.lucene.store.FSDirectory;
23
import org.openrdf.repository.sail.SailRepository;
24
import org.openrdf.sail.Sail;
25
import org.openrdf.sail.SailException;
26

    
27
import com.tinkerpop.blueprints.Graph;
28
import com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph;
29
import com.tinkerpop.blueprints.oupls.sail.GraphSail;
30
import com.tinkerpop.blueprints.oupls.sail.SailLoader;
31

    
32
/**
33
 * @author a.kohlbecker
34
 * @date Oct 19, 2015
35
 *
36
 */
37
public class Neo4jStore extends Store{
38

    
39
    private Neo4j2Graph graph;
40
    private Sail sail;
41
    private SailRepository sailRepo;
42
    private final static String STORE_TYPE = "neo4j";
43

    
44
    // January 1, 1970, 00:00:00 GMT as starting time
45
    private Date lastModified = new Date(0);
46
    // private Date lastModified = new GregorianCalendar(2016, 10, 10).getTime(); // new Date(0);
47

    
48

    
49

    
50
    public Neo4jStore(String storeName) throws Exception {
51
        super(storeName);
52
    }
53

    
54

    
55
    /**
56
     * {@inheritDoc}
57
     * @throws SailException
58
     */
59
    @Override
60
    protected void initStoreEngine() throws Exception  {
61

    
62
        graph = new Neo4j2Graph(storeLocation.toString());
63
        sail = new GraphSail<Neo4j2Graph>(graph);
64
        sail.initialize();
65
        sailRepo = new SailRepository(sail);
66

    
67
        logger.info("Using Neo4jGraph store at " + storeLocation.toString());
68
        logger.info("Neo4jGraph has " + sizeInfo());
69
    }
70

    
71
    /**
72
     * {@inheritDoc}
73
     */
74
    @Override
75
    protected void stopStoreEngine() throws Exception {
76
        sailRepo.shutDown();
77
        sail.shutDown(); // should be none by the above command already
78
    }
79

    
80
    /**
81
     * @throws Exception
82
     *
83
     */
84
    @Override
85
    protected void load(File rdfFile) throws Exception {
86

    
87
        SailLoader loader = new SailLoader(sail);
88
//            loader.setBufferSize(100000); // TODO optimize?
89
        logger.info("loading RDF/XML into Neo4jGraph store");
90
        loader.load(rdfFile);
91
        logger.info("loading RDF/XML done");
92
        logger.info("Neo4jGraph has " +  sizeInfo());
93

    
94
        logger.info("rdf loaded into Neo4jGraph store at " + storeLocation);
95
    }
96

    
97

    
98
    public long countEdges() {
99

    
100
        String indexName = graph.getRawGraph().index().getRelationshipAutoIndexer().getAutoIndex().getName();
101
        return countIndexDocuments("relationship" + File.separator +  indexName);
102
    }
103

    
104
    public long countVertexes() {
105

    
106
        String indexName = graph.getRawGraph().index().getNodeAutoIndexer().getAutoIndex().getName();
107
        return countIndexDocuments("node" + File.separator + indexName);
108
    }
109

    
110
    /**
111
     * @param nodeAutoIndexName
112
     * @return
113
     */
114
    private long countIndexDocuments(String nodeAutoIndexName) {
115
        File luceneFolder = new File(storeLocation, "index" + File.separator + "lucene");
116
        File indexFolder = new File(luceneFolder, nodeAutoIndexName);
117
        int cnt = 0;
118
        IndexReader reader = null;
119
        try {
120
            reader = IndexReader.open(FSDirectory.open(indexFolder));
121
            cnt = reader.numDocs();
122
        } catch (CorruptIndexException e) {
123
            logger.warn("CorruptIndexException", e);
124
        } catch (IOException e) {
125
            logger.warn("Lucene index can not be read, this is ok as long there store location is empty. Original error: " + e.getMessage());
126
        } finally {
127
            if(reader != null) {
128
                try {
129
                    reader.close();
130
                } catch (IOException e) {
131
                    /* IGNORE */
132
                }
133
            }
134
        }
135
        return cnt;
136
    }
137

    
138
    public String sizeInfo() {
139
        return countEdges() + " edges, " + countVertexes() + " vertexes";
140
    }
141

    
142
    public Graph graph() {
143
        return graph;
144
    }
145

    
146
    /**
147
     * @return the sailRepo
148
     */
149
    public SailRepository getSailRepo() {
150
        return sailRepo;
151
    }
152

    
153
    /**
154
     * @return the lastModified
155
     */
156
    public Date getLastModified() {
157

    
158
        try {
159
            String lastModifiedString = FileUtils.readFileToString(lastModifiedFile());
160
            lastModified = DateUtil.parseDate(lastModifiedString);
161
        } catch (IOException e) {
162
            logger.info("Could not read " + lastModifiedFile().getAbsolutePath());
163
        } catch (DateParseException e) {
164
            throw new RuntimeException("Error while parsing date in " + lastModifiedFile().getAbsolutePath(), e);
165
        }
166

    
167
        return lastModified;
168
    }
169

    
170
    /**
171
     * @param lastModified the lastModified to set
172
     * @throws IOException
173
     */
174
    public void setLastModified(Date lastModified) throws IOException {
175

    
176
        File lastModifiedFile = lastModifiedFile();
177
        File updateLogFile = updateLogFile();
178

    
179
        // write the timestamp as only content into the last modified file
180
        FileUtils.write(lastModifiedFile, DateUtil.formatDate(lastModified));
181

    
182
        // append a new line to the log file
183
        FileWriter logFileWriter = new FileWriter(updateLogFile);
184
        logFileWriter.append("\n").append(DateUtil.formatDate(lastModified));
185
        logFileWriter.close();
186

    
187
        // getLastModified() reads the date from the file
188
        this.lastModified = getLastModified();
189
        // test if file has been written correctly
190
        if(lastModified.compareTo(lastModified) != 0){
191
            logger.error("Sote timestamps differ - please check!");
192
        }
193
        logger.info(DateUtil.formatDate(lastModified) + " written to " + lastModifiedFile.getAbsolutePath());
194
    }
195

    
196
    /**
197
     * @return
198
     */
199
    private File lastModifiedFile() {
200
        return new File(storeLocation, "LAST_IMPORT_DATE");
201
    }
202

    
203
    private File updateLogFile() {
204
        return new File(storeLocation, "UPDATE.log");
205
    }
206

    
207
    /**
208
     * {@inheritDoc}
209
     */
210
    @Override
211
    protected String storeType() {
212
        return STORE_TYPE;
213
    }
214

    
215

    
216
    /**
217
     * {@inheritDoc}
218
     */
219
    @Override
220
    protected String dataFileExtension() {
221
        return "rdf";
222
    }
223

    
224

    
225
}
(2-2/8)