Ejemplo n.º 1
0
func (rn *RaftNode) fSaveTerm(i int) {
	var currentTermDB *leveldb.DB
	var err error
	currentTermDB, err = leveldb.OpenFile(PATH+"/currentTerm", nil)
	for err != nil {
		currentTermDB, err = leveldb.OpenFile(PATH+"/currentTerm", nil)
	}
	// fmt.Println("sad", currentTermDB, reflect.TypeOf(currentTermDB), err, reflect.TypeOf(err))
	defer currentTermDB.Close()
	currentTermDB.Put([]byte(strconv.Itoa(rn.sm.ServerID)), []byte(strconv.Itoa(i)), nil)

}
Ejemplo n.º 2
0
func (rn *RaftNode) fSaveVotedFor(i int) {

	var votedForDB *leveldb.DB
	var err error
	votedForDB, err = leveldb.OpenFile(PATH+"/currentTerm", nil)
	for err != nil {
		votedForDB, err = leveldb.OpenFile(PATH+"/currentTerm", nil)
	}

	defer votedForDB.Close()
	votedForDB.Put([]byte(strconv.Itoa(rn.sm.ServerID)), []byte(strconv.Itoa(i)), nil)
}
Ejemplo n.º 3
0
func main() {
	rootCmd := &cobra.Command{
		Short: "leveldb-tools",
	}
	var db *leveldb.DB
	openDB := func(cmd *cobra.Command, args []string) {
		if len(args) != 1 {
			rootCmd.Usage()
			os.Exit(1)
		}
		var err error
		opts := &opt.Options{ErrorIfMissing: cmd.Use == "dump", Strict: opt.StrictAll}
		if db, err = leveldb.OpenFile(args[0], opts); err != nil {
			log.Fatal(err)
		}
	}

	dumpCmd := &cobra.Command{
		Use:   "dump",
		Short: "dump database",
		Run: func(cmd *cobra.Command, args []string) {
			defer db.Close()
			defer os.Stdout.Close()
			w := bufio.NewWriter(os.Stdout)
			defer w.Flush()
			iter := db.NewIterator(nil, &opt.ReadOptions{Strict: opt.StrictAll, DontFillCache: true})
			defer iter.Release()
			sep := sep[:]
			for iter.Next() {
				k, v := iter.Key(), iter.Value()
				w.WriteString(fmt.Sprintf("+%d,%d:", len(k), len(v)))
				w.Write(k)
				w.Write(sep)
				w.Write(v)
				if err := w.WriteByte('\n'); err != nil {
					log.Fatal(err)
				}
			}
			if err := iter.Error(); err != nil {
				log.Fatal(err)
			}
		},
		PersistentPreRun: openDB,
	}
	rootCmd.AddCommand(dumpCmd)

	loadCmd := &cobra.Command{
		Use:   "load",
		Short: "load database",
		Run: func(cmd *cobra.Command, args []string) {
			defer func() {
				if err := db.Close(); err != nil {
					log.Fatal(err)
				}
			}()
			r := bufio.NewReader(os.Stdin)
			var lk, lv int
			var k, v []byte
			sepLen := len(sep)
			sep := sep[:]
			for {
				if _, err := fmt.Fscanf(r, "+%d,%d:", &lk, &lv); err != nil {
					if err == io.EOF || err == io.ErrUnexpectedEOF {
						break
					}
					log.Fatal(err)
				}
				if cap(k) < lk {
					k = make([]byte, lk*2)
				}
				if cap(v) < lv+sepLen+1 {
					v = make([]byte, lv*2+2)
				}
				n, err := io.ReadFull(r, k[:lk])
				if err != nil {
					log.Fatal(err)
				}
				k = k[:n]
				if n, err = io.ReadFull(r, v[:lv+sepLen+1]); err != nil {
					log.Fatal(err)
				}
				if !bytes.Equal(sep, v[:sepLen]) {
					log.Fatal("awaited %q, got %q", sep, v)
				}
				v = v[:n]
				if v[n-1] != '\n' {
					log.Fatal("should end with EOL, got %q", v)
				}
				v = v[2 : n-1]
				if err := db.Put(k, v, nil); err != nil {
					log.Fatal(err)
				}
			}
		},
		PersistentPreRun: openDB,
	}
	rootCmd.AddCommand(loadCmd)
	rootCmd.Execute()
}