Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

番組がリスケジュールされた際に、必要な番組まで削除される不具合を修正 #134

Merged
merged 5 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/Mirakurun/EPG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ interface EventState {
};

present?: true;
following?: true;
}

// forked from rndomhack/node-aribts/blob/1e7ef94bba3d6ac26aec764bf24dde2c2852bfcb/lib/epg.js
Expand All @@ -148,6 +149,7 @@ export default class EPG {
}

const isP = isPF && eit.section_number === 0;
const isF = isPF && eit.section_number === 1;

const networkId = eit.original_network_id;

Expand Down Expand Up @@ -178,7 +180,9 @@ export default class EPG {
startAt: getTimeFromMJD(e.start_time),
duration: UNKNOWN_DURATION.compare(e.duration) === 0 ? 1 : getTimeFromBCD24(e.duration),
isFree: e.free_CA_mode === 0,
_pf: isPF || undefined
_pf: isPF || undefined, // for compatibility
_isPresent: isP || undefined,
_isFollowing: isF || undefined
};
_.program.add(programItem);
}
Expand Down Expand Up @@ -210,29 +214,31 @@ export default class EPG {
version: {},
_groups: []
},

present: isP || undefined
present: isP || undefined,
following: isF || undefined
};

state.version[eit.table_id] = eit.version_number;
service[e.event_id] = state;
} else {
state = service[e.event_id];

if (!state.present && isP) {
state.present = true;
}

if ((!state.present || (state.present && isP)) && isOutOfDate(eit, state.version)) {
if ((!state.present && isP) || (!state.following && isF) || isOutOfDate(eit, state.version)) {
state.version[eit.table_id] = eit.version_number;

if (UNKNOWN_START_TIME.compare(e.start_time) !== 0) {
_.program.set(state.programId, {
startAt: getTimeFromMJD(e.start_time),
duration: UNKNOWN_DURATION.compare(e.duration) === 0 ? 1 : getTimeFromBCD24(e.duration),
isFree: e.free_CA_mode === 0,
_pf: isPF || undefined
_pf: isPF || undefined, // for compatibility
_isPresent: isP || undefined,
_isFollowing: isF || undefined
});
}

state.present = isP || undefined;
state.following = isF || undefined;
}
}

Expand Down
56 changes: 50 additions & 6 deletions src/Mirakurun/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function getProgramItemId(networkId: number, serviceId: number, eventId:
export default class Program {

private _itemMap = new Map<number, db.Program>();
private _itemMapDeleted = new Map<number, db.Program>();
private _saveTimerId: NodeJS.Timer;
private _emitTimerId: NodeJS.Timer;
private _emitRunning = false;
Expand All @@ -50,6 +51,9 @@ export default class Program {
return;
}

// purge logically deleted item
this._itemMapDeleted.delete(item.id);

if (firstAdd === false) {
this._findAndRemoveConflicts(item);
}
Expand All @@ -68,7 +72,22 @@ export default class Program {
}

set(id: number, props: Partial<db.Program>): void {
const item = this.get(id);
let item = this.get(id);
if (!item) {
// Recovers logically deleted item if that is exsts into the tempolally collection.
item = this._itemMapDeleted.get(id) || null;
if (item) {
this._itemMap.set(item.id, item);
this._itemMapDeleted.delete(item.id);
this._emitPrograms.set(item, "create");
this.save();

log.debug(
"ProgramItem#%d (networkId=%d, serviceId=%d, eventId=%d) has recovered from the logically-deleted store",
item.id, item.networkId, item.serviceId, item.eventId
);
}
}
if (item && common.updateObject(item, props) === true) {
if (props.startAt || props.duration) {
this._findAndRemoveConflicts(item);
Expand All @@ -78,16 +97,29 @@ export default class Program {
}
}

remove(id: number): void {
if (this._itemMap.delete(id)) {
this.save();
remove(id: number, logicallyDelete: boolean = false): void {
if (logicallyDelete) {
const item = this.get(id);
if (item) {
this._itemMapDeleted.set(item.id, item);
this._itemMap.delete(id);
this.save();
}
} else {
if (this._itemMap.delete(id)) {
this.save();
}
}
}

exists(id: number): boolean {
return this._itemMap.has(id);
}

isLogicallyDeleted(id: number): boolean {
return this._itemMapDeleted.has(id);
}

findByQuery(query: object): db.Program[] {
return Array.from(this._itemMap.values()).filter(sift(query));
}
Expand Down Expand Up @@ -189,9 +221,9 @@ export default class Program {
(added.startAt <= item.startAt && item.startAt < addedEndAt) ||
(item.startAt <= added.startAt && added.startAt < itemEndAt)
) &&
(!item._pf || added._pf)
(!(item._isPresent || item._isFollowing) || added._isPresent)
) {
this.remove(item.id);
this.remove(item.id, true);
Event.emit("program", "remove", { id: item.id });

log.debug(
Expand Down Expand Up @@ -227,6 +259,7 @@ export default class Program {

log.debug("saving programs...");

// TODO: Do we need to save/load logically deleted items?
db.savePrograms(
Array.from(this._itemMap.values()),
_.configIntegrity.channels
Expand Down Expand Up @@ -254,6 +287,17 @@ export default class Program {
}
}

// Perform GC for the logically-deleted store
for (const item of this._itemMapDeleted.values()) {
if (
(item.duration === 1 ? longExp : shortExp) > (item.startAt + item.duration) ||
maximum < item.startAt
) {
++count;
this._itemMapDeleted.delete(item.id);
}
}

setTimeout(this._gc.bind(this), this._programGCInterval);

log.info("Program GC has finished and removed %d programs", count);
Expand Down
4 changes: 3 additions & 1 deletion src/Mirakurun/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export interface Program {
relatedItems?: ProgramRelatedItem[];

/** (internal) indicates EIT[p/f] received */
_pf?: true;
_pf?: true; // for compatibility
_isPresent?: true;
_isFollowing?: true;
}

export interface ProgramGenre {
Expand Down
Loading