com.kolibrifx.plovercrest.server.internal.TableInfoSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plovercrest-server Show documentation
Show all versions of plovercrest-server Show documentation
Plovercrest server library.
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.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.client.TableMetadata;
import com.kolibrifx.plovercrest.client.internal.shared.PlovercrestConstants;
import com.kolibrifx.plovercrest.client.internal.shared.TableMetadataUtils;
import com.kolibrifx.plovercrest.server.TableInfo;
public final class TableInfoSerializer {
private static final String FANOUT_DISTANCE_FIELD = "com.kolibrifx.plovercrest.fanout-distance";
private static final String TABLE_NAME_FIELD = "com.kolibrifx.plovercrest.table-name";
// Prefix that indicates that the (unsanitized) file name may not be the same as the table name.
// For all file names that start with this prefix, the plovercrest engine must read contents of
// the .info file to find the actual table name. This can be considerably slower than just using
// the file name.
private static final String LONG_NAME_FILENAME_INDICATOR = "__";
private TableInfoSerializer() {
}
public static void write(final TableInfo info, final File infoFile) throws IOException {
final Map builtinFields = new HashMap<>();
builtinFields.put(FANOUT_DISTANCE_FIELD, Integer.toString(info.getFanOutDistance()));
if (!info.getName().equals(unsanitizedTableName(infoFile))) {
builtinFields.put(TABLE_NAME_FIELD, info.getName());
}
final TableMetadata metadata;
if (info.getMetadata() == null) {
metadata = new TableMetadata(builtinFields);
} else {
metadata = info.getMetadata().addFields(builtinFields);
}
FileUtils.write(infoFile, metadata.toPersistableString(), Charsets.UTF_8);
}
public static TableInfo read(final File infoFile) throws IOException {
final String metadataStr = FileUtils.readFileToString(infoFile, Charsets.UTF_8);
final TableMetadata metadata = TableMetadataUtils.parse(metadataStr);
final int fanoutDistance =
metadata.tryGetInt(FANOUT_DISTANCE_FIELD, PlovercrestConstants.DEFAULT_FANOUT_DISTANCE);
final String tableName = metadata.containsKey(TABLE_NAME_FIELD) ? metadata.get(TABLE_NAME_FIELD)
: unsanitizedTableName(infoFile);
return new TableInfo(tableName, fanoutDistance, metadata);
}
private static String unsanitizedTableName(final File infoFile) {
final String saneName = FilenameUtils.removeExtension(infoFile.getName());
final String tableName = PathUtils.unsanitize(saneName);
return tableName;
}
public static String tableNameFromInfoFile(final File infoFile) {
if (infoFile.getName().startsWith(LONG_NAME_FILENAME_INDICATOR)) {
try {
return read(infoFile).getName();
} catch (final IOException e) {
throw new PlovercrestException("Failed to read table name from info file " + infoFile, e);
}
}
return unsanitizedTableName(infoFile);
}
public static String generateUniqueFileNamePrefix() {
return LONG_NAME_FILENAME_INDICATOR + UUID.randomUUID().toString();
}
}