|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + encoder "github.com/codecrafters-io/redis-tester/internal/resp/encoder" |
| 10 | + value "github.com/codecrafters-io/redis-tester/internal/resp/value" |
| 11 | + "github.com/codecrafters-io/tester-utils/logger" |
| 12 | + "github.com/codecrafters-io/tester-utils/test_case_harness" |
| 13 | + "github.com/dustin/go-humanize/english" |
| 14 | +) |
| 15 | + |
| 16 | +// AofDirectoryCreator is used to create an append-only directory |
| 17 | +// Redis uses the same name for mannifest and the append-only file |
| 18 | +// Eg. foo.manifest and foo.1.incr.aof |
| 19 | +// But since this is used for testing user's code, to ensure that the users actually read from the |
| 20 | +// manifest file and parse the append-only file name, AppendFilenameFlag and |
| 21 | +// AppendOnlyFilenameInManifest can be specified separately |
| 22 | +type AofDirectoryCreator struct { |
| 23 | + DataDirectory string // directory inside which Aof directory is created |
| 24 | + AppendDirName string // Value of appendonlydir flag |
| 25 | + AppendFileNameInFlag string // Value of appendfilename (Used for manifest file name) |
| 26 | + AppendOnlyFileNameInManifest string // Value appendfilename to be used inside manifest |
| 27 | + CommandsInsideAppendOnlyFile [][]string // slice of commands to be written to append-only file |
| 28 | +} |
| 29 | + |
| 30 | +func (a *AofDirectoryCreator) Create(logger *logger.Logger) error { |
| 31 | + a.verifyMemberValues() |
| 32 | + |
| 33 | + appendDirPath := filepath.Join(a.DataDirectory, a.AppendDirName) |
| 34 | + manifestFileName := a.AppendFileNameInFlag + ".manifest" |
| 35 | + manifestFilePath := filepath.Join(appendDirPath, manifestFileName) |
| 36 | + actualAppendFileName := fmt.Sprintf("%s.1.incr.aof", a.AppendOnlyFileNameInManifest) |
| 37 | + actualAppendFilePath := filepath.Join(appendDirPath, actualAppendFileName) |
| 38 | + manifestFileEntry := fmt.Sprintf("file %s seq 1 type i", actualAppendFileName) |
| 39 | + |
| 40 | + if err := a.createAppendOnlyDirectory(logger, appendDirPath, manifestFileName, actualAppendFileName); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + if err := a.createAppendOnlyFile(logger, actualAppendFilePath); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + if err := a.createManifestFile(logger, manifestFilePath, manifestFileEntry); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func (a *AofDirectoryCreator) createAppendOnlyDirectory(logger *logger.Logger, appendDirPath, manifestFileName, actualAppendFileName string) error { |
| 56 | + logger.Infof("Creating append-only directory %q:", a.AppendDirName) |
| 57 | + |
| 58 | + logger.WithAdditionalSecondaryPrefix(a.AppendDirName, func() { |
| 59 | + logger.Infof(" - %s", manifestFileName) |
| 60 | + logger.Infof(" - %s", actualAppendFileName) |
| 61 | + }) |
| 62 | + |
| 63 | + if err := os.MkdirAll(appendDirPath, 0755); err != nil { |
| 64 | + return fmt.Errorf("Failed to create append-only directory %s: %w", appendDirPath, err) |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (a *AofDirectoryCreator) createAppendOnlyFile(logger *logger.Logger, actualAppendFilePath string) error { |
| 71 | + actualAppendFileName := filepath.Base(actualAppendFilePath) |
| 72 | + |
| 73 | + if len(a.CommandsInsideAppendOnlyFile) > 0 { |
| 74 | + logger.Infof( |
| 75 | + "Writing %s to append-only file %q", |
| 76 | + english.Plural(len(a.CommandsInsideAppendOnlyFile), "command", "commands"), |
| 77 | + actualAppendFileName, |
| 78 | + ) |
| 79 | + } else { |
| 80 | + logger.Infof("Creating empty append-only file %s", actualAppendFileName) |
| 81 | + } |
| 82 | + |
| 83 | + var aofFileContents []byte |
| 84 | + |
| 85 | + for _, command := range a.CommandsInsideAppendOnlyFile { |
| 86 | + commandRespBytes := a.encodeCommandAsRESPBytes(command) |
| 87 | + aofFileContents = append(aofFileContents, commandRespBytes...) |
| 88 | + |
| 89 | + // Display the command as if it would be displayed using the quoted "%q" directive |
| 90 | + // But remove the surrounding quotes |
| 91 | + comandRespBytesFormatted := strings.Trim( |
| 92 | + fmt.Sprintf("%q", commandRespBytes), |
| 93 | + "\"", |
| 94 | + ) |
| 95 | + |
| 96 | + logger.WithAdditionalSecondaryPrefix(actualAppendFileName, func() { |
| 97 | + logger.Infof("%s", comandRespBytesFormatted) |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + if err := os.WriteFile(actualAppendFilePath, aofFileContents, 0o644); err != nil { |
| 102 | + return fmt.Errorf("Failed to create append-only file %s: %w", actualAppendFilePath, err) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (a *AofDirectoryCreator) createManifestFile(logger *logger.Logger, manifestFilePath, manifestFileEntry string) error { |
| 109 | + manifestFileName := filepath.Base(manifestFilePath) |
| 110 | + |
| 111 | + logger.Infof("Creating manifest file %q", manifestFileName) |
| 112 | + |
| 113 | + logger.WithAdditionalSecondaryPrefix(manifestFileName, func() { |
| 114 | + logger.Infof("%s", manifestFileEntry) |
| 115 | + }) |
| 116 | + |
| 117 | + manifestFileRawBytes := manifestFileEntry + "\n" |
| 118 | + if err := os.WriteFile(manifestFilePath, []byte(manifestFileRawBytes), 0o644); err != nil { |
| 119 | + return fmt.Errorf("Failed to create manifest file %s: %w", manifestFilePath, err) |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (a *AofDirectoryCreator) Cleanup(stageHarness *test_case_harness.TestCaseHarness) error { |
| 125 | + return os.RemoveAll(filepath.Join(a.DataDirectory, a.AppendDirName)) |
| 126 | +} |
| 127 | + |
| 128 | +func (a *AofDirectoryCreator) verifyMemberValues() { |
| 129 | + if a.DataDirectory == "" { |
| 130 | + panic("Codecrafters Internal Error - DataDirectory cannot be empty in AofDirectoryCreator") |
| 131 | + } |
| 132 | + |
| 133 | + if a.AppendDirName == "" { |
| 134 | + panic("Codecrafters Internal Error - AppendDirName cannot be empty in AofDirectoryCreator") |
| 135 | + } |
| 136 | + |
| 137 | + if a.AppendFileNameInFlag == "" { |
| 138 | + panic("Codecrafters Internal Error - AppendFileName cannot be empty in AofDirectoryCreator") |
| 139 | + } |
| 140 | + |
| 141 | + if a.AppendOnlyFileNameInManifest == "" { |
| 142 | + panic("Codecrafters Internal Error - AppendOnlyFileNameInManifest cannot be empty in AofDirectoryCreator") |
| 143 | + } |
| 144 | + |
| 145 | + for i, cmd := range a.CommandsInsideAppendOnlyFile { |
| 146 | + if len(cmd) == 0 { |
| 147 | + panic( |
| 148 | + fmt.Sprintf( |
| 149 | + "Codecrafters Internal Error - CommandsInsideAppendOnlyFile[%d] is empty in AofDirectoryCreator", |
| 150 | + i, |
| 151 | + ), |
| 152 | + ) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// encodeCommandAsRESPBytes encodes a given command as RESP bytes to be written to the append-only file |
| 158 | +func (a *AofDirectoryCreator) encodeCommandAsRESPBytes(command []string) []byte { |
| 159 | + return encoder.Encode(value.NewStringArrayValue(command)) |
| 160 | +} |
0 commit comments