001: // plasmaStore.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, 2004
007: // last major change: 20.01.2004
008: //
009: // You agree that the Author(s) is (are) not responsible for cost,
010: // loss of data or any harm that may be caused by usage of this softare or
011: // this documentation. The usage of this software is on your own risk. The
012: // installation and usage (starting/running) of this software may allow other
013: // people or application to access your computer and any attached devices and
014: // is highly dependent on the configuration of the software which must be
015: // done by the user of the software;the author(s) is (are) also
016: // not responsible for proper configuration and usage of the software, even
017: // if provoked by documentation provided together with the software.
018: //
019: // THE SOFTWARE THAT FOLLOWS AS ART OF PROGRAMMING BELOW THIS SECTION
020: // IS PUBLISHED UNDER THE GPL AS DOCUMENTED IN THE FILE gpl.txt ASIDE THIS
021: // FILE AND AS IN http://www.gnu.org/licenses/gpl.txt
022: // ANY CHANGES TO THIS FILE ACCORDING TO THE GPL CAN BE DONE TO THE
023: // LINES THAT FOLLOWS THIS COPYRIGHT NOTICE HERE, BUT CHANGES MUST NOT
024: // BE DONE ABOVE OR INSIDE THE COPYRIGHT NOTICE. A RE-DISTRIBUTION
025: // MUST CONTAIN THE INTACT AND UNCHANGED COPYRIGHT NOTICE.
026: // CONTRIBUTIONS AND CHANGES TO THE PROGRAM CODE SHOULD BE MARKED AS SUCH.
027:
028: /*
029: This class provides storage functions for the plasma search engine.
030: Unlike the plasmaSwitchboard, which holds run-time information,
031: this class holds general index information that is in run-time
032: specific.
033: */
034:
035: package de.anomic.plasma;
036:
037: import java.io.File;
038: import java.io.FileInputStream;
039: import java.io.FileOutputStream;
040: import java.io.IOException;
041:
042: //import java.io.RandomAccessFile;
043:
044: public class plasmaStore {
045:
046: // some static helper methods
047: public static void saveGzip(File f, byte[] content)
048: throws IOException {
049: java.util.zip.GZIPOutputStream gzipout = null;
050: try {
051: f.getParentFile().mkdirs();
052: gzipout = new java.util.zip.GZIPOutputStream(
053: new FileOutputStream(f));
054: gzipout.write(content, 0, content.length);
055: } finally {
056: if (gzipout != null)
057: try {
058: gzipout.close();
059: } catch (Exception e) {
060: }
061: }
062: }
063:
064: public static byte[] loadGzip(File f) throws IOException {
065: java.util.zip.GZIPInputStream gzipin = null;
066: try {
067: gzipin = new java.util.zip.GZIPInputStream(
068: new FileInputStream(f));
069: byte[] result = new byte[1024];
070: byte[] buffer = new byte[512];
071: byte[] b;
072: int len = 0;
073: int last;
074: while ((last = gzipin.read(buffer, 0, buffer.length)) > 0) {
075: // assert the buffer to the result
076: while (result.length - len < last) {
077: // the result array is too small, increase space
078: b = new byte[result.length * 2];
079: System.arraycopy(result, 0, b, 0, len);
080: result = b;
081: b = null;
082: }
083: // copy the last read
084: System.arraycopy(buffer, 0, result, len, last);
085: len += last;
086: }
087: gzipin.close();
088: // finished with reading. now cut the result to the right size
089: b = new byte[len];
090: System.arraycopy(result, 0, b, 0, len);
091: result = null;
092: return b;
093: } finally {
094: if (gzipin != null)
095: try {
096: gzipin.close();
097: } catch (Exception e) {
098: }
099: }
100: }
101:
102: /* public static void saveProperties(File f, Properties props, String comment) throws IOException {
103: File fp = f.getParentFile();
104: if (fp != null) fp.mkdirs();
105: FileOutputStream fos = new FileOutputStream(f);
106: props.store(fos, comment);
107: fos.close();
108: }
109:
110: public static Properties loadProperties(File f) throws IOException {
111: Properties p = new Properties();
112: FileInputStream fis = new FileInputStream(f);
113: p.load(fis);
114: fis.close();
115: return p;
116: }
117: */
118:
119: /*
120: private static long[] appendFileToStack(File fragment, File dest) throws IOException {
121: // returns a long[2] with
122: // long[0] = startOfFileFragemt in dest
123: // long[1] = lengthOfFileFragment in dest
124: long l = fragment.length();
125: long p = dest.length();
126: RandomAccessFile fo = new RandomAccessFile(dest, "rw");
127: FileInputStream fi = new FileInputStream(fragment);
128: byte[] buffer = new byte[1024];
129: int c;
130: fo.seek(p);
131: while ((c = fi.read(buffer)) >= 0)
132: fo.write(buffer, 0, c);
133: fi.close();
134: fo.close();
135: long[] r = new long[2];
136: r[0] = p;
137: r[1] = l;
138: return r;
139: }
140: */
141:
142: /*
143: public static void main(String[] args) {
144: try {
145: HashSet set = new HashSet();
146: for (int i = 0; i < args.length; i++) set.add(args[i]);
147: plasmaStore store = new plasmaStore(new File("DATABASE"));
148: List result = plasmaSearch.search(set);
149: for (int i = 0; i < result.size(); i++) {
150: ((plasmaLURL.entry) result.get(i)).print();
151: }
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: }
156: */
157: }
|