Repository: codenotary/immudb
File: pkg/server/uuid.go (Go)

Code before:
	"google.golang.org/grpc/metadata"
)

// IDENTIFIER_FNAME ...
const IDENTIFIER_FNAME = "immudb.identifier"

// SERVER_UUID_HEADER ...
const SERVER_UUID_HEADER = "immudb-uuid"

type uuidContext struct {
	UUID xid.ID
}

// UUIDContext manage UUID context
type UUIDContext interface {
	UUIDStreamContextSetter(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error
	UUIDContextSetter(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
}

// NewUUIDContext return a new UUId context servive
func NewUUIDContext(id xid.ID) uuidContext {
	return uuidContext{id}
}

// getOrSetUUID either reads the identifier file or creates it if it doesn't exist
// unless useExternalIdentifier is set to true, in which case the local identifier file is ignored.
// In earlier versions, the file was located inside default DB directory. Now, it
// is moved to the data directory. This function migrates the file to data directory
// in case it exists in the default db directory.
func getOrSetUUID(dataDir, defaultDbDir string, useExternalIdentifier bool) (xid.ID, error) {
	if useExternalIdentifier {
		return xid.ID{}, nil
	}

	fileInDataDir := path.Join(dataDir, IDENTIFIER_FNAME)
	if fileExists(fileInDataDir) {
		return getUUID(fileInDataDir)
	}

	fileInDefaultDbDir := path.Join(defaultDbDir, IDENTIFIER_FNAME)
	if fileExists(fileInDefaultDbDir) {
		guid, err := getUUID(fileInDefaultDbDir)
		if err != nil {
			return guid, err
		}

		err = moveUUIDFile(guid, fileInDataDir, fileInDefaultDbDir)
		return guid, err
	}

	guid := xid.New()

Review comment (suggestion):
I think the local identifier has higher precedence. And if present, it should match the one fetched from remote storage.
If the local identifier is not present, it then could use the remote one if instructed.

Code after:
	"google.golang.org/grpc/metadata"
)

// IDENTIFIER_FNAME ...
const IDENTIFIER_FNAME = "immudb.identifier"

// SERVER_UUID_HEADER ...
const SERVER_UUID_HEADER = "immudb-uuid"

type uuidContext struct {
	UUID xid.ID
}

// UUIDContext manage UUID context
type UUIDContext interface {
	UUIDStreamContextSetter(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error
	UUIDContextSetter(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
}

// NewUUIDContext return a new UUId context servive
func NewUUIDContext(id xid.ID) uuidContext {
	return uuidContext{id}
}

// getOrSetUUID either reads the identifier file or creates it if it doesn't exist
// unless useExternalIdentifier is set to true, in which case the local identifier file is ignored.
// In earlier versions, the file was located inside default DB directory. Now, it
// is moved to the data directory. This function migrates the file to data directory
// in case it exists in the default db directory.
func getOrSetUUID(dataDir, defaultDbDir string, useExternalIdentifier bool) (xid.ID, error) {
	fileInDataDir := path.Join(dataDir, IDENTIFIER_FNAME)
	if fileExists(fileInDataDir) {
		return getUUID(fileInDataDir)
	}

	fileInDefaultDbDir := path.Join(defaultDbDir, IDENTIFIER_FNAME)
	if fileExists(fileInDefaultDbDir) {
		guid, err := getUUID(fileInDefaultDbDir)
		if err != nil {
			return guid, err
		}

		err = moveUUIDFile(guid, fileInDataDir, fileInDefaultDbDir)
		return guid, err
	}

	if useExternalIdentifier {
		return xid.ID{}, nil
	}

	guid := xid.New()