← 返回 amazon 的题目列表Event Filtering and Queue Routing Service
类型:online_judge
Problem: Event Filtering and Queue Routing Service
Implement a simplified event routing service. The system continuously produces events in JSON format. The service has a set of registered routing rules. Each rule contains:
queue: the target queue name
filter: a filter expression
For each input event, find all rules whose filters match the event, and send the event to the corresponding queues. For this programming version, output the matched queue names.
Filter Expression Format
A filter expression is also a JSON object and supports the following forms.
1. Comparison Expression
{"field":"user.country","op":"==","value":"US"}
Fields:
field: a field path inside the event, using . for nested fields, e.g. user.country
op: one of:
==
!=
>
>=
<
<=
contains
exists
value: the value to compare against. For exists, value is a boolean indicating whether the field should exist.
If the field does not exist:
exists is evaluated based on whether the field exists
all other comparison operators return false
2. Logical Expressions
The following logical expressions are supported:
{"and":[expr1, expr2, ...]}
{"or":[expr1, expr2, ...]}
{"not":expr}
Semantics of contains
If the field value is an array, check whether value is in the array
If the field value is a string, convert value to a string and check whether it is a substring
If the field value is an object, check whether value is a key
Otherwise return false
Input Format
n
rule_1
rule_2
...
rule_n
m
event_1
event_2
...
event_m
Where:
n is the number of routing rules
each rule_i is a JSON line in the following format:
{"queue":"queue-name","filter":expr}
m is the number of events
each event_i is a JSON object on one line
Output Format
For each event, output one line:
If at least one rule matches, output matched queue names in registration order, separated by commas
If no rule matches, output -
Constraints
1 <= n <= 10^4
1 <= m <= 10^4
Each JSON line length is at most 10KB
Each filter expression has at most 100 nodes
Field path depth is at most 20
All input JSON strings are valid
Example
Input
2
{"queue":"purchase-q","filter":{"field":"type","op":"==","value":"purchase"}}
{"queue":"us-q","filter":{"field":"user.country","op":"==","value":"US"}}
3
{"id":1,"type":"purchase","user":{"country":"US"}}
{"id":2,"type":"view","user":{"country":"CA"}}
{"id":3,"type":"purchase","user":{"country":"CA"}}
Output
purchase-q,us-q
-
purchase-q
Example
Input
2
{"queue":"purchase-q","filter":{"field":"type","op":"==","value":"purchase"}}
{"queue":"us-q","filter":{"field":"user.country","op":"==","value":"US"}}
3
{"id":1,"type":"purchase","user":{"country":"US"}}
{"id":2,"type":"view","user":{"country":"CA"}}
{"id":3,"type":"purchase","user":{"country":"CA"}}
Output
purchase-q,us-q
-
purchase-q