use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class MemoryHashIndexManager method start.
@Override
public void start(LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
LOGGER.log(Level.SEVERE, "loading in memory all the keys for mem index {0}", new Object[] { index.name });
bootSequenceNumber = sequenceNumber;
if (LogSequenceNumber.START_OF_TIME.equals(sequenceNumber)) {
/* Empty index (booting from the start) */
LOGGER.log(Level.SEVERE, "loaded empty index {0}", new Object[] { index.name });
} else {
IndexStatus status;
try {
status = dataStorageManager.getIndexStatus(tableSpaceUUID, index.uuid, sequenceNumber);
} catch (DataStorageManagerException e) {
LOGGER.log(Level.SEVERE, "cannot load index {0} due to {1}, it will be rebuilt", new Object[] { index.name, e });
rebuild();
return;
}
for (long pageId : status.activePages) {
LOGGER.log(Level.SEVERE, "recovery index " + index.name + ", load " + pageId);
Map<Bytes, List<Bytes>> read = dataStorageManager.readIndexPage(tableSpaceUUID, index.uuid, pageId, in -> {
Map<Bytes, List<Bytes>> deserialized = new HashMap<>();
// version
long version = in.readVLong();
// flags for future implementations
long flags = in.readVLong();
if (version != 1 || flags != 0) {
throw new DataStorageManagerException("corrupted index page");
}
int size = in.readVInt();
for (int i = 0; i < size; i++) {
byte[] indexKey = in.readArray();
int entrySize = in.readVInt();
List<Bytes> value = new ArrayList<>(entrySize);
for (int kk = 0; kk < entrySize; kk++) {
byte[] tableKey = in.readArray();
value.add(Bytes.from_array(tableKey));
}
deserialized.put(Bytes.from_array(indexKey), value);
}
return deserialized;
});
data.putAll(read);
}
newPageId.set(status.newPageId);
LOGGER.log(Level.SEVERE, "loaded {0} keys for index {1}", new Object[] { data.size(), index.name });
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class MemoryDataStorageManager method writeIndexPage.
@Override
public void writeIndexPage(String tableSpace, String indexName, long pageId, DataWriter writer) throws DataStorageManagerException {
Bytes page_wrapper;
try (ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
ExtendedDataOutputStream eout = new ExtendedDataOutputStream(out)) {
writer.write(eout);
eout.flush();
page_wrapper = Bytes.from_array(out.toByteArray());
} catch (IOException ex) {
throw new DataStorageManagerException(ex);
}
Bytes prev = indexpages.putIfAbsent(tableSpace + "." + indexName + "_" + pageId, page_wrapper);
if (prev != null) {
throw new DataStorageManagerException("pages are immutable");
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class MemoryDataStorageManager method indexCheckpoint.
@Override
public List<PostCheckpointAction> indexCheckpoint(String tableSpace, String indexName, IndexStatus indexStatus, boolean pin) throws DataStorageManagerException {
/* Checkpoint pinning */
final Map<Long, Integer> pins = pinIndexAndGetPages(tableSpace, indexName, indexStatus, pin);
final Set<LogSequenceNumber> checkpoints = pinIndexAndGetCheckpoints(tableSpace, indexName, indexStatus, pin);
List<Long> pagesForIndex = new ArrayList<>();
String prefix = tableSpace + "." + indexName + "_";
for (String key : indexpages.keySet()) {
if (key.startsWith(prefix)) {
long pageId = Long.parseLong(key.substring(prefix.length()));
if (!pins.containsKey(pageId)) {
pagesForIndex.add(pageId);
}
}
}
pagesForIndex.removeAll(indexStatus.activePages);
List<PostCheckpointAction> result = new ArrayList<>();
for (long pageId : pagesForIndex) {
result.add(new PostCheckpointAction(indexName, "drop page " + pageId) {
@Override
public void run() {
// remove only after checkpoint completed
indexpages.remove(prefix + pageId);
}
});
}
for (String oldStatus : indexStatuses.keySet()) {
if (oldStatus.startsWith(prefix)) {
/* Check for checkpoint skip only if match expected structure */
final LogSequenceNumber log = evaluateLogSequenceNumber(prefix.substring(0, prefix.length()));
if (log != null) {
/* If is pinned skip this status*/
if (checkpoints.contains(log)) {
continue;
}
}
result.add(new PostCheckpointAction(indexName, "drop index checkpoint " + oldStatus) {
@Override
public void run() {
// remove only after checkpoint completed
indexStatuses.remove(oldStatus);
}
});
}
}
VisibleByteArrayOutputStream oo = new VisibleByteArrayOutputStream(1024);
try (ExtendedDataOutputStream dataOutputKeys = new ExtendedDataOutputStream(oo)) {
indexStatus.serialize(dataOutputKeys);
dataOutputKeys.flush();
oo.write(oo.xxhash64());
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
/* Uses a copy to limit byte[] size at the min needed */
indexStatuses.put(checkpointName(tableSpace, indexName, indexStatus.sequenceNumber), oo.toByteArray());
return result;
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class MemoryDataStorageManager method getIndexStatus.
@Override
public IndexStatus getIndexStatus(String tableSpace, String indexName, LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
final String checkPoint = checkpointName(tableSpace, indexName, sequenceNumber);
byte[] data = indexStatuses.get(checkPoint);
if (data == null) {
throw new DataStorageManagerException("no such index checkpoint: " + checkPoint);
}
try {
try (InputStream input = new SimpleByteArrayInputStream(data);
ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
return IndexStatus.deserialize(dataIn);
}
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class MemoryDataStorageManager method tableCheckpoint.
@Override
public List<PostCheckpointAction> tableCheckpoint(String tableSpace, String tableName, TableStatus tableStatus, boolean pin) throws DataStorageManagerException {
/* Checkpoint pinning */
final Map<Long, Integer> pins = pinTableAndGetPages(tableSpace, tableName, tableStatus, pin);
final Set<LogSequenceNumber> checkpoints = pinTableAndGetCheckpoints(tableSpace, tableName, tableStatus, pin);
List<Long> pagesForTable = new ArrayList<>();
String prefix = tableSpace + "." + tableName + "_";
for (String key : pages.keySet()) {
if (key.startsWith(prefix)) {
long pageId = Long.parseLong(key.substring(prefix.length()));
if (!pins.containsKey(pageId)) {
pagesForTable.add(pageId);
}
}
}
pagesForTable.removeAll(tableStatus.activePages.keySet());
List<PostCheckpointAction> result = new ArrayList<>();
for (long pageId : pagesForTable) {
result.add(new PostCheckpointAction(tableName, "drop page " + pageId) {
@Override
public void run() {
// remove only after checkpoint completed
pages.remove(prefix + pageId);
LOGGER.log(Level.SEVERE, "removing " + (prefix + pageId));
}
});
}
for (String oldStatus : tableStatuses.keySet()) {
if (oldStatus.startsWith(prefix)) {
/* Check for checkpoint skip only if match expected structure */
final LogSequenceNumber log = evaluateLogSequenceNumber(prefix.substring(0, prefix.length()));
if (log != null) {
/* If is pinned skip this status*/
if (checkpoints.contains(log)) {
continue;
}
}
result.add(new PostCheckpointAction(tableName, "drop table checkpoint " + oldStatus) {
@Override
public void run() {
// remove only after checkpoint completed
tableStatuses.remove(oldStatus);
}
});
}
}
VisibleByteArrayOutputStream oo = new VisibleByteArrayOutputStream(1024);
try (ExtendedDataOutputStream dataOutputKeys = new ExtendedDataOutputStream(oo)) {
tableStatus.serialize(dataOutputKeys);
dataOutputKeys.flush();
oo.write(oo.xxhash64());
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
/* Uses a copy to limit byte[] size at the min needed */
tableStatuses.put(checkpointName(tableSpace, tableName, tableStatus.sequenceNumber), oo.toByteArray());
return result;
}
Aggregations