001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.cpd.index;
021    
022    import java.util.Collection;
023    import java.util.Collections;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.sonar.api.database.model.Snapshot;
028    import org.sonar.api.resources.Project;
029    import org.sonar.api.resources.Resource;
030    import org.sonar.batch.index.ResourcePersister;
031    import org.sonar.duplications.block.Block;
032    import org.sonar.duplications.block.ByteArray;
033    import org.sonar.persistence.dao.DuplicationDao;
034    import org.sonar.persistence.model.DuplicationUnit;
035    
036    import com.google.common.collect.Lists;
037    import com.google.common.collect.Maps;
038    
039    public class DbDuplicationsIndex {
040    
041      private final Map<ByteArray, Collection<Block>> cache = Maps.newHashMap();
042    
043      private final ResourcePersister resourcePersister;
044      private final int currentProjectSnapshotId;
045      private final Integer lastSnapshotId;
046    
047      private DuplicationDao dao;
048    
049      public DbDuplicationsIndex(ResourcePersister resourcePersister, Project currentProject, DuplicationDao dao) {
050        this.dao = dao;
051        this.resourcePersister = resourcePersister;
052        Snapshot currentSnapshot = resourcePersister.getSnapshotOrFail(currentProject);
053        Snapshot lastSnapshot = resourcePersister.getLastSnapshot(currentSnapshot, false);
054        this.currentProjectSnapshotId = currentSnapshot.getId();
055        this.lastSnapshotId = lastSnapshot == null ? null : lastSnapshot.getId();
056      }
057    
058      /**
059       * For tests.
060       */
061      DbDuplicationsIndex(DuplicationDao dao, ResourcePersister resourcePersister, Integer currentProjectSnapshotId, Integer prevSnapshotId) {
062        this.dao = dao;
063        this.resourcePersister = resourcePersister;
064        this.currentProjectSnapshotId = currentProjectSnapshotId;
065        this.lastSnapshotId = prevSnapshotId;
066      }
067    
068      int getSnapshotIdFor(Resource resource) {
069        return resourcePersister.getSnapshotOrFail(resource).getId();
070      }
071    
072      public void prepareCache(Resource resource) {
073        int resourceSnapshotId = getSnapshotIdFor(resource);
074        List<DuplicationUnit> units = dao.selectCandidates(resourceSnapshotId, lastSnapshotId);
075        cache.clear();
076        // TODO Godin: maybe remove conversion of units to blocks?
077        for (DuplicationUnit unit : units) {
078          String hash = unit.getHash();
079          String resourceKey = unit.getResourceKey();
080          int indexInFile = unit.getIndexInFile();
081          int startLine = unit.getStartLine();
082          int endLine = unit.getEndLine();
083    
084          // TODO Godin: in fact we could work directly with id instead of key - this will allow to decrease memory consumption
085          Block block = new Block(resourceKey, new ByteArray(hash), indexInFile, startLine, endLine);
086    
087          // Group blocks by hash
088          Collection<Block> sameHash = cache.get(block.getBlockHash());
089          if (sameHash == null) {
090            sameHash = Lists.newArrayList();
091            cache.put(block.getBlockHash(), sameHash);
092          }
093          sameHash.add(block);
094        }
095      }
096    
097      public Collection<Block> getByHash(ByteArray hash) {
098        Collection<Block> result = cache.get(hash);
099        if (result != null) {
100          return result;
101        } else {
102          return Collections.emptyList();
103        }
104      }
105    
106      public void insert(Resource resource, Collection<Block> blocks) {
107        int resourceSnapshotId = getSnapshotIdFor(resource);
108    
109        // TODO Godin: maybe remove conversion of blocks to units?
110        List<DuplicationUnit> units = Lists.newArrayList();
111        for (Block block : blocks) {
112          DuplicationUnit unit = new DuplicationUnit(
113              currentProjectSnapshotId,
114              resourceSnapshotId,
115              block.getBlockHash().toString(),
116              block.getIndexInFile(),
117              block.getFirstLineNumber(),
118              block.getLastLineNumber());
119          units.add(unit);
120        }
121    
122        dao.insert(units);
123      }
124    
125    }