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

org.apache.cassandra.io.util.FileStoreUtils Maven / Gradle / Ivy

Go to download

The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.

There is a newer version: 5.0-rc1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.cassandra.io.util;

import java.io.IOException;
import java.nio.file.FileStore;
import java.util.function.Consumer;

public class FileStoreUtils
{
    /**
     * Try and get the total space of the given filestore
     * @return long value of available space if no errors
     *         Long.MAX_VALUE if on a large file system that overflows
     *         0 on exception during IOToLongFunction
     */
    public static long tryGetSpace(FileStore filestore, PathUtils.IOToLongFunction getSpace)
    {
        return tryGetSpace(filestore, getSpace, ignore -> {});
    }

    public static long tryGetSpace(FileStore filestore, PathUtils.IOToLongFunction getSpace, Consumer orElse)
    {
        try
        {
            return handleLargeFileSystem(getSpace.apply(filestore));
        }
        catch (IOException e)
        {
            orElse.accept(e);
            return 0L;
        }
    }

    /**
     * Private constructor as the class contains only static methods.
     */
    private FileStoreUtils()
    {
    }

    /**
     * Handle large file system by returning {@code Long.MAX_VALUE} when the size overflows.
     * @param size returned by the Java's FileStore methods
     * @return the size or {@code Long.MAX_VALUE} if the size was bigger than {@code Long.MAX_VALUE}
     */
    private static long handleLargeFileSystem(long size)
    {
        return size < 0 ? Long.MAX_VALUE : size;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy