Exemplo n.º 1
0
        public static int LookupWordId(SQLiteConnection sqlite, Cache cache, string word)
        {
            var cachedInt = cache.GetItemInt("id:" + word);

            if (cachedInt != null)
            {
                return(cachedInt.Value);
            }

            string        sql = @"
                SELECT Value 
                FROM [WordId] 
                WHERE [Word] = @Phrase
                ";
            SQLiteCommand cmd = sqlite.CreateCommand();

            cmd.CommandText = sql;
            cmd.Parameters.Add(new SQLiteParameter("@Phrase", word));
            SQLiteDataReader reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                var val = reader.GetInt32(0);
                cache.AddItem("id:" + word, val);
                return(val);
            }
            cache.AddItem("id:" + word, -1);
            return(-1);
        }
Exemplo n.º 2
0
        public static int LookupIdentifierSequenceFrequency(SQLiteConnection sqlite, Cache cache, string phrase)
        {
            var cachedInt = cache.GetItemInt("idseqfreq:" + phrase);

            if (cachedInt != null)
            {
                return(cachedInt.Value);
            }

            string        sql = @"
                SELECT IdentSeqValue 
                FROM [SeqFrequency] 
                WHERE [Key] = @Phrase
                ";
            SQLiteCommand cmd = sqlite.CreateCommand();

            cmd.CommandText = sql;
            cmd.Parameters.Add(new SQLiteParameter("@Phrase", phrase));
            SQLiteDataReader reader = cmd.ExecuteReader();

            if (reader.Read() && reader.HasRows)
            {
                var val = reader.GetInt32(0);
                cache.AddItem("idseqfreq:" + phrase, val);
                return(val);
            }

            cache.AddItem("idseqfreq:" + phrase, 0);
            return(0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the desired item from the server
        /// </summary>
        /// <typeparam name="T">The type of the desired DriveItem</typeparam>
        /// <param name="id">The identifier of the item</param>
        /// <param name="isRetrying">Determines if the method is retrying after an unauthorized response</param>
        /// <returns></returns>
        public async Task <T> LoadItemAsync <T>(string id, bool isRetrying = false) where T : DriveItem
        {
            using (var client = new HttpClient())
            {
                var url = new Url(BaseUrl)
                          .AppendPathSegments("items", id);

                var request = new HttpRequestMessage(HttpMethod.Get, url.ToUri());
                request.Headers.Authorization = AuthService.Instance.CreateAuthenticationHeader();

                var response = await Task.Run(() => client.SendAsync(request));

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();

                    var item = JsonConvert.DeserializeObject <T>(json);

                    Cache.AddItem(item);
                    return(item);
                }

                if (response.StatusCode == HttpStatusCode.Unauthorized && !isRetrying)
                {
                    await AuthService.Instance.LoginAsync();

                    return(await LoadItemAsync <T>(id, true));
                }

                throw new WebException(await response.Content.ReadAsStringAsync());
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends the last part of the data and gets the uploaded file
        /// </summary>
        /// <param name="client">The HTTP client to send the chunk</param>
        /// <param name="uploadUrl">The target URL</param>
        /// <param name="content">The content to be sent</param>
        /// <param name="offset">The offset of the sent bytes</param>
        /// <param name="length">The total length of the file from which the bytes are uploaded</param>
        /// <returns>The uploaded file</returns>
        private async Task <DriveFile> SendLastChunkAsync(HttpClient client, string uploadUrl, byte[] content, long offset, long length)
        {
            var lastRequest = new HttpRequestMessage(HttpMethod.Put, uploadUrl);

            lastRequest.Content = new ByteArrayContent(content);
            lastRequest.Content.Headers.ContentRange = new ContentRangeHeaderValue(offset * ChunkSize, length - 1, length);

            var lastResponse = await Task.Run(() => client.SendAsync(lastRequest));

            if (lastResponse.IsSuccessStatusCode)
            {
                var json = await lastResponse.Content.ReadAsStringAsync();

                var file = JsonConvert.DeserializeObject <DriveFile>(json);

                Cache.AddItem(file);
                return(file);
            }

            throw new WebException(await lastResponse.Content.ReadAsStringAsync());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a folder as a child of the target folder and adds it to the list of children
        /// </summary>
        /// <param name="parent">The parent of the newly created folder</param>
        /// <param name="name">The name of the folder to be created</param>
        /// <param name="isRetrying">Determines if the method is retrying after an unauthorized response</param>
        /// <returns></returns>
        public async Task <DriveFolder> CreateFolderAsync(DriveFolder parent, string name, bool isRetrying = false)
        {
            using (var client = new HttpClient())
            {
                var url = new Url(BaseUrl)
                          .AppendPathSegments("items", parent.Id, "children");

                var request = new HttpRequestMessage(HttpMethod.Post, url.ToUri());
                request.Headers.Authorization = AuthService.Instance.CreateAuthenticationHeader();

                var content = new JObject
                {
                    { "name", name },
                    { "folder", new JObject() },
                    { "@microsoft.graph.conflictBehaviour", "fail" }
                };
                request.Content = new StringContent(content.ToString(), Encoding.UTF8, "application/json");

                var response = await Task.Run(() => client.SendAsync(request));

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();

                    var folder = JsonConvert.DeserializeObject <DriveFolder>(json);
                    Cache.AddItem(folder);

                    return(folder);
                }

                if (response.StatusCode == HttpStatusCode.Unauthorized && !isRetrying)
                {
                    await AuthService.Instance.LoginAsync();

                    return(await CreateFolderAsync(parent, name, true));
                }

                throw new WebException(await response.Content.ReadAsStringAsync());
            }
        }
Exemplo n.º 6
0
        internal object Find(XmlQualifiedName name, Type type, bool checkCache)
        {
            if (!IsCompiled)
            {
                foreach (XmlSchema schema in List)
                {
                    Preprocess(schema);
                }
            }
            IList values = (IList)SchemaSet.Schemas(name.Namespace);

            if (values == null)
            {
                return(null);
            }

            foreach (XmlSchema schema in values)
            {
                Preprocess(schema);

                XmlSchemaObject ret = null;
                if (typeof(XmlSchemaType).IsAssignableFrom(type))
                {
                    ret = schema.SchemaTypes[name];
                    if (ret == null || !type.IsAssignableFrom(ret.GetType()))
                    {
                        continue;
                    }
                }
                else if (type == typeof(XmlSchemaGroup))
                {
                    ret = schema.Groups[name];
                }
                else if (type == typeof(XmlSchemaAttributeGroup))
                {
                    ret = schema.AttributeGroups[name];
                }
                else if (type == typeof(XmlSchemaElement))
                {
                    ret = schema.Elements[name];
                }
                else if (type == typeof(XmlSchemaAttribute))
                {
                    ret = schema.Attributes[name];
                }
                else if (type == typeof(XmlSchemaNotation))
                {
                    ret = schema.Notations[name];
                }
#if DEBUG
                else
                {
                    // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                    throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "XmlSchemas.Find: Invalid object type " + type.FullName));
                }
#endif

                if (ret != null && _shareTypes && checkCache && !IsReference(ret))
                {
                    ret = Cache.AddItem(ret, name, this);
                }
                if (ret != null)
                {
                    return(ret);
                }
            }
            return(null);
        }
Exemplo n.º 7
0
 public void AddItem(string userId, string productId, int quantity)
 {
     DataStore.AddItem(userId, productId, quantity);
     Cache.AddItem(userId, productId, quantity);
 }