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

com.kolibrifx.plovercrest.server.internal.DeferredTableInfo Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
 */

package com.kolibrifx.plovercrest.server.internal;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
import com.kolibrifx.common.Safety;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.server.TableInfo;

/**
 * TableInfo wrapper that delays reading the .info file until the first time it is needed. This
 * strategy speeds up plovercrest startup a lot if there are hundreds of thousands of tables.
 */
public class DeferredTableInfo {
    private final File file;
    private final AtomicReference info = new AtomicReference<>(null);
    private final String name;

    public DeferredTableInfo(final File file) {
        Safety.argumentNotNull("file", file);
        this.file = file;
        this.name = TableInfoSerializer.tableNameFromInfoFile(file);
    }

    public TableInfo getInfo() {
        if (info.get() == null) {
            // A harmless thread race condition could cause TableInfoSerializer.read()
            // to be called more than once.
            try {
                info.compareAndSet(null, TableInfoSerializer.read(file));
            } catch (final IOException e) {
                throw new PlovercrestException("Failed to read table info file: " + file, e);
            }
        }
        return info.get();
    }

    public String getName() {
        return name;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy