All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.cache.persistence.soplog.LevelTracker Maven / Gradle / Ivy

/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.cache.persistence.soplog;

import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Writer;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicLong;

import com.gemstone.gemfire.internal.cache.persistence.soplog.Compactor.CompactionTracker;
import com.gemstone.gemfire.internal.cache.persistence.soplog.Compactor.Fileset;

/**
 * A simple, non-robust file tracker for tracking soplogs by level.
 * 
 * @author bakera
 */
public class LevelTracker implements Fileset, CompactionTracker, Closeable {
  private final String name;
  private final File manifest;
  
  private final SortedMap> levels;
  private final AtomicLong file;
  
  public LevelTracker(String name, File manifest) throws IOException {
    this.name = name;
    this.manifest = manifest;
    file = new AtomicLong(0);
    
    levels = new TreeMap>();
    if (!manifest.exists()) {
      return;
    }
    
    LineNumberReader rdr = new LineNumberReader(new FileReader(manifest));
    try {
      String line;
      while ((line = rdr.readLine()) != null) {
        String[] parts = line.split(",");
        int level = Integer.parseInt(parts[0]);
        File f = new File(parts[1]);
        add(f, level);
      }
    } finally {
      rdr.close();
    }
  }
  
  @Override
  public SortedMap> recover() {
    return levels;
  }

  @Override
  public File getNextFilename() {
    return new File(manifest.getParentFile(),  name + "-" + System.currentTimeMillis() 
        + "-" + file.getAndIncrement() + ".soplog");
  }

  @Override
  public void fileAdded(File f, Integer attach) {
    add(f, attach);
  }

  @Override
  public void fileRemoved(File f, Integer attach) {
    levels.get(attach).remove(f);
  }

  @Override
  public void fileDeleted(File f) {
  }

  @Override
  public void close() throws IOException {
    Writer wtr = new FileWriter(manifest);
    try {
      for (Map.Entry> entry : levels.entrySet()) {
        for (File f : entry.getValue()) {
          wtr.write(entry.getKey() + "," + f + "\n");
        }
      }
    } finally {
      wtr.flush();
      wtr.close();
    }
  }
  
  private void add(File f, int level) {
    Set files = levels.get(level);
    if (files == null) {
      files = new HashSet();
      levels.put(level, files);
    }
    files.add(f);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy