-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(core): use dedicated identity maps for each entity
Related: #732
- Loading branch information
Showing
16 changed files
with
138 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { AnyEntity, Constructor, EntityMetadata } from '../typings'; | ||
|
||
export class IdentityMap { | ||
|
||
private readonly registry = new Map<Constructor<AnyEntity>, Map<string, AnyEntity>>(); | ||
|
||
store<T extends AnyEntity<T>>(item: T) { | ||
this.getStore(item.__meta!.root).set(item.__helper!.getSerializedPrimaryKey(), item); | ||
} | ||
|
||
delete<T extends AnyEntity<T>>(item: T) { | ||
this.getStore(item.__meta!.root).delete(item.__helper!.getSerializedPrimaryKey()); | ||
} | ||
|
||
getByHash<T>(meta: EntityMetadata<T>, hash: string): T | undefined { | ||
const store = this.getStore(meta); | ||
return store.has(hash) ? store.get(hash) : undefined; | ||
} | ||
|
||
getStore<T extends AnyEntity<T>>(meta: EntityMetadata<T>): Map<string, T> { | ||
const store = this.registry.get(meta.class) as Map<string, T>; | ||
|
||
if (store) { | ||
return store; | ||
} | ||
|
||
const newStore = new Map(); | ||
this.registry.set(meta.class, newStore); | ||
|
||
return newStore; | ||
} | ||
|
||
clear() { | ||
this.registry.clear(); | ||
} | ||
|
||
values(): AnyEntity[] { | ||
const ret: AnyEntity[] = []; | ||
|
||
for (const store of this.registry.values()) { | ||
ret.push(...store.values()); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
keys(): string[] { | ||
const ret: string[] = []; | ||
|
||
for (const [cls, store] of this.registry) { | ||
ret.push(...[...store.keys()].map(hash => `${cls.name}-${hash}`)); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
/** | ||
* For back compatibility only. | ||
*/ | ||
get<T>(hash: string): T | undefined { | ||
const [name, id] = hash.split('-', 2); | ||
const cls = [...this.registry.keys()].find(k => k.name === name); | ||
|
||
if (!cls) { | ||
return undefined; | ||
} | ||
|
||
const store = this.registry.get(cls) as Map<string, T>; | ||
return store.has(id) ? store.get(id) : undefined; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.