Exemplo n.º 1
0
func NewLevelDB(path string, restore bool) (*LevelDB, error) {
	fpath, err := filepath.Abs(path)
	if err != nil {
		return nil, err
	}

	if !restore {
		err = os.RemoveAll(fpath)
		if err != nil {
			return nil, err
		}
	}

	ldb, err := leveldb.Open(fpath, nil) // TODO: tune the option
	if err != nil {
		return nil, err
	}

	ret := &LevelDB{
		fpath: fpath,
		ldb:   ldb,
		wsync: &db.WriteOptions{Sync: true},
	}
	return ret, nil
}
Exemplo n.º 2
0
func Open() *DB {
	db, err := leveldb.Open("db", nil)
	if err != nil {
		panic(fmt.Sprintf("Couldn't open database: %v", err))
	}
	return &DB{
		db: db,
	}
}
Exemplo n.º 3
0
func NewStorage(c *Config) (*SStorage, error) {
	assert(nil != c)

	// TODO: using leveldb/memfs ?
	db, err := leveldb.Open(
		path.Join(c.Path, strconv.FormatUint(c.Selfid, 10)), nil)
	if nil != err {
		return nil, err
	}

	store := &SStorage{db: db}
	return store, nil
}
Exemplo n.º 4
0
func main() {
	PROG_NAME := filepath.Base(os.Args[0])

	var moveFiles bool
	flag.BoolVar(&moveFiles, "m", false, "move files to destination instead of copying")

	flag.Parse()

	if len(flag.Args()) < 2 {
		fmt.Printf("error: Invalid parameters\n")
		fmt.Printf("usage: %s [-m] dst src ...\n", PROG_NAME)
		os.Exit(ERR_INVALID_PARAMS)
	}

	// Initialize LevelDB
	var dbh *leveldb.DB

	if dbDir, err := ioutil.TempDir("", PROG_NAME); err != nil {
		fmt.Printf("error: Creation of tmp dir failed: %s\n", err)
		os.Exit(ERR_CREATE_TMP_DIR_FAILED)
	} else {
		defer cleanupDbDir(dbDir)
		dbh, err = leveldb.Open(dbDir, &db.Options{})

		if err != nil {
			fmt.Printf("error: Temporary working DB initialization failed: %s", err)
			os.Exit(ERR_OPEN_DB_FAILED)
		}

		defer dbh.Close()
	}

	// Create channels
	hashingCh := make(chan *fileEntry)
	defer close(hashingCh)

	processingCh := make(chan *fileEntry)

	// Go for it
	go hashFiles(hashingCh, processingCh)
	go processFile(dbh, processingCh, moveFiles)

	for _, d := range flag.Args()[1:] {
		filepath.Walk(d, getPathProcessor(hashingCh, d, flag.Args()[0]))
	}
}