forked from perkeep/perkeep
/
dp_idx_rebuild.go
128 lines (115 loc) · 3.67 KB
/
dp_idx_rebuild.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"camlistore.org/pkg/blobserver/diskpacked"
"camlistore.org/pkg/cmdmain"
"camlistore.org/pkg/osutil"
"camlistore.org/pkg/serverinit"
"go4.org/jsonconfig"
)
type reindexdpCmd struct {
overwrite, verbose bool
}
func init() {
cmdmain.RegisterCommand("reindex-diskpacked",
func(flags *flag.FlagSet) cmdmain.CommandRunner {
cmd := new(reindexdpCmd)
flags.BoolVar(&cmd.overwrite, "overwrite", false,
"Whether to overwrite the existing index. If false, only check.")
return cmd
})
}
func (c *reindexdpCmd) Describe() string {
return "Rebuild the index of the diskpacked blob store"
}
func (c *reindexdpCmd) Usage() {
fmt.Fprintln(os.Stderr, "Usage: camtool [globalopts] reindex-diskpacked [reindex-opts]")
fmt.Fprintln(os.Stderr, " camtool reindex-diskpacked [--overwrite] # dir from server config")
fmt.Fprintln(os.Stderr, " camtool reindex-diskpacked [--overwrite] /path/to/directory")
}
func (c *reindexdpCmd) RunCommand(args []string) error {
var path string
var indexConf jsonconfig.Obj
switch len(args) {
case 0:
case 1:
path = args[0]
default:
return errors.New("More than 1 argument not allowed")
}
cfg, err := serverinit.LoadFile(osutil.UserServerConfigPath())
if err != nil {
return err
}
prefixes, ok := cfg.Obj["prefixes"].(map[string]interface{})
if !ok {
return fmt.Errorf("No 'prefixes' object in low-level (or converted) config file %s", osutil.UserServerConfigPath())
}
paths, confs := []string{}, []jsonconfig.Obj{}
for prefix, vei := range prefixes {
pmap, ok := vei.(map[string]interface{})
if !ok {
log.Printf("prefix %q value is a %T, not an object", prefix, vei)
continue
}
pconf := jsonconfig.Obj(pmap)
handlerType := pconf.RequiredString("handler")
handlerArgs := pconf.OptionalObject("handlerArgs")
// no pconf.Validate, as this is a recover tool
if handlerType != "storage-diskpacked" {
continue
}
log.Printf("handlerArgs of %q: %v", prefix, handlerArgs)
if handlerArgs == nil {
log.Printf("no handlerArgs for %q", prefix)
continue
}
aconf := jsonconfig.Obj(handlerArgs)
apath := aconf.RequiredString("path")
// no aconv.Validate, as this is a recover tool
if apath == "" {
log.Printf("path is missing for %q", prefix)
continue
}
if path != "" && path != apath {
continue
}
paths = append(paths, apath)
confs = append(confs, aconf)
}
if len(paths) == 0 {
return fmt.Errorf("Server config file %s doesn't specify a disk-packed storage handler.",
osutil.UserServerConfigPath())
}
if len(paths) > 1 {
return fmt.Errorf("Ambiguity. Server config file %s d specify more than 1 disk-packed storage handler. Please specify one of: %v", osutil.UserServerConfigPath(), paths)
}
path = paths[0]
if path == "" {
return errors.New("no path is given/found")
}
// If no index is specified, the default will be used (as on the regular path).
if mi := confs[0]["metaIndex"]; mi != nil {
if mi, ok := mi.(map[string]interface{}); ok {
indexConf = jsonconfig.Obj(mi)
}
}
log.Printf("indexConf: %v", indexConf)
return diskpacked.Reindex(path, c.overwrite, indexConf)
}