com.github.brandtg.switchboard.LogServerPurgeTask.java Source code

Java tutorial

Introduction

Here is the source code for com.github.brandtg.switchboard.LogServerPurgeTask.java

Source

/**
 * Copyright (C) 2015 Greg Brandt (brandt.greg@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.brandtg.switchboard;

import com.google.common.collect.ImmutableMultimap;

import io.dropwizard.servlets.tasks.Task;

import java.io.PrintWriter;
import java.util.Collection;

public class LogServerPurgeTask extends Task {
    private final LogIndex logIndex;

    public LogServerPurgeTask(LogIndex logIndex) {
        super("purge");
        this.logIndex = logIndex;
    }

    @Override
    public void execute(ImmutableMultimap<String, String> params, PrintWriter printWriter) throws Exception {
        String collection = getAndCheck(params, "collection");
        long startIndex = Long.valueOf(getAndCheck(params, "startIndex"));

        printWriter.println("Purging " + collection + " until " + startIndex);
        printWriter.flush();

        int rowCount = logIndex.purgeTo(collection, startIndex);

        printWriter.println("Purged " + rowCount + " entries ... Done!");
        printWriter.flush();
    }

    private static String getAndCheck(ImmutableMultimap<String, String> params, String name) {
        Collection<String> values = params.get(name);
        if (values == null || values.isEmpty()) {
            throw new IllegalArgumentException("Must provide " + name);
        }
        return values.iterator().next();
    }
}