/*
 * Copyright 2018-2018 The Last Pickle Ltd
 *
 *
 * 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 io.cassandrareaper.storage;

import io.cassandrareaper.ReaperException;
import io.cassandrareaper.service.RingRange;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class JsonParseUtils {

  private static final Logger LOG = LoggerFactory.getLogger(JsonParseUtils.class);

  private JsonParseUtils() {
    throw new IllegalStateException("Utility class");
  }

  private static <T> T parseJson(String json, TypeReference<T> ref) {
    try {
      return new ObjectMapper().readValue(json, ref);
    } catch (IOException e) {
      LOG.error("error parsing json", e);
      throw new RuntimeException(e);
    }
  }

  public static List<RingRange> parseRingRangeList(Optional<String> json) {
    if (json.isPresent()) {
      return parseJson(json.get(), new TypeReference<List<RingRange>>() {});
    }

    return Lists.newArrayList();
  }

  public static String writeTokenRangesTxt(List<RingRange> tokenRanges) throws ReaperException {
    try {
      return new ObjectMapper().writeValueAsString(tokenRanges);
    } catch (JsonProcessingException e) {
      throw new ReaperException(e);
    }
  }
}

@@ -646,51 +646,8 @@
 e;%0A%0A
-import io.cassandrareaper.ReaperException;%0A
 impo
@@ -928,24 +928,128 @@
 jectMapper;%0A
+import com.fasterxml.jackson.databind.ObjectReader;%0Aimport com.fasterxml.jackson.databind.ObjectWriter;%0A
 import com.g
@@ -1269,560 +1269,570 @@
 ate 
-JsonParseUtils() %7B%0A    throw new IllegalStateException(%22Utility class%22);%0A  %7D%0A%0A  private static %3CT%3E T parseJson(String json, TypeReference%3CT%3E ref) %7B%0A    try %7B%0A      return new ObjectMapper().readValue(json, ref);%0A    %7D catch (IOException e) %7B%0A      LOG.error(%22error parsing json%22, e);%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D%0A%0A  public static List%3CRingRange%3E parseRingRangeList(Optional%3CString%3E json) %7B%0A    if (json.isPresent()) %7B%0A      return parseJson(json.get(), new TypeReference%3CList%3CRingRange%3E%3E() %7B%7D);%0A    %7D%0A%0A    return Lists.newArrayList();
+static final ObjectReader READER = new ObjectMapper().readerFor(new TypeReference%3CList%3CRingRange%3E%3E() %7B%7D);%0A  private static final ObjectWriter WRITER = new ObjectMapper().writer();%0A%0A  private JsonParseUtils() %7B%0A    throw new IllegalStateException(%22Utility class%22);%0A  %7D%0A%0A  public static List%3CRingRange%3E parseRingRangeList(Optional%3CString%3E json) %7B%0A    try %7B%0A      return json.isPresent() ? READER.readValue(json.get()) : Lists.newArrayList();%0A    %7D catch (IOException e) %7B%0A      LOG.error(%22error parsing json%22, e);%0A      throw new IllegalArgumentException(e);%0A    %7D
 %0A  %7D
@@ -1908,31 +1908,8 @@
 ges)
- throws ReaperException
  %7B%0A 
@@ -1930,34 +1930,22 @@
  return 
-new ObjectMapper()
+WRITER
 .writeVa
@@ -2032,14 +2032,23 @@
 new 
-Reaper
+IllegalArgument
 Exce

Improve performance of JsonParseUtils - was taking up 92% of calls to `storage.getNextFreeSegmentInRange(..)` - which is 81%of `RepairRunner.run()` - was taking up 99% of cpu time to `ClusterResource.getCluster(..)`