Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lazy initializer #423

Merged
merged 2 commits into from Sep 15, 2020
Merged

feat: add lazy initializer #423

merged 2 commits into from Sep 15, 2020

Conversation

olavloite
Copy link
Contributor

@olavloite olavloite commented Sep 12, 2020

Adds a LazyInitializer for Spanner instances that can be used in environments where it is desirable to initialize one instance once, and reuse this for multiple purposes, such as for example Google Cloud Functions.

This change would allow us to simplify the Google Cloud Functions Java sample.

Towards GoogleCloudPlatform/java-docs-samples#2862.

@google-cla google-cla bot added the cla: yes label Sep 12, 2020
@olavloite olavloite requested review from skuruppu and thiagotnunes Sep 12, 2020
@codecov
Copy link

@codecov codecov bot commented Sep 12, 2020

Codecov Report

Merging #423 into master will increase coverage by 0.00%.
The diff coverage is 87.50%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #423   +/-   ##
=========================================
  Coverage     82.16%   82.17%           
- Complexity     2455     2460    +5     
=========================================
  Files           136      138    +2     
  Lines         13589    13605   +16     
  Branches       1307     1309    +2     
=========================================
+ Hits          11166    11180   +14     
- Misses         1895     1896    +1     
- Partials        528      529    +1     
Impacted Files Coverage Δ Complexity Δ
...m/google/cloud/spanner/LazySpannerInitializer.java 50.00% <50.00%> (ø) 1.00 <1.00> (?)
.../google/cloud/spanner/AbstractLazyInitializer.java 92.85% <92.85%> (ø) 4.00 <4.00> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6154008...be9ddd1. Read the comment docs.

@product-auto-label product-auto-label bot added the api: spanner label Sep 13, 2020
* Generic {@link AbstractLazyInitializer} for any heavy-weight object that might throw an exception
* during initialization. The underlying object is initialized at most once.
*/
public abstract class AbstractLazyInitializer<T> {
Copy link
Contributor

@thiagotnunes thiagotnunes Sep 14, 2020

Choose a reason for hiding this comment

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

Could we use composition instead of inheritance (pass in the initialiser function instead of overriding it)?

Copy link
Contributor Author

@olavloite olavloite Sep 14, 2020

Choose a reason for hiding this comment

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

That would be possible, but as we have to support Java 7, that would mean that we would have to use com.google.cloud.base.Function in the public interface. I think that it is more idiomatic to use inheritance than composition for Java 7 (once we drop Java 7 support, I would agree that using composition would be a lot nicer in this case).

Copy link
Contributor

@thiagotnunes thiagotnunes left a comment

LGTM

@olavloite olavloite merged commit e8522b9 into master Sep 15, 2020
18 checks passed
@olavloite olavloite deleted the lazy-initializer branch Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants