You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FMWK-468 Allow send key classes to use PK instead of Bin (#160)
* Allowed the use of the Aerospike Key instead of re-writing a key field.
Added a new annotation field "storeInPkOnly" to use the PK field as
storing the key, rather than explicitly setting a bin in the database.
- Added unit tests for the same, both reactive and normal
- Tested with annotations, YAML config, code config.
* Rolled back accidental push of wrong connection port.
* Format
* Cleanup and format
* Updated field name to storeAsBin
Changed the config field name from `storeInPkOnly` to `storeAsBin`,
added unit test to ensure config validation works.
* Rolled back test port to port 3000
* Remove old "storeInPkOnly" terminology, add javadocs, cleanup
* Apply suggestions from code review
Co-authored-by: Eugene R. <[email protected]>
* Revert change, causing NPE on runTestViaConfig() test
---------
Co-authored-by: roimenashe <[email protected]>
Co-authored-by: Roi Menashe <[email protected]>
Co-authored-by: Eugene R. <[email protected]>
Copy file name to clipboardexpand all lines: README.md
+53-2
Original file line number
Diff line number
Diff line change
@@ -484,7 +484,57 @@ public String getKey() {
484
484
485
485
Note that it is not required to have a key on an object annotated with @AerospikeRecord. This is because an object can be embedded in another object (as a map or list) and hence not require a key to identify it to the database.
486
486
487
-
Also, the existence of @AerospikeKey on a field does not imply that the field will get stored in the database explicitly. Use @AerospikeBin or mapAll attribute to ensure that the key gets mapped to the database too.
487
+
Also, the existence of `@AerospikeKey` on a field does not imply that the field will get stored in the database explicitly. Use `@AerospikeBin` or `mapAll` attribute to ensure that the key gets mapped to the database too.
488
+
489
+
By default, the key will always be stored in a separate column in the database. So for a class defined as
490
+
491
+
```java
492
+
@AerospikeRecord(namespace = "test", set = "testSet")
493
+
public static class A {
494
+
@AerospikeKey
495
+
private long key;
496
+
private String value;
497
+
}
498
+
```
499
+
500
+
there will be a bin in the database called `key`, whose value will be the same as the value used in the primary key. This is because Aerospike does not implicitly store the value of the key in the database, but rather uses a hash of the primary key as a unique representation. So the value in the database might look like:
501
+
502
+
```
503
+
aql> select * from test.testSet
504
+
+-----+--------+
505
+
| key | value |
506
+
+-----+--------+
507
+
| 1 | "test" |
508
+
+-----+--------+
509
+
```
510
+
511
+
If it is desired to force the primary key to be stored in the database and NOT have key added explicitly as a column then two things must be set:
512
+
513
+
1. The `@AerospikeRecord` annotation must have `sendKey = true`
514
+
2. The `@AerospikeKey` annotation must have `storeAsBin = false`
515
+
516
+
So the object would look like:
517
+
518
+
```java
519
+
@AerospikeRecord(namespace = "test", set = "testSet", sendKey = true)
520
+
public static class A {
521
+
@AerospikeKey(storeAsBin = false)
522
+
private long key;
523
+
private String value;
524
+
}
525
+
```
526
+
527
+
When data is inserted, the field `key` is not saved, but rather the key is saved as the primary key. When the value is read from the database, the stored primary key is put back into the `key` field. So the data in the database might be:
528
+
529
+
```
530
+
aql> select * from test.testSet
531
+
+----+--------+
532
+
| PK | value |
533
+
+----+--------+
534
+
| 1 | "test" |
535
+
+----+--------+
536
+
```
537
+
488
538
489
539
----
490
540
@@ -679,7 +729,7 @@ Here are how standard Java types are mapped to Aerospike types:
679
729
| Map<?,?> | Map |
680
730
| Object Reference (@AerospikeRecord) | List or Map |
681
731
682
-
These types are built into the converter. However, if you wish to change them, you can use a [Custom Object Converter](#Custom-Object-Converters). For example, if you want Dates stored in the database as a string, you could do:
732
+
These types are built into the converter. However, if you wish to change them, you can use a [Custom Object Converter](#custom-object-converters). For example, if you want Dates stored in the database as a string, you could do:
683
733
684
734
```java
685
735
public static class DateConverter {
@@ -1975,6 +2025,7 @@ The key structure is used to specify the key to a record. Keys are optional in s
1975
2025
1976
2026
The key structure contains:
1977
2027
- **field**: The name of the field which to which this key is mapped. If this is provided, the getter and setter cannot be provided.
2028
+
- **storeAsBin**: Store the primary key as a bin in the database, alternatively it is recommended to use the `sendKey` facility related to Aerospike to save the key in the record's metadata (and set this flag to false). When the record is read, the value will be pulled back and placed in the key field.
1978
2029
- **getter**: The getter method used to populate the key. This must be used in conjunction with a setter method, and excludes the use of the field attribute.
1979
2030
- **setter**: The setter method used to map data back to the Java key. This is used in conjunction with the getter method and precludes the use of the field attribute. Note that the return type of the getter must match the type of the first parameter of the setter, and the setter can have either 1 or 2 parameters, with the second (optional) parameter being either of type [com.aerospike.client.Key](https://www.aerospike.com/apidocs/java/com/aerospike/client/Key.html) or Object.
0 commit comments