001: // yacyNewsQueue.java
002: // -----------------------
003: // part of YaCy
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2005
007: //
008: // $LastChangedDate: 2008-01-24 22:49:00 +0000 (Do, 24 Jan 2008) $
009: // $LastChangedRevision: 4397 $
010: // $LastChangedBy: orbiter $
011: //
012: // This program is free software; you can redistribute it and/or modify
013: // it under the terms of the GNU General Public License as published by
014: // the Free Software Foundation; either version 2 of the License, or
015: // (at your option) any later version.
016: //
017: // This program is distributed in the hope that it will be useful,
018: // but WITHOUT ANY WARRANTY; without even the implied warranty of
019: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: // GNU General Public License for more details.
021: //
022: // You should have received a copy of the GNU General Public License
023: // along with this program; if not, write to the Free Software
024: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: // Using this software in any meaning (reading, learning, copying, compiling,
027: // running) means that you agree that the Author(s) is (are) not responsible
028: // for cost, loss of data or any harm that may be caused directly or indirectly
029: // by usage of this softare or this documentation. The usage of this software
030: // is on your own risk. The installation and usage (starting/running) of this
031: // software may allow other people or application to access your computer and
032: // any attached devices and is highly dependent on the configuration of the
033: // software which must be done by the user of the software; the author(s) is
034: // (are) also not responsible for proper configuration and usage of the
035: // software, even if provoked by documentation provided together with
036: // the software.
037: //
038: // Any changes to this file according to the GPL as documented in the file
039: // gpl.txt aside this file in the shipment you received can be done to the
040: // lines that follows this copyright notice here, but changes must not be
041: // done inside the copyright notice above. A re-distribution must contain
042: // the intact and unchanged copyright notice.
043: // Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.yacy;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.io.UnsupportedEncodingException;
050: import java.util.Iterator;
051:
052: import de.anomic.kelondro.kelondroBase64Order;
053: import de.anomic.kelondro.kelondroEcoTable;
054: import de.anomic.kelondro.kelondroException;
055: import de.anomic.kelondro.kelondroIndex;
056: import de.anomic.kelondro.kelondroRow;
057: import de.anomic.server.serverCodings;
058: import de.anomic.server.serverDate;
059:
060: public class yacyNewsDB {
061:
062: private File path;
063: protected kelondroIndex news;
064:
065: public yacyNewsDB(File path) {
066: this .path = path;
067: this .news = new kelondroEcoTable(path, yacyNewsRecord.rowdef,
068: kelondroEcoTable.tailCacheUsageAuto, 10, 0);
069: //this.news = new kelondroCache(kelondroTree.open(path, true, preloadTime, yacyNewsRecord.rowdef));
070: }
071:
072: private void resetDB() {
073: try {
074: close();
075: } catch (Exception e) {
076: }
077: if (path.exists())
078: path.delete();
079: this .news = new kelondroEcoTable(path, yacyNewsRecord.rowdef,
080: kelondroEcoTable.tailCacheUsageAuto, 10, 0);
081: }
082:
083: public void close() {
084: if (news != null)
085: news.close();
086: news = null;
087: }
088:
089: public void finalize() {
090: close();
091: }
092:
093: public int size() {
094: return news.size();
095: }
096:
097: public void remove(String id) throws IOException {
098: news.remove(id.getBytes(), false);
099: }
100:
101: public synchronized yacyNewsRecord put(yacyNewsRecord record)
102: throws IOException {
103: try {
104: return b2r(news.put(r2b(record)));
105: } catch (kelondroException e) {
106: resetDB();
107: return b2r(news.put(r2b(record)));
108: }
109: }
110:
111: public synchronized Iterator<yacyNewsRecord> news()
112: throws IOException {
113: // the iteration iterates yacyNewsRecord - type objects
114: return new recordIterator();
115: }
116:
117: public class recordIterator implements Iterator<yacyNewsRecord> {
118:
119: Iterator<kelondroRow.Entry> rowIterator;
120:
121: public recordIterator() throws IOException {
122: rowIterator = news.rows(true, null);
123: }
124:
125: public boolean hasNext() {
126: return rowIterator.hasNext();
127: }
128:
129: public yacyNewsRecord next() {
130: return b2r((kelondroRow.Entry) rowIterator.next());
131: }
132:
133: public void remove() {
134: }
135:
136: }
137:
138: public synchronized yacyNewsRecord get(String id)
139: throws IOException {
140: try {
141: return b2r(news.get(id.getBytes()));
142: } catch (kelondroException e) {
143: resetDB();
144: return null;
145: }
146: }
147:
148: protected final static yacyNewsRecord b2r(kelondroRow.Entry b) {
149: if (b == null)
150: return null;
151: return yacyNewsRecord.newRecord(b.getColString(0, null), b
152: .getColString(1, "UTF-8"), (b.empty(2)) ? null
153: : serverDate.parseShortSecond(b.getColString(2, null),
154: serverDate.UTCDiffString()), (int) b
155: .getColLong(3), serverCodings.string2map(b
156: .getColString(4, "UTF-8"), ","));
157: }
158:
159: protected final kelondroRow.Entry r2b(yacyNewsRecord r) {
160: if (r == null)
161: return null;
162: try {
163: String attributes = r.attributes().toString();
164: if (attributes.length() > yacyNewsRecord.attributesMaxLength)
165: throw new IllegalArgumentException("attribute length="
166: + attributes.length()
167: + " exceeds maximum size="
168: + yacyNewsRecord.attributesMaxLength);
169: kelondroRow.Entry entry = this .news.row().newEntry();
170: entry.setCol(0, r.id().getBytes());
171: entry.setCol(1, r.category().getBytes("UTF-8"));
172: entry.setCol(2, (r.received() == null) ? null : serverDate
173: .formatShortSecond(r.received()).getBytes());
174: entry.setCol(3, kelondroBase64Order.enhancedCoder
175: .encodeLong(r.distributed(), 2).getBytes());
176: entry.setCol(4, attributes.getBytes("UTF-8"));
177: return entry;
178: } catch (UnsupportedEncodingException e) {
179: // ignore this. this should never occure
180: return null;
181: }
182: }
183:
184: }
|