import { DocMetaFileRef , DocMetaRef } from './DocMetaRef' ; import { BackendFileRefData , BinaryFileData , Datastore , DatastoreCapabilities , DatastoreConsistency , DatastoreInitOpts , DatastoreOverview , DeleteResult , DocMetaSnapshot , DocMetaSnapshotEventListener , DocMetaSnapshotOpts , DocMetaSnapshotResult , ErrorListener , GroupIDStr , SnapshotResult , WriteFileOpts , WriteFileProgressListener , WriteOptsBase } from './Datastore' ; import { Backend } from 'polar-shared/src/datastore/Backend' ; import { DocFileMeta } from 'polar-shared/src/datastore/DocFileMeta' ; import { DatastoreMutation } from './DatastoreMutation' ; import { IDocInfo } from "polar-shared/src/metadata/IDocInfo" ; import { IDocMeta } from "polar-shared/src/metadata/IDocMeta" ; import { Visibility } from "polar-shared/src/datastore/Visibility" ; import { FileRef } from "polar-shared/src/datastore/FileRef" ; import { ListenablePersistenceLayer } from "./ListenablePersistenceLayer" ; import { UserTagsDB } from "./UserTagsDB" ; import { DocMetas } from "../metadata/DocMetas" ; import { GetFileOpts } from "polar-shared/src/datastore/IDatastore" ; export interface PersistenceLayer { readonly id : PersistenceLayerID ; /**
     * The underlying datastore backing this persistence layer.
     */ readonly datastore : Datastore ; init ( errorListener ? , opts ? ) : Promise < void > ; stop ( ) : Promise < void > ; /**
     * Return true if the DiskDatastore contains a document for the given
     * fingerprint.
     */ contains ( fingerprint ) : Promise < boolean > ; getDocMeta ( fingerprint ) : Promise < IDocMeta | undefined > ; getDocMetaSnapshot ( opts ) : Promise < DocMetaSnapshotResult > ; getDocMetaRefs ( ) : Promise < ReadonlyArray < DocMetaRef > > ; /**
     * Get a current snapshot of the internal state of the Datastore by
     * receiving DocMetaSnapshotEvent on the initial state.
     */ snapshot ( listener , errorListener ? ) : Promise < SnapshotResult > ; createBackup ( ) : Promise < void > ; /**
     * Delete a file from PersistenceLayer.
     */ delete ( docMetaFileRef , datastoreMutation ? ) : Promise < DeleteResult > ; writeDocMeta ( docMeta , datastoreMutation ? ) : Promise < IDocInfo > ; /**
     * Make sure the docs with the given fingerprints are synchronized with
     * this datastore. Only implemented in cloud datastores.
     */ synchronizeDocs ( ... docMetaRefs ) : Promise < void > ; /**
     * Return the DocInfo written. The DocInfo may be updated on commit
     * including updating lastUpdated, etc.
     */ write ( fingerprint , docMeta , opts ? ) : Promise < IDocInfo > ; writeFile ( backend , ref , data , opts ? ) : Promise < DocFileMeta > ; getFile ( backend , ref , opts ? ) : DocFileMeta ; containsFile ( backend , ref ) : Promise < boolean > ; deleteFile ( backend , ref ) : Promise < void > ; addDocMetaSnapshotEventListener ( docMetaSnapshotEventListener ) : void ; deactivate ( ) : Promise < void > ; overview ( ) : Promise < DatastoreOverview | undefined > ; capabilities ( ) : DatastoreCapabilities ; } export abstract class AbstractPersistenceLayer { public readonly abstract datastore : Datastore ; public async getDocMetaSnapshot ( opts ) { const onSnapshot = ( snapshot ) => { if ( snapshot . data ) { const docMeta = DocMetas . deserialize ( snapshot . data , opts . fingerprint ) ; opts . onSnapshot ( { ... snapshot , data : docMeta } ) ; } else { opts . onSnapshot ( { ... snapshot , data : undefined } ) ; } } ; return await this . datastore . getDocMetaSnapshot ( { fingerprint : opts . fingerprint , onSnapshot : snapshot => onSnapshot ( snapshot ) , onError : opts . onError } ) ; } } export interface WriteOpts extends WriteOptsBase < IDocInfo > { } export type PersistenceLayerID = string ; export type PersistenceLayerProvider = ( ) => PersistenceLayer ; export type ListenablePersistenceLayerProvider = ( ) => ListenablePersistenceLayer ;