君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理

连接器里面采用的什么样的数据结构,我们先从Document迭代器开始入手,具体的Document迭代器类都实现了DocumentList接口,该接口定义了两个方法

public interface DocumentList {

  public Document nextDocument() throws RepositoryException;

  public String checkpoint() throws RepositoryException;
}

前者用于获取Document对象,后者获取断点状态

上文中分析的DiffingConnectorDocumentList类即实现了DocumentList接口,从List<CheckpointAndChange> guaranteedChanges集合的迭代器中迭代获取CheckpointAndChange对象然后包装为Document类型对象

Document也是一接口类型

public interface Document {

  public Property findProperty(String name) throws RepositoryException;

  public Set<String> getPropertyNames() throws RepositoryException;
}

从Document接口定义的方法可以看出,Document接口类似于Map容器结构,如果进一步考察String类型的key对应的value类型Property,可以发现Document接口很类似于HashMap结构

public interface Property {

  public Value nextValue() throws RepositoryException;
}

下面继续考察Document接口的具体实现类,以JsonDocument类说明: 

/**
 *省略了其他部分成员属性及方法
 * A simple {@link Document} implementation created from a {@link JSONObject}.
 */
public class JsonDocument implements Document {

   private final Map<String, List<Value>> properties;

/**
   * Constructor used by {@link DBHandle} when deserializing a
   * {@code DocumentHandle} from the recovery file.
   */
  public JsonDocument(JSONObject jsonObject) {
    this(buildJsonProperties(jsonObject), jsonObject);
  }

  /**
   * Constructor used by the {@link DocumentBuilder} for creating a
   * {@link JsonDocument} object used by {@link RepositoryHandler}
   * for building a collection over JsonDocument.
   */
  public JsonDocument(Map<String, List<Value>> properties,
                      JSONObject jsonObject) {
    this.properties = properties;
    this.jsonObject = jsonObject;
    objectId = getSingleValueString(SpiConstants.PROPNAME_DOCID);
    if (Strings.isNullOrEmpty(objectId)) {
      throw new IllegalArgumentException(
          "Unable to parse for docID from the properties:" + properties);
    }
  }

@Override
  public Set<String> getPropertyNames() {
    return properties.keySet();
  }

  @Override
  public Property findProperty(String name) throws RepositoryException {
    List<Value> property = properties.get(name);
    if (name.equals(SpiConstants.PROPNAME_CONTENT) && filterMimeType()) {
        property = null;
    }
    return (property == null) ? null : new SimpleProperty(property);
  }

}

JsonDocument类还有什么好说的呢,内部实际是对Map<String, List<Value>> properties的封装

属性类型SimpleProperty实现了Property接口

/**
 * Simple implementation of the {@link Property} interface.
 * Implementors may use this directly or for reference.
 *
 * @since 1.0
 */
public class SimpleProperty implements Property {

  final Iterator<Value> iterator;

  /**
   * Constructs a property with a single value.
   *
   * @param value the property's {@link Value}
   * @since 2.4
   */
  public SimpleProperty(Value value) {
    this(Collections.singletonList(value));
  }

  /**
   * Constructs a property with multiple values.
   *
   * @param values a {@code List} of the property's {@link Value Values}
   */
  public SimpleProperty(List<Value> values) {
    this.iterator = values.iterator();
  }

  @Override
  public Value nextValue() {
    return (iterator.hasNext()) ? iterator.next() : null;
  }
}

成员属性final Iterator<Value> iterator保存值的迭代器,功能与HashMap的entry链表类似

---------------------------------------------------------------------------

本系列企业搜索引擎开发之连接器connector系本人原创

转载请注明出处 博客园 刺猬的温驯

本人邮箱: chenying998179@163#com (#改为.)

本文链接 http://www.cnblogs.com/chenying99/p/3789695.html 

posted on 2014-06-15 18:36  刺猬的温驯  阅读(520)  评论(0编辑  收藏  举报