| 47 | * ``` |
| 48 | */ |
| 49 | export class QuerySystem { |
| 50 | private readonly _logger = createLogger('QuerySystem'); |
| 51 | private _entities: Entity[] = []; |
| 52 | private _entityIndex: EntityIndex; |
| 53 | |
| 54 | private _version = 0; |
| 55 | |
| 56 | private _queryCache = new Map<string, QueryCacheEntry>(); |
| 57 | private _cacheMaxSize = 1000; |
| 58 | private _cacheTimeout = 5000; |
| 59 | |
| 60 | private _componentMaskCache = new Map<string, BitMask64Data>(); |
| 61 | |
| 62 | private _archetypeSystem: ArchetypeSystem; |
| 63 | |
| 64 | private _queryStats = { |
| 65 | totalQueries: 0, |
| 66 | cacheHits: 0, |
| 67 | indexHits: 0, |
| 68 | linearScans: 0, |
| 69 | archetypeHits: 0, |
| 70 | dirtyChecks: 0 |
| 71 | }; |
| 72 | |
| 73 | constructor() { |
| 74 | this._entityIndex = { |
| 75 | byTag: new Map(), |
| 76 | byName: new Map() |
| 77 | }; |
| 78 | |
| 79 | this._archetypeSystem = new ArchetypeSystem(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * 设置实体列表并重建索引 |
| 84 | * |
| 85 | * 当实体集合发生大规模变化时调用此方法。 |
| 86 | * 系统将重新构建所有索引以确保查询性能。 |
| 87 | * |
| 88 | * @param entities 新的实体列表 |
| 89 | */ |
| 90 | public setEntities(entities: Entity[]): void { |
| 91 | this._entities = entities; |
| 92 | this.clearQueryCache(); |
| 93 | this.clearReactiveQueries(); |
| 94 | this.rebuildIndexes(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * 添加单个实体到查询系统 |
| 99 | * |
| 100 | * 将新实体添加到查询系统中,并自动更新相关索引。 |
| 101 | * 为了提高批量添加性能,可以延迟缓存清理。 |
| 102 | * |
| 103 | * @param entity 要添加的实体 |
| 104 | * @param deferCacheClear 是否延迟缓存清理(用于批量操作) |
| 105 | */ |
| 106 | public addEntity(entity: Entity, deferCacheClear: boolean = false): void { |
nothing calls this directly
no test coverage detected