leveldb-impl:filename()

filename定义了fileType

public enum FileType
    {
        LOG,
        DB_LOCK,
        TABLE,
        DESCRIPTOR,
        CURRENT,
        TEMP,
        INFO_LOG  // Either the current one, or an old one
    }

对以上fileType设置name和number

/**
     * Return the name of the log file with the specified number.
     */
    public static String logFileName(long number)
    {
        return makeFileName(number, "log");
    }

    /**
     * Return the name of the sstable with the specified number.
     */
    public static String tableFileName(long number)
    {
        return makeFileName(number, "sst");
    }

    /**
     * Return the name of the descriptor file with the specified incarnation number.
     */
    public static String descriptorFileName(long number)
    {
        checkArgument(number >= 0, "number is negative");
        return String.format("MANIFEST-%06d", number);
    }

    /**
     * Return the name of the current file.
     */
    public static String currentFileName()
    {
        return "CURRENT";
    }

    /**
     * Return the name of the lock file.
     */
    public static String lockFileName()
    {
        return "LOCK";
    }

    /**
     * Return the name of a temporary file with the specified number.
     */
    public static String tempFileName(long number)
    {
        return makeFileName(number, "dbtmp");
    }

    /**
     * Return the name of the info log file.
     */
    public static String infoLogFileName()
    {
        return "LOG";
    }

    /**
     * Return the name of the old info log file.
     */
    public static String oldInfoLogFileName()
    {
        return "LOG.old";
    }

如果 filename 是 leveldb 文件,则将文件的类型存储在 type 中。文件名中编码的数字存储在数字中。如果文件名被成功解析,则返回 true。否则返回假。

public static FileInfo parseFileName(File file)
    {
        // Owned filenames have the form:
        //    dbname/CURRENT
        //    dbname/LOCK
        //    dbname/LOG
        //    dbname/LOG.old
        //    dbname/MANIFEST-[0-9]+
        //    dbname/[0-9]+.(log|sst|dbtmp)
        String fileName = file.getName();
        if ("CURRENT".equals(fileName)) {
            return new FileInfo(FileType.CURRENT);
        }
        else if ("LOCK".equals(fileName)) {
            return new FileInfo(FileType.DB_LOCK);
        }
        else if ("LOG".equals(fileName)) {
            return new FileInfo(FileType.INFO_LOG);
        }
        else if ("LOG.old".equals(fileName)) {
            return new FileInfo(FileType.INFO_LOG);
        }
        else if (fileName.startsWith("MANIFEST-")) {
            long fileNumber = Long.parseLong(removePrefix(fileName, "MANIFEST-"));
            return new FileInfo(FileType.DESCRIPTOR, fileNumber);
        }
        else if (fileName.endsWith(".log")) {
            long fileNumber = Long.parseLong(removeSuffix(fileName, ".log"));
            return new FileInfo(FileType.LOG, fileNumber);
        }
        else if (fileName.endsWith(".sst")) {
            long fileNumber = Long.parseLong(removeSuffix(fileName, ".sst"));
            return new FileInfo(FileType.TABLE, fileNumber);
        }
        else if (fileName.endsWith(".dbtmp")) {
            long fileNumber = Long.parseLong(removeSuffix(fileName, ".dbtmp"));
            return new FileInfo(FileType.TEMP, fileNumber);
        }
        return null;
    }

使 CURRENT 文件指向具有指定编号的描述符文件,如果成功则返回 true;否则为假。

public static boolean setCurrentFile(File databaseDir, long descriptorNumber)
            throws IOException
    {
        String manifest = descriptorFileName(descriptorNumber);
        String temp = tempFileName(descriptorNumber);

        File tempFile = new File(databaseDir, temp);
        writeStringToFileSync(manifest + "\n", tempFile);

        File to = new File(databaseDir, currentFileName());
        boolean ok = tempFile.renameTo(to);
        if (!ok) {
            tempFile.delete();
            writeStringToFileSync(manifest + "\n", to);
        }
        return ok;
    }

将String写入file

private static void writeStringToFileSync(String str, File file)
            throws IOException
    {
        try (FileOutputStream stream = new FileOutputStream(file)) {
            stream.write(str.getBytes(UTF_8));
            stream.flush();
            stream.getFD().sync();
        }
    }

LOG文件,以.log结尾,存储文件名是db/000003.log,

该文件作为文件锁存在,文件名为LOCK,不保存任何东西。

DESCRIPTOR:这就是常说的manifest文件,以MANIFEST-开头。例如:db/MANIFEST-000002

CURRENT文件名就是CURRENT,里面的内容就是当前MANIFEST的文件名。

在使用新Manifest文件时,会创建一个dbtmp文件,然后rename成为真的,例如:db/000005.dbtmp。

LOG INFO文件名为LOG,当下次启动时,会以后缀LOG.old保存之前的LOG数据,新数据还会在LOG中。

FileNumber,每个文件会对应一个数字,且独一无二,不会出现MANIFEST-000002、000002.log这种情况,是全局递增的

在该函数中,会创建dbname+6位number+后缀组成的文件名。例如:testname=testdb,文件名可以是:testdb/000005.ldb ldb与sst都是sstable的有效后缀

private static String makeFileName(long number, String suffix)
    {
        checkArgument(number >= 0, "number is negative");
        requireNonNull(suffix, "suffix is null");
        return String.format("%06d.%s", number, suffix);
    }

去掉prefix和suffix

private static String removePrefix(String value, String prefix)
    {
        return value.substring(prefix.length());
    }

    private static String removeSuffix(String value, String suffix)
    {
        return value.substring(0, value.length() - suffix.length());
    }
