package org.apache.cassandra.index.sasi.conf;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.index.sasi.SSTableIndex;
import org.apache.cassandra.index.sasi.conf.view.View;
import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataTracker
{
private static final Logger logger = LoggerFactory.getLogger(DataTracker.class);
private final AbstractType<?> keyValidator;
private final ColumnIndex columnIndex;
private final AtomicReference<View> view = new AtomicReference<>();
public DataTracker(AbstractType<?> keyValidator, ColumnIndex index)
{
this.keyValidator = keyValidator;
this.columnIndex = index;
this.view.set(new View(index, Collections.<SSTableIndex>emptySet()));
}
public View getView()
{
return view.get();
}
public Iterable<SSTableReader> update(Collection<SSTableReader> oldSSTables, Collection<SSTableReader> newSSTables)
{
final Pair<Set<SSTableIndex>, Set<SSTableReader>> built = getBuiltIndexes(newSSTables);
final Set<SSTableIndex> newIndexes = built.left;
final Set<SSTableReader> indexedSSTables = built.right;
View currentView, newView;
do
{
currentView = view.get();
newView = new View(columnIndex, currentView.getIndexes(), oldSSTables, newIndexes);
}
while (!view.compareAndSet(currentView, newView));
return newSSTables.stream().filter(sstable -> !indexedSSTables.contains(sstable)).collect(Collectors.toList());
}
public boolean hasSSTable(SSTableReader sstable)
{
View currentView = view.get();
for (SSTableIndex index : currentView)
{
if (index.getSSTable().equals(sstable))
return true;
}
return false;
}
public void dropData(Collection<SSTableReader> sstablesToRebuild)
{
View currentView = view.get();
if (currentView == null)
return;
Set<SSTableReader> toRemove = new HashSet<>(sstablesToRebuild);
for (SSTableIndex index : currentView)
{
SSTableReader sstable = index.getSSTable();
if (!sstablesToRebuild.contains(sstable))
continue;
index.markObsolete();
}
update(toRemove, Collections.<SSTableReader>emptyList());
}
public void dropData(long truncateUntil)
{
View currentView = view.get();
if (currentView == null)
return;
Set<SSTableReader> toRemove = new HashSet<>();
for (SSTableIndex index : currentView)
{
SSTableReader sstable = index.getSSTable();
if (sstable.getMaxTimestamp() > truncateUntil)
continue;
index.markObsolete();
toRemove.add(sstable);
}
update(toRemove, Collections.<SSTableReader>emptyList());
}
private Pair<Set<SSTableIndex>, Set<SSTableReader>> getBuiltIndexes(Collection<SSTableReader> sstables)
{
Set<SSTableIndex> indexes = new HashSet<>(sstables.size());
Set<SSTableReader> builtSSTables = new HashSet<>(sstables.size());
for (SSTableReader sstable : sstables)
{
if (sstable.isMarkedCompacted())
continue;
File indexFile = new File(sstable.descriptor.filenameFor(columnIndex.getComponent()));
if (!indexFile.exists())
continue;
if (indexFile.length() == 0)
{
builtSSTables.add(sstable);
continue;
}
SSTableIndex index = null;
try
{
index = new SSTableIndex(columnIndex, indexFile, sstable);
logger.info("SSTableIndex.open(column: {}, minTerm: {}, maxTerm: {}, minKey: {}, maxKey: {}, sstable: {})",
columnIndex.getColumnName(),
columnIndex.getValidator().getString(index.minTerm()),
columnIndex.getValidator().getString(index.maxTerm()),
keyValidator.getString(index.minKey()),
keyValidator.getString(index.maxKey()),
index.getSSTable());
if (indexes.add(index))
builtSSTables.add(sstable);
else
index.release();
}
catch (Throwable t)
{
logger.error("Can't open index file at " + indexFile.getAbsolutePath() + ", skipping.", t);
if (index != null)
index.release();
}
}
return Pair.create(indexes, builtSSTables);
}
}