示例#1
0
        public async Task <Result> Read(string collectionId, HttpRequest request)
        {
            Query query = null;

            try
            {
                query = _httpQueryParser.Parse(collectionId, request, _tokenizer);

                ulong keyHash = query.Term.Key.ToString().ToHash();
                long  keyId;
                var   timer = new Stopwatch();

                timer.Start();

                if (_sessionFactory.TryGetKeyId(keyHash, out keyId))
                {
                    using (var session = _sessionFactory.CreateReadSession(collectionId))
                    {
                        var result = await session.Read(query, query.Take);

                        var docs = result.Docs;

                        _log.Log(string.Format("fetched {0} docs from disk in {1}", docs.Count, timer.Elapsed));

                        timer.Restart();

                        var stream = new MemoryStream();

                        Serialize(docs, stream);

                        _log.Log(string.Format("serialized {0} docs in {1}", docs.Count, timer.Elapsed));

                        return(new Result {
                            MediaType = "application/json", Data = stream, Documents = docs, Total = result.Total
                        });
                    }
                }

                return(new Result {
                    Total = 0
                });
            }
            catch (Exception ex)
            {
                _log.Log(string.Format("read failed for query: {0} {1}", query.ToString() ?? "unknown", ex));

                throw;
            }
        }
示例#2
0
文件: Reader.cs 项目: kreeben/u-sir
        public IEnumerable <IDictionary> Read(Query query)
        {
            ulong keyHash = query.Term.Key.ToString().ToHash();
            uint  keyId;

            if (_sessionFactory.TryGetKeyId(keyHash, out keyId))
            {
                using (var session = _sessionFactory.CreateReadSession(query.CollectionId))
                {
                    foreach (var model in session.Read(query))
                    {
                        yield return(model);
                    }
                }
            }
        }
示例#3
0
文件: StoreReader.cs 项目: ikvm/resin
        public ResponseModel Read(string collectionName, IStringModel model, HttpRequest request)
        {
            var timer        = Stopwatch.StartNew();
            var collectionId = collectionName.ToHash();

            using (var session = _sessionFactory.CreateReadSession(collectionName, collectionId))
            {
                var query = _httpQueryParser.Parse(collectionId, model, request);

                if (query == null)
                {
                    return(new ResponseModel {
                        MediaType = "application/json", Total = 0
                    });
                }

                var  result = session.Read(query);
                long total  = result.Total;

                this.Log(string.Format("executed query {0} in {1}", query, timer.Elapsed));

                if (request.Query.ContainsKey("create"))
                {
                    var newCollectionName = request.Query["newCollection"].ToString();

                    if (string.IsNullOrWhiteSpace(newCollectionName))
                    {
                        newCollectionName = Guid.NewGuid().ToString();
                    }

                    _sessionFactory.ExecuteWrite(new Job(newCollectionName, result.Docs, model));
                }

                var mem = new MemoryStream();

                Serialize(result.Docs, mem);

                return(new ResponseModel
                {
                    MediaType = "application/json",
                    Documents = result.Docs,
                    Total = total,
                    Body = mem.ToArray()
                });
            }
        }
示例#4
0
        public async Task <ResponseModel> Read(string collectionName, HttpRequest request)
        {
            var timer = Stopwatch.StartNew();

            var vec1FileName = Path.Combine(_sessionFactory.Dir, string.Format("{0}.vec1", collectionName.ToHash()));

            if (File.Exists(vec1FileName))
            {
                using (var readSession = _sessionFactory.CreateReadSession(collectionName, collectionName.ToHash()))
                    using (var bowReadSession = _sessionFactory.CreateBOWReadSession(collectionName, collectionName.ToHash()))
                    {
                        int skip = 0;
                        int take = 10;

                        if (request.Query.ContainsKey("take"))
                        {
                            take = int.Parse(request.Query["take"]);
                        }

                        if (request.Query.ContainsKey("skip"))
                        {
                            skip = int.Parse(request.Query["skip"]);
                        }

                        var query  = _httpBowQueryParser.Parse(collectionName, request, readSession, _sessionFactory);
                        var result = bowReadSession.Read(query, readSession, skip, take);
                        var docs   = result.Docs;

                        this.Log(string.Format("executed query {0} and read {1} docs from disk in {2}", query, docs.Count, timer.Elapsed));

                        var stream = new MemoryStream();

                        Serialize(docs, stream);

                        return(new ResponseModel {
                            MediaType = "application/json", Stream = stream, Documents = docs, Total = result.Total
                        });
                    }
            }
            else
            {
                using (var session = _sessionFactory.CreateReadSession(collectionName, collectionName.ToHash()))
                {
                    IList <IDictionary> docs;
                    long total;
                    var  stream = new MemoryStream();

                    if (request.Query.ContainsKey("id"))
                    {
                        var ids = request.Query["id"].Select(s => long.Parse(s));

                        docs  = session.ReadDocs(ids);
                        total = docs.Count;

                        this.Log(string.Format("executed lookup by id in {0}", timer.Elapsed));
                    }
                    else
                    {
                        var query = _httpQueryParser.Parse(collectionName, request);

                        if (query == null)
                        {
                            return(new ResponseModel {
                                MediaType = "application/json", Total = 0
                            });
                        }

                        var result = session.Read(query);

                        docs  = result.Docs;
                        total = result.Total;

                        this.Log(string.Format("executed query {0} in {1}", query, timer.Elapsed));

                        if (request.Query.ContainsKey("create"))
                        {
                            var newCollectionName = request.Query["newCollection"].ToString();

                            if (string.IsNullOrWhiteSpace(newCollectionName))
                            {
                                newCollectionName = Guid.NewGuid().ToString();
                            }

                            await _storeWriter.ExecuteWrite(newCollectionName, docs);
                        }
                    }

                    Serialize(docs, stream);

                    return(new ResponseModel {
                        MediaType = "application/json", Stream = stream, Documents = docs, Total = total
                    });
                }
            }
        }