001: // kelondroSQLTable.java
002: // this class was written by Martin Thelian
003: // (the class was once a sub-class of dbtest.java)
004: //
005: // This is a part of YaCy, a peer-to-peer based web search engine
006: //
007: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
008: // $LastChangedRevision: 1986 $
009: // $LastChangedBy: orbiter $
010: //
011: // LICENSE
012: //
013: // This program is free software; you can redistribute it and/or modify
014: // it under the terms of the GNU General Public License as published by
015: // the Free Software Foundation; either version 2 of the License, or
016: // (at your option) any later version.
017: //
018: // This program is distributed in the hope that it will be useful,
019: // but WITHOUT ANY WARRANTY; without even the implied warranty of
020: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: // GNU General Public License for more details.
022: //
023: // You should have received a copy of the GNU General Public License
024: // along with this program; if not, write to the Free Software
025: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026:
027: package de.anomic.kelondro;
028:
029: import java.io.IOException;
030: import java.sql.Connection;
031: import java.sql.DriverManager;
032: import java.sql.PreparedStatement;
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.Date;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: import de.anomic.kelondro.kelondroRow.Entry;
041:
042: /*
043: * Commands to create a database using mysql:
044: *
045: * CREATE database yacy;
046: * USE yacy;
047: * CREATE TABLE hash CHAR(12) not null primary key, value BLOB);
048: * insert into user (Host, User, Password) values ('%','yacy',password('yacy'));
049: * insert into db (User, Db, Select_priv, Insert_priv, Update_priv, Delete_priv) values ('yacy@%','yacy','Y','Y','Y','Y')
050: * grant ALL on yacy.* to yacy;
051: */
052:
053: public class kelondroSQLTable implements kelondroIndex {
054:
055: private final String db_driver_str_mysql = "org.gjt.mm.mysql.Driver";
056: private final String db_driver_str_pgsql = "org.postgresql.Driver";
057:
058: private final String db_conn_str_mysql = "jdbc:mysql://192.168.0.2:3306/yacy";
059: private final String db_conn_str_pgsql = "jdbc:postgresql://192.168.0.2:5432";
060:
061: private final String db_usr_str = "yacy";
062: private final String db_pwd_str = "yacy";
063:
064: private Connection theDBConnection = null;
065: private final kelondroByteOrder order = new kelondroNaturalOrder(
066: true);
067: private kelondroRow rowdef;
068:
069: public kelondroSQLTable(String dbType, kelondroRow rowdef)
070: throws Exception {
071: this .rowdef = rowdef;
072: openDatabaseConnection(dbType);
073: }
074:
075: private void openDatabaseConnection(String dbType) throws Exception {
076:
077: if (dbType == null)
078: throw new IllegalArgumentException();
079:
080: String dbDriverStr = null, dbConnStr = null;
081: if (dbType.equalsIgnoreCase("mysql")) {
082: dbDriverStr = db_driver_str_mysql;
083: dbConnStr = db_conn_str_mysql;
084: } else if (dbType.equalsIgnoreCase("pgsql")) {
085: dbDriverStr = db_driver_str_pgsql;
086: dbConnStr = db_conn_str_pgsql;
087: }
088: try {
089: Class.forName(dbDriverStr).newInstance();
090: } catch (Exception e) {
091: throw new Exception("Unable to load the jdbc driver: "
092: + e.getMessage(), e);
093: }
094: try {
095: this .theDBConnection = DriverManager.getConnection(
096: dbConnStr, this .db_usr_str, this .db_pwd_str);
097: } catch (Exception e) {
098: throw new Exception(
099: "Unable to establish a database connection: "
100: + e.getMessage(), e);
101: }
102:
103: }
104:
105: public String filename() {
106: return "dbtest." + theDBConnection.hashCode();
107: }
108:
109: public void close() {
110: if (this .theDBConnection != null)
111: try {
112: this .theDBConnection.close();
113: } catch (SQLException e) {
114: e.printStackTrace();
115: }
116: this .theDBConnection = null;
117: }
118:
119: public int size() {
120: int size = -1;
121: try {
122: String sqlQuery = new String(
123: "SELECT count(value) from test");
124:
125: PreparedStatement sqlStatement = this .theDBConnection
126: .prepareStatement(sqlQuery);
127: ResultSet result = sqlStatement.executeQuery();
128:
129: while (result.next()) {
130: size = result.getInt(1);
131: }
132:
133: result.close();
134: sqlStatement.close();
135:
136: return size;
137: } catch (Exception e) {
138: e.printStackTrace();
139: return -1;
140: }
141: }
142:
143: public kelondroRow row() {
144: return this .rowdef;
145: }
146:
147: public boolean has(byte[] key) throws IOException {
148: return (get(key) != null);
149: }
150:
151: public ArrayList<kelondroRowSet> removeDoubles() {
152: return new ArrayList<kelondroRowSet>();
153: }
154:
155: public kelondroRow.Entry get(byte[] key) throws IOException {
156: try {
157: String sqlQuery = new String(
158: "SELECT value from test where hash = ?");
159:
160: PreparedStatement sqlStatement = this .theDBConnection
161: .prepareStatement(sqlQuery);
162: sqlStatement.setString(1, new String(key));
163:
164: byte[] value = null;
165: ResultSet result = sqlStatement.executeQuery();
166: while (result.next()) {
167: value = result.getBytes("value");
168: }
169:
170: result.close();
171: sqlStatement.close();
172:
173: if (value == null)
174: return null;
175: kelondroRow.Entry entry = this .rowdef.newEntry(value);
176: return entry;
177: } catch (Exception e) {
178: throw new IOException(e.getMessage());
179: }
180: }
181:
182: public synchronized void putMultiple(List<kelondroRow.Entry> rows)
183: throws IOException {
184: Iterator<kelondroRow.Entry> i = rows.iterator();
185: while (i.hasNext())
186: put((Entry) i.next());
187: }
188:
189: public kelondroRow.Entry put(kelondroRow.Entry row, Date entryDate)
190: throws IOException {
191: return put(row);
192: }
193:
194: public kelondroRow.Entry put(kelondroRow.Entry row)
195: throws IOException {
196: try {
197:
198: kelondroRow.Entry oldEntry = remove(row.getColBytes(0),
199: false);
200:
201: String sqlQuery = new String("INSERT INTO test ("
202: + "hash, " + "value) " + "VALUES (?,?)");
203:
204: PreparedStatement sqlStatement = this .theDBConnection
205: .prepareStatement(sqlQuery);
206:
207: sqlStatement.setString(1, new String(row.getColString(0,
208: null)));
209: sqlStatement.setBytes(2, row.bytes());
210: sqlStatement.execute();
211:
212: sqlStatement.close();
213:
214: return oldEntry;
215: } catch (Exception e) {
216: throw new IOException(e.getMessage());
217: }
218: }
219:
220: public synchronized void addUnique(kelondroRow.Entry row)
221: throws IOException {
222: throw new UnsupportedOperationException();
223: }
224:
225: public synchronized void addUnique(kelondroRow.Entry row,
226: Date entryDate) {
227: throw new UnsupportedOperationException();
228: }
229:
230: public synchronized void addUniqueMultiple(
231: List<kelondroRow.Entry> rows) throws IOException {
232: throw new UnsupportedOperationException();
233: }
234:
235: public kelondroRow.Entry remove(byte[] key, boolean keepOrder)
236: throws IOException {
237: try {
238:
239: kelondroRow.Entry entry = this .get(key);
240: if (entry == null)
241: return entry;
242:
243: String sqlQuery = new String(
244: "DELETE FROM test WHERE hash = ?");
245:
246: PreparedStatement sqlStatement = this .theDBConnection
247: .prepareStatement(sqlQuery);
248: sqlStatement.setString(1, new String(key));
249: sqlStatement.execute();
250:
251: return entry;
252: } catch (Exception e) {
253: throw new IOException(e.getMessage());
254: }
255: }
256:
257: public kelondroRow.Entry removeOne() {
258: return null;
259: }
260:
261: public kelondroCloneableIterator<kelondroRow.Entry> rows(
262: boolean up, byte[] startKey) throws IOException {
263: // Objects are of type kelondroRow.Entry
264: return null;
265: }
266:
267: public kelondroCloneableIterator<byte[]> keys(boolean up,
268: byte[] startKey) {
269: // Objects are of type byte[]
270: return null;
271: }
272:
273: public int columns() {
274: // TODO Auto-generated method stub
275: return 0;
276: }
277:
278: public int columnSize(int column) {
279: // TODO Auto-generated method stub
280: return 0;
281: }
282:
283: public kelondroByteOrder order() {
284: return this .order;
285: }
286:
287: public int primarykey() {
288: return 0;
289: }
290:
291: public kelondroProfile profile() {
292: return new kelondroProfile();
293: }
294:
295: public final int cacheObjectChunkSize() {
296: // dummy method
297: return -1;
298: }
299:
300: public long[] cacheObjectStatus() {
301: // dummy method
302: return null;
303: }
304:
305: public final int cacheNodeChunkSize() {
306: return -1;
307: }
308:
309: public final int[] cacheNodeStatus() {
310: return new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
311: }
312:
313: public void reset() {
314: // TODO Auto-generated method stub
315:
316: }
317: }
|