Search code examples
jsondatabasegoboltdb

How can I ensure that all of my write transaction functions get resolved in order? Also, why is the else function not executing?


I'm trying to create a very simple Bolt database called "ledger.db" that includes one Bucket, called "Users", which contains Usernames as a Key and Balances as the value that allows users to transfer their balance to one another. I am using Bolter to view the database in the command line

There are two problems, both contained in this transfer function issue resides in the transfer function.

The First: Inside the transfer function is an if/else. If the condition is true, it executes as it should. If it's false, nothing happens. There's no syntax errors and the program runs as though nothing is wrong, it just doesn't execute the else statement.

The Second: Even if the condition is true, when it executes, it doesn't update BOTH the respective balance values in the database. It updates the balance of the receiver, but it doesn't do the same for the sender. The mathematical operations are completed and the values are marshaled into a JSON-compatible format.

The problem is that the sender balance is not updated in the database.

Everything from the second "Success!" fmt.Println() function onward is not processed

I've tried changing the "db.Update()" to "db.Batch()". I've tried changing the order of the Put() functions. I've tried messing with goroutines and defer, but I have no clue how to use those, as I am rather new to golang.

func (from *User) transfer(to User, amount int) error{
        var fbalance int = 0
        var tbalance int = 0

        db, err := bolt.Open("ledger.db", 0600, nil)
        if err != nil {
              log.Fatal(err)
        }
        defer db.Close()



        return db.Update(func(tx *bolt.Tx) error {
              uBuck := tx.Bucket([]byte("Users"))

              json.Unmarshal(uBuck.Get([]byte(from.username)), &fbalance)
              json.Unmarshal(uBuck.Get([]byte(to.username)), &tbalance)

              if (amount <= fbalance) {
                fbalance = fbalance - amount

                encoded, err := json.Marshal(fbalance)
                if err != nil {
                  return err
                }

                tbalance = tbalance + amount

                encoded2, err := json.Marshal(tbalance)
                if err != nil {
                  return err
                }

                fmt.Println("Success!")

                c := uBuck

                err = c.Put([]byte(to.username), encoded2)
                return err

                fmt.Println("Success!")

                err = c.Put([]byte(from.username), encoded)
                return err

                fmt.Println("Success!")

              } else {
                return fmt.Errorf("Not enough in balance!", amount)
              }



            return nil


        })




        return nil
}

func main() {
    /*
    db, err := bolt.Open("ledger.db", 0600, nil)
    if err != nil {
          log.Fatal(err)
        }
    defer db.Close()
    */

    var b User = User{"Big", "jig", 50000, 0}
    var t User = User{"Trig", "pig", 40000, 0}

    // These two functions add each User to the database, they aren't 
    // the problem
    b.createUser()
    t.createUser()

    /*
    db.View(func(tx *bolt.Tx) error {
      c := tx.Bucket([]byte("Users"))
      get := c.Get([]byte(b.username))
      fmt.Printf("The return value %v",get)

      return nil

    })
    */

    t.transfer(b, 40000)


}

I expect the database to show Big:90000 Trig:0 from the beginning values of Big:50000 Trig:40000

Instead, the program outputs Big:90000 Trig:40000


Solution

  • You return unconditionally:

    c := uBuck
    
    err = c.Put([]byte(to.username), encoded2)
    return err
    
    fmt.Println("Success!")
    
    err = c.Put([]byte(from.username), encoded)
    return err
    
    fmt.Println("Success!")
    

    You are not returning and checking errors.

    json.Unmarshal(uBuck.Get([]byte(from.username)), &fbalance)
    json.Unmarshal(uBuck.Get([]byte(to.username)), &tbalance)
    
    t.transfer(b, 40000)
    

    And so on.


    Debug your code statement by statement.