EftarFile.java revision 459
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.web;
0N/A
0N/Aimport java.io.BufferedOutputStream;
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.DataOutputStream;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.FileReader;
0N/Aimport java.io.IOException;
0N/Aimport java.io.RandomAccessFile;
0N/Aimport java.util.Map;
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.util.TreeMap;
0N/Aimport java.util.logging.Level;
0N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
0N/A
0N/A
0N/A/**
0N/A * An Extremely Fast Tagged Attribute Read-only File System
0N/A * Created on October 12, 2005
0N/A *
0N/A * A Eftar File has the following format
0N/A * FILE --> Record ( Record | tagString ) *
0N/A * Record --> 64bit:Hash 16bit:childrenOffset 16bit:(numberChildren|lenthOfTag) 16bit:tagOffset
0N/A *
0N/A * It is a tree of tagged names,
0N/A * doing binary search in sorted list of children
0N/A *
0N/A * @author Chandan
0N/A */
0N/Apublic class EftarFile {
0N/A
0N/A public static final int RECORD_LENGTH = 14;
0N/A private long offset;
0N/A private DataOutputStream out;
0N/A
0N/A class Node {
0N/A
0N/A public long hash;
0N/A public String tag;
0N/A public Map<Long, Node> children;
0N/A public long tagOffset;
0N/A public long childOffset;
0N/A public long myOffset;
0N/A
0N/A public Node(long hash, String tag) {
0N/A this.hash = hash;
0N/A this.tag = tag;
0N/A children = new TreeMap<Long, Node>();
0N/A }
0N/A
0N/A public Node put(long hash, String desc) {
0N/A if (children.get(hash) == null) {
0N/A children.put(hash, new Node(hash, desc));
0N/A }
0N/A return children.get(hash);
0N/A }
0N/A
0N/A public Node get(long hash) {
0N/A return children.get(hash);
0N/A }
0N/A }
0N/A
0N/A class FNode {
0N/A
0N/A public long offset;
0N/A public long hash;
0N/A public int childOffset;
0N/A public int numChildren;
0N/A public int tagOffset;
0N/A
0N/A public FNode(RandomAccessFile f) throws Throwable {
0N/A offset = f.getFilePointer();
0N/A hash = f.readLong();
0N/A childOffset = f.readUnsignedShort();
0N/A numChildren = f.readUnsignedShort();
0N/A tagOffset = f.readUnsignedShort();
0N/A }
0N/A
0N/A public FNode(long hash, long offset, int childOffset, int num, int tagOffset) {
0N/A this.hash = hash;
0N/A this.offset = offset;
0N/A this.childOffset = childOffset;
0N/A this.numChildren = num;
0N/A this.tagOffset = tagOffset;
0N/A }
0N/A
0N/A public FNode get(long hash, RandomAccessFile f) throws Throwable {
0N/A if (childOffset == 0) {
0N/A return null;
0N/A }
0N/A return sbinSearch(offset + childOffset, numChildren, hash, f);
0N/A }
0N/A
0N/A private FNode sbinSearch(long start, int len, long hash, RandomAccessFile f) throws Throwable {
0N/A int b = 0;
0N/A int e = len;
0N/A while (b <= e) {
0N/A int m = (b + e) / 2;
0N/A f.seek(start + m * RECORD_LENGTH);
0N/A long mhash = f.readLong();
0N/A if (hash > mhash) {
0N/A b = m + 1;
0N/A } else if (hash < mhash) {
0N/A e = m - 1;
0N/A } else {
0N/A return new FNode(mhash, f.getFilePointer() - 8l, f.readUnsignedShort(), f.readUnsignedShort(), f.readUnsignedShort());
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public static long myHash(String name) {
0N/A if (name == null || name.length() == 0) {
0N/A return 0;
0N/A }
0N/A long hash = 2861;
0N/A int n = name.length();
0N/A if (n > 100) {
0N/A n = 100;
0N/A }
0N/A for (int i = 0; i < n; i++) {
0N/A hash = (hash * 641 + name.charAt(i) * 2969 + hash << 6) % 9322397;
0N/A }
0N/A return hash;
0N/A }
0N/A
0N/A private void write(Node n) throws IOException {
0N/A if (n.tag != null) {
0N/A out.write(n.tag.getBytes());
0N/A offset += n.tag.length();
0N/A }
0N/A for (Node childnode : n.children.values()) {
0N/A out.writeLong(childnode.hash);
0N/A if (childnode.children.size() > 0) {
0N/A out.writeShort((short) (childnode.childOffset - offset));
0N/A out.writeShort((short) childnode.children.size());
0N/A } else {
0N/A out.writeShort(0);
0N/A if (childnode.tag != null) {
0N/A out.writeShort((short) childnode.tag.length());
0N/A } else {
0N/A out.writeShort((short) 0);
0N/A }
0N/A }
0N/A if (childnode.tag != null) {
0N/A out.writeShort((short) (childnode.tagOffset - offset));
0N/A } else {
0N/A out.writeShort(0);
0N/A }
0N/A offset += RECORD_LENGTH;
0N/A }
0N/A for (Node childnode : n.children.values()) {
0N/A write(childnode);
0N/A }
0N/A }
0N/A
0N/A private void traverse(Node n) {
0N/A if (n.tag == null) {
0N/A n.tagOffset = 0;
0N/A } else {
0N/A n.tagOffset = offset;
0N/A offset += n.tag.length();
0N/A }
0N/A if (n.children.size() > 0) {
0N/A n.childOffset = offset;
0N/A offset += (RECORD_LENGTH * n.children.size());
0N/A } else {
0N/A n.childOffset = 0;
0N/A }
0N/A for (Node childnode : n.children.values()) {
0N/A traverse(childnode);
0N/A }
0N/A }
0N/A private Node root;
0N/A
0N/A public void readInput(String tagsPath) throws IOException {
0N/A BufferedReader r = new BufferedReader(new FileReader(tagsPath));
0N/A if (root == null) {
0N/A root = new Node(1, null);
0N/A }
0N/A String line;
0N/A int size = 0;
0N/A while ((line = r.readLine()) != null) {
0N/A int tab = line.indexOf('\t');
0N/A if (tab > 0) {
0N/A String path = line.substring(0, tab);
0N/A String desc = line.substring(tab + 1);
0N/A size += desc.length() + 1 + 15;
0N/A StringTokenizer toks = new StringTokenizer(path, "\\/");
0N/A Node n = root;
0N/A while (toks.hasMoreTokens()) {
0N/A n = n.put(myHash(toks.nextToken()), null);
0N/A }
0N/A n.tag = desc;
0N/A }
0N/A }
0N/A try {
0N/A r.close();
0N/A } catch (IOException e) {
0N/A }
0N/A }
0N/A
0N/A public void write(String outPath) throws FileNotFoundException, IOException {
0N/A offset = RECORD_LENGTH;
0N/A traverse(root);
0N/A out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outPath)));
0N/A out.writeLong(0x5e33);
0N/A out.writeShort(RECORD_LENGTH);
0N/A out.writeShort(root.children.size());
0N/A out.writeShort(0);
0N/A offset = RECORD_LENGTH;
write(root);
out.close();
}
public void create(String[] args) throws IOException, FileNotFoundException {
for (int i = 0; i < args.length - 1; i++) {
readInput(args[i]);
}
write(args[args.length - 1]);
}
@SuppressWarnings("PMD.SystemPrintln")
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Usage inputFile [inputFile ...] outputFile");
System.exit(1);
}
try {
EftarFile ef = new EftarFile();
ef.create(args);
} catch (Exception e) {
OpenGrokLogger.getLogger().log(Level.WARNING, "EftarFile error", e);
}
}
}