-
Notifications
You must be signed in to change notification settings - Fork 12
Stand-in-memory #185
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
Stand-in-memory #185
Changes from all commits
646964a
d29cc4d
6405863
1c78731
5e9077f
ae74d27
8037a41
5ba27cd
d5248f2
b17a1db
b089546
7ef1d7a
49d09a2
f4f610a
79e723a
995f3a7
461d6b9
dd879bf
9ab0bce
4b19a14
b6feb01
491ced2
e82f6e2
b52fc7f
1f1b324
7a4da06
e75ddf2
a7f715b
5261f5b
a8ae84d
327a436
c3f8468
1f2270a
5531914
f2e79b4
8d06b0b
caa6c59
bfb9e96
9b1d55b
604265b
65b04b0
ce9c2b4
727edd8
6efcf89
e3f497a
fb8a89d
de6e6bd
c4a142d
062b6f8
1598bd6
a37aaa9
a015559
f8db697
fde5d75
1e417a9
a12f5a0
5fcdd2f
ede4059
96df5a0
afe3725
2eee97d
1989a93
8555920
a252995
c3c046d
c196ec5
3ddc163
a7ed2f5
9e60556
521145d
b6e841c
ecc76ac
b9971fc
128f50a
c8abf39
bee4cf4
f0047a6
8854aa0
dcc7f86
2e9857f
0f91aa7
6d6f3c2
73e1d1e
10d63eb
f0dcda5
311be3d
9834f41
2b765fe
50de0a9
af7fc02
a4e8fd9
1f3c4a0
855f820
1cea140
80b710f
fb20049
8bf368f
7525ce0
021490c
0f1a1b0
b450c9b
179767e
8840361
be594a2
1960f7f
1c45f5c
76b1589
c4e31b2
d804eca
73c6cbf
6f8bd40
3f059a2
9d7c432
8a10bab
dc4dbaa
4527a4f
9fb8748
4927555
56b1745
68d0da9
0f9ddb7
70c1180
e91e1e7
447dbcd
40c9a08
d134c26
1f17a8d
ffb4ce0
ac7eefc
158ede1
8956f6f
3a1a29b
6514e04
9a1225c
ffb572a
55ba8d1
a3b01f4
a8f55d0
48714b5
cae736f
76d236e
b812937
cd6ec22
92f3369
2e1854d
c799df9
81adba6
57eb094
b390ba3
b645edb
8b0e6cd
9be366a
d6a336f
07df1e5
8e0ae41
34bf838
d6a4c05
f54def5
c9c97c5
ba51276
1547d4e
373c91b
f401855
ddbf154
21676be
eede00d
d576b5f
baeddf5
d8dad1a
70b5d66
080ae9b
063200d
422b0c8
00c0103
b5378ee
b561938
f872c30
b399298
bf86807
b80b4df
88240e1
711d80e
a6342ef
176540f
75eab6a
2d13933
08fbc4c
f5d4de0
b098d69
63e7de5
f516e06
bebaa50
ccdaf70
69ea772
3e70827
1d8dd94
8a9b2b5
fc84cd1
ff19cc9
8c29176
2b03281
379ae60
2250cf7
035b118
ea22f01
550a303
d10ca9c
03ab9c1
895b540
8b3534d
b20a668
0136ac0
ae25407
782365b
a1cefab
f881b83
b7a891f
4c11fea
755436f
79feace
032e28e
ba08bbb
8a157f2
c3d6558
e45dc75
9b2d4da
3d02a98
371b5f3
7e97ee8
cb1f2c6
e88cb1d
0d398e2
a42031a
37eaef2
84c660d
5942815
ea23468
b34a4ca
5ac35fb
58c1cfd
1bea96b
aede0ca
1689163
4e3b72b
15696f5
e563e1a
0dae52e
d78ea1b
cd221cd
0d84014
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| /* | ||
| * Copyright 2016, TeamDev Ltd. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and/or binary forms, with or without | ||
| * modification, must retain the above copyright notice and the following | ||
| * disclaimer. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.spine3.base; | ||
|
|
||
| import com.google.protobuf.Any; | ||
| import com.google.protobuf.FieldMask; | ||
| import com.google.protobuf.Message; | ||
| import org.spine3.client.EntityFilters; | ||
| import org.spine3.client.EntityId; | ||
| import org.spine3.client.EntityIdFilter; | ||
| import org.spine3.client.Query; | ||
| import org.spine3.client.Target; | ||
| import org.spine3.protobuf.AnyPacker; | ||
| import org.spine3.protobuf.KnownTypes; | ||
| import org.spine3.protobuf.TypeUrl; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.Arrays; | ||
| import java.util.Set; | ||
|
|
||
| import static org.spine3.base.Queries.Targets.allOf; | ||
| import static org.spine3.base.Queries.Targets.someOf; | ||
|
|
||
| /** | ||
| * Client-side utilities for working with queries. | ||
| * | ||
| * @author Alex Tymchenko | ||
| * @author Dmytro Dashenkov | ||
| */ | ||
| public class Queries { | ||
|
|
||
| private Queries() { | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Query} to read certain entity states by IDs with the {@link FieldMask} | ||
| * applied to each of the results. | ||
| * | ||
| * <p>Allows to specify a set of identifiers to be used during the {@code Query} processing. The processing | ||
| * results will contain only the entities, which IDs are present among the {@code ids}. | ||
| * | ||
| * <p>Allows to set property paths for a {@link FieldMask}, applied to each of the query results. | ||
| * This processing is performed according to the | ||
| * <a href="https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.FieldMask>FieldMask specs</a>. | ||
| * | ||
| * <p>In case the {@code paths} array contains entries inapplicable to the resulting entity | ||
| * (e.g. a {@code path} references a missing field), such invalid paths are silently ignored. | ||
| * | ||
| * @param entityClass the class of a target entity | ||
| * @param ids the entity IDs of interest | ||
| * @param paths the property paths for the {@code FieldMask} applied to each of results | ||
| * @return an instance of {@code Query} formed according to the passed parameters | ||
| */ | ||
| public static Query readByIds(Class<? extends Message> entityClass, Set<? extends Message> ids, String... paths) { | ||
| final FieldMask fieldMask = FieldMask.newBuilder() | ||
| .addAllPaths(Arrays.asList(paths)) | ||
| .build(); | ||
| final Query result = composeQuery(entityClass, ids, fieldMask); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Query} to read all entity states with the {@link FieldMask} | ||
| * applied to each of the results. | ||
| * | ||
| * <p>Allows to set property paths for a {@link FieldMask}, applied to each of the query results. | ||
| * This processing is performed according to the | ||
| * <a href="https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.FieldMask>FieldMask specs</a>. | ||
| * | ||
| * <p>In case the {@code paths} array contains entries inapplicable to the resulting entity | ||
| * (e.g. a {@code path} references a missing field), such invalid paths are silently ignored. | ||
| * | ||
| * @param entityClass the class of a target entity | ||
| * @param paths the property paths for the {@code FieldMask} applied to each of results | ||
| * @return an instance of {@code Query} formed according to the passed parameters | ||
| */ | ||
| public static Query readAll(Class<? extends Message> entityClass, String... paths) { | ||
| final FieldMask fieldMask = FieldMask.newBuilder() | ||
| .addAllPaths(Arrays.asList(paths)) | ||
| .build(); | ||
| final Query result = composeQuery(entityClass, null, fieldMask); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Query} to read certain entity states by IDs. | ||
| * | ||
| * <p>Allows to specify a set of identifiers to be used during the {@code Query} processing. The processing | ||
| * results will contain only the entities, which IDs are present among the {@code ids}. | ||
| * | ||
| * <p>Unlike {@link Queries#readByIds(Class, Set, String...)}, the {@code Query} processing will not change | ||
| * the resulting entities. | ||
| * | ||
| * @param entityClass the class of a target entity | ||
| * @param ids the entity IDs of interest | ||
| * @return an instance of {@code Query} formed according to the passed parameters | ||
| */ | ||
| public static Query readByIds(Class<? extends Message> entityClass, Set<? extends Message> ids) { | ||
| return composeQuery(entityClass, ids, null); | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Query} to read all states of a certain entity. | ||
| * | ||
| * <p>Unlike {@link Queries#readAll(Class, String...)}, the {@code Query} processing will not change | ||
| * the resulting entities. | ||
| * | ||
| * @param entityClass the class of a target entity | ||
| * @return an instance of {@code Query} formed according to the passed parameters | ||
| */ | ||
| public static Query readAll(Class<? extends Message> entityClass) { | ||
| return composeQuery(entityClass, null, null); | ||
| } | ||
|
|
||
| private static Query composeQuery(Class<? extends Message> entityClass, @Nullable Set<? extends Message> ids, @Nullable FieldMask fieldMask) { | ||
| final Target target = ids == null ? allOf(entityClass) : someOf(entityClass, ids); | ||
| final Query.Builder queryBuilder = Query.newBuilder() | ||
| .setTarget(target); | ||
| if (fieldMask != null) { | ||
| queryBuilder.setFieldMask(fieldMask); | ||
| } | ||
| final Query result = queryBuilder | ||
| .build(); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Extract the type of {@link Target} for the given {@link Query}. | ||
| * | ||
| * <p>Returns null if the {@code Target} type is unknown to the application. | ||
| * | ||
| * @param query the query of interest. | ||
| * @return the type of the {@code Query#getTarget()} or null, if the type is unknown. | ||
| */ | ||
| @Nullable | ||
| public static TypeUrl typeOf(Query query) { | ||
| final Target target = query.getTarget(); | ||
| final String typeAsString = target.getType(); | ||
| final TypeUrl type = KnownTypes.getTypeUrl(typeAsString); | ||
| return type; | ||
| } | ||
|
|
||
| /** | ||
| * Client-side utilities for working with {@link Query} and {@link org.spine3.client.Subscription} targets. | ||
| * | ||
| * @author Alex Tymchenko | ||
| * @author Dmytro Dashenkov | ||
| */ | ||
| public static class Targets { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add Javadoc to this class.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| private Targets() { | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Target} for a subset of the entity states by specifying their IDs. | ||
| * | ||
| * @param entityClass the class of a target entity | ||
| * @param ids the IDs of interest | ||
| * @return the instance of {@code Target} assembled according to the parameters. | ||
| */ | ||
| public static Target someOf(Class<? extends Message> entityClass, Set<? extends Message> ids) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document public methods. |
||
| final Target result = composeTarget(entityClass, ids); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Target} for all of the specified entity states. | ||
| * | ||
| * @param entityClass the class of a target entity | ||
| * @return the instance of {@code Target} assembled according to the parameters. | ||
| */ | ||
| public static Target allOf(Class<? extends Message> entityClass) { | ||
| final Target result = composeTarget(entityClass, null); | ||
| return result; | ||
| } | ||
|
|
||
| /* package */ | ||
| static Target composeTarget(Class<? extends Message> entityClass, @Nullable Set<? extends Message> ids) { | ||
| final TypeUrl type = TypeUrl.of(entityClass); | ||
|
|
||
| final boolean includeAll = ids == null; | ||
|
|
||
| final EntityIdFilter.Builder idFilterBuilder = EntityIdFilter.newBuilder(); | ||
|
|
||
| if (!includeAll) { | ||
| for (Message rawId : ids) { | ||
| final Any packedId = AnyPacker.pack(rawId); | ||
| final EntityId entityId = EntityId.newBuilder() | ||
| .setId(packedId) | ||
| .build(); | ||
| idFilterBuilder.addIds(entityId); | ||
| } | ||
| } | ||
| final EntityIdFilter idFilter = idFilterBuilder.build(); | ||
| final EntityFilters filters = EntityFilters.newBuilder() | ||
| .setIdFilter(idFilter) | ||
| .build(); | ||
| final Target.Builder builder = Target.newBuilder() | ||
| .setType(type.getTypeName()); | ||
| if (includeAll) { | ||
| builder.setIncludeAll(true); | ||
| } else { | ||
| builder.setFilters(filters); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the redundant line.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| return builder.build(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add Javadocs for these methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.