1   package eu.fbk.knowledgestore.datastore;
2   
3   import com.google.common.collect.Iterables;
4   import com.zaxxer.hikari.HikariDataSource;
5   import eu.fbk.knowledgestore.data.Record;
6   import eu.fbk.knowledgestore.data.Stream;
7   import eu.fbk.knowledgestore.data.XPath;
8   import eu.fbk.knowledgestore.runtime.DataCorruptedException;
9   import eu.fbk.knowledgestore.vocabulary.KS;
10  import org.openrdf.model.URI;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import javax.annotation.Nullable;
15  import java.io.*;
16  import java.sql.*;
17  import java.util.*;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: alessio
22   * Date: 08/09/14
23   * Time: 17:57
24   * To change this template use File | Settings | File Templates.
25   */
26  
27  public class SolrDataStore implements DataStore {
28  
29  	static Logger logger = LoggerFactory.getLogger(SolrDataStore.class);
30  	public HikariDataSource dataSource;
31  
32  	public SolrDataStore() {
33  
34  	}
35  
36  	public class SolrTransaction implements DataTransaction {
37  
38  		private Connection con;
39  		boolean readOnly;
40  
41  		public SolrTransaction(boolean readOnly) throws SQLException {
42  			this.readOnly = readOnly;
43  
44  			// stuff here
45  		}
46  
47  		private void connect(String dbUser, String dbPass) throws SQLException {
48  			// stuff here
49  		}
50  
51  		@Override
52  		public Stream<Record> lookup(URI type, Set<? extends URI> ids, @Nullable Set<? extends URI> properties) throws IOException, IllegalArgumentException, IllegalStateException {
53  //			String tableName = getTableName(type);
54  //			List<Record> returns = new ArrayList<>();
55  //
56  //			for (URI id : ids) {
57  //				String uri;
58  //				try {
59  //					uri = id.toString();
60  //				} catch (NullPointerException e) {
61  //					throw new IOException(e);
62  //				}
63  //
64  //				logger.debug(String.format("Selecting %s", uri));
65  //				String query = selectQuery.replace("$tableName", tableName);
66  //				try {
67  //					PreparedStatement stmt = con.prepareStatement(query);
68  //					stmt.setString(1, uri);
69  //
70  //					ResultSet set = stmt.executeQuery();
71  //
72  //					while (set.next()) {
73  //						Record r = unserializeRecord(set.getBytes("value"));
74  //						if (properties != null) {
75  //							r.retain(Iterables.toArray(properties, URI.class));
76  //						}
77  //						returns.add(r);
78  //					}
79  //				} catch (SQLException e) {
80  //					throw new IOException(e);
81  //				}
82  //
83  //			}
84  //
85  //			return Stream.create(returns);
86  			return Stream.create();
87  		}
88  
89  		@Override
90  		public Stream<Record> retrieve(URI type, @Nullable XPath condition, @Nullable Set<? extends URI> properties) throws IOException, IllegalArgumentException, IllegalStateException {
91  //			String tableName = getTableName(type);
92  //			List<Record> returns = new ArrayList<>();
93  //
94  //			logger.debug("Retrieving all lines");
95  //			String query = selectAllQuery.replace("$tableName", tableName);
96  //
97  //			try {
98  //				Statement statement = con.createStatement();
99  //				ResultSet resultSet = statement.executeQuery(query);
100 //
101 //				while (resultSet.next()) {
102 //					Record r = unserializeRecord(resultSet.getBytes("value"));
103 //					if (condition != null && !condition.evalBoolean(r)) {
104 //						continue;
105 //					}
106 //
107 //					if (properties != null) {
108 //						r.retain(Iterables.toArray(properties, URI.class));
109 //					}
110 //					returns.add(r);
111 //				}
112 //			} catch (SQLException e) {
113 //				throw new IOException(e);
114 //			}
115 //
116 //			return Stream.create(returns);
117 			return Stream.create();
118 		}
119 
120 		@Override
121 		public long count(URI type, @Nullable XPath condition) throws IOException, IllegalArgumentException, IllegalStateException {
122 //			String tableName = getTableName(type);
123 //			logger.debug("Counting rows");
124 //			String query = countQuery.replace("$tableName", tableName);
125 //
126 //			try {
127 //				Statement statement = con.createStatement();
128 //				ResultSet resultSet = statement.executeQuery(query);
129 //
130 //				if (resultSet.next()) {
131 //					return resultSet.getLong(1);
132 //				}
133 //			} catch (SQLException e) {
134 //				throw new IOException(e);
135 //			}
136 //
137 //			throw new IOException();
138 			return 0;
139 		}
140 
141 		@Override
142 		public Stream<Record> match(Map<URI, XPath> conditions, Map<URI, Set<URI>> ids, Map<URI, Set<URI>> properties) throws IOException, IllegalStateException {
143 			return null;  //To change body of implemented methods use File | Settings | File Templates.
144 		}
145 
146 		@Override
147 		public void store(URI type, Record record) throws IOException, IllegalStateException {
148 
149 			System.out.println(type);
150 			System.out.println(record);
151 		}
152 
153 		@Override
154 		public void delete(URI type, URI id) throws IOException, IllegalStateException {
155 			// stuff here
156 		}
157 
158 		@Override
159 		public void end(boolean commit) throws DataCorruptedException, IOException, IllegalStateException {
160 			// stuff here
161 		}
162 	}
163 
164 	@Override
165 	public DataTransaction begin(boolean readOnly) throws DataCorruptedException, IOException, IllegalStateException {
166 
167 		SolrTransaction ret = null;
168 		try {
169 			ret = new SolrTransaction(readOnly);
170 		} catch (Exception e) {
171 			throw new IOException(e);
172 		}
173 
174 		return ret;
175 	}
176 
177 	@Override
178 	public void init() throws IOException, IllegalStateException {
179 	}
180 
181 	@Override
182 	public void close() {
183 	}
184 }