0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
137N/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/*
137N/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
137N/Aimport java.io.BufferedOutputStream;
137N/Aimport java.io.BufferedReader;
137N/Aimport java.io.DataOutputStream;
137N/Aimport java.io.FileNotFoundException;
137N/Aimport java.io.FileOutputStream;
137N/Aimport java.io.FileReader;
137N/Aimport java.io.IOException;
137N/Aimport java.io.RandomAccessFile;
457N/Aimport java.util.Map;
137N/Aimport java.util.StringTokenizer;
137N/Aimport java.util.TreeMap;
587N/Aimport java.util.logging.Level;
1327N/Aimport java.util.logging.Logger;
1327N/A
1195N/Aimport org.opensolaris.opengrok.util.IOUtils;
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 */
137N/Apublic class EftarFile {
0N/A
0N/A public static final int RECORD_LENGTH = 14;
0N/A private long offset;
0N/A private DataOutputStream out;
137N/A
0N/A class Node {
137N/A
0N/A public long hash;
0N/A public String tag;
457N/A public Map<Long, Node> children;
0N/A public long tagOffset;
0N/A public long childOffset;
0N/A public long myOffset;
137N/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 }
137N/A
0N/A public Node put(long hash, String desc) {
137N/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 }
137N/A
0N/A public Node get(long hash) {
0N/A return children.get(hash);
0N/A }
0N/A }
137N/A
0N/A class FNode {
137N/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;
137N/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 }
137N/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 }
137N/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 }
137N/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;
137N/A while (b <= e) {
137N/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 {
137N/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 }
137N/A
0N/A public static long myHash(String name) {
137N/A if (name == null || name.length() == 0) {
0N/A return 0;
0N/A }
0N/A long hash = 2861;
0N/A int n = name.length();
137N/A if (n > 100) {
0N/A n = 100;
137N/A }
137N/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 }
137N/A
0N/A private void write(Node n) throws IOException {
137N/A if (n.tag != null) {
0N/A out.write(n.tag.getBytes());
0N/A offset += n.tag.length();
0N/A }
137N/A for (Node childnode : n.children.values()) {
0N/A out.writeLong(childnode.hash);
137N/A if (childnode.children.size() > 0) {
0N/A out.writeShort((short) (childnode.childOffset - offset));
137N/A out.writeShort((short) childnode.children.size());
0N/A } else {
0N/A out.writeShort(0);
460N/A if (childnode.tag == null) {
460N/A out.writeShort((short) 0);
460N/A } else {
137N/A out.writeShort((short) childnode.tag.length());
0N/A }
0N/A }
460N/A if (childnode.tag == null) {
460N/A out.writeShort(0);
460N/A } else {
0N/A out.writeShort((short) (childnode.tagOffset - offset));
0N/A }
0N/A offset += RECORD_LENGTH;
0N/A }
137N/A for (Node childnode : n.children.values()) {
0N/A write(childnode);
0N/A }
0N/A }
137N/A
137N/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 }
137N/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 }
137N/A for (Node childnode : n.children.values()) {
0N/A traverse(childnode);
0N/A }
0N/A }
0N/A private Node root;
137N/A
0N/A public void readInput(String tagsPath) throws IOException {
0N/A BufferedReader r = new BufferedReader(new FileReader(tagsPath));
521N/A try {
521N/A readInput(r);
521N/A } finally {
1195N/A IOUtils.close(r);
521N/A }
521N/A }
521N/A
521N/A private void readInput(BufferedReader r) throws IOException {
137N/A if (root == null) {
0N/A root = new Node(1, null);
0N/A }
0N/A String line;
0N/A while ((line = r.readLine()) != null) {
0N/A int tab = line.indexOf('\t');
137N/A if (tab > 0) {
0N/A String path = line.substring(0, tab);
137N/A String desc = line.substring(tab + 1);
283N/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 }
137N/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;
0N/A write(root);
1195N/A IOUtils.close(out);
0N/A }
137N/A
459N/A public void create(String[] args) throws IOException, FileNotFoundException {
137N/A for (int i = 0; i < args.length - 1; i++) {
137N/A readInput(args[i]);
137N/A }
137N/A write(args[args.length - 1]);
137N/A }
587N/A
587N/A /**
587N/A * Main method is used to generate eftar file from the path description
587N/A * file in the run scripts.
1190N/A *
587N/A * @param args Input files and output file
587N/A */
587N/A @SuppressWarnings("PMD.SystemPrintln")
587N/A public static void main(String[] args) {
587N/A if (args.length < 2) {
587N/A System.err.println("Usage inputFile [inputFile ...] outputFile");
587N/A System.exit(1);
587N/A }
587N/A
587N/A try {
587N/A EftarFile ef = new EftarFile();
587N/A ef.create(args);
587N/A } catch (Exception e) {
1327N/A Logger logger = Logger.getLogger(EftarFile.class.getName());
1327N/A logger.warning("EftarFile error: " + e.getMessage());
1327N/A logger.log(Level.FINE, "main", e);
587N/A }
587N/A }
587N/A
396N/A}