func (n *NoteEntry) save() bool { appDataDb, err := leveldb.OpenFile(Configs["conf/db.conf"].String("app::path"), nil) if err != nil { beego.Error(err) beego.Critical("unable to open " + Configs["conf/db.conf"].String("app::path")) return false } defer appDataDb.Close() tmp := int64(1014) if urlCountStr, err := appDataDb.Get([]byte("urlCount"), nil); err == nil { tmp, _ = strconv.ParseInt(string(urlCountStr), 10, 64) } if tmp < urlCount && appDataDb.Put([]byte("urlCount"), []byte(strconv.FormatInt(urlCount, 10)), nil) != nil { beego.Critical("unable to save to db") return false } urlDb, err := leveldb.OpenFile(Configs["conf/db.conf"].String("url::path"), nil) if err != nil { beego.Critical("unable to open " + Configs["conf/db.conf"].String("url::path")) return false } defer urlDb.Close() if buf, err := json.Marshal(n.toPublic()); err != nil { beego.Error("unable to Marshal object", n) return false } else { urlDb.Put([]byte(n.url), buf, nil) } return true }
func TestLabelConversion(t *testing.T) { os.RemoveAll("testdata/oldformat.db") defer os.RemoveAll("testdata/oldformat.db") os.RemoveAll("testdata/newformat.db") defer os.RemoveAll("testdata/newformat.db") if err := unzip("testdata/oldformat.db.zip", "testdata"); err != nil { t.Fatal(err) } odb, err := leveldb.OpenFile("testdata/oldformat.db", nil) if err != nil { t.Fatal(err) } ldb, err := leveldb.OpenFile("testdata/newformat.db", nil) if err != nil { t.Fatal(err) } if err = convertKeyFormat(odb, ldb); err != nil { t.Fatal(err) } ldb.Close() odb.Close() inst, err := Open("testdata/newformat.db") if err != nil { t.Fatal(err) } fs := NewFileSet("default", inst) files, deleted, _ := fs.GlobalSize() if files+deleted != 953 { // Expected number of global entries determined by // ../../bin/stindex testdata/oldformat.db/ | grep global | grep -c default t.Errorf("Conversion error, global list differs (%d != 953)", files+deleted) } files, deleted, _ = fs.LocalSize() if files+deleted != 953 { t.Errorf("Conversion error, device list differs (%d != 953)", files+deleted) } f := NewBlockFinder(inst) // [block] F:"default" H:1c25dea9003cc16216e2a22900be1ec1cc5aaf270442904e2f9812c314e929d8 N:"f/f2/f25f1b3e6e029231b933531b2138796d" I:3 h := []byte{0x1c, 0x25, 0xde, 0xa9, 0x00, 0x3c, 0xc1, 0x62, 0x16, 0xe2, 0xa2, 0x29, 0x00, 0xbe, 0x1e, 0xc1, 0xcc, 0x5a, 0xaf, 0x27, 0x04, 0x42, 0x90, 0x4e, 0x2f, 0x98, 0x12, 0xc3, 0x14, 0xe9, 0x29, 0xd8} found := 0 f.Iterate([]string{"default"}, h, func(folder, file string, idx int32) bool { if folder == "default" && file == filepath.FromSlash("f/f2/f25f1b3e6e029231b933531b2138796d") && idx == 3 { found++ } return true }) if found != 1 { t.Errorf("Found %d blocks instead of expected 1", found) } inst.Close() }
func leveldbOpener(url *config.URL) (driver.Driver, error) { value := url.Value if !filepath.IsAbs(value) { value = pathutil.Relative(value) } opts := &opt.Options{} if url.Fragment["nocompress"] != "" { opts.Compression = opt.NoCompression } if url.Fragment["nocreate"] != "" { opts.ErrorIfMissing = true } filesDir := filepath.Join(value, "files") files, err := leveldb.OpenFile(filesDir, opts) if err != nil { return nil, err } copts := *opts copts.Filter = filter.NewBloomFilter(8 * sha1.Size) chunksDir := filepath.Join(value, "chunks") chunks, err := leveldb.OpenFile(chunksDir, &copts) if err != nil { return nil, err } return &leveldbDriver{ files: files, chunks: chunks, dir: value, }, nil }
func serverMain() { // Database to store file version db, _ := leveldb.OpenFile("$GOPATH/src/github.com/aakashdeshpande/cs733/assignment1/versionMap", nil) files, _ := leveldb.OpenFile("$GOPATH/src/github.com/aakashdeshpande/cs733/assignment1/files", nil) expiry, _ := leveldb.OpenFile("$GOPATH/src/github.com/aakashdeshpande/cs733/assignment1/expiryTimes", nil) defer db.Close() defer files.Close() defer expiry.Close() var mutex = &sync.RWMutex{} // Listen for TCP connection on port 8080 ln, err := net.Listen("tcp", ":8080") if err != nil { fmt.Println("Error listening:", err.Error()) os.Exit(1) } // Keep listening for new connections for { conn, err := ln.Accept() if err != nil { fmt.Println("Error accepting: ", err.Error()) os.Exit(1) } // Handle new connection in a new thread go handleConnection(conn, db, files, expiry, mutex) } }
func Open(file string) (*Instance, error) { opts := &opt.Options{ OpenFilesCacheCapacity: 100, WriteBuffer: 4 << 20, } db, err := leveldb.OpenFile(file, opts) if leveldbIsCorrupted(err) { db, err = leveldb.RecoverFile(file, opts) } if leveldbIsCorrupted(err) { // The database is corrupted, and we've tried to recover it but it // didn't work. At this point there isn't much to do beyond dropping // the database and reindexing... l.Infoln("Database corruption detected, unable to recover. Reinitializing...") if err := os.RemoveAll(file); err != nil { return nil, err } db, err = leveldb.OpenFile(file, opts) } if err != nil { return nil, err } return newDBInstance(db, file), nil }
func serverMain() { // Setting the port to listen on sock, err := net.Listen(CONNECTION_TYPE, HOST+":"+PORT) if err != nil { log.Print("Error in listening:", err.Error()) } // Close the listener when the application closes. defer sock.Close() log.Println("Listening on " + HOST + ":" + PORT + " for incoming connections") datadb, err := leveldb.OpenFile("datadb", nil) defer datadb.Close() metadatadb, err := leveldb.OpenFile("metadatadb", nil) defer metadatadb.Close() // Keep listening for incoming connections for { conn, err := sock.Accept() if err != nil { log.Println("Error while accepting incoming connection: ", err.Error()) } // Connections handled in new goroutine go request_handler(conn, datadb, metadatadb) } }
func (rn *RaftNode) ShutDown() { rn.quitCh <- true rn.sm.status = "Closed" // Database to store currentTerm db1, _ := leveldb.OpenFile("currentTerm", nil) defer db1.Close() // Database to store votedFor db2, _ := leveldb.OpenFile("votedFor", nil) defer db2.Close() db1.Put([]byte(strconv.FormatInt(rn.sm.id, 10)), []byte(strconv.FormatInt(rn.sm.Term, 10)), nil) db2.Put([]byte(strconv.FormatInt(rn.sm.id, 10)), []byte(strconv.FormatInt(rn.sm.votedFor, 10)), nil) out := CommitInfo{nil, -1, errors.New("Stop"), false} rn.commitCh <- out close(rn.clientCh) close(rn.commitCh) //fmt.Println("Stopped ", rn.Id) close(rn.actionCh) close(rn.quitCh) rn.timer.Stop() rn.server.Close() rn.lg.Close() }
func NewStateMachine(servers int64, id int64, actionCh chan events, electionTimeout int, lg *log.Log) *StateMachine { var sm *StateMachine = new(StateMachine) sm.servers = servers sm.id = id sm.status = "Follower" currentTerm, _ := leveldb.OpenFile("currentTerm", nil) defer currentTerm.Close() // Database to store votedFor voted, _ := leveldb.OpenFile("votedFor", nil) defer voted.Close() //sm.currentTerm = currentTerm //defer sm.currentTerm.Close() Term, err := currentTerm.Get([]byte(strconv.FormatInt(sm.id, 10)), nil) if err == nil { sm.Term, _ = strconv.ParseInt(string(Term), 10, 64) } else { sm.Term = int64(0) } //sm.voted = voted //defer sm.voted.Close() vote, err := voted.Get([]byte(strconv.FormatInt(sm.id, 10)), nil) if err == nil { sm.votedFor, _ = strconv.ParseInt(string(vote), 10, 64) } else { sm.votedFor = int64(-1) } sm.log = make([][]byte, 10000000) sm.logTerm = make(map[int64]int64) size := lg.GetLastIndex() + 1 if size == 0 { sm.LastLogIndex = -1 sm.LastLogTerm = 0 } else { for i := int64(0); i < size; i++ { b, _ := lg.Get(i) var entry LogInfo json.Unmarshal(b.([]byte), &entry) sm.log[i] = entry.Data sm.logTerm[i] = entry.Term } sm.LastLogIndex = size - 1 sm.LastLogTerm = sm.logTerm[size-1] } sm.commitIndex = -1 sm.actionCh = actionCh sm.electionTimeout = time.Millisecond * time.Duration(electionTimeout) sm.votesMap = make(map[int64]int) sm.nextIndex = make(map[int64]int64) sm.matchIndex = make(map[int64]int64) //sm.acksRecieved = make(map[int64]int64) return sm }
func NewStateMachine(id int, peerIds []int, electionTimeout float64, heartbeatTimeout float64, lg *log.Log) *StateMachine { sm := StateMachine{ ServerID: id, State: "follower", VoteGranted: make(map[int]bool), NextIndex: make(map[int]int), MatchIndex: make(map[int]int), updateCh: make(chan interface{}, 250000), netCh: make(chan interface{}, 250000), actionCh: make(chan interface{}, 250000), LastLogIndex: -1, CommitIndex: -1, PeerIds: peerIds, Log: make([]LogEntry, 0), //see } sm.ELECTION_TIMEOUT = electionTimeout sm.HEARTBEAT_TIMEOUT = heartbeatTimeout NUMBER_OF_NODES = len(peerIds) CurrentTermDB, _ := leveldb.OpenFile(PATH+"/currentTerm", nil) defer CurrentTermDB.Close() termStr, err := CurrentTermDB.Get([]byte(strconv.FormatInt(int64(sm.ServerID), 10)), nil) if err == nil { sm.CurrentTerm, _ = strconv.Atoi(string(termStr)) } else { sm.CurrentTerm = int(0) } VotedForDB, _ := leveldb.OpenFile(PATH+"/votedFor", nil) defer VotedForDB.Close() votedForStr, err := VotedForDB.Get([]byte(strconv.Itoa(sm.ServerID)), nil) if err == nil { sm.VotedFor, _ = strconv.Atoi(string(votedForStr)) } else { sm.VotedFor = int(0) } lastIndex := int(lg.GetLastIndex()) //fmt.Println("last index is ", lastIndex) if lastIndex != -1 { var i int for i = 0; i <= lastIndex; i++ { b, _ := lg.Get(int64(i)) //see // entry := b.([]byte).(LogEntry) var entry LogEntry json.Unmarshal(b.([]byte), &entry) sm.Log = append(sm.Log, entry) //see } sm.LastLogIndex = lastIndex sm.LastLogTerm = sm.getLogTerm(lastIndex) } return &sm }
func (m *Machine) loadDataFromLevelDB() (*leveldb.DB, *leveldb.DB) { fmt.Println("Loading Values from KVStores : logKVStore , dataKVStore") fmt.Println("Opening Store : ", "logKVStore"+strconv.Itoa(m.server.Pid())) // Remember that the contents of the returned slice should not be modified. logKVStore, err1 := leveldb.OpenFile("logKVStore"+strconv.Itoa(m.server.Pid()), nil) if err1 != nil { fmt.Println("\nError :- ", err1) } else { dataKVStore, err2 := leveldb.OpenFile("dataKVStore"+strconv.Itoa(m.server.Pid()), nil) if err2 != nil { fmt.Println("\nError :- ", err2) } else { m.LastLogIndex = -1 m.CommitIndex = -1 m.LastApplied = -1 logIter := logKVStore.NewIterator(nil, nil) for logIter.Next() { // only valid until the next call to Next. key := logIter.Key() value := logIter.Value() //fmt.Println("\nKey : ",key," , Value : ",value) logKey, _ := strconv.Atoi(string(key)) tempvalueKeyPart := strings.Split(string(value), ":") valueKeyPart, _ := strconv.Atoi(tempvalueKeyPart[0]) logEntryValue := cluster.LogEntry{Index: int64(logKey), Term: valueKeyPart, Data: tempvalueKeyPart[1]} m.Log = append(m.Log, logEntryValue) m.CommitIndex += 1 } if m.CommitIndex != -1 { m.LastLogIndex = m.CommitIndex m.LastApplied = m.CommitIndex m.term = m.Log[m.CommitIndex].Term } else { m.term = 0 } //fmt.Println("Total Log Now : ",m.Log) return logKVStore, dataKVStore } } return nil, nil }
func NewFS(Id int, receiver []chan Response, input <-chan CommitInfo) *FileService { var f *FileService = &FileService{} f.Id = int64(Id) f.receiver = receiver f.input = input f.datadb, _ = leveldb.OpenFile("datadb"+strconv.Itoa(Id), nil) f.metadatadb, _ = leveldb.OpenFile("metadatadb"+strconv.Itoa(Id), nil) f.mutex = &sync.RWMutex{} return f }
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) }
func NewFS(Id int, routes []chan Response, input <-chan CommitInfo) *FileService { var f *FileService = &FileService{} f.Id = int64(Id) f.routes = routes f.input = input // Database to store file version f.db, _ = leveldb.OpenFile("versionMap"+strconv.Itoa(Id), nil) f.files, _ = leveldb.OpenFile("files"+strconv.Itoa(Id), nil) f.expiry, _ = leveldb.OpenFile("expiryTimes"+strconv.Itoa(Id), nil) f.mutex = &sync.RWMutex{} return f }
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) }
// ListenAndServe starts an instance of the API with a // heimdall configuration. func ListenAndServe(config *heimdall.Config) { authdb, err := leveldb.OpenFile(config.Auth.DBPath, nil) if err != nil { log.Fatal(err) } auth := authentication.Authenticator{authdb} blacklistdb, err := leveldb.OpenFile(config.Blacklist.DBPath, nil) if err != nil { log.Fatal(err) } blacklist := persistence.OpenDB(blacklistdb) whitelistdb, err := leveldb.OpenFile(config.Whitelist.DBPath, nil) if err != nil { log.Fatal(err) } whitelist := persistence.OpenDB(whitelistdb) environment := NewEnvironment(whitelist, blacklist, &auth) blistTTL, _ := time.ParseDuration(config.Blacklist.DefaultTTL) wlistTTL, _ := time.ParseDuration(config.Whitelist.DefaultTTL) blistHandler := Blacklist{&environment, config.Blacklist.Path, blistTTL} wlistHandler := Whitelist{&environment, config.Whitelist.Path, wlistTTL} http.Handle(blistHandler.Path, &blistHandler) http.Handle(wlistHandler.Path, &wlistHandler) pruneInterval, _ := time.ParseDuration(config.CleanupInterval) go blacklist.AutoPrune(pruneInterval) go whitelist.AutoPrune(pruneInterval) if config.PanOS.Enabled { var panosAllowedHosts []net.IP for _, host := range config.PanOS.AllowedHosts { panosAllowedHosts = append(panosAllowedHosts, net.ParseIP(host)) } panosHandler := PanOS{&environment, config.PanOS.Path, panosAllowedHosts} http.Handle(config.PanOS.Path, &panosHandler) } if err := http.ListenAndServeTLS(config.Port, config.Cert, config.Key, nil); err != nil { log.Fatal(err) } }
func createNewLevelDB(path string, _ graph.Options) error { opts := &opt.Options{} db, err := leveldb.OpenFile(path, opts) if err != nil { clog.Errorf("Error: could not create database: %v", err) return err } defer db.Close() qs := &QuadStore{} qs.db = db qs.writeopts = &opt.WriteOptions{ Sync: true, } qs.readopts = &opt.ReadOptions{} _, err = qs.db.Get([]byte(horizonKey), qs.readopts) if err != nil && err != leveldb.ErrNotFound { clog.Errorf("couldn't read from leveldb during init") return err } if err != leveldb.ErrNotFound { return graph.ErrDatabaseExists } // Write some metadata qs.Close() return nil }
func NewFileListInLevelDb(dir string) (fl *FileListInLevelDb, err error) { fl = &FileListInLevelDb{} if fl.db, err = leveldb.OpenFile(dir, nil); err != nil { return } return }
// NewLDBDatabase returns a LevelDB wrapped object. func NewLDBDatabase(file string, cache int, handles int) (*LDBDatabase, error) { // Calculate the cache and file descriptor allowance for this particular database cache = int(float64(cache) * cacheRatio[filepath.Base(file)]) if cache < 16 { cache = 16 } handles = int(float64(handles) * handleRatio[filepath.Base(file)]) if handles < 16 { handles = 16 } glog.V(logger.Info).Infof("Alloted %dMB cache and %d file handles to %s", cache, handles, file) // Open the db and recover any potential corruptions db, err := leveldb.OpenFile(file, &opt.Options{ OpenFilesCacheCapacity: handles, BlockCacheCapacity: cache / 2 * opt.MiB, WriteBuffer: cache / 4 * opt.MiB, // Two of these are used internally Filter: filter.NewBloomFilter(10), }) if _, corrupted := err.(*errors.ErrCorrupted); corrupted { db, err = leveldb.RecoverFile(file, nil) } // (Re)check for errors and abort if opening of the db failed if err != nil { return nil, err } return &LDBDatabase{ fn: file, db: db, }, nil }
func (pdb *PDB) open() { db, err := leveldb.OpenFile(DB_PATH, nil) if err != nil { log.Fatalf("Error opening db! %s\n", err) } pdb.db = *db }
/* /Open the DATABASE /return leveldb Object */ func dbOpen() *leveldb.DB { temp, openError := leveldb.OpenFile("/Users/takumigashira/test.ldb", nil) if openError != nil { fmt.Println("DB Open ERROR") } return temp }
func New(mo store.MergeOperator, config map[string]interface{}) (store.KVStore, error) { path, ok := config["path"].(string) if !ok { return nil, fmt.Errorf("must specify path") } opts, err := applyConfig(&opt.Options{}, config) if err != nil { return nil, err } db, err := leveldb.OpenFile(path, opts) if err != nil { return nil, err } rv := Store{ path: path, opts: opts, db: db, mo: mo, defaultReadOptions: &opt.ReadOptions{}, defaultWriteOptions: &opt.WriteOptions{}, } rv.defaultWriteOptions.Sync = true return &rv, nil }
func NewQDB(path string, syncWrites bool) *QDB { // Open db, err := leveldb.OpenFile(path, nil) if err != nil { panic(fmt.Sprintf("goq: Unable to open db: %v", err)) } log.Println("goq: Starting health check") // Health check each record iter := db.NewIterator(nil, nil) defer iter.Release() for iter.Next() { _, err := strconv.Atoi(string(iter.Key())) if err != nil { panic(fmt.Sprintf("goq: Health check failure (key not int): %s, %s, %v", string(iter.Key()), string(iter.Value()), err)) } } // General if iter.Error() != nil { panic(fmt.Sprintf("goq: Error loading db: %v", err)) } log.Println("goq: Health check successful") return &QDB{ path: path, wo: opt.WriteOptions{Sync: syncWrites}, db: db, } }
func main() { configPathPtr := flag.String("config", "config.json", "path to config file") flag.Parse() configReader, err := os.Open(*configPathPtr) if err != nil { log.Fatalf("Failed to open configuration file: %s", err) } cfg := &proto.VerifierConfig{} err = jsonpb.Unmarshal(configReader, cfg) if err != nil { log.Fatalf("Failed to parse configuration file: %s", err) } leveldb, err := leveldb.OpenFile(cfg.LevelDBPath, nil) if err != nil { log.Fatalf("Couldn't open DB in directory %s: %s", cfg.LevelDBPath, err) } db := leveldbkv.Wrap(leveldb) server, err := verifier.Start(cfg, db, getKey) if err != nil { panic(err) } defer server.Stop() ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt) <-ch }
func newTripleStore(path string, options graph.Options) (graph.TripleStore, error) { var ts TripleStore ts.path = path cache_size := DefaultCacheSize if val, ok := options.IntKey("cache_size_mb"); ok { cache_size = val } ts.dbOpts = &opt.Options{ BlockCache: cache.NewLRUCache(cache_size * opt.MiB), } ts.dbOpts.ErrorIfMissing = true write_buffer_mb := DefaultWriteBufferSize if val, ok := options.IntKey("write_buffer_mb"); ok { write_buffer_mb = val } ts.dbOpts.WriteBuffer = write_buffer_mb * opt.MiB ts.hasher = sha1.New() ts.writeopts = &opt.WriteOptions{ Sync: false, } ts.readopts = &opt.ReadOptions{} db, err := leveldb.OpenFile(ts.path, ts.dbOpts) if err != nil { panic("Error, couldn't open! " + err.Error()) } ts.db = db glog.Infoln(ts.GetStats()) ts.getSize() return &ts, nil }
func getDB(p string) (*leveldb.DB, error) { var err error if p == "" { p, err = DefaultCachePath() if err != nil { return nil, err } } err = mkDir(p) var version string if err != nil { return nil, err } fp := path.Join(p, dbName) mp := path.Join(p, metaName) if exists(fp) && exists(mp) { b, err := ioutil.ReadFile(mp) if err != nil { return nil, err } version = strings.Trim(string(b[:]), "\n\r") } err = updateDB(version, p) if err != nil { return nil, err } db, err := leveldb.OpenFile(fp, nil) if err != nil { return nil, err } return db, nil }
// Open a DB by fileName. func Open(fileName string) (*DB, error) { db, err := leveldb.OpenFile(fileName, nil) if err != nil { return nil, err } return &DB{db}, nil }
func check(count int) bool { var allDB [6]*leveldb.DB var allDBMAP [6]map[string]string var allIterator [6]iterator.Iterator for i := 1; i <= count; i++ { allDBMAP[i] = make(map[string]string) allD, err := leveldb.OpenFile("db_"+strconv.Itoa(i)+".db", nil) if err != nil { panic(err) } else { allDB[i] = allD allIterator[i] = allD.NewIterator(nil, nil) } } for k := 1; k <= count; k++ { for allIterator[k].Next() { allDBMAP[k][string(allIterator[k].Key())] = string(allIterator[k].Value()) } } for k := 1; k < count; k++ { if reflect.DeepEqual(allDBMAP[k], allDBMAP[k+1]) { log.Println(reflect.DeepEqual(allDBMAP[k], allDBMAP[k+1]), k) } else { return false } } return true }
func newTripleStore(path string, options graph.Options) (graph.TripleStore, error) { var qs TripleStore var err error qs.path = path cache_size := DefaultCacheSize if val, ok := options.IntKey("cache_size_mb"); ok { cache_size = val } qs.dbOpts = &opt.Options{ BlockCache: cache.NewLRUCache(cache_size * opt.MiB), } qs.dbOpts.ErrorIfMissing = true write_buffer_mb := DefaultWriteBufferSize if val, ok := options.IntKey("write_buffer_mb"); ok { write_buffer_mb = val } qs.dbOpts.WriteBuffer = write_buffer_mb * opt.MiB qs.writeopts = &opt.WriteOptions{ Sync: false, } qs.readopts = &opt.ReadOptions{} db, err := leveldb.OpenFile(qs.path, qs.dbOpts) if err != nil { glog.Errorln("Error, couldn't open! ", err) return nil, err } qs.db = db glog.Infoln(qs.GetStats()) err = qs.getMetadata() if err != nil { return nil, err } return &qs, nil }
// Resets all logs and term, votedFor values func termReset() { currentTerm, _ := leveldb.OpenFile("$GOPATH/src/github.com/aakashdeshpande/cs733/assignment3/currentTerm", nil) defer currentTerm.Close() // Database to store votedFor voted, _ := leveldb.OpenFile("$GOPATH/src/github.com/aakashdeshpande/cs733/assignment3/votedFor", nil) defer voted.Close() for i := 0; i < len(configs.Peers); i++ { currentTerm.Put([]byte(strconv.FormatInt(int64(i), 10)), []byte(strconv.FormatInt(int64(0), 10)), nil) voted.Put([]byte(strconv.FormatInt(int64(i), 10)), []byte(strconv.FormatInt(int64(-1), 10)), nil) lg, _ := log.Open("$GOPATH/src/github.com/aakashdeshpande/cs733/assignment3/Log" + strconv.Itoa(i)) lg.TruncateToEnd(0) lg.Close() } }
// Resets all logs and term, votedFor values func termReset() { currentTerm, _ := leveldb.OpenFile("currentTerm", nil) defer currentTerm.Close() // Database to store votedFor voted, _ := leveldb.OpenFile("votedFor", nil) defer voted.Close() for i := 0; i < len(configs.Peers); i++ { currentTerm.Put([]byte(strconv.FormatInt(int64(i), 10)), []byte(strconv.FormatInt(int64(0), 10)), nil) voted.Put([]byte(strconv.FormatInt(int64(i), 10)), []byte(strconv.FormatInt(int64(-1), 10)), nil) lg, _ := log.Open("Logs/Log" + strconv.Itoa(i)) lg.TruncateToEnd(0) lg.Close() } }