Example #1
0
// Save will store the organization to cached memory and disk. On successfull
// save the object will be automatically unlocked.
// NB: If error occure the unlocking of the object need to be done manually.
func (o *Organization) Save() (err error) {
	return database.GetPureDB().Update(func(tx *bolt.Tx) (err error) {
		// open the bucket
		b := tx.Bucket([]byte(OrganizationBucketName))

		// Checks if the bucket was opened, and creates a new one if not existing. Returns error on any other situation.
		if b == nil {
			return errors.New("Missing bucket")
		}

		buf := &bytes.Buffer{}
		encoder := gob.NewEncoder(buf)

		if err = encoder.Encode(o); err != nil {
			return
		}

		data, err := ioutil.ReadAll(buf)
		if err != nil {
			return err
		}

		err = b.Put([]byte(o.Name), data)
		if err != nil {
			return err
		}

		o.Unlock()
		return nil
	})
}
Example #2
0
// ListRegisteredOrganizations will list all the organizations registered in autograder.
func ListRegisteredOrganizations() (out []*Organization) {
	out = make([]*Organization, 0)
	keys := []string{}

	database.GetPureDB().View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(OrganizationBucketName))
		c := b.Cursor()

		for k, _ := c.First(); k != nil; k, _ = c.Next() {
			keys = append(keys, string(k))
		}

		return nil
	})

	for _, key := range keys {
		org, err := NewOrganization(key, true)
		if err != nil {
			log.Println(err)
			continue
		}

		out = append(out, org)
	}

	return
}
Example #3
0
// ListAllMembers lists all members stored in the system.
func ListAllMembers() (out []*Member) {
	out = make([]*Member, 0)
	keys := []string{}

	database.GetPureDB().View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(MemberBucketName))
		if b == nil {
			return errors.New("Unable to find bucket")
		}
		c := b.Cursor()

		for k, _ := c.First(); k != nil; k, _ = c.Next() {
			keys = append(keys, string(k))
		}

		return nil
	})

	for _, key := range keys {
		m, err := NewMemberFromUsername(key, true)
		if err != nil {
			continue
		}

		out = append(out, m)
	}

	return
}
Example #4
0
func (br *BuildResult) loadStoredData(lock bool) error {
	return database.GetPureDB().View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(BuildBucketName))
		if b == nil {
			return errors.New("Bucket not found. Are you sure the bucket was registered correctly?")
		}

		data := b.Get([]byte(strconv.Itoa(br.ID)))
		if data == nil {
			return errors.New("No data in database")
		}

		buf := &bytes.Buffer{}
		decoder := gob.NewDecoder(buf)

		n, _ := buf.Write(data)

		if n != len(data) {
			return errors.New("Couldn't write all data to buffer while getting data from database. " + strconv.Itoa(n) + " != " + strconv.Itoa(len(data)))
		}

		err := decoder.Decode(br)
		if err != nil {
			return err
		}

		return nil
	})
}
Example #5
0
// Save will store the build results to the database.
func (br *BuildResult) Save() error {
	return database.GetPureDB().Update(func(tx *bolt.Tx) error {
		// open the bucket
		b := tx.Bucket([]byte(BuildBucketName))

		// Checks if the bucket was opened, and creates a new one if not existing. Returns error on any other situation.
		if b == nil {
			return errors.New("Missing bucket")
		}

		buf := &bytes.Buffer{}
		encoder := gob.NewEncoder(buf)

		if err := encoder.Encode(br); err != nil {
			return err
		}

		data, err := ioutil.ReadAll(buf)
		if err != nil {
			return err
		}

		err = b.Put([]byte(strconv.Itoa(br.ID)), data)
		if err != nil {
			return err
		}

		return nil
	})
}
Example #6
0
// Delete will remove the group object.
func (g *Group) Delete() error {
	for username := range g.Members {
		user, err := NewMemberFromUsername(username, true)
		if err != nil {
			continue
		}

		courseopt := user.Courses[g.Course]
		if courseopt.GroupNum == g.ID {
			user.Lock()
			courseopt.IsGroupMember = false
			courseopt.GroupNum = 0
			user.Courses[g.Course] = courseopt
			if err = user.Save(); err != nil {
				user.Unlock()
				log.Println(err)
			}
		}
	}

	delete(InMemoryGroups, g.ID)

	return database.GetPureDB().Update(func(tx *bolt.Tx) error {
		return tx.Bucket([]byte(GroupsBucketName)).Delete([]byte(strconv.Itoa(g.ID)))
	})
}
Example #7
0
// loadData loads data from storage if it exists.
func (m *Member) loadStoredData(lock bool) (err error) {
	err = database.GetPureDB().View(func(tx *bolt.Tx) error {
		// locks the object directly in order to ensure consistent info from DB.
		if lock {
			m.Lock()
		}

		b := tx.Bucket([]byte(MemberBucketName))
		if b == nil {
			return errors.New("Bucket not found. Are you sure the bucket was registered correctly?")
		}

		data := b.Get([]byte(m.Username))
		if data == nil {
			return errors.New("No data in database")
		}

		buf := &bytes.Buffer{}
		decoder := gob.NewDecoder(buf)

		n, _ := buf.Write(data)

		if n != len(data) {
			return errors.New("Couldn't write all data to buffer while getting data from database. " + strconv.Itoa(n) + " != " + strconv.Itoa(len(data)))
		}

		err = decoder.Decode(m)
		if err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		if err.Error() == "No data in database" {
			err = nil
		}
	}

	if !m.accessToken.HasTokenInStore() {
		m.accessToken.SetUsernameToTokenInStore(m.Username)
	}

	return
}
Example #8
0
// GetNextBuildID will find the next available build ID.
// returns -1 on error
func GetNextBuildID() int {
	nextid := -1
	if err := database.GetPureDB().Update(func(tx *bolt.Tx) error {
		// open the bucket
		b := tx.Bucket([]byte(BuildBucketName))

		// Checks if the bucket was opened, and creates a new one if not existing. Returns error on any other situation.
		if b == nil {
			return errors.New("Missing bucket")
		}

		var err error
		data := b.Get([]byte(BuildLengthKey))
		if data == nil {
			nextid = 0
		} else {
			nextid, err = strconv.Atoi(string(data))
			if err != nil {
				return err
			}
		}

		nextid++

		data = []byte(strconv.Itoa(nextid))

		err = b.Put([]byte(BuildLengthKey), data)
		if err != nil {
			return err
		}

		return nil

	}); err != nil {
		return -1
	}

	return nextid
}
Example #9
0
// LoadStoredData fetches the organization data stored on disk or in cached memory.
func (o *Organization) LoadStoredData(lock bool) (err error) {
	database.GetPureDB().View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(OrganizationBucketName))
		if b == nil {
			return errors.New("Bucket not found. Are you sure the bucket was registered correctly?")
		}

		data := b.Get([]byte(o.Name))
		if data == nil {
			return errors.New("No data in database")
		}

		buf := &bytes.Buffer{}
		decoder := gob.NewDecoder(buf)

		n, _ := buf.Write(data)

		if n != len(data) {
			return errors.New("Couldn't write all data to buffer while getting data from database. " + strconv.Itoa(n) + " != " + strconv.Itoa(len(data)))
		}

		err = decoder.Decode(o)
		if err != nil {
			return err
		}

		return nil
	})

	// locks the object directly in order to ensure consistent info from DB.
	if lock {
		o.Lock()
	}

	return
}