package org.apache.nutch.crawl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MapFile;
import org.apache.hadoop.io.UTF8;

public class CrawlDBTestUtil {

  private static final Log LOG = LogFactory.getLog(CrawlDBTestUtil.class);

  /**
   * Creates synthetic crawldb
   * 
   * @param fs
   *          filesystem where db will be created
   * @param crawldb
   *          path were db will be created
   * @param init
   *          urls to be inserted, objects are of type URLCrawlDatum
   * @throws Exception
   */
  public static void createCrawlDb(FileSystem fs, Path crawldb, List<URLCrawlDatum> init)
      throws Exception {
    LOG.trace("* creating crawldb: " + crawldb);
    Path dir = new Path(crawldb, CrawlDatum.DB_DIR_NAME);
    MapFile.Writer writer = new MapFile.Writer(fs, new Path(dir, "part-00000")
        .toString(), UTF8.class, CrawlDatum.class);
    Iterator<URLCrawlDatum> it = init.iterator();
    while (it.hasNext()) {
      URLCrawlDatum row = it.next();
      LOG.info("adding:" + row.url.toString());
      writer.append(new UTF8(row.url), row.datum);
    }
    writer.close();
  }

  /**
   * For now we need to manually construct our Configuration, because we need to
   * override the default one and it is currently not possible to use dynamically
   * set values.
   * 
   * @return
   */
  public static Configuration create(){
    Configuration conf=new Configuration();
    conf.addDefaultResource("nutch-default.xml");
    conf.addFinalResource("crawl-tests.xml");
    return conf;
  }

  public static class URLCrawlDatum {

    UTF8 url;

    CrawlDatum datum;

    public URLCrawlDatum(UTF8 url, CrawlDatum datum) {
      this.url = url;
      this.datum = datum;
    }
  }
}

@@ -1,8 +1,622 @@
+/*%0A * Copyright 2006 The Apache Software Foundation%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 

add license