/**
 * Created by The eXo Platform SAS.
 *
 * @author <a href="mailto:[email protected]">Sergey Kabashnyuk</a>
 * @version $Id: InMemoryIndexDataKeeperFactory.java 2 2010-02-04 17:21:49Z andrew00x $
 */
public class InMemoryIndexDataKeeperFactory extends LuceneIndexDataKeeperFactory {
  /** Class logger. */
  private static final Logger LOG = Logger.getLogger(InMemoryIndexDataKeeperFactory.class);

  /**
   * {@inheritDoc}
   *
   * @throws IndexException
   */
  public LuceneIndexDataManager createNewIndexDataKeeper(final IndexTransaction<Document> changes)
      throws IndexException {
    if (!(changes instanceof LoggedIndexTransactionImpl)) {
      throw new IndexException("Fail to create in memory storage for not loged transaction");
    }
    return new ReducibleInMemoryIndexDataKeeper((LoggedIndexTransactionImpl) changes);
  }

  /** {@inheritDoc} */
  public LuceneIndexDataManager merge(final Collection<LuceneIndexDataManager> chains)
      throws IndexException {
    final List<TransactionLog> transactionsLogs = new ArrayList<TransactionLog>();
    final List<Directory> mergeDirectorys = new ArrayList<Directory>();
    final Map<String, Document> documentsBuffer = new HashMap<String, Document>();
    final Map<String, Document> pendingBuffer = new HashMap<String, Document>();

    for (final IndexDataKeeper<Document> indexDataKeeper : chains) {
      final ReducibleInMemoryIndexDataKeeper reducibleInMemoryIndexDataKeeper =
          (ReducibleInMemoryIndexDataKeeper) indexDataKeeper;

      if (reducibleInMemoryIndexDataKeeper.getDocumentCount() > 0) {

        final RAMDirectory directory =
            (RAMDirectory) reducibleInMemoryIndexDataKeeper.getDirectory();
        if (directory.sizeInBytes() > 0) {
          mergeDirectorys.add(directory);
        }
        pendingBuffer.putAll(reducibleInMemoryIndexDataKeeper.getPendingDocumentsBuffer());
        documentsBuffer.putAll(reducibleInMemoryIndexDataKeeper.getDocumentsBuffer());
        transactionsLogs.add(reducibleInMemoryIndexDataKeeper.getTransactionLog());
      }
    }
    LuceneIndexDataManager reducibleInMemoryIndexDataKeeper = null;
    try {
      RAMDirectory newDirectory = null;

      if (mergeDirectorys.size() > 0) {

        newDirectory = new RAMDirectory();
        final IndexWriter newWriter =
            new IndexWriter(newDirectory, new StandardAnalyzer(), MaxFieldLength.UNLIMITED);
        final Directory[] dirsToMerge = new Directory[mergeDirectorys.size()];
        newWriter.addIndexesNoOptimize(mergeDirectorys.toArray(dirsToMerge));
        newWriter.optimize();
        newWriter.close();

        //
      } else {
        newDirectory = new RAMDirectory();
      }
      reducibleInMemoryIndexDataKeeper =
          new ReducibleInMemoryIndexDataKeeper(
              newDirectory,
              documentsBuffer,
              pendingBuffer,
              new CompositeTransactionLog(transactionsLogs));

    } catch (final IOException e) {
      throw new IndexException(e.getLocalizedMessage(), e);
    } catch (final TransactionLogException e) {
      throw new IndexException(e.getLocalizedMessage(), e);
    }

    return reducibleInMemoryIndexDataKeeper;
  }
}
示例#2
0
/**
 * @author <a href="mailto:[email protected]">Andrey Parfonov</a>
 * @version $Id: FolderDataImpl.java 1197 2010-05-28 08:15:37Z [email protected] $
 */
class FolderDataImpl extends BaseObjectData implements FolderData {

  private static final Logger LOG = Logger.getLogger(FolderDataImpl.class);

  public FolderDataImpl(Entry entry, TypeDefinition type, StorageImpl storage) {
    super(entry, type, storage);
  }

