Skip to content

Instantly share code, notes, and snippets.

@kishoreg
Created August 16, 2019 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishoreg/71896f7eda799ea71f55a4a747ed9125 to your computer and use it in GitHub Desktop.
Save kishoreg/71896f7eda799ea71f55a4a747ed9125 to your computer and use it in GitHub Desktop.
Indexed Table Impl
class DefaultIndexedTable implements IndexedTable {
List<TableRecord> rows = new ArrayList<>();
Map<TableRecord, Integer> lookUpTable;
private DataSchema _schema;
private String[] _keyColumns;
private String[] _orderByColumn;
private int _maxCapacity;
private int _reorderThreshold;
@Override
public void init(DataSchema schema, String[] keyColumns, String[] orderByColumn, int maxCapacity) {
_schema = schema;
_keyColumns = keyColumns;
_orderByColumn = orderByColumn;
_maxCapacity = maxCapacity;
_reorderThreshold = (int) (_maxCapacity * .75);
lookUpTable = new HashMap<>(maxCapacity);
}
@Override
public boolean upsert(TableRecord newRecord) {
Integer rowIndex = lookUpTable.get(newRecord);
if (rowIndex != null) {
TableRecord existingRecord = rows.get(rowIndex);
aggregate(existingRecord, newRecord);
if (rows.size() > _maxCapacity) {
//sort and drop the rows below threshold
}
} else {
rows.add(newRecord);
lookUpTable.put(newRecord, rows.size());
}
return true;
}
/**
* aggregate the values
* @param existingRecord
* @param newRecord
*/
private void aggregate(TableRecord existingRecord, TableRecord newRecord) {
//TODO: merge the values
}
@Override
public boolean merge(IndexedTable table) {
//TODO
return false;
}
@Override
public int size() {
return rows.size();
}
@Override
public Iterator<TableRecord> iterator() {
return rows.iterator();
}
@Override
public boolean sort() {
//TODO:
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment