aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/frontend/html/indexer/worker.js
blob: b8183cc6f8bcf537af5cd68ba8d11a08133b1fb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Epic Games, Inc. All Rights Reserved.

"use strict";

import { Fetcher } from "../util/fetcher.js"

////////////////////////////////////////////////////////////////////////////////
export class Message
{
	static None		= 0;	//
	static Init		= 1;	// project_id, oplog
	static Map		= 2;	// start, end, page_size, stride
	static MapPage	= 3;	// page
	static MapDone	= 4;	//

	static create(msg, ...args) { return [msg, ...args]; }
}



////////////////////////////////////////////////////////////////////////////////
async function map_id_to_key(project_id, oplog, start, end, page_size, stride)
{
	if (start >= end)
		return postMessage(Message.create(Message.MapDone));

	const uri = "/prj/" + project_id + "/oplog/" + oplog + "/entries";

	const fetch_page = async function(index) {
		const cbo = new Fetcher()
			.resource(uri)
			.param("start", index)
			.param("count", page_size)
			.param("fieldfilter", "packagedata,key")
			.cbo()

		const entry_count = Math.min(page_size, -(index - end));
		return [await cbo, entry_count];
	};

	var fetch = fetch_page(start);
	while (fetch !== undefined)
	{
		performance.mark("fetch");

		const [cbo, entry_count] = await fetch;
		start += stride;
		fetch = (start < end) ? fetch_page(start) : undefined;

		var entries = (await cbo).as_object().find("entries");
		if (entries == undefined)
			break;

		entries = entries.as_array();
		if (entries.num() == 0)
			break;

		performance.mark("build");
		var count = 0;
		var result = new Array(entry_count);
		for (var entry of entries)
		{
			if (!entry.is_object())
				continue
			entry = entry.as_object();

			var key = undefined;
			var pkg_data = undefined;
			for (const field of entry)
			{
				if (field.is_named("key")) key = field;
				else if (field.is_named("packagedata")) pkg_data = field;
			}
			if (key == undefined || pkg_data == undefined)
				continue;

			var id = 0n;
			for (var item of pkg_data.as_array())
			{
				var pkg_id = item.as_object().find("id");
				if (pkg_id == undefined)
					continue;

				pkg_id = pkg_id.as_value().subarray(0, 8);
				for (var i = 7; i >= 0; --i)
				{
					id <<= 8n;
					id  |= BigInt(pkg_id[i]);
				}
				break;
			}

			if (id == 0)
				continue;

			result[count] = [id, key.as_value()];
			count++;
		}

		if (count == 0)
			continue;

		if (count != result.length)
			result = result.slice(0, count);

		performance.mark("sort");
		result.sort(function(l, r) { return Number(l[0] - r[0]); });

		const msg = Message.create(Message.MapPage, result);
		postMessage(msg);
	}

	postMessage(Message.create(Message.MapDone));
}

////////////////////////////////////////////////////////////////////////////////
function worker_scope()
{
	var project_id;
	var oplog;

	return (evt) => {
		const [msg_id, ...params] = evt.data;
		switch (msg_id)
		{
		case Message.Init:
			[project_id, oplog] = params;
			break;

		case Message.Map:
			var [start, end, page_size, stride] = params;
			map_id_to_key(project_id, oplog, start, end, page_size, stride);
			break;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
if (typeof DedicatedWorkerGlobalScope != "undefined" && self instanceof DedicatedWorkerGlobalScope)
{
	onmessage = worker_scope();
}