  /** {@inheritDoc} */
  public void addObject(ObjectData object) throws ConstraintException {
    storage.children.get(getObjectId()).add(object.getObjectId());
    storage.parents.get(object.getObjectId()).add(getObjectId());
    storage.unfiled.remove(object.getObjectId());
    storage.indexListener.updated(object);
  }

  /** {@inheritDoc} */
  public ItemsIterator<ObjectData> getChildren(String orderBy) {

    Set<String> childrenIds = storage.children.get(getObjectId());
    List<ObjectData> children = new ArrayList<ObjectData>(childrenIds.size());
    for (String ch : childrenIds) {
      ObjectData object = null;
      try {
        object = storage.getObjectById(ch);
      } catch (ObjectNotFoundException e) {
        LOG.warn("Object " + ch + " not found in storage.");
        continue;
      }
      if (object.getBaseType() == BaseType.DOCUMENT && !((DocumentData) object).isLatestVersion()) {
        continue;
      }
      children.add(object);
    }

    return new BaseItemsIterator<ObjectData>(children);
  }

  /** {@inheritDoc} */
  public ContentStream getContentStream(String streamId) {
    // TODO : renditions for Folder object.
    // It may be XML or HTML representation direct child or full tree.
    return null;
  }

  /** {@inheritDoc} */
  public String getPath() {
    return calculatePath().toString();
  }

  /** {@inheritDoc} */
  public boolean hasChildren() {
    return storage.children.get(getObjectId()).size() > 0;
  }

  /** {@inheritDoc} */
  public boolean isAllowedChildType(String typeId) {
    Value value = entry.getValue(CmisConstants.ALLOWED_CHILD_OBJECT_TYPE_IDS);
    if (value != null
        && value.getStrings().length > 0
        && !Arrays.asList(value.getStrings()).contains(typeId)) {
      return false;
    }
    return true;
  }

  /** {@inheritDoc} */
  public boolean isRoot() {
    return StorageImpl.ROOT_FOLDER_ID.equals(getObjectId());
  }

  /** {@inheritDoc} */
  public void removeObject(ObjectData object) {
    storage.children.get(getObjectId()).remove(object.getObjectId());
    storage.parents.get(object.getObjectId()).remove(getObjectId());
    if (storage.parents.get(object.getObjectId()).size() == 0) {
      storage.unfiled.add(object.getObjectId());
    }
    storage.indexListener.updated(object);
  }

  private String calculatePath() {
    if (isRoot()) {
      return "/";
    }

    LinkedList<String> pathSegms = new LinkedList<String>();
    pathSegms.add(getName());

    try {
      FolderData parent = getParent();
      while (!parent.isRoot()) {
        pathSegms.addFirst(parent.getName());
        parent = parent.getParent();
      }
    } catch (ConstraintException e) {
      // Should not happen:
      // 1. this object is folder
      // 2. not root folder
      // 3. when traversing up always check is folder root
      throw new CmisRuntimeException("Unable get object path.", e);
    }

    StringBuilder path = new StringBuilder();
    path.append('/');
    for (String seg : pathSegms) {
      if (path.length() > 1) {
        path.append('/');
      }
      path.append(seg);
    }

    return path.toString();
  }

  /** {@inheritDoc} */
  protected void delete() throws UpdateConflictException, VersioningException, StorageException {
    TypeDefinition relationshipType = storage.types.get(CmisConstants.RELATIONSHIP);
    ItemsIterator<RelationshipData> relationships =
        getRelationships(RelationshipDirection.EITHER, relationshipType, true);
    if (relationships.hasNext()) {
      throw new StorageException(
          "Object can't be deleted cause to storage referential integrity. "
              + "Object is source or target at least one Relationship.");
    }

    String objectId = getObjectId();
    storage.entries.remove(objectId);
    for (String parent : storage.parents.get(objectId)) {
      storage.children.get(parent).remove(objectId);
    }
    storage.parents.remove(objectId);
    storage.children.remove(objectId);
  }
}