// Save saves an Entry in the database. func (e *Entry) Save() error { if !database.Main.Opened { return fmt.Errorf("db must be opened before saving") } return database.Main.DB.Update(func(tx *bolt.Tx) error { var err error var b *bolt.Bucket var enc []byte var id uint64 if b, err = tx.CreateBucketIfNotExists([]byte(Bucket)); err != nil { return fmt.Errorf("Error creating bucket : %s", err) } if enc, err = e.Encode(); err != nil { return fmt.Errorf("Could not encode : %s", err) } if e.ID == 0 { if id, err = b.NextSequence(); err != nil { return fmt.Errorf("Could not generate ID : %s", err) } e.ID = int(id) } return b.Put([]byte(strconv.Itoa(e.ID)), enc) }) }
// NOTE: Definitely definitely only call this from inside a transaction func newGameID(b *bolt.Bucket) gameID { id, err := b.NextSequence() if err != nil { // Screw it, they're getting a random ID return gameID(genName(64)) } return gameID(strconv.FormatUint(id, 16)) }