Revert "add cmd/dump - a dumper"

This reverts commit 5d2a1e8d48.
This commit is contained in:
Chris Lu 2013-01-17 00:15:05 -08:00
parent d1494ea786
commit bf9c4ed033
36 changed files with 466 additions and 622 deletions

View file

@ -1,96 +0,0 @@
// Copyright Tamás Gulácsi 2013 All rights reserved
// Use of this source is governed by the same rules as the weed-fs library.
// If this would be ambigous, than Apache License 2.0 has to be used.
//
// dump dumps the files of a volume to tar or unique files.
// Each file will have id#mimetype#original_name file format
package main
import (
"archive/tar"
"bytes"
"flag"
"fmt"
// "io"
"log"
"os"
"pkg/storage"
"strings"
"time"
)
var (
volumePath = flag.String("dir", "/tmp", "volume directory")
volumeId = flag.Int("id", 0, "volume Id")
dest = flag.String("out", "-", "output path. Produces tar if path ends with .tar; creates files otherwise.")
tarFh *tar.Writer
tarHeader tar.Header
counter int
)
func main() {
var err error
flag.Parse()
if *dest == "-" {
*dest = ""
}
if *dest == "" || strings.HasSuffix(*dest, ".tar") {
var fh *os.File
if *dest == "" {
fh = os.Stdout
} else {
if fh, err = os.Create(*dest); err != nil {
log.Printf("cannot open output tar %s: %s", *dest, err)
return
}
}
defer fh.Close()
tarFh = tar.NewWriter(fh)
defer tarFh.Close()
t := time.Now()
tarHeader = tar.Header{Mode: 0644,
ModTime: t, Uid: os.Getuid(), Gid: os.Getgid(),
Typeflag: tar.TypeReg,
AccessTime: t, ChangeTime: t}
}
v, err := storage.NewVolume(*volumePath, storage.VolumeId(*volumeId), storage.CopyNil)
if v == nil || v.Version() == 0 || err != nil {
log.Printf("cannot load volume %d from %s (%s): %s", *volumeId, *volumePath, v, err)
return
}
log.Printf("volume: %s (ver. %d)", v, v.Version())
if err := v.WalkValues(walker); err != nil {
log.Printf("error while walking: %s", err)
return
}
log.Printf("%d files written.", counter)
}
func walker(n *storage.Needle) (err error) {
// log.Printf("Id=%d Size=%d Name=%s mime=%s", n.Id, n.Size, n.Name, n.Mime)
nm := fmt.Sprintf("%d#%s#%s", n.Id, bytes.Replace(n.Mime, []byte{'/'}, []byte{'_'}, -1), n.Name)
// log.Print(nm)
if tarFh != nil {
tarHeader.Name, tarHeader.Size = nm, int64(len(n.Data))
if err = tarFh.WriteHeader(&tarHeader); err != nil {
return err
}
_, err = tarFh.Write(n.Data)
} else {
if fh, e := os.Create(*dest + "/" + nm); e != nil {
return e
} else {
defer fh.Close()
_, err = fh.Write(n.Data)
}
}
if err == nil {
counter++
}
return
}

View file

@ -24,6 +24,7 @@ type Command struct {
// Flag is a set of flags specific to this command. // Flag is a set of flags specific to this command.
Flag flag.FlagSet Flag flag.FlagSet
} }
// Name returns the command's name: the first word in the usage line. // Name returns the command's name: the first word in the usage line.

View file

