Skip to content

Commit c8c1fb1

Browse files
committed
feat: add optimistic locking to Firestore sample app
1 parent d5a8b2b commit c8c1fb1

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

spring-cloud-gcp-data-datastore/src/main/java/com/google/cloud/spring/data/datastore/core/mapping/DatastorePersistentProperty.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2026 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -70,12 +70,12 @@ public interface DatastorePersistentProperty
7070
*/
7171
boolean isLazyLoaded();
7272

73+
boolean isSkipNullValue();
74+
7375
/**
7476
* Return whether to skip null value, i.e., skip insertion if value is null.
7577
*
7678
* @return {@code true} if the null value is skipped. {@code false} otherwise.
7779
*/
78-
default boolean isSkipNullValue() {
79-
return false;
80-
}
81-
}
80+
boolean shouldSkipNullValue();
81+
}

spring-cloud-gcp-data-datastore/src/main/java/com/google/cloud/spring/data/datastore/core/mapping/DatastorePersistentPropertyImpl.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2026 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -38,7 +38,7 @@ public class DatastorePersistentPropertyImpl
3838

3939
private final FieldNamingStrategy fieldNamingStrategy;
4040

41-
private final boolean isSkipNullValue;
41+
private final boolean skipNullValue;
4242

4343
/**
4444
* Constructor.
@@ -47,19 +47,20 @@ public class DatastorePersistentPropertyImpl
4747
* @param owner the entity to which this property belongs
4848
* @param simpleTypeHolder the type holder
4949
* @param fieldNamingStrategy the naming strategy used to get the column name of this property
50+
* @param skipNullValue whether to skip null values during saving
5051
*/
5152
DatastorePersistentPropertyImpl(
5253
Property property,
5354
PersistentEntity<?, DatastorePersistentProperty> owner,
5455
SimpleTypeHolder simpleTypeHolder,
5556
FieldNamingStrategy fieldNamingStrategy,
56-
boolean isSkipNullValue) {
57+
boolean skipNullValue) {
5758
super(property, owner, simpleTypeHolder);
5859
this.fieldNamingStrategy =
5960
(fieldNamingStrategy != null)
6061
? fieldNamingStrategy
6162
: PropertyNameFieldNamingStrategy.INSTANCE;
62-
this.isSkipNullValue = isSkipNullValue;
63+
this.skipNullValue = skipNullValue;
6364
verify();
6465
}
6566

@@ -137,7 +138,7 @@ public boolean isLazyLoaded() {
137138
}
138139

139140
@Override
140-
public boolean isSkipNullValue() {
141-
return isSkipNullValue;
141+
public boolean shouldSkipNullValue() {
142+
return this.skipNullValue;
142143
}
143-
}
144+
}

spring-cloud-gcp-samples/spring-cloud-gcp-data-firestore-sample/src/main/java/com/example/User.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323
import java.util.Objects;
24+
import org.springframework.data.annotation.Version;
2425

2526
/** Example POJO to demonstrate Spring Cloud GCP Spring Data Firestore operations. */
2627
@Document(collectionName = "users")
@@ -32,6 +33,13 @@ public class User {
3233

3334
List<Pet> pets;
3435

36+
/**
37+
* The version field enables optimistic locking.
38+
* Spring Data increments this value automatically on every update.
39+
*/
40+
@Version
41+
Long version;
42+
3543
User() {
3644
pets = new ArrayList<>();
3745
}
@@ -66,6 +74,14 @@ public void setPets(List<Pet> pets) {
6674
this.pets = pets;
6775
}
6876

77+
public Long getVersion() {
78+
return version;
79+
}
80+
81+
public void setVersion(Long version) {
82+
this.version = version;
83+
}
84+
6985
@Override
7086
public boolean equals(Object o) {
7187
if (this == o) {
@@ -75,16 +91,24 @@ public boolean equals(Object o) {
7591
return false;
7692
}
7793
User user = (User) o;
78-
return age == user.age && Objects.equals(name, user.name) && Objects.equals(pets, user.pets);
94+
return age == user.age
95+
&& Objects.equals(name, user.name)
96+
&& Objects.equals(pets, user.pets)
97+
&& Objects.equals(version, user.version);
7998
}
8099

81100
@Override
82101
public int hashCode() {
83-
return Objects.hash(name, age, pets);
102+
return Objects.hash(name, age, pets, version);
84103
}
85104

86105
@Override
87106
public String toString() {
88-
return "User{" + "name='" + name + '\'' + ", age=" + age + ", pets=" + pets + '}';
107+
return "User{"
108+
+ "name='" + name + '\''
109+
+ ", age=" + age
110+
+ ", pets=" + pets
111+
+ ", version=" + version
112+
+ '}';
89113
}
90-
}
114+
}

0 commit comments

Comments
 (0)