Repository: syncthing/syncthing
File: lib/db/observed.go (Go)

Code before:
	iter, err := db.NewPrefixIterator(prefixKey)
	if err != nil {
		return nil, err
	}
	defer iter.Release()
	oldest = oldest.Round(time.Second)
	var res []string
	for iter.Next() {
		var bs []byte
		var of ObservedFolder
		if bs, err = db.Get(iter.Key()); err != nil {
			l.Infof("Invalid pending folder entry, deleting from database: %x", iter.Key())
		} else if err = of.Unmarshal(bs); err != nil {
			l.Infof("Invalid pending folder entry, deleting from database: %x", iter.Key())
		} else if of.Time.Before(oldest) {
			l.Infof("Removing stale pending folder %v from device %s, last seen %v",
				of.Label, device.Short(), of.Time)
		} else {
			// Keep entries younger or equal to the given timestamp
			continue
		}
		if err := db.Delete(iter.Key()); err != nil {
			l.Warnf("Failed to remove pending folder entry: %v", err)
		}
		folderID := string(db.keyer.FolderFromPendingFolderKey(iter.Key()))
		res = append(res, folderID)
	}
	return res, nil
}

// Consolidated information about a pending folder
type PendingFolder struct {
	OfferedBy map[protocol.DeviceID]ObservedFolder `json:"offeredBy"`
}

func (db *Lowlevel) PendingFolders() (map[string]PendingFolder, error) {
	return db.PendingFoldersForDevice(protocol.EmptyDeviceID)
}

// PendingFoldersForDevice enumerates only entries matching the given device ID, unless it
// is EmptyDeviceID.  Invalid ones are dropped from the database after a warning log
// message, as a side-effect.
func (db *Lowlevel) PendingFoldersForDevice(device protocol.DeviceID) (map[string]PendingFolder, error) {
	var err error
	prefixKey := []byte{KeyTypePendingFolder}
	if device != protocol.EmptyDeviceID {
		prefixKey, err = db.keyer.GeneratePendingFolderKey(nil, device[:], nil)
		if err != nil {
			return nil, err
		}
	}

Review comment (bug):
I think we only want to append in the "expired" case?
Or do we really want to report folders that we can't read/unmarshal/etc as removed?
I guess in theory they are removed, but if Get(iter.Key()) failed, not sure what we are removing?

Code after:
		return nil, err
	}
	iter, err := db.NewPrefixIterator(prefixKey)
	if err != nil {
		return nil, err
	}
	defer iter.Release()
	oldest = oldest.Round(time.Second)
	var res []string
	for iter.Next() {
		var of ObservedFolder
		var folderID string
		if err = of.Unmarshal(iter.Value()); err != nil {
			l.Infof("Invalid pending folder entry, deleting from database: %x", iter.Key())
		} else if of.Time.Before(oldest) {
			folderID = string(db.keyer.FolderFromPendingFolderKey(iter.Key()))
			l.Infof("Removing stale pending folder %s (%s) from device %s, last seen %v",
				folderID, of.Label, device.Short(), of.Time)
		} else {
			// Keep entries younger or equal to the given timestamp
			continue
		}
		if err := db.Delete(iter.Key()); err != nil {
			l.Warnf("Failed to remove pending folder entry: %v", err)
		} else if len(folderID) > 0 {
			res = append(res, folderID)
		}
	}
	return res, nil
}

// Consolidated information about a pending folder
type PendingFolder struct {
	OfferedBy map[protocol.DeviceID]ObservedFolder `json:"offeredBy"`
}

func (db *Lowlevel) PendingFolders() (map[string]PendingFolder, error) {
	return db.PendingFoldersForDevice(protocol.EmptyDeviceID)
}

// PendingFoldersForDevice enumerates only entries matching the given device ID, unless it
// is EmptyDeviceID.  Invalid ones are dropped from the database after a warning log
// message, as a side-effect.
func (db *Lowlevel) PendingFoldersForDevice(device protocol.DeviceID) (map[string]PendingFolder, error) {
	var err error
	prefixKey := []byte{KeyTypePendingFolder}
	if device != protocol.EmptyDeviceID {
		prefixKey, err = db.keyer.GeneratePendingFolderKey(nil, device[:], nil)
		if err != nil {
			return nil, err
		}