Skip to content

Conversation

@rahul2393
Copy link
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> ☕️

If you write sample code, please follow the samples format.

This commit adds the foundational SsFormat class that provides sortable
string format (ssformat) encoding utilities. This encoding is used by
Spanner for key ordering and routing.

Key features:
- Composite tag encoding for interleaved tables
- Signed/unsigned integer encoding (increasing/decreasing)
- String and bytes encoding with proper escaping
- Double encoding with proper sign handling
- Timestamp and UUID encoding
- Null value markers with configurable ordering
- TargetRange class for key range representation

Includes unit tests for all encoding functions.

This is part of the experimental location-aware routing for improved
latency.
@rahul2393 rahul2393 requested review from a team as code owners January 8, 2026 05:57
@product-auto-label product-auto-label bot added size: xl Pull request size is extra large. api: spanner Issues related to the googleapis/java-spanner API. labels Jan 8, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @rahul2393, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive system for encoding and caching keys and key ranges within the Google Cloud Spanner client library. It provides a structured way to define key encoding "recipes" based on schema information, efficiently cache these recipes, and then use them to compute precise routing hints for various database operations like reads and mutations. This enhancement is designed to improve the performance and accuracy of key-based operations by standardizing and optimizing the serialization of keys.

Highlights

  • New Key Encoding Logic: Introduced KeyRecipe.java to define how keys and key ranges are encoded into a specific byte format, supporting various data types, ordering, and null handling.
  • Key Recipe Caching: Added KeyRecipeCache.java to efficiently manage and cache KeyRecipe instances, using schema generation for invalidation and fingerprinting for ReadRequest matching.
  • Low-Level Byte Encoding Utilities: Implemented SsFormat.java to provide the core byte-level encoding for primitive types, strings, bytes, timestamps, and UUIDs, essential for the key recipe system.
  • Key Range Representation: Defined TargetRange.java as a data structure to encapsulate start and limit keys for routing, including functionality to merge ranges.
  • Golden Test Suite: Included RecipeGoldenTest.java and RecipeTestCases.java to ensure the correctness of the key encoding and caching logic against predefined golden test data.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@rahul2393 rahul2393 changed the title Feat/key recipe cache feat: experimental location aware routing- Part 3 Jan 8, 2026
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a key recipe cache to improve routing performance. The implementation includes new classes for key encoding (KeyRecipe, SsFormat), caching (KeyRecipeCache), and data representation (TargetRange), along with comprehensive golden tests. My review focuses on correctness and adherence to Java best practices. I've identified critical issues with date validation that could lead to incorrect behavior, and I've also suggested improvements for logging and class design to enhance maintainability and robustness.

Comment on lines +422 to +428
if (month < 1 || month > 12 || day < 1 || day > 31 || hour > 23 || minute > 59 || second > 59) {
throw new IllegalArgumentException("Invalid TIMESTAMP string: " + ts);
}
// Year must be 0000-9999 (year 0 is allowed)
if (year < 0 || year > 9999) {
throw new IllegalArgumentException("Invalid TIMESTAMP string: " + ts);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The date validation for timestamps is incomplete. The current check allows invalid dates like 2023-02-31. This could lead to incorrect behavior when processing invalid timestamp strings.

For more robust validation, I suggest using java.time.LocalDateTime.of() to validate the date and time components together. This will correctly handle complexities like leap years and the number of days in each month.

    // Year must be 0000-9999 (year 0 is allowed)
    if (year < 0 || year > 9999) {
      throw new IllegalArgumentException("Invalid TIMESTAMP string: " + ts);
    }
    try {
      java.time.LocalDateTime.of(year, month, day, hour, minute, second);
    } catch (java.time.DateTimeException e) {
      throw new IllegalArgumentException("Invalid date part in TIMESTAMP string: " + ts, e);
    }

Comment on lines +517 to +522
if (month < 1 || month > 12 || day < 1 || day > 31) {
throw new IllegalArgumentException("Invalid DATE string: " + dateStr);
}
// Year can be 0000-9999 for DATE
if (year < 0 || year > 9999) {
throw new IllegalArgumentException("Invalid DATE string: " + dateStr);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The date validation in validateDate is incomplete. The check day < 1 || day > 31 can allow invalid dates, for example, 2023-02-31. This could lead to incorrect key encoding.

To ensure correct date validation, I recommend using java.time.LocalDate.of() to validate the year, month, and day components.

    // Year can be 0000-9999 for DATE
    if (year < 0 || year > 9999) {
      throw new IllegalArgumentException("Invalid DATE string: " + dateStr);
    }
    try {
      java.time.LocalDate.of(year, month, day);
    } catch (java.time.DateTimeException e) {
      throw new IllegalArgumentException("Invalid DATE string: " + dateStr, e);
    }

}
} catch (IllegalArgumentException e) {
// Log or handle failed recipe creation
System.err.println("Failed to add recipe: " + recipeProto + ", error: " + e.getMessage());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using System.err.println for logging in a library is not recommended as it cannot be configured by the library's users. Please use a logging framework like java.util.logging (JUL) instead. This would allow applications to control log levels and output destinations.

For example: logger.log(Level.WARNING, "Failed to add recipe: " + recipeProto, e);

This should be applied to all System.err.println calls in this file (e.g., lines 101, 114, 122, 131, 141, 146).

Comment on lines +23 to +25
public ByteString start;
public ByteString limit;
public boolean approximate;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The fields start, limit, and approximate are public and mutable. This can lead to unexpected behavior if a TargetRange object is modified from different parts of the code.

Consider making this class immutable by declaring the fields as private final and providing public getter methods. This would make the class thread-safe and its behavior more predictable. If you make this change, the mergeFrom method will also need to be updated to return a new TargetRange instance instead of modifying the existing one.

This commit adds the key recipe system for translating requests to
sortable string format (ssformat) keys for routing.

Key components:
- KeyRecipe: Translates requests to ssformat keys using server-provided recipes
- KeyRecipeCache: Caches recipes by request fingerprint for efficient reuse
- PreparedRead: Helper class for preparing read requests with routing info
- RecipeGoldenTest: Golden tests for recipe encoding validation
- RecipeTestCases: Test case definitions for recipe tests

This is part of the experimental location-aware routing for improved latency.
@rahul2393 rahul2393 force-pushed the feat/key-recipe-cache branch from ef2c8a1 to f71ee0e Compare January 8, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/java-spanner API. size: xl Pull request size is extra large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants