Package com.ourlinc.conference

Source Code of com.ourlinc.conference.DataSource

package com.ourlinc.conference;

import java.io.File;
import java.util.HashMap;

import com.ourlinc.omni.persistence.MappedAnnotation;
import com.ourlinc.omni.persistence.Persistence;
import com.ourlinc.omni.persistence.Persister;
import com.ourlinc.omni.persistence.PersisterSet;
import com.ourlinc.omni.persistence.PersisterStorage;
import com.ourlinc.omni.search.Searcher;
import com.ourlinc.omni.search.quick.QuickSearcher;
import com.ourlinc.omni.storage.Storage;
import com.ourlinc.omni.storage.StorageManager;
import com.ourlinc.swift.Flusher;
import com.ourlinc.swift.Shutdown;
import com.ourlinc.swift.ThreadFlusher;
import com.ourlinc.swift.exception.NotFindException;
import com.ourlinc.swift.exception.TransactionException;
import com.ourlinc.swift.util.Misc;

/**
* 用于统一管理系统的各种数据源,减少配置文件的信息量,同时也提供了一个集中的数据管理中心
*
* @author liangyi
*
*/
public class DataSource extends PersisterSet {
  public static final String TICKER_COUNTER = "ticker"; // 用于临时存储的计数器

  final StorageManager m_StorageManager;
  final HashMap<String, Searcher> m_Searchrs;
 
  final ThreadFlusher m_Flusher; // 基于线程的刷写器(在每个页面请求结束后统一执行)
   
  /*final DataProvider m_dpTrafficPlanIndexer;
  final DataProvider m_dpCarpoolIndexer;
  final DelayFlusher m_DelayFlusher = new DelayFlusher(); // 延时刷写缓存对象
  final DataProvider m_dpCounter; // 计数器数据库*/
  final String m_dbPath;

  public DataSource(StorageManager sm, ThreadFlusher flusher, String dbPath) {
    m_StorageManager = sm;
    m_Flusher = flusher;
    m_Searchrs = new HashMap<String, Searcher>();

    if (null == dbPath) {
      dbPath = "";
    } else if (dbPath.length() > 0 && File.separatorChar != dbPath.charAt(dbPath.length() - 1)) {
      dbPath += File.separatorChar;
    }
    m_dbPath = dbPath;
  }
 
  public Flusher getFlusher() {
    return m_Flusher;
  }
 
  public <E extends Persistence> PersisterStorage<E> newPersister(Class<E> classOf, PodiBase podi) {
    return newPersister(getStorage(classOf.getSimpleName()), classOf, podi);
  }

  synchronized public <E extends Persistence> PersisterStorage<E> newPersister(Storage storage,
      Class<E> classOf, PodiBase podi) {
    Persister<E> check = getPersister(classOf);
    if (null != check) {
      throw new TransactionException("已有同名的存储器:" + check);
    }

    PersisterStorage<E> persister = new PersisterStorage<E>(storage, MappedAnnotation
        .getInstance(classOf, this, podi), classOf);
    persister.setFlusher(m_Flusher);
    regsiter(persister);
    return persister;
  }

  /*
    public <E extends ObjectBase<? extends PodiBase>> PersisterStorage<E> getPersister(
        Class<E> classOf) {
      return (PersisterStorage<E>) super.getPersister(classOf);
    }
  */
  // 取得存储器
  public Storage getStorage(String name) {
    return m_StorageManager.getStorage(name.toLowerCase());
  }

  // 取得搜索器
  public Searcher getSearcher(String name) {
    Searcher p = m_Searchrs.get(name);
    if (null != p)
      return p;

    throw new NotFindException("没能找到名为'" + name + "'的查询器");
  }

  // 取得持久器
  public PersisterSet getPersisters() {
    return this;
 

  // 在关闭系统时安全地执行清除缓存等处理
  public void shutdown() {
    Shutdown.shutdown();
 

  /**
   * 创建指定名称的查询器
   *
   * @param name
   *            查询器名
   * @return 查询器
   */
  synchronized public Searcher newSearcher(String name) {
    Searcher check = m_Searchrs.get(name);
    if (null != check) {
      // throw new TransactionException("已有同名的查询器:" + check);
      if (Misc.DebugEnabled) {
        Misc.getLogger().debug("已有同名的查询器:" + check, new Throwable());
      }
      return check;
    }
    QuickSearcher searcher = new QuickSearcher(m_dbPath + name + "_index.db");
    m_Searchrs.put(name, searcher);
    return searcher;
  }

 
}

TOP

Related Classes of com.ourlinc.conference.DataSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.