|
| 1 | +package org.tron.common.storage; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import lombok.Getter; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.rocksdb.CompressionType; |
| 10 | + |
| 11 | +@Slf4j |
| 12 | +public class DBSettings { |
| 13 | + |
| 14 | + private static DBSettings settings; |
| 15 | + |
| 16 | + @Getter |
| 17 | + private int levelNumber; |
| 18 | + @Getter |
| 19 | + private int maxOpenFiles; |
| 20 | + @Getter |
| 21 | + private int compactThreads; |
| 22 | + @Getter |
| 23 | + private long blockSize; |
| 24 | + @Getter |
| 25 | + private long maxBytesForLevelBase; |
| 26 | + @Getter |
| 27 | + private double maxBytesForLevelMultiplier; |
| 28 | + @Getter |
| 29 | + private int level0FileNumCompactionTrigger; |
| 30 | + @Getter |
| 31 | + private List<CompressionType> compressionTypeList; |
| 32 | + @Getter |
| 33 | + private long targetFileSizeBase; |
| 34 | + @Getter |
| 35 | + private int targetFileSizeMultiplier; |
| 36 | + @Getter |
| 37 | + private boolean enableStatistics; |
| 38 | + |
| 39 | + private DBSettings() { |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + public static DBSettings getDefaultSettings() { |
| 44 | + DBSettings defaultSettings = new DBSettings(); |
| 45 | + return defaultSettings.withLevelNumber(7).withBlockSize(64).withCompactThreads(32) |
| 46 | + .withCompressionTypeList("no:no:no:lz4:lz4:zstd:zstd").withTargetFileSizeBase(256) |
| 47 | + .withMaxBytesForLevelMultiplier(10).withTargetFileSizeMultiplier(1).withMaxBytesForLevelBase(256).withMaxOpenFiles(-1) |
| 48 | + .withEnableStatistics(false); |
| 49 | + } |
| 50 | + |
| 51 | + public static DBSettings getSettings() { |
| 52 | + if (settings == null) { |
| 53 | + return getDefaultSettings(); |
| 54 | + } |
| 55 | + return settings; |
| 56 | + } |
| 57 | + |
| 58 | + public static DBSettings initCustomSettings(int levelNumber, int compactThreads, int blocksize, |
| 59 | + long maxBytesForLevelBase, |
| 60 | + double maxBytesForLevelMultiplier, String compressionStr, |
| 61 | + int level0FileNumCompactionTrigger, long targetFileSizeBase, int targetFileSizeMultiplier) { |
| 62 | + settings = new DBSettings() |
| 63 | + .withMaxOpenFiles(-1) |
| 64 | + .withEnableStatistics(false) |
| 65 | + .withLevelNumber(levelNumber) |
| 66 | + .withCompactThreads(compactThreads) |
| 67 | + .withBlockSize(blocksize) |
| 68 | + .withMaxBytesForLevelBase(maxBytesForLevelBase) |
| 69 | + .withMaxBytesForLevelMultiplier(maxBytesForLevelMultiplier) |
| 70 | + .withCompressionTypeList(compressionStr) |
| 71 | + .withLevel0FileNumCompactionTrigger(level0FileNumCompactionTrigger) |
| 72 | + .withTargetFileSizeBase(targetFileSizeBase) |
| 73 | + .withTargetFileSizeMultiplier(targetFileSizeMultiplier); |
| 74 | + return settings; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public DBSettings withMaxOpenFiles(int maxOpenFiles) { |
| 79 | + this.maxOpenFiles = maxOpenFiles; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public DBSettings withCompactThreads(int compactThreads) { |
| 84 | + this.compactThreads = compactThreads; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public DBSettings withBlockSize(long blockSize) { |
| 89 | + this.blockSize = blockSize * 1024; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public DBSettings withMaxBytesForLevelBase(long maxBytesForLevelBase) { |
| 94 | + this.maxBytesForLevelBase = maxBytesForLevelBase * 1024 * 1024; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public DBSettings withMaxBytesForLevelMultiplier(double maxBytesForLevelMultiplier) { |
| 99 | + this.maxBytesForLevelMultiplier = maxBytesForLevelMultiplier; |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public DBSettings withLevel0FileNumCompactionTrigger(int level0FileNumCompactionTrigger) { |
| 104 | + this.level0FileNumCompactionTrigger = level0FileNumCompactionTrigger; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public DBSettings withCompressionTypeList(String compressionTypeListString) { |
| 109 | + List<String> compressionTypeStringList = Arrays.asList(compressionTypeListString.split(":")); |
| 110 | + List<CompressionType> compressionTypeList = new ArrayList<>(); |
| 111 | + for (String str : compressionTypeStringList) { |
| 112 | + if (str.equals("no")) { |
| 113 | + compressionTypeList.add(CompressionType.NO_COMPRESSION); |
| 114 | + } else if (str.equals("lz4")) { |
| 115 | + compressionTypeList.add(CompressionType.LZ4_COMPRESSION); |
| 116 | + } else if (str.equals("zstd")) { |
| 117 | + compressionTypeList.add(CompressionType.ZSTD_COMPRESSION); |
| 118 | + } else if (str.equals("zlib")) { |
| 119 | + compressionTypeList.add(CompressionType.ZLIB_COMPRESSION); |
| 120 | + } |
| 121 | + } |
| 122 | + this.compressionTypeList = compressionTypeList; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public DBSettings withEnableStatistics(boolean enable) { |
| 127 | + this.enableStatistics = enable; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + public DBSettings withLevelNumber(int levelNumber) { |
| 132 | + this.levelNumber = levelNumber; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + public DBSettings withTargetFileSizeBase(long targetFileSizeBase) { |
| 138 | + this.targetFileSizeBase = targetFileSizeBase * 1024 * 1024; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public DBSettings withTargetFileSizeMultiplier(int targetFileSizeMultiplier) { |
| 143 | + this.targetFileSizeMultiplier = targetFileSizeMultiplier; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public static void loggingSettings() { |
| 148 | + logger.info(String.format( |
| 149 | + "level number: %d, CompactThreads: %d, Blocksize: %d, maxBytesForLevelBase: %d, withMaxBytesForLevelMultiplier: %f, level0FileNumCompactionTrigger: %d, withTargetFileSizeBase: %d, withTargetFileSizeMultiplier: %d", |
| 150 | + settings.getLevelNumber(), |
| 151 | + settings.getCompactThreads(), settings.getBlockSize(), settings.getMaxBytesForLevelBase(), |
| 152 | + settings.getMaxBytesForLevelMultiplier(), settings.getLevel0FileNumCompactionTrigger(), |
| 153 | + settings.getTargetFileSizeBase(), settings.getTargetFileSizeMultiplier())); |
| 154 | + } |
| 155 | +} |
0 commit comments