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

Code before:

func (t readOnlyTransaction) getFileByKey(key []byte) (protocol.FileInfo, bool) {
	if f, ok := t.getFileTrunc(key, false); ok {
		return f.(protocol.FileInfo), true
	}
	return protocol.FileInfo{}, false
}

func (t readOnlyTransaction) getFileTrunc(key []byte, trunc bool) (FileIntf, bool) {
	bs, err := t.Get(key, nil)
	if err == leveldb.ErrNotFound {
		return nil, false
	}
	if err != nil {
		l.Debugln("surprise error:", err)
		return nil, false
	}
	f, err := unmarshalTrunc(bs, trunc)
	if err != nil {
		l.Debugln("unmarshal error:", err)
		return nil, false
	}
	return f, true
}

func (t readOnlyTransaction) getGlobal(buf, folder, file []byte, truncate bool) ([]byte, FileIntf, bool) {
	buf = t.db.keyer.GenerateGlobalVersionKey(buf, folder, file)

	bs, err := t.Get(buf, nil)
	if err != nil {
		return buf, nil, false
	}

	vl, ok := unmarshalVersionList(bs)
	if !ok {
		return buf, nil, false
	}

	buf = t.db.keyer.GenerateDeviceFileKey(buf, folder, vl.Versions[0].Device, file)
	if fi, ok := t.getFileTrunc(buf, truncate); ok {
		return buf, fi, true
	}

	return buf, nil, false
}

// A readWriteTransaction is a readOnlyTransaction plus a batch for writes.
// The batch will be committed on close() or by checkFlush() if it exceeds the
// batch size.
type readWriteTransaction struct {
	readOnlyTransaction

Review comment (suggestion):
So `buf` in the calling function is fine, but calling it `buf` in here makes no sense. Maybe as the parameter name it's ok, because it's a random buffer of sorts and not actually the key "on the way in" - but then the assign on the next line should be `key :=` and that's what we use in the rest of the function and return. Other things like `t.Get(buf, nil)` looks like we're getting stuff into the buf or whatever.


Code after:

func (t readOnlyTransaction) getFileByKey(key []byte) (protocol.FileInfo, bool) {
	if f, ok := t.getFileTrunc(key, false); ok {
		return f.(protocol.FileInfo), true
	}
	return protocol.FileInfo{}, false
}

func (t readOnlyTransaction) getFileTrunc(key []byte, trunc bool) (FileIntf, bool) {
	bs, err := t.Get(key, nil)
	if err == leveldb.ErrNotFound {
		return nil, false
	}
	if err != nil {
		l.Debugln("surprise error:", err)
		return nil, false
	}
	f, err := unmarshalTrunc(bs, trunc)
	if err != nil {
		l.Debugln("unmarshal error:", err)
		return nil, false
	}
	return f, true
}

func (t readOnlyTransaction) getGlobal(keyBuf, folder, file []byte, truncate bool) ([]byte, FileIntf, bool) {
	keyBuf = t.db.keyer.GenerateGlobalVersionKey(keyBuf, folder, file)

	bs, err := t.Get(keyBuf, nil)
	if err != nil {
		return keyBuf, nil, false
	}

	vl, ok := unmarshalVersionList(bs)
	if !ok {
		return keyBuf, nil, false
	}

	keyBuf = t.db.keyer.GenerateDeviceFileKey(keyBuf, folder, vl.Versions[0].Device, file)
	if fi, ok := t.getFileTrunc(keyBuf, truncate); ok {
		return keyBuf, fi, true
	}

	return keyBuf, nil, false
}

// A readWriteTransaction is a readOnlyTransaction plus a batch for writes.
// The batch will be committed on close() or by checkFlush() if it exceeds the
// batch size.
type readWriteTransaction struct {
	readOnlyTransaction