forked from google/create-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiplexstore.dart
65 lines (54 loc) · 2.09 KB
/
multiplexstore.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../../elements.dart';
import 'base.dart';
class SubDatastore<R extends CompositeData> {
final Datastore<R> datastore;
final Ref<bool> active = new Boxed<bool>(true);
final ReadRef<String> name;
SubDatastore(this.datastore, String name) : name = new Constant<String>(name);
}
class MuliplexDatastore<R extends CompositeData> implements Datastore<R> {
// TODO(dynin): smarter support for multiple datastores.
Set<DataType> get dataTypes => substores.elements[0].datastore.dataTypes;
ReadList<SubDatastore<R>> substores;
int addIndex;
MuliplexDatastore(List<SubDatastore<R>> substores, this.addIndex)
: substores = new ImmutableList<SubDatastore<R>>(substores);
Iterable<SubDatastore<R>> get _activeSubstores =>
substores.elements.where((SubDatastore<R> substore) => substore.active.value);
@override
ReadList<R> runQuery(QueryType<R> query, Lifespan lifespan, Priority priority) {
if (lifespan == null) {
List<R> result = new List<R>();
for (SubDatastore<R> substore in _activeSubstores) {
result.addAll(substore.datastore.runQuery(query, lifespan, priority).elements);
}
return new ImmutableList<R>(result);
} else {
JoinedList<R> result = new JoinedList<R>(lifespan);
for (SubDatastore<R> substore in _activeSubstores) {
result.addList(substore.datastore.runQuery(query, lifespan, priority));
}
return result;
}
}
@override
ReadRef<int> count(QueryType<R> query, Lifespan lifespan, Priority priority) {
// TODO: optimize
return runQuery(query, lifespan, priority).size;
}
@override
ImmutableList<R> runQuerySync(QueryType<R> query) {
List<R> result = [];
for (SubDatastore<R> substore in _activeSubstores) {
result.addAll(substore.datastore.runQuerySync(query).elements);
}
return ImmutableList<R>(result);
}
@override
void add(R record) {
substores.elements[addIndex].datastore.add(record);
}
}