首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StatisticsDB在Crawler4j开源代码中做了什么?

StatisticsDB在Crawler4j开源代码中做了什么?
EN

Stack Overflow用户
提问于 2013-05-17 12:15:46
回答 1查看 446关注 0票数 1

我正在尝试理解Crawler4j开源网络爬虫。虽然我有一些疑问,如下所示,

问题:-

  1. StatisticsDB在计数器类中是做什么的。请解释以下代码部分, 公共计数器(Environment env,CrawlConfig config)抛出DatabaseException { super(config);this.env = env;this.counterValues =newHashMap();/* *当爬行设置为可恢复时,我们必须将统计信息*保存在事务性数据库中,以确保如果爬虫*意外崩溃或终止,它们不会丢失。*/ if (config.isResumableCrawling()) { DatabaseConfig dbConfig =新的DatabaseConfig();dbConfig.setAllowCreate(真);dbConfig.setTransactional(真);dbConfig.setDeferredWrite(false);statisticsDB =env.openDatabase(空,“统计”,dbConfig);OperationStatus结果;DatabaseEntry key =新DatabaseEntry();DatabaseEntry值=新DatabaseEntry();Transaction tnx = env.beginTransaction(null,null);游标= statisticsDB.openCursor(tnx,空);结果=cursor.getFirst(键,值,空);而(结果== OperationStatus.SUCCESS) { if (value.getData().length > 0) { String name =新字符串(key.getData());long counterValue = Util.byteArray2Long(value.getData());counterValues.put(名称,counterValue);}cursor.getNext=cursor.close(键,值,空);} cursor.close();tnx.commit();}

据我所知,它保存了爬行URLS,这有助于在爬虫崩溃时,那么网络爬虫不需要从一开始就开始。请您逐行解释上面的代码,请.

2.我没有找到任何向我解释SleepyCat的好链接,因为Crawlers4j使用SleepyCat存储中间信息。所以请告诉我一些好的资源,在那里我可以学习SleepyCat的基础知识。(我不知道上面代码中使用的事务、游标的含义)。

请帮帮我。期待您的好意答复。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-07 08:54:07

基本上,Crawler4j通过从DB加载所有值,从数据库加载现有的统计信息。实际上,代码是非常不正确的,因为一个事务是打开的,并且没有对DB进行任何修改。因此,可以删除处理tnx的行。

评论逐行:

代码语言:javascript
复制
//Create a database configuration object 
DatabaseConfig dbConfig = new DatabaseConfig();
//Set some parameters : allow creation, set to transactional db and don't use deferred    write
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
dbConfig.setDeferredWrite(false);
//Open the database called "Statistics" with the upon created configuration
statisticsDB = env.openDatabase(null, "Statistics", dbConfig);

 OperationStatus result;
//Create new database entries key and values
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
//Start a transaction
    Transaction tnx = env.beginTransaction(null, null);
//Get the cursor on the DB
    Cursor cursor = statisticsDB.openCursor(tnx, null);
//Position the cursor to the first occurrence of key/value
    result = cursor.getFirst(key, value, null);
//While result is success
    while (result == OperationStatus.SUCCESS) {
//If the value at the current cursor position is not null, get the name and the value of     the counter and add it to the Hashmpa countervalues
        if (value.getData().length > 0) {
            String name = new String(key.getData());
            long counterValue = Util.byteArray2Long(value.getData());
            counterValues.put(name, counterValue);
        }
        result = cursor.getNext(key, value, null);
    }
    cursor.close();
//Commit the transaction, changes will be operated on th DB
    tnx.commit();

我还回答了一个类似的问题,这里。关于SleepyCat,你是在说吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16608790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档