@ -2,8 +2,8 @@ package main
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
"fmt"
) )
func init() { func init() {
@ -18,7 +18,8 @@ var cmdShell = &Command{
`, `,
} }
var () var (
)
func runShell(command *Command, args []string) bool { func runShell(command *Command, args []string) bool {
r := bufio.NewReader(os.Stdin) r := bufio.NewReader(os.Stdin)
@ -27,11 +28,11 @@ func runShell(command *Command, args []string) bool {
prompt := func () { prompt := func () {
o.WriteString("> ") o.WriteString("> ")
o.Flush() o.Flush()
} };
readLine := func () string { readLine := func () string {
ret, err := r.ReadString('\n') ret, err := r.ReadString('\n')
if err != nil { if err != nil {
fmt.Fprint(e, err) fmt.Fprint(e,err);
os.Exit(1) os.Exit(1)
} }
return ret return ret

View file

@ -3,8 +3,8 @@ package directory
import ( import (
"encoding/hex" "encoding/hex"
"pkg/storage" "pkg/storage"
"pkg/util"
"strings" "strings"
"pkg/util"
) )
type FileId struct { type FileId struct {

View file

@ -1,8 +1,8 @@
package operation package operation
import ( import (
"log"
"net/http" "net/http"
"log"
) )
func Delete(url string) error { func Delete(url string) error {

View file

@ -2,11 +2,11 @@ package operation
import ( import (
"encoding/json" "encoding/json"
"errors"
_ "fmt"
"net/url" "net/url"
"pkg/storage" "pkg/storage"
"pkg/util" "pkg/util"
_ "fmt"
"errors"
) )
type Location struct { type Location struct {

View file

@ -3,13 +3,13 @@ package operation
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
_ "fmt" _ "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"errors"
) )
type UploadResult struct { type UploadResult struct {

View file

@ -127,3 +127,4 @@ func TestReserveOneVolume(t *testing.T) {
t.Log("reserved", c) t.Log("reserved", c)
} }
} }

View file

@ -2,10 +2,10 @@ package sequence
import ( import (
"encoding/gob" "encoding/gob"
"log"
"os" "os"
"path" "path"
"sync" "sync"
"log"
) )
const ( const (

View file

@ -175,23 +175,3 @@ func (cm *CompactMap) Peek() {
} }
} }
} }
// iterate over the keys by calling iterate on each key till error is returned
func (cm *CompactMap) Walk(pedestrian func(*NeedleValue) error) (err error) {
var i int
for _, cs := range cm.list {
for key := cs.start; key < cs.end; key++ {
if i = cs.binarySearchValues(key); i >= 0 {
if err = pedestrian(&cs.values[i]); err != nil {
return
}
}
}
for _, val := range cs.overflow {
if err = pedestrian(val); err != nil {
return err
}
}
}
return nil
}

View file

@ -1,10 +1,10 @@
package storage package storage
import ( import (
"testing"
"log" "log"
"os" "os"
"pkg/util" "pkg/util"
"testing"
) )
func TestMemoryUsage(t *testing.T) { func TestMemoryUsage(t *testing.T) {

View file

@ -98,8 +98,3 @@ func (nm *NeedleMap) Close() {
func (nm *NeedleMap) ContentSize() uint64 { func (nm *NeedleMap) ContentSize() uint64 {
return nm.fileByteCounter return nm.fileByteCounter
} }
// iterate through all needles using the iterator function
func (nm *NeedleMap) Walk(pedestrian func(*NeedleValue) error) (err error) {
return nm.m.Walk(pedestrian)
}

View file

@ -2,10 +2,10 @@ package storage
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"os" "os"
"pkg/util" "pkg/util"
"fmt"
) )
func (n *Needle) Append(w io.Writer, version Version) uint32 { func (n *Needle) Append(w io.Writer, version Version) uint32 {
@ -62,8 +62,7 @@ func (n *Needle) Append(w io.Writer, version Version) uint32 {
return n.Size return n.Size
} }
func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) { func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
switch version { if version == Version1 {
case Version1:
bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize) bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
ret, e := r.Read(bytes) ret, e := r.Read(bytes)
n.readNeedleHeader(bytes) n.readNeedleHeader(bytes)
@ -73,7 +72,7 @@ func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
return 0, errors.New("CRC error! Data On Disk Corrupted!") return 0, errors.New("CRC error! Data On Disk Corrupted!")
} }
return ret, e return ret, e
case Version2: } else if version == Version2 {
if size == 0 { if size == 0 {
return 0, nil return 0, nil
} }
@ -96,7 +95,7 @@ func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
} }
return ret, e return ret, e
} }
return 0, fmt.Errorf("Unsupported Version! (%d)", version) return 0, errors.New("Unsupported Version!")
} }
func (n *Needle) readNeedleHeader(bytes []byte) { func (n *Needle) readNeedleHeader(bytes []byte) {
n.Cookie = util.BytesToUint32(bytes[0:4]) n.Cookie = util.BytesToUint32(bytes[0:4])

View file

@ -65,13 +65,13 @@ func (s *Store) AddVolume(volumeListString string, replicationType string) error
} }
return e return e
} }
func (s *Store) addVolume(vid VolumeId, replicationType ReplicationType) (err error) { func (s *Store) addVolume(vid VolumeId, replicationType ReplicationType) error {
if s.volumes[vid] != nil { if s.volumes[vid] != nil {
return errors.New("Volume Id " + vid.String() + " already exists!") return errors.New("Volume Id " + vid.String() + " already exists!")
} }
log.Println("In dir", s.dir, "adds volume =", vid, ", replicationType =", replicationType) log.Println("In dir", s.dir, "adds volume =", vid, ", replicationType =", replicationType)
s.volumes[vid], err = NewVolume(s.dir, vid, replicationType) s.volumes[vid] = NewVolume(s.dir, vid, replicationType)
return err return nil
} }
func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString string) (error, bool) { func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString string) (error, bool) {
@ -107,7 +107,7 @@ func (s *Store) loadExistingVolumes() {
base := name[:len(name)-len(".dat")] base := name[:len(name)-len(".dat")]
if vid, err := NewVolumeId(base); err == nil { if vid, err := NewVolumeId(base); err == nil {
if s.volumes[vid] == nil { if s.volumes[vid] == nil {
if v, e := NewVolume(s.dir, vid, CopyNil); e == nil { v := NewVolume(s.dir, vid, CopyNil)
s.volumes[vid] = v s.volumes[vid] = v
log.Println("In dir", s.dir, "read volume =", vid, "replicationType =", v.replicaType, "version =", v.version, "size =", v.Size()) log.Println("In dir", s.dir, "read volume =", vid, "replicationType =", v.replicaType, "version =", v.version, "size =", v.Size())
} }
@ -116,7 +116,6 @@ func (s *Store) loadExistingVolumes() {
} }
} }
} }
}
func (s *Store) Status() []*VolumeInfo { func (s *Store) Status() []*VolumeInfo {
var stats []*VolumeInfo var stats []*VolumeInfo
for k, v := range s.volumes { for k, v := range s.volumes {

View file

@ -24,9 +24,9 @@ type Volume struct {
accessLock sync.Mutex accessLock sync.Mutex
} }
func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume, e error) { func NewVolume(dirname string, id VolumeId, replicationType ReplicationType) (v *Volume) {
v = &Volume{dir: dirname, Id: id, replicaType: replicationType} v = &Volume{dir: dirname, Id: id, replicaType: replicationType}
e = v.load() v.load()
return return
} }
func (v *Volume) load() error { func (v *Volume) load() error {
@ -43,7 +43,6 @@ func (v *Volume) load() error {
} else { } else {
v.maybeWriteSuperBlock() v.maybeWriteSuperBlock()
} }
// TODO: if .idx not exists, but .cdb exists, then use (but don't load!) that
indexFile, ie := os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644) indexFile, ie := os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
if ie != nil { if ie != nil {
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e) return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
@ -80,23 +79,21 @@ func (v *Volume) maybeWriteSuperBlock() {
v.dataFile.Write(header) v.dataFile.Write(header)
} }
} }
func (v *Volume) readSuperBlock() (err error) { func (v *Volume) readSuperBlock() error {
v.dataFile.Seek(0, 0) v.dataFile.Seek(0, 0)
header := make([]byte, SuperBlockSize) header := make([]byte, SuperBlockSize)
if _, e := v.dataFile.Read(header); e != nil { if _, e := v.dataFile.Read(header); e != nil {
return fmt.Errorf("cannot read superblock: %s", e) return fmt.Errorf("cannot read superblock: %s", e)
} }
var err error
v.version, v.replicaType, err = ParseSuperBlock(header) v.version, v.replicaType, err = ParseSuperBlock(header)
return err return err
} }
func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, err error) { func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, e error) {
version = Version(header[0]) version = Version(header[0])
if version == 0 { var err error
err = errors.New("Zero version impossible - bad superblock!")
return
}
if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil { if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
err = fmt.Errorf("cannot read replica type: %s", err) e = fmt.Errorf("cannot read replica type: %s", err)
} }
return return
} }
@ -224,39 +221,3 @@ func (v *Volume) copyDataAndGenerateIndexFile(srcName, dstName, idxName string)
func (v *Volume) ContentSize() uint64 { func (v *Volume) ContentSize() uint64 {
return v.nm.fileByteCounter return v.nm.fileByteCounter
} }
// Walk over the contained needles (call the function with each NeedleValue till error is returned)
func (v *Volume) WalkValues(pedestrian func(*Needle) error) error {
pedplus := func(nv *NeedleValue) (err error) {
n := new(Needle)
if nv.Offset > 0 {
v.dataFile.Seek(int64(nv.Offset)*NeedlePaddingSize, 0)
if _, err = n.Read(v.dataFile, nv.Size, v.version); err != nil {
return
}
if err = pedestrian(n); err != nil {
return
}
}
return nil
}
return v.nm.Walk(pedplus)
}
// Walk over the keys
func (v *Volume) WalkKeys(pedestrian func(Key) error) error {
pedplus := func(nv *NeedleValue) (err error) {
if nv.Offset > 0 && nv.Key > 0 {
if err = pedestrian(nv.Key); err != nil {
return
}
}
return nil
}
return v.nm.Walk(pedplus)
}
func (v *Volume) String() string {
return fmt.Sprintf("%d@%s:v%d:r%s", v.Id, v.dataFile.Name(),
v.Version(), v.replicaType)
}

