Esempio n. 1
0
func init() {
	if os.Getenv("CREATE_BENCHDATA") != "true" {
		return
	}

	// Put data to the database for the benchmark.
	db, err := bolt.Open(benchmarkDB, 0666, nil)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	err = db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucket([]byte(shared.DefaultBucketName))
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		panic(err)
	}
	data, err := proto.Marshal(shared.NewSession([]byte{}, shared.DefaultMaxAge))
	if err != nil {
		panic(err)
	}

	fmt.Printf("Start putting data for the benchmark %+v\n", time.Now())

	for i := 0; i < 100; i++ {
		err = db.Update(func(tx *bolt.Tx) error {
			bucket := tx.Bucket([]byte(shared.DefaultBucketName))
			for j := 0; j < 100000; j++ {
				if err := bucket.Put([]byte(strconv.Itoa(100000*i+j)), data); err != nil {
					return err
				}
			}
			return nil
		})
		if err != nil {
			panic(err)
		}
		if (i+1)%10 == 0 {
			fmt.Printf("%d key-values were put.\n", (i+1)*100000)
		}
	}

	fmt.Printf("End putting data for the benchmark %+v\n", time.Now())
	if err != nil {
		panic(err)
	}
}
Esempio n. 2
0
// save stores the session data in the database.
func (s *Store) save(session *sessions.Session) error {
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err := enc.Encode(session.Values)
	if err != nil {
		return err
	}
	data, err := proto.Marshal(shared.NewSession(buf.Bytes(), session.Options.MaxAge))
	if err != nil {
		return err
	}
	err = s.db.Update(func(tx *bolt.Tx) error {
		return tx.Bucket(s.config.DBOptions.BucketName).Put([]byte(session.ID), data)
	})
	return err
}
Esempio n. 3
0
func Test_reap(t *testing.T) {
	// When the target bucket does not exist
	db, err := bolt.Open("./sessions.db", 0666, nil)
	if err != nil {
		t.Error(err.Error())
	}
	defer db.Close()
	options := Options{}
	options.setDefault()
	quitC, doneC := make(chan struct{}), make(chan struct{})
	go reap(db, options, quitC, doneC)
	time.Sleep(2 * time.Second)
	Quit(quitC, doneC)

	// When no keys exist
	bucketName := []byte(fmt.Sprintf("reapTest-%d", time.Now().Unix()))
	err = db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucketIfNotExists(bucketName)
		return err
	})
	if err != nil {
		t.Error(err.Error())
	}

	options.BucketName = bucketName

	go reap(db, options, quitC, doneC)
	time.Sleep(2 * time.Second)
	Quit(quitC, doneC)

	// When shared.Session returns an error
	err = db.Update(func(tx *bolt.Tx) error {
		session := shared.NewSession([]byte{}, -1)
		data, err := proto.Marshal(session)
		if err != nil {
			return err
		}
		return tx.Bucket(bucketName).Put([]byte("test"), data)
	})
	if err != nil {
		t.Error(err.Error())
	}
	go reap(db, options, quitC, doneC)
	time.Sleep(2 * time.Second)
	Quit(quitC, doneC)

	// When the target session is expired
	err = db.Update(func(tx *bolt.Tx) error {
		err := tx.Bucket(bucketName).Delete([]byte("test"))
		if err != nil {
			return err
		}
		session := shared.NewSession([]byte{}, -1)
		data, err := proto.Marshal(session)
		if err != nil {
			return err
		}
		return tx.Bucket(bucketName).Put([]byte("test"), data)
	})
	if err != nil {
		t.Error(err.Error())
	}
	go reap(db, options, quitC, doneC)
	time.Sleep(2 * time.Second)
	Quit(quitC, doneC)

	// When options.BatchSize == i
	err = db.Update(func(tx *bolt.Tx) error {
		err := tx.Bucket(bucketName).Delete([]byte("test"))
		if err != nil {
			return err
		}
		session := shared.NewSession([]byte{}, 60*60)
		data, err := proto.Marshal(session)
		if err != nil {
			return err
		}
		err = tx.Bucket(bucketName).Put([]byte("test1"), data)
		if err != nil {
			return err
		}
		err = tx.Bucket(bucketName).Put([]byte("test2"), data)
		if err != nil {
			return err
		}
		err = tx.Bucket(bucketName).Put([]byte("test3"), data)
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		t.Error(err.Error())
	}
	options.BatchSize = 3
	go reap(db, options, quitC, doneC)
	time.Sleep(2 * time.Second)
	Quit(quitC, doneC)
}