apache / apache/pulsar

PIP-197: Add Schema hash and equals to public API

Open
#16,959 1 comment 0 reactions 0 assignees View on GitHub
Stale type/PIP
Dominant language
Java
Stars
15.3k
Forks
3.8k
Avg merge
1d 14h
Merged PRs (30d)
160

Description

ML discussion thread: https://lists.apache.org/thread/brf946dgzojlqkd8y9s2h11b0rg47qcd

## Motivation

Currently, the `Schema` interface in the public client-api does not provide access to a sensible hash function. The fallback to Java’s object-equality makes it unfit for use in most hash-based collections. For example, it prevents usage as a key in a cache.
Further, the lack of a reliable equals function means that there is no way to identify if two schemas are the same thing.

## Goal

The goal of this proposal is to provide a sensible `hashCode` and `equals` implementation for Schema as part of the public API.

Currently, pulsar-common contains `SchemaHash`, a wrapper class that exists to solve the aforementioned problems. However, `SchemaHash` is not part of the public API, so users should not depend on it.

## API Changes

There is no further change required as moving `SchemaHash` from pulsar-common to the public API. The only further change could be to re-think the class name, as the wrapper offers more than just a schemas hash.

## Implementation

Move `SchemaHash` from pulsar-common `org.apache.pulsar.common.protocol.schema` package into pulsar-client-api `org.apache.pulsar.common.schema` package.

## Reject Alternatives

Providing default methods for `equals` and `hashCode` directly on the `Schema` interface is not possible because Java prohibits overriding the base Object methods.
Another option would be to provide the `hashCode` and `equals` functionality through similarly-named default methods that could be used by any `Schema` implementation. The drawback of this idea is that it requires developers to override the equals and hashCode to use these provided methods, as well as possibly polluting the interface.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing SchemaHash in the org.apache.pulsar.common.protocol.schema package and the Schema API in pulsar-client-api. Confirm the proposed move to org.apache.pulsar.common.schema and review the linked mailing-list discussion for the agreed API direction. Done means the schema hash and equality functionality is available through the public client API.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.