(srcDBPath string, cfg surgeryMetaUpdateOptions)
| 135 | } |
| 136 | |
| 137 | func surgeryMetaUpdateFunc(srcDBPath string, cfg surgeryMetaUpdateOptions) error { |
| 138 | if _, err := checkSourceDBPath(srcDBPath); err != nil { |
| 139 | return err |
| 140 | } |
| 141 | |
| 142 | if err := common.CopyFile(srcDBPath, cfg.outputDBFilePath); err != nil { |
| 143 | return fmt.Errorf("[meta update] copy file failed: %w", err) |
| 144 | } |
| 145 | |
| 146 | // read the page size from the first meta page if we want to edit the second meta page. |
| 147 | var pageSize uint32 |
| 148 | if cfg.metaPageId == 1 { |
| 149 | m0, _, err := ReadMetaPageAt(cfg.outputDBFilePath, 0, pageSize) |
| 150 | if err != nil { |
| 151 | return fmt.Errorf("read the first meta page failed: %w", err) |
| 152 | } |
| 153 | pageSize = m0.PageSize() |
| 154 | } |
| 155 | |
| 156 | // update the specified meta page |
| 157 | m, buf, err := ReadMetaPageAt(cfg.outputDBFilePath, cfg.metaPageId, pageSize) |
| 158 | if err != nil { |
| 159 | return fmt.Errorf("read meta page %d failed: %w", cfg.metaPageId, err) |
| 160 | } |
| 161 | mChanged := updateMetaField(m, parseFields(cfg.fields)) |
| 162 | if mChanged { |
| 163 | if err := writeMetaPageAt(cfg.outputDBFilePath, buf, cfg.metaPageId, pageSize); err != nil { |
| 164 | return fmt.Errorf("[meta update] write meta page %d failed: %w", cfg.metaPageId, err) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if cfg.metaPageId == 1 && pageSize != m.PageSize() { |
| 169 | fmt.Fprintf(os.Stdout, "WARNING: The page size (%d) in the first meta page doesn't match the second meta page (%d)\n", pageSize, m.PageSize()) |
| 170 | } |
| 171 | |
| 172 | // Display results |
| 173 | if !mChanged { |
| 174 | fmt.Fprintln(os.Stdout, "Nothing changed!") |
| 175 | } |
| 176 | |
| 177 | if mChanged { |
| 178 | fmt.Fprintf(os.Stdout, "The meta page %d has been updated!\n", cfg.metaPageId) |
| 179 | } |
| 180 | |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | func parseFields(fields []string) map[string]uint64 { |
| 185 | fieldsMap := make(map[string]uint64) |
no test coverage detected