// FIXME: I don't really like this, the collection could be huge
        public ArrayList GetOlderThan(DateTime dt)
        {
            SqliteCommand    command;
            SqliteDataReader reader;

            lock (connection) {
                command             = new SqliteCommand();
                command.Connection  = connection;
                command.CommandText =
                    "SELECT uid FROM mapping WHERE last_seen < " +
                    StringFu.DateTimeToString(dt);

                reader = SqliteUtils.ExecuteReaderOrWait(command);

                ArrayList uids = new ArrayList();

                while (SqliteUtils.ReadOrWait(reader))
                {
                    uids.Add(reader.GetString(0));
                }

                reader.Close();
                command.Dispose();

                return(uids);
            }
        }
        ///////////////////////////////////////////////////////////////////

        // Return all attributes in the attributes database, used for merging

        private ICollection ReadAllAttributes()
        {
            ArrayList attributes = new ArrayList();

            SqliteCommand    command;
            SqliteDataReader reader;

            lock (connection) {
                command             = new SqliteCommand();
                command.Connection  = connection;
                command.CommandText =
                    "SELECT unique_id, directory, filename, last_mtime, last_attrtime, filter_name, filter_version " +
                    "FROM file_attributes";

                reader = SqliteUtils.ExecuteReaderOrWait(command);

                while (SqliteUtils.ReadOrWait(reader))
                {
                    attributes.Add(GetFromReader(reader));
                }
                reader.Close();
                command.Dispose();
            }

            return(attributes);
        }
        public bool Get(string uid, out uint flags)
        {
            SqliteCommand    command;
            SqliteDataReader reader;

            lock (connection) {
                command             = new SqliteCommand();
                command.Connection  = connection;
                command.CommandText = String.Format(
                    "SELECT flags FROM mapping WHERE uid='{0}'",
                    uid.Replace("'", "''"));

                reader = SqliteUtils.ExecuteReaderOrWait(command);

                try {
                    if (SqliteUtils.ReadOrWait(reader))
                    {
                        flags = (uint)reader.GetInt32(0);
                        return(true);
                    }
                    else
                    {
                        flags = 0;
                        return(false);
                    }
                } finally {
                    reader.Close();
                    command.Dispose();
                }
            }
        }
        ///////////////////////////////////////////////////////////////////

        public FileAttributes Read(string path)
        {
            // Sanitize the path; remove the last '/'
            if (path != null && path != "/" && path.EndsWith("/"))
            {
                path = path.TrimEnd('/');
            }

            if (!GetPathFlag(path))
            {
                return(null);
            }

            FileAttributes attr = null;

            // We need to quote any 's that appear in the strings
            // (int particular, in the path)
            string directory = FileSystem.GetDirectoryNameRootOk(path).Replace("'", "''");
            string filename  = Path.GetFileName(path).Replace("'", "''");

            lock (connection) {
                ReadCommand.Parameters.AddWithValue("@dir", directory);
                ReadCommand.Parameters.AddWithValue("@fname", filename);
                using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(ReadCommand)) {
                    if (SqliteUtils.ReadOrWait(reader))
                    {
                        attr = GetFromReader(reader);
                    }
                }
            }

            return(attr);
        }
Beispiel #5
0
        // Returns raw path as stored in the db i.e. relative path wrt the text_cache_dir
        private string LookupPathRawUnlocked(Uri uri)
        {
            //SqliteCommand command;
            string path = null;

            LookupPathCommand.Parameters.AddWithValue("@uri", UriToString(uri));
            using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(LookupPathCommand)) {
                if (SqliteUtils.ReadOrWait(reader))
                {
                    path = reader.GetString(0);
                }
            }

            return(path);
        }
Beispiel #6
0
        public IList <string> GetLinks(Uri uri)
        {
            string        links_text = null;
            List <string> links      = null;

            lock (connection) {
                LookupLinksCommand.Parameters.AddWithValue("@uri", UriToString(uri));
                using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(LookupLinksCommand)) {
                    if (!SqliteUtils.ReadOrWait(reader))
                    {
                        return(null);
                    }

                    links_text = reader.GetString(0);
                }
            }

            if (String.IsNullOrEmpty(links_text))
            {
                return(null);
            }

            return(links_text.Split(links_separator, StringSplitOptions.RemoveEmptyEntries));
        }
