initializeTimestamp static method

Future<void> initializeTimestamp(
  1. StorageController controller
)

Initializes the timestamp with the persisted and possibly un-persisted changes of the database.

Implementation

static Future<void> initializeTimestamp(StorageController controller) async {
  if (_init == 0) {
    _init = 1;
    _logger.log(Level.FINE, 'Initializing timestamp');
    _controller = controller as GenericController;
    Node n;
    try {
      n = await _controller!.get(timestampName);
      if ((await n.getValue('lastUpdated')) == null) {
        _lastTimestamp = DateTime.now().millisecondsSinceEpoch;
        await n.addValue(NodeValueImpl('lastUpdated', '$_lastTimestamp'));
      }
    } on StorageException {
      n = NodeImpl(timestampName, 'internal', null, Visibility.black);
      _lastTimestamp = DateTime.now().millisecondsSinceEpoch;
      await n.addValue(NodeValueImpl('lastUpdated', '$_lastTimestamp'));
      try {
        await _controller!.add(n);
      } on StorageException {
        // someone else already added it... lets fetch it
        Node n2 = await _controller!.get(timestampName);
        if ((await n.getValue('lastUpdated')) != null) {
          n = n2;
        }
      }
    }
    String? lastUpdated = (await n.getValue('lastUpdated'))!.toSimpleString();

    _lastTimestamp = int.parse(lastUpdated!);
    _init = 2;

    List<Node> nl = await (_controller as GenericController).search(
        SearchCriteria(searchPath: ':', nodeValueLastModified: lastUpdated));
    for (Node n in nl) {
      if (_lastTimestamp < n.lastModified) {
        _lastTimestamp = n.lastModified;
      }
    }
    _logger.log(Level.INFO, 'timestamp initialized');
  } else if (_init == 1) {
    await _waitForInit();
  }
  if (_uuid == null) {
    try {
      Node n2 = await _controller!.get(':Local');
      _uuid = (await n2.getValue('currentDevice'))!.toSimpleString();
      if (_uuid == null) {
        throw StorageException(
            'OOPS! no current device uuid found in database... that is a serious problem');
      }
    } on StorageException catch (e, st) {
      _logger.log(Level.SEVERE,
          'OOPS! No current device available in storage... contact developer');
      throw StorageException(
          'OOPS! no current device uuid found in database... that is a serious problem',
          null,
          e,
          st);
    }
  }
}