create method
hive does not support directly storing emoji characters, therefore keys
are encoded in HiveKeyStoreHelper.prepareKey using utf7 before storing.
Implementation
@override
@server
Future<int?> create(String key, AtData value,
{bool skipCommit = false}) async {
key = key.toLowerCase();
final atKey = AtKey.getKeyType(key, enforceNameSpace: false);
if (atKey == KeyType.invalidKey) {
logger.warning('Key $key is invalid');
throw InvalidAtKeyException('Key $key is invalid');
}
CommitOp commitOp;
String hive_key = HiveKeyStoreHelper.prepareKey(key);
_checkMaxLength(hive_key);
var hive_data = HiveKeyStoreHelper.prepareDataForKeystoreOperation(
value,
atSign: atSign,
);
// Default commitOp to Update.
commitOp = CommitOp.UPDATE;
// Set CommitOp to UPDATE_ALL if any of the metadata args are not null.
// Direct null-check chain rather than allocating a 17-element Set per call.
final m = value.metaData;
if (m != null &&
(m.ttl != null ||
m.ttb != null ||
m.ttr != null ||
m.isCascade != null ||
m.isBinary != null ||
m.isEncrypted != null ||
m.dataSignature != null ||
m.sharedKeyEnc != null ||
m.pubKeyCS != null ||
m.encoding != null ||
m.encKeyName != null ||
m.encAlgo != null ||
m.ivNonce != null ||
m.skeEncKeyName != null ||
m.skeEncAlgo != null ||
m.pubKeyHash != null ||
m.immutable != null)) {
commitOp = CommitOp.UPDATE_ALL;
}
try {
await getBox().put(hive_key, hive_data);
_updateMetadataCache(key, hive_data.metaData);
_changesController.add(KeyAdded(key));
if (skipCommit) {
return -1;
} else {
// `_commitLog` is null on a commit-log-free keystore — the
// write still succeeds, it just produces no sequence number.
return await _commitLog?.commit(hive_key, commitOp);
}
} on Exception catch (exception) {
logger.severe('HiveAtKeyValueStore create exception: $exception');
throw DataStoreException('exception in create: ${exception.toString()}');
} on HiveError catch (error) {
await _restartHiveBox(error);
logger.severe('HiveAtKeyValueStore error: $error');
throw DataStoreException(error.message);
}
}