View file

@ -5,7 +5,6 @@ import (
) )
type VolumeId uint32 type VolumeId uint32
func NewVolumeId(vid string) (VolumeId,error) { func NewVolumeId(vid string) (VolumeId,error) {
volumeId, err := strconv.ParseUint(vid, 10, 64) volumeId, err := strconv.ParseUint(vid, 10, 64)
return VolumeId(volumeId), err return VolumeId(volumeId), err

View file

@ -1,6 +1,7 @@
package storage package storage
import () import (
)
type Version uint8 type Version uint8

View file

@ -1,6 +1,7 @@
package topology package topology
import () import (
)
type DataCenter struct { type DataCenter struct {
NodeImpl NodeImpl

View file

@ -1,9 +1,9 @@
package topology package topology
import ( import (
_ "fmt"
"strconv" "strconv"
"testing" "testing"
_ "fmt"
) )
func TestXYZ(t *testing.T) { func TestXYZ(t *testing.T) {

View file

@ -1,6 +1,7 @@
package topology package topology
import () import (
)
func (t *Topology) ToMap() interface{} { func (t *Topology) ToMap() interface{} {
m := make(map[string]interface{}) m := make(map[string]interface{})

View file

@ -31,3 +31,4 @@ func Uint32toBytes(b []byte, v uint32) {
func Uint8toBytes(b []byte, v uint8){ func Uint8toBytes(b []byte, v uint8){
b[0] = byte(v) b[0] = byte(v)
} }