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-23 23:08:32 +0000 (Mi, 23 Jan 2008) $
009: // $LastChangedRevision: 4382 $
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.util.Date;
050: import java.util.Iterator;
051:
052: import de.anomic.kelondro.kelondroColumn;
053: import de.anomic.kelondro.kelondroNaturalOrder;
054: import de.anomic.kelondro.kelondroRow;
055: import de.anomic.kelondro.kelondroStack;
056: import de.anomic.server.serverDate;
057:
058: public class yacyNewsQueue {
059:
060: private File path;
061: private kelondroStack queueStack;
062: private yacyNewsDB newsDB;
063:
064: public static final kelondroRow rowdef = new kelondroRow(
065: new kelondroColumn[] {
066: new kelondroColumn("newsid",
067: kelondroColumn.celltype_string,
068: kelondroColumn.encoder_bytes,
069: yacyNewsRecord.idLength,
070: "id = created + originator"),
071: new kelondroColumn("last touched",
072: kelondroColumn.celltype_string,
073: kelondroColumn.encoder_bytes,
074: serverDate.PATTERN_SHORT_SECOND.length(),
075: "") }, kelondroNaturalOrder.naturalOrder, 0);
076:
077: public yacyNewsQueue(File path, yacyNewsDB newsDB) {
078: this .path = path;
079: this .newsDB = newsDB;
080: this .queueStack = kelondroStack.open(path, rowdef);
081: }
082:
083: private void resetDB() {
084: try {
085: close();
086: } catch (Exception e) {
087: }
088: if (path.exists())
089: path.delete();
090: queueStack = kelondroStack.open(path, rowdef);
091: }
092:
093: public void clear() {
094: resetDB();
095: }
096:
097: public void close() {
098: if (queueStack != null)
099: queueStack.close();
100: queueStack = null;
101: }
102:
103: public void finalize() {
104: close();
105: }
106:
107: public int size() {
108: return queueStack.size();
109: }
110:
111: public synchronized void push(yacyNewsRecord entry)
112: throws IOException {
113: queueStack.push(r2b(entry, true));
114: }
115:
116: public synchronized yacyNewsRecord pop() throws IOException {
117: if (queueStack.size() == 0)
118: return null;
119: return b2r(queueStack.pop());
120: }
121:
122: public synchronized yacyNewsRecord top() throws IOException {
123: if (queueStack.size() == 0)
124: return null;
125: return b2r(queueStack.top());
126: }
127:
128: public synchronized yacyNewsRecord topInc() throws IOException {
129: if (queueStack.size() == 0)
130: return null;
131: yacyNewsRecord entry = pop();
132: if (entry != null) {
133: entry.incDistribution();
134: push(entry);
135: }
136: return entry;
137: }
138:
139: public synchronized yacyNewsRecord get(String id) {
140: yacyNewsRecord record;
141: Iterator<yacyNewsRecord> i = records(true);
142: while (i.hasNext()) {
143: record = i.next();
144: if ((record != null) && (record.id().equals(id)))
145: return record;
146: }
147: return null;
148: }
149:
150: public synchronized yacyNewsRecord remove(String id) {
151: yacyNewsRecord record;
152: Iterator<yacyNewsRecord> i = records(true);
153: while (i.hasNext()) {
154: record = i.next();
155: if ((record != null) && (record.id().equals(id))) {
156: i.remove();
157: return record;
158: }
159: }
160: return null;
161: }
162:
163: private yacyNewsRecord b2r(kelondroRow.Entry b) throws IOException {
164: if (b == null)
165: return null;
166: String id = b.getColString(0, null);
167: //Date touched = yacyCore.parseUniversalDate(new String(b[1]));
168: return newsDB.get(id);
169: }
170:
171: private kelondroRow.Entry r2b(yacyNewsRecord r, boolean updateDB)
172: throws IOException {
173: if (r == null)
174: return null;
175: if (updateDB) {
176: newsDB.put(r);
177: } else {
178: yacyNewsRecord r1 = newsDB.get(r.id());
179: if (r1 == null)
180: newsDB.put(r);
181: }
182: kelondroRow.Entry b = queueStack.row().newEntry(
183: new byte[][] {
184: r.id().getBytes(),
185: serverDate.formatShortSecond(new Date())
186: .getBytes() });
187: return b;
188: }
189:
190: public Iterator<yacyNewsRecord> records(boolean up) {
191: // iterates yacyNewsRecord-type objects
192: return new newsIterator(up);
193: }
194:
195: public class newsIterator implements Iterator<yacyNewsRecord> {
196: // iterates yacyNewsRecord-type objects
197:
198: Iterator<kelondroRow.Entry> stackNodeIterator;
199:
200: public newsIterator(boolean up) {
201: stackNodeIterator = queueStack.stackIterator(up);
202: }
203:
204: public boolean hasNext() {
205: return stackNodeIterator.hasNext();
206: }
207:
208: public yacyNewsRecord next() {
209: kelondroRow.Entry row = (kelondroRow.Entry) stackNodeIterator
210: .next();
211: try {
212: return b2r(row);
213: } catch (IOException e) {
214: return null;
215: }
216: }
217:
218: public void remove() {
219: stackNodeIterator.remove();
220: }
221:
222: }
223:
224: }
|