1 2 3 package net.nutch.db; 4 5 import java.io.*; 6 import java.util.*; 7 8 import net.nutch.io.*; 9 import net.nutch.fs.*; 10 import net.nutch.util.*; 11 12 20 public class EditSectionWriter { 21 private final int COMPLETION_VERSION = 0; 22 public final static String WRITE_METAINFO_PREFIX = "metainfo."; 23 public final static String EDITS_PREFIX = "edits."; 24 25 static int numSections; 26 27 30 42 43 46 53 54 55 File editsDir, editsList; 56 File editsListFile; 57 int numEdits = 0; 58 boolean closed = false; 59 NutchFileSystem nfs; 60 SequenceFile.Writer seqWriter; 61 62 65 public EditSectionWriter(NutchFileSystem nfs, String label, int targetNum, int writerNum, Class keyClass, Class valClass) throws IOException { 66 this.nfs = nfs; 67 68 File allEditsDir = new File("editsection." + targetNum, "editsdir." + writerNum); 69 this.editsDir = new File(allEditsDir, label); 70 this.editsList = new File(editsDir, "editslist"); 71 this.seqWriter = new SequenceFile.Writer(nfs, editsList.getPath(), keyClass, valClass); 72 } 73 74 77 public synchronized void append(WritableComparable key, Writable val) throws IOException { 78 if (closed) { 79 throw new IOException("EditSectionWriter is closed"); 80 } 81 seqWriter.append(key, val); 82 numEdits++; 83 } 84 85 89 public synchronized void close() throws IOException { 90 if (closed) { 91 throw new IOException("EditSectionWriter is already closed"); 92 } 93 94 seqWriter.close(); 96 97 File editsInfo = new File(editsDir, "editsinfo"); 99 DataOutputStream out = new DataOutputStream(nfs.create(editsInfo)); 100 try { 101 out.write(COMPLETION_VERSION); 102 out.writeInt(numEdits); 103 } finally { 104 out.close(); 105 } 106 } 107 } 108 | Popular Tags |