Skip to content

Commit 23f0c23

Browse files
Merge branch '117-sync-shared-global-ids' into 'main'
Support shared global IDs #117 See merge request objectbox/objectbox-dart!90
2 parents f07afeb + 05e170b commit 23f0c23

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

generator/integration-tests/basics/1.dart

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ void main() {
3333

3434
expect(entity(model, 'D').flags, equals(OBXEntityFlags.SYNC_ENABLED));
3535
expect(entity(jsonModel, 'D').flags, equals(OBXEntityFlags.SYNC_ENABLED));
36+
37+
expect(entity(model, 'E').flags,
38+
equals(OBXEntityFlags.SYNC_ENABLED | OBXEntityFlags.SHARED_GLOBAL_IDS));
39+
expect(entity(jsonModel, 'E').flags,
40+
equals(OBXEntityFlags.SYNC_ENABLED | OBXEntityFlags.SHARED_GLOBAL_IDS));
3641
});
3742

3843
test('types', () {

generator/integration-tests/basics/lib/lib.dart

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class D {
2828
D();
2929
}
3030

31+
@Entity()
32+
@Sync(sharedGlobalIds: true)
33+
class E {
34+
@Id(assignable: true)
35+
int id = 0;
36+
37+
E({this.id = 0});
38+
}
39+
3140
@Entity()
3241
class T {
3342
int? id;

generator/lib/src/entity_resolver.dart

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ class EntityResolver extends Builder {
6868
null,
6969
uidRequest: !entityUid.isNull && entityUid.intValue == 0);
7070

71-
// Sync: check if enabled
72-
if (_syncChecker.hasAnnotationOfExact(classElement)) {
71+
// Sync: check if enabled and options
72+
_syncChecker.runIfMatches(classElement, (annotation) {
7373
entity.flags |= OBXEntityFlags.SYNC_ENABLED;
74-
}
74+
if (annotation.getField('sharedGlobalIds')!.toBoolValue()!) {
75+
entity.flags |= OBXEntityFlags.SHARED_GLOBAL_IDS;
76+
}
77+
});
7578

7679
log.info(entity);
7780

generator/test/code_builder_test.dart

+25
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,31 @@ void main() {
352352
expect(vectorProperty.hnswParams!.reparationBacklinkProbability, 0.95);
353353
expect(vectorProperty.hnswParams!.vectorCacheHintSizeKB, 2097152);
354354
});
355+
356+
test('Sync annotation with shared global IDs', () async {
357+
final source = r'''
358+
library example;
359+
import 'package:objectbox/objectbox.dart';
360+
361+
@Entity()
362+
@Sync(sharedGlobalIds: true)
363+
class Example {
364+
@Id(assignable: true)
365+
int id = 0;
366+
}
367+
''';
368+
369+
final testEnv = GeneratorTestEnv();
370+
await testEnv.run(source);
371+
372+
// Assert final model created by generator
373+
var entity = testEnv.model.entities[0];
374+
expect(entity.flags & OBXEntityFlags.SYNC_ENABLED != 0, true);
375+
expect(entity.flags & OBXEntityFlags.SHARED_GLOBAL_IDS != 0, true);
376+
// Only a single property
377+
final idProperty = testEnv.model.entities[0].properties[0];
378+
expect(idProperty.flags & OBXPropertyFlags.ID_SELF_ASSIGNABLE != 0, true);
379+
});
355380
});
356381
}
357382

objectbox/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## latest
22

3+
* Sync: support option to enable [shared global IDs](https://sync.objectbox.io/advanced/object-ids#shared-global-ids).
4+
35
## 4.0.1 (2024-05-27)
46

57
* Export `ObjectWithScore` and `IdWithScore` used by the new find with score `Query` methods. [#637](https://github.com/objectbox/objectbox-dart/issues/637)

objectbox/lib/src/native/sync.dart

+22-2
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,33 @@ class _SyncListenerGroup<StreamValueType> {
583583
}
584584
}
585585

586+
// Note: because in Dart can't have two classes exported with the same name,
587+
// this class doubles as the annotation class (compare annotations.dart) and
588+
// configuration class for Sync.
589+
586590
/// [ObjectBox Sync](https://objectbox.io/sync/) makes data available and
587591
/// synchronized across devices, online and offline.
588592
///
589593
/// Start a client using [Sync.client()] and connect to a remote server.
590594
class Sync {
591-
/// Create a Sync annotation, enabling synchronization for an entity.
592-
const Sync();
595+
/// Set to `true` to enable shared global IDs for a Sync-enabled entity class.
596+
///
597+
/// By default, each Sync client has its own local ID space for Objects.
598+
/// IDs are mapped to global IDs when syncing behind the scenes.
599+
/// Turn this on to treat Object IDs as global and turn of ID mapping.
600+
/// The ID of an Object will then be the same on all clients.
601+
///
602+
/// When using this, it is recommended to use assignable IDs (`@Id(assignable: true)`)
603+
/// to turn off automatically assigned IDs. Without special care, two Sync
604+
/// clients are likely to overwrite each others Objects if IDs are assigned
605+
/// automatically.
606+
final bool sharedGlobalIds;
607+
608+
/// Enables sync for an `@Entity` class.
609+
///
610+
/// Note that currently sync can not be enabled or disabled for existing entities.
611+
/// Also synced entities can not have relations to non-synced entities.
612+
const Sync({this.sharedGlobalIds = false});
593613

594614
static final bool _syncAvailable = C.has_feature(OBXFeature.Sync);
595615

0 commit comments

Comments
 (0)