Beispiel #1
0
 public bool VisitCommand(AHGHEEParser.CommandContext context)
 {
     return(true);
 }
Beispiel #2
0
        public override async Task Query(QueryRequest request, IServerStreamWriter <QueryResponse> responseStream, ServerCallContext context)
        {
            var operationQueue = new Queue <Task>();

            Func <Task, Task> queueIt = (Task t) =>
            {
                operationQueue.Enqueue(t);
                return(t);
            };

            var parser = new AHGHEEParser(ParserUtils.makeStream(request.QueryText));

            parser.BuildParseTree = true;
            parser.AddParseListener(listener: new Listener(async(nodes) =>
            {
                // Handle inserting of nodes via a Put command
                var tcs = new TaskCompletionSource <short>();
                try
                {
                    // todo: Should we batch in chucks of these nodes?
                    await queueIt(tcs.Task);
                    foreach (var node in nodes)
                    {
                        await queueIt(_db.AddNoQueue(node));
                    }
                    tcs.SetResult(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ex {ex} stack: {ex.StackTrace}");
                    tcs.SetException(ex);
                }
            }, async(nids) =>
            {
                // Handle querying nodes
                var gr = new GetRequest();
                gr.Iris.AddRange(nids.Select(nid => nid.Iri));
                await queueIt(Get(gr, responseStream, context));
            }, () => { },
                                                           async(loadType, path) =>
            {
                var lf  = new LoadFile();
                lf.Type = loadType;
                lf.Path = path.Trim('\"');
                await queueIt(Load(lf, responseStream, context));
            }));
            parser.AddErrorListener(new ErrorListener());
            AHGHEEParser.CommandContext cc = null;

            var sw = Stopwatch.StartNew();

            Console.WriteLine("-- Parsing started --");
            for (; ; cc = parser.command())
            {
                if (cc?.exception != null)
                {
                    Console.WriteLine($"{cc.exception.Message} - found {cc.exception.OffendingToken.Text} at Line {cc.exception.OffendingToken.Line} offset at {cc.exception.OffendingToken.StartIndex}");
                }

                if (parser.CurrentToken.Type == TokenConstants.Eof)
                {
                    break;
                }
            }
            sw.Stop();
            Console.WriteLine($"-- Parsing finished in {sw.ElapsedMilliseconds}ms");
            sw.Restart();
            try
            {
                await Task.WhenAll(operationQueue);
            }
            finally
            {
                sw.Stop();
                Console.WriteLine($"-- Operation queue finished in another {sw.ElapsedMilliseconds}ms");
            }
        }