private byte[] Serialize(PostingsFile item)
 {
     using (var stream = new MemoryStream())
     {
         FileBase.Serializer.Serialize(stream, item);
         return(stream.ToArray());
     }
 }
        public void Put(PostingsFile item)
        {
            if (_postingsFiles == null)
            {
                _postingsFiles = new Dictionary <string, PostingsFile>();
            }

            _postingsFiles[item.Token] = item;
        }
        public bool TryGet(string token, out PostingsFile item)
        {
            InitReadSession();

            PostingsFile pf;

            if (_postingsFiles.TryGetValue(token, out pf))
            {
                item = pf;
                return(true);
            }

            var timer = new Stopwatch();

            timer.Start();
            var fileName = Path.Combine(_directory, _containerId + ".pc");

            if (!File.Exists(fileName))
            {
                item = null;
                return(false);
            }
            using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new StreamReader(fs, Encoding.ASCII))
                {
                    var    readerToken = string.Empty;
                    string line;
                    string data = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        readerToken = line.Substring(0, line.IndexOf(':'));
                        if (readerToken == token)
                        {
                            data = line;
                            break;
                        }
                    }

                    if (data == null)
                    {
                        item = null;
                        return(false);
                    }
                    var base64 = data.Substring(readerToken.Length + 1);
                    var bytes  = Convert.FromBase64String(base64);
                    using (var memStream = new MemoryStream(bytes))
                    {
                        var obj = Deserialize(memStream);
                        _postingsFiles[token] = obj;
                        Log.DebugFormat("extracted {0} from {1} in {2}", obj, fileName, timer.Elapsed);
                        item = obj;
                        return(true);
                    }
                }
        }