Example #1
0
// NewEntry creates a record from the provided data, appends that record onto
// the end of the mlog, then returns the created record.
func (b *BoltStore) NewEntry(message []byte, remoteAddr string) (*mlog.Record, error) {
	tx, err := b.conn.Begin(true)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	// no need to sync b/c the conn.Begin(true) call will block
	bucket := tx.Bucket(bucketName)

	record := mlog.NewRecord(message, remoteAddr)
	record.Index, err = bucket.NextSequence()
	if err != nil {
		return nil, err
	}

	key := make([]byte, 8)
	binary.BigEndian.PutUint64(key, record.Index)
	val, err := record.MarshalMsg(nil) // TODO nil will alloc for us; keep this zero-alloc
	if err != nil {
		return nil, err
	}

	if err = bucket.Put(key, val); err != nil {
		return nil, err
	}

	if err = tx.Commit(); err != nil {
		return nil, err
	}

	return record, nil
}
Example #2
0
// NewEntry creates a record from the provided data, appends that record onto
// the end of the mlog, then returns the created record.
func (s *memMessageLog) NewEntry(message []byte, remoteAddr string) (*mlog.Record, error) {
	s.lock.Lock()

	record := mlog.NewRecord(message, remoteAddr)
	record.Index = uint64(len(s.j) + 1)

	s.j = append(s.j, record)

	s.lock.Unlock()
	return record, nil
}