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