Esempio n. 1
0
// GetRecentRolls retrieves the most recent N issues from the database.
func (d *db) GetRecentRolls(N int) ([]*autoroll.AutoRollIssue, error) {
	var rv []*autoroll.AutoRollIssue
	if err := d.db.View(func(tx *bolt.Tx) error {
		// Retrieve the issue keys from the by-date bucket.
		byDate := tx.Bucket(BUCKET_ROLLS_BY_DATE)
		c := byDate.Cursor()
		keys := make([][]byte, 0, N)
		for k, v := c.Last(); k != nil && len(keys) < N; k, v = c.Prev() {
			keys = append(keys, v)
		}

		// Retrieve the issues themselves.
		b := tx.Bucket(BUCKET_ROLLS)
		rv = make([]*autoroll.AutoRollIssue, 0, len(keys))
		for _, k := range keys {
			serialized := b.Get(k)
			if serialized == nil {
				return fmt.Errorf("DB consistency error: bucket %s contains data not present in bucket %s!", BUCKET_ROLLS_BY_DATE, BUCKET_ROLLS)
			}
			var a autoroll.AutoRollIssue
			if err := json.Unmarshal(serialized, &a); err != nil {
				return err
			}
			a.Created = a.Created.UTC()
			a.Modified = a.Modified.UTC()
			rv = append(rv, &a)
		}
		return nil
	}); err != nil {
		return nil, err
	}
	return rv, nil
}
Esempio n. 2
0
// GetRoll retrieves the given issue from the database.
func (d *db) GetRoll(issue int64) (*autoroll.AutoRollIssue, error) {
	var a *autoroll.AutoRollIssue
	if err := d.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(BUCKET_ROLLS)

		serialized := b.Get(issueToRollKey(issue))
		if serialized == nil {
			return nil
		}
		a = &autoroll.AutoRollIssue{}
		if err := json.Unmarshal(serialized, a); err != nil {
			return err
		}
		a.Created = a.Created.UTC()
		a.Modified = a.Modified.UTC()
		return nil
	}); err != nil {
		return nil, err
	}
	return a, nil
}