Java Code Examples for org.apache.distributedlog.api.DistributedLogManager#getLogRecordCount()

The following examples show how to use org.apache.distributedlog.api.DistributedLogManager#getLogRecordCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected int runCmd() throws Exception {
    DistributedLogManager dlm = getNamespace().openLog(getStreamName());
    long totalCount = dlm.getLogRecordCount();
    try {
        AsyncLogReader reader;
        Object startOffset;
        try {
            DLSN lastDLSN = FutureUtils.result(dlm.getLastDLSNAsync());
            System.out.println("Last DLSN : " + lastDLSN);
            if (null == fromDLSN) {
                reader = dlm.getAsyncLogReader(fromTxnId);
                startOffset = fromTxnId;
            } else {
                reader = dlm.getAsyncLogReader(fromDLSN);
                startOffset = fromDLSN;
            }
        } catch (LogNotFoundException lee) {
            System.out.println("No stream found to dump records.");
            return 0;
        }
        try {
            System.out.println(String.format("Dump records for %s (from = %s, dump"
                    + " count = %d, total records = %d)", getStreamName(), startOffset, count, totalCount));

            dumpRecords(reader);
        } finally {
            Utils.close(reader);
        }
    } finally {
        dlm.close();
    }
    return 0;
}
 
Example 2
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private int truncateStream(final Namespace namespace, String streamName, DLSN dlsn) throws Exception {
    DistributedLogManager dlm = namespace.openLog(streamName);
    try {
        long totalRecords = dlm.getLogRecordCount();
        long recordsAfterTruncate = FutureUtils.result(dlm.getLogRecordCountAsync(dlsn));
        long recordsToTruncate = totalRecords - recordsAfterTruncate;
        if (!getForce() && !IOUtils.confirmPrompt("Do you want to truncate "
                + streamName + " at dlsn " + dlsn + " (" + recordsToTruncate + " records)?")) {
            return 0;
        } else {
            AsyncLogWriter writer = dlm.startAsyncLogSegmentNonPartitioned();
            try {
                if (!FutureUtils.result(writer.truncate(dlsn))) {
                    System.out.println("Failed to truncate.");
                }
                return 0;
            } finally {
                Utils.close(writer);
            }
        }
    } catch (Exception ex) {
        System.err.println("Failed to truncate " + ex);
        return 1;
    } finally {
        dlm.close();
    }
}