Beispiel #7
0
        // If self_cache is true when called, then self_cache will be set upon return
        public TextReader GetReader(Uri uri, ref bool self_cache)
        {
            byte[] blob     = null;
            string filename = null;

            lock (connection) {
                LookupDataCommand.Parameters.AddWithValue("@uri", UriToString(uri));
                using (SqliteDataReader reader = SqliteUtils.ExecuteReaderOrWait(LookupDataCommand)) {
                    if (!SqliteUtils.ReadOrWait(reader))
                    {
                        if (self_cache)
                        {
                            self_cache = false;
                        }
                        return(null);
                    }

                    filename = reader.GetString(0);
                    if (!reader.IsDBNull(1))
                    {
                        blob = reader.GetValue(1) as byte [];
                    }
                }
            }

            if (filename == SELF_CACHE_TAG)
            {
                if (self_cache)
                {
                    self_cache = true;
                    return(null);
                }

                if (!uri.IsFile)
                {
                    string msg = String.Format("non-file uri {0} flagged as self-cached", uri);
                    throw new Exception(msg);
                }
                return(new StreamReader(uri.LocalPath));
            }

            if (self_cache)
            {
                self_cache = false;
            }

            if (filename == BLOB_TAG && (blob == null || blob.Length == 0))
            {
                return(null);
            }

            Stream stream;

            if (filename == BLOB_TAG)
            {
                stream = new MemoryStream(blob);
            }
            else
            {
                stream = new FileStream(Path.Combine(text_cache_dir, filename), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }

            stream = new GZipInputStream(stream);
            TextReader text_reader = new StreamReader(new BufferedStream(stream));

            return(text_reader);
        }
        public FileAttributesStore_Sqlite(string directory, string index_fingerprint)
        {
            bool create_new_db = false;

            path_flags = new BitArray(65536);

            if (!File.Exists(GetDbPath(directory)))
            {
                create_new_db = true;
            }
            else
            {
                // Funky logic here to deal with sqlite versions.
                //
                // When sqlite 3 tries to open an sqlite 2 database,
                // it will throw an SqliteException with SqliteError
                // NOTADB when trying to execute a command.
                //
                // When sqlite 2 tries to open an sqlite 3 database,
                // it will throw an ApplicationException when it
                // tries to open the database.

                try {
                    connection = Open(directory);
                } catch (ApplicationException) {
                    Logger.Log.Warn("Likely sqlite database version mismatch trying to open {0}.  Purging.", GetDbPath(directory));
                    create_new_db = true;
                }

                if (!create_new_db)
                {
                    SqliteCommand    command;
                    SqliteDataReader reader   = null;
                    int    stored_version     = 0;
                    string stored_fingerprint = null;


                    command             = new SqliteCommand();
                    command.Connection  = connection;
                    command.CommandText =
                        "SELECT version, fingerprint FROM db_info";
                    try {
                        reader = SqliteUtils.ExecuteReaderOrWait(command);
                    } catch (Exception ex) {
                        Logger.Log.Warn("Likely sqlite database version mismatch trying to read from {0}.  Purging.", GetDbPath(directory));
                        create_new_db = true;
                    }
                    if (reader != null && !create_new_db)
                    {
                        if (SqliteUtils.ReadOrWait(reader))
                        {
                            stored_version     = reader.GetInt32(0);
                            stored_fingerprint = reader.GetString(1);
                        }
                        reader.Close();
                    }
                    command.Dispose();

                    if (VERSION != stored_version ||
                        (index_fingerprint != null && index_fingerprint != stored_fingerprint))
                    {
                        create_new_db = true;
                    }
                }
            }

            if (create_new_db)
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
                File.Delete(GetDbPath(directory));
                connection = Open(directory);

                SqliteUtils.DoNonQuery(connection,
                                       "CREATE TABLE db_info (             " +
                                       "  version       INTEGER NOT NULL,  " +
                                       "  fingerprint   TEXT NOT NULL    " +
                                       ")");

                SqliteUtils.DoNonQuery(connection,
                                       "INSERT INTO db_info (version, fingerprint) VALUES (@version, @index_fingerprint)",
                                       new string [] { "@version", "@index_fingerprint" },
                                       new object [] { VERSION, index_fingerprint });

                SqliteUtils.DoNonQuery(connection,
                                       "CREATE TABLE file_attributes (         " +
                                       "  unique_id      TEXT NOT NULL,        " +
                                       "  directory      TEXT NOT NULL,        " +
                                       "  filename       TEXT NOT NULL,        " +
                                       "  last_mtime     TEXT NOT NULL,        " +
                                       "  last_attrtime  TEXT NOT NULL,        " +
                                       "  filter_name    TEXT NOT NULL,        " +
                                       "  filter_version TEXT NOT NULL,        " +
                                       "  UNIQUE (directory, filename)         " +
                                       ")");
            }
            else
            {
                SqliteCommand    command;
                SqliteDataReader reader;
                int count = 0;

                DateTime dt1 = DateTime.Now;

                // Select all of the files and use them to populate our bit-vector.
                command             = new SqliteCommand();
                command.Connection  = connection;
                command.CommandText = "SELECT directory, filename FROM file_attributes";

                reader = SqliteUtils.ExecuteReaderOrWait(command);

                while (SqliteUtils.ReadOrWait(reader))
                {
                    string dir  = reader.GetString(0);
                    string file = reader.GetString(1);
                    string path = Path.Combine(dir, file);
                    SetPathFlag(path);
                    ++count;
                }

                reader.Close();
                command.Dispose();

                DateTime dt2 = DateTime.Now;

                Logger.Log.Debug("Loaded {0} records from {1} in {2:0.000}s",
                                 count, GetDbPath(directory), (dt2 - dt1).TotalSeconds);
            }
            ReadCommand             = new SqliteCommand(this.connection);
            ReadCommand.CommandText = "SELECT unique_id, directory, filename, last_mtime, last_attrtime, filter_name, filter_version " +
                                      "FROM file_attributes WHERE directory=@dir AND filename=@fname";
            InsertCommand             = new SqliteCommand(this.connection);
            InsertCommand.CommandText = "INSERT OR REPLACE INTO file_attributes " +
                                        " (unique_id, directory, filename, last_mtime, last_attrtime, filter_name, filter_version) " +
                                        " VALUES (@unique_id, @directory, @filename, @last_mtime, @last_attrtime, @filter_name, @filter_version)";
            DeleteCommand             = new SqliteCommand(this.connection);
            DeleteCommand.CommandText = "DELETE FROM file_attributes WHERE directory=@directory AND filename=@filename";
        }