make<K1, V1> static method

PersistentMap<K1, V1> make<K1, V1>(
  1. File file,
  2. Uint8List keySerializer(
    1. K1
    ),
  3. K1 keyDeserializer(
    1. Uint8List
    ),
  4. Uint8List valueSerializer(
    1. V1
    ),
  5. V1 valueDeserializer(
    1. Uint8List
    ), {
  6. bool create = false,
  7. bool comparator(
    1. V1,
    2. V1
    )?,
})

Create a PersistentMap with K1 for keys and V1 for values, with file being the database file, and create specifying whether to create a new file if it doesn't exist. If file already exists, the file will not be overwritten, but rather opened. The serializers for the keys and values must be provided when calling this function. If file contains and incompatible database, behavior is unspecified.

Implementation

static PersistentMap<K1, V1> make<K1, V1>(
    final File file,
    final Uint8List Function(K1) keySerializer,
    final K1 Function(Uint8List) keyDeserializer,
    final Uint8List Function(V1) valueSerializer,
    final V1 Function(Uint8List) valueDeserializer,
    {bool create = false,
    bool Function(V1, V1)? comparator}) {
  late final RandomAccessFile _random;
  if (file.existsSync()) {
    _random = file.openSync(mode: FileMode.append);
  } else if (create) {
    file.createSync(recursive: true);
    _random = file.openSync(mode: FileMode.write);
  } else {
    throw StateError('File does not exist and create flag not specified.');
  }
  DBM dbm = HashDBM(_random);
  return PersistentMap<K1, V1>(
      dbm, keySerializer, keyDeserializer, valueSerializer, valueDeserializer,
      valueComparator: comparator);
}