001: // yacyNewsRecord.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-06 19:23:38 +0000 (So, 06 Jan 2008) $
009: // $LastChangedRevision: 4305 $
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.util.Date;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.Map;
051: import java.util.Properties;
052: import java.util.Map.Entry;
053:
054: import de.anomic.kelondro.kelondroNaturalOrder;
055: import de.anomic.kelondro.kelondroRow;
056: import de.anomic.server.serverCodings;
057: import de.anomic.server.serverDate;
058:
059: public class yacyNewsRecord {
060:
061: public static final int maxNewsRecordLength = 512;
062: public static final int categoryStringLength = 8;
063: public static final int idLength = serverDate.PATTERN_SHORT_SECOND
064: .length()
065: + yacySeedDB.commonHashLength;
066:
067: private String originator; // hash of originating peer
068: private Date created; // Date when news was created by originator
069: private Date received; // Date when news was received here at this peer
070: private String category; // keyword that adresses possible actions
071: private int distributed; // counter that counts number of distributions of this news record
072: private Map<String, String> attributes; // elemets of the news for a special category
073:
074: public static final int attributesMaxLength = maxNewsRecordLength
075: - idLength - categoryStringLength
076: - serverDate.PATTERN_SHORT_SECOND.length() - 2;
077:
078: public static final kelondroRow rowdef = new kelondroRow(
079: "String idx-" + idLength
080: + " \"id = created + originator\"," + "String cat-"
081: + categoryStringLength + "," + "String rec-"
082: + serverDate.PATTERN_SHORT_SECOND.length() + ","
083: + "short dis-2 {b64e}," + "String att-"
084: + attributesMaxLength,
085: kelondroNaturalOrder.naturalOrder, 0);
086:
087: public static yacyNewsRecord newRecord(String newsString) {
088: try {
089: return new yacyNewsRecord(newsString);
090: } catch (IllegalArgumentException e) {
091: yacyCore.log.logWarning("rejected bad yacy news record: "
092: + e.getMessage());
093: return null;
094: }
095: }
096:
097: public static yacyNewsRecord newRecord(String category,
098: Properties attributes) {
099: try {
100: HashMap<String, String> m = new HashMap<String, String>();
101: Iterator<Entry<Object, Object>> e = attributes.entrySet()
102: .iterator();
103: Map.Entry<Object, Object> entry;
104: while (e.hasNext()) {
105: entry = e.next();
106: m.put((String) entry.getKey(), (String) entry
107: .getValue());
108: }
109: return new yacyNewsRecord(category, m);
110: } catch (IllegalArgumentException e) {
111: yacyCore.log.logWarning("rejected bad yacy news record: "
112: + e.getMessage());
113: return null;
114: }
115: }
116:
117: public static yacyNewsRecord newRecord(String category,
118: Map<String, String> attributes) {
119: try {
120: return new yacyNewsRecord(category, attributes);
121: } catch (IllegalArgumentException e) {
122: yacyCore.log.logWarning("rejected bad yacy news record: "
123: + e.getMessage());
124: return null;
125: }
126: }
127:
128: public static yacyNewsRecord newRecord(String id, String category,
129: Date received, int distributed,
130: Map<String, String> attributes) {
131: try {
132: return new yacyNewsRecord(id, category, received,
133: distributed, attributes);
134: } catch (IllegalArgumentException e) {
135: yacyCore.log.logWarning("rejected bad yacy news record: "
136: + e.getMessage());
137: return null;
138: }
139: }
140:
141: public yacyNewsRecord(String newsString) {
142: this .attributes = serverCodings.string2map(newsString, ",");
143: if (attributes.toString().length() > attributesMaxLength)
144: throw new IllegalArgumentException("attributes length ("
145: + attributes.toString().length()
146: + ") exceeds maximum (" + attributesMaxLength + ")");
147: this .category = (attributes.containsKey("cat")) ? (String) attributes
148: .get("cat")
149: : "";
150: if (category.length() > categoryStringLength)
151: throw new IllegalArgumentException("category length ("
152: + category.length() + ") exceeds maximum ("
153: + categoryStringLength + ")");
154: this .received = (attributes.containsKey("rec")) ? serverDate
155: .parseShortSecond((String) attributes.get("rec"),
156: serverDate.UTCDiffString()) : new Date();
157: this .created = (attributes.containsKey("cre")) ? serverDate
158: .parseShortSecond((String) attributes.get("cre"),
159: serverDate.UTCDiffString()) : new Date();
160: this .distributed = (attributes.containsKey("dis")) ? Integer
161: .parseInt((String) attributes.get("dis")) : 0;
162: this .originator = (attributes.containsKey("ori")) ? (String) attributes
163: .get("ori")
164: : "";
165: removeStandards();
166: }
167:
168: public yacyNewsRecord(String category,
169: Map<String, String> attributes) {
170: if (category.length() > categoryStringLength)
171: throw new IllegalArgumentException("category length ("
172: + category.length() + ") exceeds maximum ("
173: + categoryStringLength + ")");
174: if (attributes.toString().length() > attributesMaxLength)
175: throw new IllegalArgumentException("attributes length ("
176: + attributes.toString().length()
177: + ") exceeds maximum (" + attributesMaxLength + ")");
178: this .attributes = attributes;
179: this .received = null;
180: this .created = new Date();
181: this .category = category;
182: this .distributed = 0;
183: this .originator = yacyCore.seedDB.mySeed().hash;
184: removeStandards();
185: }
186:
187: protected yacyNewsRecord(String id, String category, Date received,
188: int distributed, Map<String, String> attributes) {
189: if (category.length() > categoryStringLength)
190: throw new IllegalArgumentException("category length ("
191: + category.length() + ") exceeds maximum ("
192: + categoryStringLength + ")");
193: if (attributes.toString().length() > attributesMaxLength)
194: throw new IllegalArgumentException("attributes length ("
195: + attributes.toString().length()
196: + ") exceeds maximum (" + attributesMaxLength + ")");
197: this .attributes = attributes;
198: this .received = received;
199: this .created = serverDate.parseShortSecond(id.substring(0,
200: serverDate.PATTERN_SHORT_SECOND.length()), serverDate
201: .UTCDiffString());
202: this .category = category;
203: this .distributed = distributed;
204: this .originator = id.substring(serverDate.PATTERN_SHORT_SECOND
205: .length());
206: removeStandards();
207: }
208:
209: private void removeStandards() {
210: attributes.remove("ori");
211: attributes.remove("cat");
212: attributes.remove("cre");
213: attributes.remove("rec");
214: attributes.remove("dis");
215: }
216:
217: public String toString() {
218: // this creates the string that shall be distributed
219: // attention: this has no additional encoding
220: if (this .originator != null)
221: attributes.put("ori", this .originator);
222: if (this .category != null)
223: attributes.put("cat", this .category);
224: if (this .created != null)
225: attributes.put("cre", serverDate
226: .formatShortSecond(this .created));
227: if (this .received != null)
228: attributes.put("rec", serverDate
229: .formatShortSecond(this .received));
230: attributes.put("dis", Integer.toString(this .distributed));
231: String theString = attributes.toString();
232: removeStandards();
233: return theString;
234: }
235:
236: public String id() {
237: return serverDate.formatShortSecond(created) + originator;
238: }
239:
240: public String originator() {
241: return originator;
242: }
243:
244: public Date created() {
245: return created;
246: }
247:
248: public Date received() {
249: return received;
250: }
251:
252: public String category() {
253: return category;
254: }
255:
256: public int distributed() {
257: return distributed;
258: }
259:
260: public void incDistribution() {
261: distributed++;
262: }
263:
264: public Map<String, String> attributes() {
265: return attributes;
266: }
267:
268: public String attribute(String key, String dflt) {
269: String s = (String) attributes.get(key);
270: if ((s == null) || (s.length() == 0))
271: return dflt;
272: return s;
273: }
274:
275: public static void main(String[] args) {
276: System.out.println((newRecord(args[0])).toString());
277: }
278: }
|