mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-30 12:01:57 +00:00
hashmap
This commit is contained in:
@@ -2,7 +2,7 @@ import { merge, Observable, of } from 'rxjs'
|
|||||||
import { concatMap, finalize, tap } from 'rxjs/operators'
|
import { concatMap, finalize, tap } from 'rxjs/operators'
|
||||||
import { Source } from './source/source'
|
import { Source } from './source/source'
|
||||||
import { Store } from './store'
|
import { Store } from './store'
|
||||||
import { DBCache, Http } from './types'
|
import { DBCache, HashMap, Http } from './types'
|
||||||
|
|
||||||
export class PatchDB<T> {
|
export class PatchDB<T> {
|
||||||
store: Store<T>
|
store: Store<T>
|
||||||
@@ -15,7 +15,7 @@ export class PatchDB<T> {
|
|||||||
this.store = new Store(this.http, this.initialCache)
|
this.store = new Store(this.http, this.initialCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
sync$ (): Observable<DBCache<T>> {
|
sync$ (): Observable<DBCache<HashMap>> {
|
||||||
return merge(...this.sources.map(s => s.watch$(this.store)))
|
return merge(...this.sources.map(s => s.watch$(this.store)))
|
||||||
.pipe(
|
.pipe(
|
||||||
tap(update => this.store.update(update)),
|
tap(update => this.store.update(update)),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { DBCache, Dump, Http, Revision, Update } from './types'
|
import { DBCache, Dump, HashMap, Http, Revision, Update } from './types'
|
||||||
import { BehaviorSubject, Observable } from 'rxjs'
|
import { BehaviorSubject, Observable } from 'rxjs'
|
||||||
import { applyOperation, getValueByPointer, Operation } from './json-patch-lib'
|
import { applyOperation, getValueByPointer, Operation } from './json-patch-lib'
|
||||||
import BTree from 'sorted-btree'
|
import BTree from 'sorted-btree'
|
||||||
@@ -9,7 +9,7 @@ export interface StashEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Store<T> {
|
export class Store<T> {
|
||||||
cache: DBCache<T>
|
cache: DBCache<HashMap>
|
||||||
sequence$: BehaviorSubject<number>
|
sequence$: BehaviorSubject<number>
|
||||||
private watchedNodes: { [path: string]: BehaviorSubject<any> } = { }
|
private watchedNodes: { [path: string]: BehaviorSubject<any> } = { }
|
||||||
private stash = new BTree<number, StashEntry>()
|
private stash = new BTree<number, StashEntry>()
|
||||||
@@ -66,6 +66,8 @@ export class Store<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private handleDump (dump: Dump<T>): void {
|
private handleDump (dump: Dump<T>): void {
|
||||||
|
Object.keys(this.cache.data).forEach(key => delete this.cache.data[key])
|
||||||
|
Object.assign(this.cache.data, dump)
|
||||||
this.cache.data = dump.value
|
this.cache.data = dump.value
|
||||||
this.stash.deleteRange(this.cache.sequence, dump.id, false)
|
this.stash.deleteRange(this.cache.sequence, dump.id, false)
|
||||||
this.updateWatchedNodes('')
|
this.updateWatchedNodes('')
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ import { Operation } from './json-patch-lib'
|
|||||||
// revise a collection of nodes.
|
// revise a collection of nodes.
|
||||||
export type Revision = { id: number, patch: Operation[], expireId: string | null }
|
export type Revision = { id: number, patch: Operation[], expireId: string | null }
|
||||||
// dump/replace the entire store with T
|
// dump/replace the entire store with T
|
||||||
export type Dump<T> = { id: number, value: T, expireId: string | null }
|
export type Dump<T extends HashMap> = { id: number, value: T, expireId: string | null }
|
||||||
|
|
||||||
export type Update<T> = Revision | Dump<T>
|
export type Update<T extends HashMap> = Revision | Dump<T>
|
||||||
|
|
||||||
export enum PatchOp {
|
export enum PatchOp {
|
||||||
ADD = 'add',
|
ADD = 'add',
|
||||||
@@ -13,17 +13,20 @@ export enum PatchOp {
|
|||||||
REPLACE = 'replace',
|
REPLACE = 'replace',
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Http<T> {
|
export interface Http<T extends HashMap> {
|
||||||
getRevisions (since: number): Promise<Revision[] | Dump<T>>
|
getRevisions (since: number): Promise<Revision[] | Dump<T>>
|
||||||
getDump (): Promise<Dump<T>>
|
getDump (): Promise<Dump<T>>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Bootstrapper<T> {
|
export interface Bootstrapper<T extends HashMap> {
|
||||||
init (): Promise<DBCache<T>>
|
init (): Promise<DBCache<T>>
|
||||||
update (cache: DBCache<T>): Promise<void>
|
update (cache: DBCache<T>): Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DBCache<T>{
|
export interface DBCache<T extends HashMap>{
|
||||||
sequence: number,
|
sequence: number,
|
||||||
data: T
|
data: T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HashMap = { [type: string]: any }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user