————————

filename定义了fileType

public enum FileType
    {
        LOG,
        DB_LOCK,
        TABLE,
        DESCRIPTOR,
        CURRENT,
        TEMP,
        INFO_LOG  // Either the current one, or an old one
    }

对以上fileType设置name和number

/**
     * Return the name of the log file with the specified number.
     */
    public static String logFileName(long number)
    {
        return makeFileName(number, "log");
    }

    /**
     * Return the name of the sstable with the specified number.
     */
    public static String tableFileName(long number)
    {
        return makeFileName(number, "sst");
    }

    /**
     * Return the name of the descriptor file with the specified incarnation number.
     */
    public static String descriptorFileName(long number)
    {
        checkArgument(number >= 0, "number is negative");
        return String.format("MANIFEST-%06d", number);
    }

    /**
     * Return the name of the current file.
     */
    public static String currentFileName()
    {
        return "CURRENT";
    }

    /**
     * Return the name of the lock file.
     */
    public static String lockFileName()
    {
        return "LOCK";
    }

    /**
     * Return the name of a temporary file with the specified number.
     */
    public static String tempFileName(long number)
    {
        return makeFileName(number, "dbtmp");
    }

    /**
     * Return the name of the info log file.
     */
    public static String infoLogFileName()
    {
        return "LOG";
    }

    /**
     * Return the name of the old info log file.
     */
    public static String oldInfoLogFileName()
    {
        return "LOG.old";
    }

如果 filename 是 leveldb 文件,则将文件的类型存储在 type 中。文件名中编码的数字存储在数字中。如果文件名被成功解析,则返回 true。否则返回假。

public static FileInfo parseFileName(File file)
    {
        // Owned filenames have the form:
        //    dbname/CURRENT
        //    dbname/LOCK
        //    dbname/LOG
        //    dbname/LOG.old
        //    dbname/MANIFEST-[0-9]+
        //    dbname/[0-9]+.(log|sst|dbtmp)
        String fileName = file.getName();
        if ("CURRENT".equals(fileName)) {
            return new FileInfo(FileType.CURRENT);
        }
        else if ("LOCK".equals(fileName)) {
            return new FileInfo(FileType.DB_LOCK);
        }
        else if ("LOG".equals(fileName)) {
            return new FileInfo(FileType.INFO_LOG);
        }
        else if ("LOG.old".equals(fileName)) {
            return new FileInfo(FileType.INFO_LOG);
        }
        else if (fileName.startsWith("MANIFEST-")) {
            long fileNumber = Long.parseLong(removePrefix(fileName, "MANIFEST-"));
            return new FileInfo(FileType.DESCRIPTOR, fileNumber);
        }
        else if (fileName.endsWith(".log")) {
            long fileNumber = Long.parseLong(removeSuffix(fileName, ".log"));
            return new FileInfo(FileType.LOG, fileNumber);
        }
        else if (fileName.endsWith(".sst")) {
            long fileNumber = Long.parseLong(removeSuffix(fileName, ".sst"));
            return new FileInfo(FileType.TABLE, fileNumber);
        }
        else if (fileName.endsWith(".dbtmp")) {
            long fileNumber = Long.parseLong(removeSuffix(fileName, ".dbtmp"));
            return new FileInfo(FileType.TEMP, fileNumber);
        }
        return null;
    }

使 CURRENT 文件指向具有指定编号的描述符文件,如果成功则返回 true;否则为假。

public static boolean setCurrentFile(File databaseDir, long descriptorNumber)
            throws IOException
    {
        String manifest = descriptorFileName(descriptorNumber);
        String temp = tempFileName(descriptorNumber);

        File tempFile = new File(databaseDir, temp);
        writeStringToFileSync(manifest + "\n", tempFile);

        File to = new File(databaseDir, currentFileName());
        boolean ok = tempFile.renameTo(to);
        if (!ok) {
            tempFile.delete();
            writeStringToFileSync(manifest + "\n", to);
        }
        return ok;
    }

将String写入file

private static void writeStringToFileSync(String str, File file)
            throws IOException
    {
        try (FileOutputStream stream = new FileOutputStream(file)) {
            stream.write(str.getBytes(UTF_8));
            stream.flush();
            stream.getFD().sync();
        }
    }

LOG文件,以.log结尾,存储文件名是db/000003.log,

该文件作为文件锁存在,文件名为LOCK,不保存任何东西。

DESCRIPTOR:这就是常说的manifest文件,以MANIFEST-开头。例如:db/MANIFEST-000002

CURRENT文件名就是CURRENT,里面的内容就是当前MANIFEST的文件名。

在使用新Manifest文件时,会创建一个dbtmp文件,然后rename成为真的,例如:db/000005.dbtmp。

LOG INFO文件名为LOG,当下次启动时,会以后缀LOG.old保存之前的LOG数据,新数据还会在LOG中。

FileNumber,每个文件会对应一个数字,且独一无二,不会出现MANIFEST-000002、000002.log这种情况,是全局递增的

在该函数中,会创建dbname+6位number+后缀组成的文件名。例如:testname=testdb,文件名可以是:testdb/000005.ldb ldb与sst都是sstable的有效后缀

private static String makeFileName(long number, String suffix)
    {
        checkArgument(number >= 0, "number is negative");
        requireNonNull(suffix, "suffix is null");
        return String.format("%06d.%s", number, suffix);
    }

去掉prefix和suffix

private static String removePrefix(String value, String prefix)
    {
        return value.substring(prefix.length());
    }

    private static String removeSuffix(String value, String suffix)
    {
        return value.substring(0, value.length() - suffix.length());
    }