text:
using System;
using System.Data;
using System.Reflection;
using OpenSim.Data;
using OpenSim.Framework;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
using log4net;
using Npgsql;

namespace OpenSim.Data.PGSQL
{
    public class UserProfilesData: IProfilesData
    {
        static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        protected PGSQLManager m_database;

        #region Properites
        string ConnectionString
        {
            get; set;
        }

        protected virtual Assembly Assembly
        {
            get { return GetType().Assembly; }
        }

        #endregion Properties

        #region class Member Functions
        public UserProfilesData(string connectionString)
        {
            ConnectionString = connectionString;
            Init();
        }

        void Init()
        {
            using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
            {
                dbcon.Open();

                Migration m = new Migration(dbcon, Assembly, "UserProfiles");
                m.Update();
                m_database = new PGSQLManager(ConnectionString);
            }
        }
        #endregion Member Functions

        #region Classifieds Queries
        /// <summary>
        /// Gets the classified records.
        /// </summary>
        /// <returns>
        /// Array of classified records
        /// </returns>
        /// <param name='creatorId'>
        /// Creator identifier.
        /// </param>
        public OSDArray GetClassifiedRecords(UUID creatorId)
        {
            OSDArray data = new OSDArray();

            using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
            {
                string query = @"SELECT classifieduuid, name FROM classifieds WHERE creatoruuid = :Id";
                dbcon.Open();
                using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                {
                    cmd.Parameters.Add(m_database.CreateParameter("Id", creatorId));
                    using( NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default))
                    {
                        if(reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                OSDMap n = new OSDMap();
                                UUID Id = UUID.Zero;

                                string Name = null;
                                try
                                {
                                    Id = DBGuid.FromDB(reader["classifieduuid"]);
                                    Name = Convert.ToString(reader["name"]);
                                }
                                catch (Exception e)
                                {
                                    m_log.Error("[PROFILES_DATA]: UserAccount exception ", e);
                                }

                                n.Add("classifieduuid", OSD.FromUUID(Id));
                                n.Add("name", OSD.FromString(Name));
                                data.Add(n);
                            }
                        }
                    }
                }
            }
            return data;
        }

        public bool UpdateClassifiedRecord(UserClassifiedAdd ad, ref string result)
        {
            string query = string.Empty;

            query = @"WITH upsert AS (
                        UPDATE classifieds SET
                            classifieduuid = :ClassifiedId, creatoruuid = :CreatorId, creationdate = :CreatedDate,
                            expirationdate = :ExpirationDate,category =:Category, name = :Name, description = :Description,
                            parceluuid = :ParcelId, parentestate = :ParentEstate, snapshotuuid = :SnapshotId,
                            simname = :SimName, posglobal = :GlobalPos, parcelname = :ParcelName, classifiedflags = :Flags,
                            priceforlisting = :ListingPrice
                        RETURNING * )
                      INSERT INTO classifieds (classifieduuid,creatoruuid,creationdate,expirationdate,category,name,
                            description,parceluuid,parentestate,snapshotuuid,simname,posglobal,parcelname,classifiedflags,
                            priceforlisting)
                      SELECT
                            :ClassifiedId,:CreatorId,:CreatedDate,:ExpirationDate,:Category,:Name,:Description,
                            :ParcelId,:ParentEstate,:SnapshotId,:SimName,:GlobalPos,:ParcelName,:Flags,:ListingPrice
                      WHERE NOT EXISTS (
                        SELECT * FROM upsert )";

            if(string.IsNullOrEmpty(ad.ParcelName))
                ad.ParcelName = "Unknown";
            if(ad.ParcelId == null)
                ad.ParcelId = UUID.Zero;
            if(string.IsNullOrEmpty(ad.Description))
                ad.Description = "No Description";

            DateTime epoch = new DateTime(1970, 1, 1);
            DateTime now = DateTime.Now;
            TimeSpan epochnow = now - epoch;
            TimeSpan duration;
            DateTime expiration;
            TimeSpan epochexp;

            if(ad.Flags == 2)
            {
                duration = new TimeSpan(7,0,0,0);
                expiration = now.Add(duration);
                epochexp = expiration - epoch;
            }
            else
            {
                duration = new TimeSpan(365,0,0,0);
                expiration = now.Add(duration);
                epochexp = expiration - epoch;
            }
            ad.CreationDate = (int)epochnow.TotalSeconds;
            ad.ExpirationDate = (int)epochexp.TotalSeconds;

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("ClassifiedId", ad.ClassifiedId));
                        cmd.Parameters.Add(m_database.CreateParameter("CreatorId", ad.CreatorId));
                        cmd.Parameters.Add(m_database.CreateParameter("CreatedDate", (int)ad.CreationDate));
                        cmd.Parameters.Add(m_database.CreateParameter("ExpirationDate", (int)ad.ExpirationDate));
                        cmd.Parameters.Add(m_database.CreateParameter("Category", ad.Category.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("Name", ad.Name.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("Description", ad.Description.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("ParcelId", ad.ParcelId));
                        cmd.Parameters.Add(m_database.CreateParameter("ParentEstate", (int)ad.ParentEstate));
                        cmd.Parameters.Add(m_database.CreateParameter("SnapshotId", ad.SnapshotId));
                        cmd.Parameters.Add(m_database.CreateParameter("SimName", ad.SimName.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("GlobalPos", ad.GlobalPos.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("ParcelName", ad.ParcelName.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("Flags", (int)Convert.ToInt32(ad.Flags)));
                        cmd.Parameters.Add(m_database.CreateParameter("ListingPrice", (int)Convert.ToInt32(ad.Price)));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: ClassifiedsUpdate exception ", e);
                result = e.Message;
                return false;
            }

            return true;
        }

        public bool DeleteClassifiedRecord(UUID recordId)
        {
            string query = string.Empty;

            query = @"DELETE FROM classifieds WHERE classifieduuid = :ClasifiedId ;";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();

                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("ClassifiedId", recordId));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: DeleteClassifiedRecord exception ", e);
                return false;
            }

            return true;
        }

        public bool GetClassifiedInfo(ref UserClassifiedAdd ad, ref string result)
        {
            string query = string.Empty;

            query += "SELECT * FROM classifieds WHERE ";
            query += "classifieduuid = :AdId";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("AdId", ad.ClassifiedId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader())
                        {
                            if(reader.Read ())
                            {
                                ad.CreatorId = DBGuid.FromDB(reader["creatoruuid"]);
                                ad.ParcelId = DBGuid.FromDB(reader["parceluuid"]);
                                ad.SnapshotId = DBGuid.FromDB(reader["snapshotuuid"]);
                                ad.CreationDate = Convert.ToInt32(reader["creationdate"]);
                                ad.ExpirationDate = Convert.ToInt32(reader["expirationdate"]);
                                ad.ParentEstate = Convert.ToInt32(reader["parentestate"]);
                                ad.Flags = (byte)Convert.ToInt16(reader["classifiedflags"]);
                                ad.Category = Convert.ToInt32(reader["category"]);
                                ad.Price = Convert.ToInt16(reader["priceforlisting"]);
                                ad.Name = reader["name"].ToString();
                                ad.Description = reader["description"].ToString();
                                ad.SimName = reader["simname"].ToString();
                                ad.GlobalPos = reader["posglobal"].ToString();
                                ad.ParcelName = reader["parcelname"].ToString();
                            }
                        }
                    }
                    dbcon.Close();
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetClassifiedInfo exception ", e);
            }

            return true;
        }

        public static UUID GetUUID(object uuidValue)
        {

            UUID ret = UUID.Zero;

            UUID.TryParse(uuidValue.ToString(), out ret);

            return ret;
        }

        #endregion Classifieds Queries

        #region Picks Queries
        public OSDArray GetAvatarPicks(UUID avatarId)
        {
            string query = string.Empty;

            query += "SELECT pickuuid, name FROM userpicks WHERE ";
            query += "creatoruuid = :Id";
            OSDArray data = new OSDArray();

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", avatarId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader())
                        {
                            if(reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    OSDMap record = new OSDMap();

                                    record.Add("pickuuid",OSD.FromUUID(DBGuid.FromDB(reader["pickuuid"])));
                                    record.Add("name",OSD.FromString((string)reader["name"]));
                                    data.Add(record);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetAvatarPicks exception ", e);
            }

            return data;
        }

        public UserProfilePick GetPickInfo(UUID avatarId, UUID pickId)
        {
            string query = string.Empty;
            UserProfilePick pick = new UserProfilePick();

            query += "SELECT * FROM userpicks WHERE ";
            query += "creatoruuid = :CreatorId AND ";
            query += "pickuuid =  :PickId";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("CreatorId", avatarId));
                        cmd.Parameters.Add(m_database.CreateParameter("PickId", pickId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader())
                        {
                            if(reader.HasRows)
                            {
                                reader.Read();

                                string description = (string)reader["description"];

                                if (string.IsNullOrEmpty(description))
                                    description = "No description given.";

                                pick.PickId = DBGuid.FromDB(reader["pickuuid"]);
                                pick.CreatorId = DBGuid.FromDB(reader["creatoruuid"]);
                                pick.ParcelId = DBGuid.FromDB(reader["parceluuid"]);
                                pick.SnapshotId = DBGuid.FromDB(reader["snapshotuuid"]);
                                pick.GlobalPos = (string)reader["posglobal"].ToString();
                                pick.TopPick = Convert.ToBoolean(reader["toppick"]);
                                pick.Enabled = Convert.ToBoolean(reader["enabled"]);
                                pick.Name = reader["name"].ToString ();
                                pick.Desc = reader["description"].ToString();
                                pick.ParcelName = reader["user"].ToString();
                                pick.OriginalName = reader["originalname"].ToString();
                                pick.SimName = reader["simname"].ToString();
                                pick.SortOrder = (int)reader["sortorder"];
                            }
                        }
                    }
                    dbcon.Close();
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetPickInfo exception ", e);
            }

            return pick;
        }

        public bool UpdatePicksRecord(UserProfilePick pick)
        {
            string query = string.Empty;

            query = @"WITH upsert AS (
                        UPDATE userpicks SET
                            pickuuid = :PickId, creatoruuid = :CreatorId, toppick = :TopPick, parceluuid = :ParcelId,
                            name = :Name, description = :Desc, snapshotuuid = :SnapshotId, ""user"" = :User,
                            originalname = :Original, simname = :SimName, posglobal = :GlobalPos,
                            sortorder = :SortOrder, enabled = :Enabled
                        RETURNING * )
                      INSERT INTO userpicks (pickuuid,creatoruuid,toppick,parceluuid,name,description,
                            snapshotuuid,""user"",originalname,simname,posglobal,sortorder,enabled)
                      SELECT
                            :PickId,:CreatorId,:TopPick,:ParcelId,:Name,:Desc,:SnapshotId,:User,
                            :Original,:SimName,:GlobalPos,:SortOrder,:Enabled
                      WHERE NOT EXISTS (
                        SELECT * FROM upsert )";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("PickId", pick.PickId));
                        cmd.Parameters.Add(m_database.CreateParameter("CreatorId", pick.CreatorId));
                        cmd.Parameters.Add(m_database.CreateParameter("TopPick", pick.TopPick));
                        cmd.Parameters.Add(m_database.CreateParameter("ParcelId", pick.ParcelId));
                        cmd.Parameters.Add(m_database.CreateParameter("Name", pick.Name));
                        cmd.Parameters.Add(m_database.CreateParameter("Desc", pick.Desc));
                        cmd.Parameters.Add(m_database.CreateParameter("SnapshotId", pick.SnapshotId));
                        cmd.Parameters.Add(m_database.CreateParameter("User", pick.ParcelName));
                        cmd.Parameters.Add(m_database.CreateParameter("Original", pick.OriginalName));
                        cmd.Parameters.Add(m_database.CreateParameter("SimName",pick.SimName));
                        cmd.Parameters.Add(m_database.CreateParameter("GlobalPos", pick.GlobalPos));
                        cmd.Parameters.Add(m_database.CreateParameter("SortOrder", pick.SortOrder));
                        cmd.Parameters.Add(m_database.CreateParameter("Enabled", pick.Enabled));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: UpdateAvatarNotes exception ", e);
                return false;
            }

            return true;
        }

        public bool DeletePicksRecord(UUID pickId)
        {
            string query = string.Empty;

            query += "DELETE FROM userpicks WHERE ";
            query += "pickuuid = :PickId";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();

                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("PickId", pickId));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: DeleteUserPickRecord exception ", e);
                return false;
            }

            return true;
        }

        #endregion Picks Queries

        #region Avatar Notes Queries

        public bool GetAvatarNotes(ref UserProfileNotes notes)
        {  // WIP
            string query = string.Empty;

            query += "SELECT notes FROM usernotes WHERE ";
            query += "useruuid = :Id AND ";
            query += "targetuuid = :TargetId";
            OSDArray data = new OSDArray();

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", notes.UserId));
                        cmd.Parameters.Add(m_database.CreateParameter("TargetId", notes.TargetId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if(reader.HasRows)
                            {
                                reader.Read();
                                notes.Notes = OSD.FromString((string)reader["notes"]);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetAvatarNotes exception ", e);
            }

            return true;
        }

        public bool UpdateAvatarNotes(ref UserProfileNotes note, ref string result)
        {
            string query = string.Empty;
            bool remove;

            if (string.IsNullOrEmpty(note.Notes))
            {
                remove = true;
                query += "DELETE FROM usernotes WHERE ";
                query += "useruuid=:UserId AND ";
                query += "targetuuid=:TargetId";
            }
            else
            {
                remove = false;

                query = @"WITH upsert AS (
                          UPDATE usernotes SET notes = :Notes, useruuid = :UserId, targetuuid = :TargetId RETURNING * )
                          INSERT INTO usernotes (notes,useruuid,targetuuid)
                          SELECT :Notes,:UserId,:TargetId
                            WHERE NOT EXISTS (
                              SELECT * FROM upsert
                            )";
            }

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        if(!remove)
                            cmd.Parameters.Add(m_database.CreateParameter("Notes", note.Notes));

                        cmd.Parameters.Add(m_database.CreateParameter("TargetId", note.TargetId));
                        cmd.Parameters.Add(m_database.CreateParameter("UserId", note.UserId));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: UpdateAvatarNotes exception ", e);
                return false;
            }

            return true;
        }

        #endregion Avatar Notes Queries

        #region Avatar Properties

        public bool GetAvatarProperties(ref UserProfileProperties props, ref string result)
        {
            string query = string.Empty;

            query += "SELECT * FROM userprofile WHERE ";
            query += "useruuid = :Id";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", props.UserId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if(reader.HasRows)
                            {
                                // m_log.DebugFormat("[PROFILES_DATA]" +
                                //                  ": Getting data for {0}.", props.UserId);
                                reader.Read();
                                props.WebUrl = (string)reader["profileURL"].ToString();
                                props.ImageId = DBGuid.FromDB(reader["profileImage"]);
                                props.AboutText = (string)reader["profileAboutText"];
                                props.FirstLifeImageId = DBGuid.FromDB(reader["profileFirstImage"]);
                                props.FirstLifeText = (string)reader["profileFirstText"];
                                props.PartnerId = DBGuid.FromDB(reader["profilePartner"]);
                                props.WantToMask = (int)reader["profileWantToMask"];
                                props.WantToText = (string)reader["profileWantToText"];
                                props.SkillsMask = (int)reader["profileSkillsMask"];
                                props.SkillsText = (string)reader["profileSkillsText"];
                                props.Language = (string)reader["profileLanguages"];
                            }
                            else
                            {
                                //m_log.DebugFormat("[PROFILES_DATA]" +
                                //                 ": No data for {0}", props.UserId);

                                props.WebUrl = string.Empty;
                                props.ImageId = UUID.Zero;
                                props.AboutText = string.Empty;
                                props.FirstLifeImageId = UUID.Zero;
                                props.FirstLifeText = string.Empty;
                                props.PartnerId = UUID.Zero;
                                props.WantToMask = 0;
                                props.WantToText = string.Empty;
                                props.SkillsMask = 0;
                                props.SkillsText = string.Empty;
                                props.Language = string.Empty;
                                props.PublishProfile = false;
                                props.PublishMature = false;

                                query = "INSERT INTO userprofile (";
                                query += "useruuid, ";
                                query += "\"profilePartner\", ";
                                query += "\"profileAllowPublish\", ";
                                query += "\"profileMaturePublish\", ";
                                query += "\"profileURL\", ";
                                query += "\"profileWantToMask\", ";
                                query += "\"profileWantToText\", ";
                                query += "\"profileSkillsMask\", ";
                                query += "\"profileSkillsText\", ";
                                query += "\"profileLanguages\", ";
                                query += "\"profileImage\", ";
                                query += "\"profileAboutText\", ";
                                query += "\"profileFirstImage\", ";
                                query += "\"profileFirstText\") VALUES (";
                                query += ":userId, ";
                                query += ":profilePartner, ";
                                query += ":profileAllowPublish, ";
                                query += ":profileMaturePublish, ";
                                query += ":profileURL, ";
                                query += ":profileWantToMask, ";
                                query += ":profileWantToText, ";
                                query += ":profileSkillsMask, ";
                                query += ":profileSkillsText, ";
                                query += ":profileLanguages, ";
                                query += ":profileImage, ";
                                query += ":profileAboutText, ";
                                query += ":profileFirstImage, ";
                                query += ":profileFirstText)";

                                dbcon.Close();
                                dbcon.Open();

                                using (NpgsqlCommand put = new NpgsqlCommand(query, dbcon))
                                {
                                    //m_log.DebugFormat("[PROFILES_DATA]" +
                                    //                  ": Adding new data for {0}", props.UserId);

                                    put.Parameters.Add(m_database.CreateParameter("userId", props.UserId));
                                    put.Parameters.Add(m_database.CreateParameter("profilePartner", props.PartnerId));
                                    put.Parameters.Add(m_database.CreateParameter("profileAllowPublish", props.PublishProfile));
                                    put.Parameters.Add(m_database.CreateParameter("profileMaturePublish", props.PublishMature));
                                    put.Parameters.Add(m_database.CreateParameter("profileURL", props.WebUrl));
                                    put.Parameters.Add(m_database.CreateParameter("profileWantToMask", props.WantToMask));
                                    put.Parameters.Add(m_database.CreateParameter("profileWantToText", props.WantToText));
                                    put.Parameters.Add(m_database.CreateParameter("profileSkillsMask", props.SkillsMask));
                                    put.Parameters.Add(m_database.CreateParameter("profileSkillsText", props.SkillsText));
                                    put.Parameters.Add(m_database.CreateParameter("profileLanguages", props.Language));
                                    put.Parameters.Add(m_database.CreateParameter("profileImage", props.ImageId));
                                    put.Parameters.Add(m_database.CreateParameter("profileAboutText", props.AboutText));
                                    put.Parameters.Add(m_database.CreateParameter("profileFirstImage", props.FirstLifeImageId));
                                    put.Parameters.Add(m_database.CreateParameter("profileFirstText", props.FirstLifeText));

                                    put.ExecuteNonQuery();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetAvatarProperties exception ", e);
                result = e.Message;
                return false;
            }

            return true;
        }

        public bool UpdateAvatarProperties(ref UserProfileProperties props, ref string result)
        {
            string query = string.Empty;

            query += "UPDATE userprofile SET ";
            query += "\"profileURL\"=:profileURL, ";
            query += "\"profileImage\"=:image, ";
            query += "\"profileAboutText\"=:abouttext,";
            query += "\"profileFirstImage\"=:firstlifeimage,";
            query += "\"profileFirstText\"=:firstlifetext ";
            query += "WHERE \"useruuid\"=:uuid";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("profileURL", props.WebUrl));
                        cmd.Parameters.Add(m_database.CreateParameter("image", props.ImageId));
                        cmd.Parameters.Add(m_database.CreateParameter("abouttext", props.AboutText));
                        cmd.Parameters.Add(m_database.CreateParameter("firstlifeimage", props.FirstLifeImageId));
                        cmd.Parameters.Add(m_database.CreateParameter("firstlifetext", props.FirstLifeText));
                        cmd.Parameters.Add(m_database.CreateParameter("uuid", props.UserId));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: AgentPropertiesUpdate exception ", e);
                return false;
            }

            return true;
        }

        #endregion Avatar Properties

        #region Avatar Interests

        public bool UpdateAvatarInterests(UserProfileProperties up, ref string result)
        {
            string query = string.Empty;

            query += "UPDATE userprofile SET ";
            query += "\"profileWantToMask\"=:WantMask, ";
            query += "\"profileWantToText\"=:WantText,";
            query += "\"profileSkillsMask\"=:SkillsMask,";
            query += "\"profileSkillsText\"=:SkillsText, ";
            query += "\"profileLanguages\"=:Languages ";
            query += "WHERE \"useruuid\"=:uuid";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("WantMask", up.WantToMask));
                        cmd.Parameters.Add(m_database.CreateParameter("WantText", up.WantToText));
                        cmd.Parameters.Add(m_database.CreateParameter("SkillsMask", up.SkillsMask));
                        cmd.Parameters.Add(m_database.CreateParameter("SkillsText", up.SkillsText));
                        cmd.Parameters.Add(m_database.CreateParameter("Languages", up.Language));
                        cmd.Parameters.Add(m_database.CreateParameter("uuid", up.UserId));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: UpdateAvatarInterests exception ", e);
                result = e.Message;
                return false;
            }

            return true;
        }

        #endregion Avatar Interests

        public OSDArray GetUserImageAssets(UUID avatarId)
        {
            OSDArray data = new OSDArray();
            string query = "SELECT \"snapshotuuid\" FROM {0} WHERE \"creatoruuid\" = :Id";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();

                    using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format (query,"\"classifieds\""), dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", avatarId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if(reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    data.Add(new OSDString(reader["snapshotuuid"].ToString()));
                                }
                            }
                        }
                    }

                    dbcon.Close();
                    dbcon.Open();

                    using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format (query,"\"userpicks\""), dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", avatarId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if(reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    data.Add(new OSDString(reader["snapshotuuid"].ToString()));
                                }
                            }
                        }
                    }

                    dbcon.Close();
                    dbcon.Open();

                    query = "SELECT \"profileImage\", \"profileFirstImage\" FROM \"userprofile\" WHERE \"useruuid\" = :Id";

                    using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format (query,"\"userpicks\""), dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", avatarId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if(reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    data.Add(new OSDString(reader["profileImage"].ToString()));
                                    data.Add(new OSDString(reader["profileFirstImage"].ToString()));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetUserImageAssets exception ", e);
            }

            return data;
        }

        #region User Preferences

        public bool GetUserPreferences(ref UserPreferences pref, ref string result)
        {
            string query = string.Empty;

            query += "SELECT imviaemail,visible,email FROM ";
            query += "usersettings WHERE ";
            query += "useruuid = :Id";

            OSDArray data = new OSDArray();

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", pref.UserId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader())
                        {
                            if(reader.HasRows)
                            {
                                reader.Read();
                                bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
                                bool.TryParse((string)reader["visible"], out pref.Visible);
                                pref.EMail = (string)reader["email"];
                            }
                            else
                            {
                                using (NpgsqlCommand put = new NpgsqlCommand(query, dbcon))
                                {
                                    put.Parameters.Add(m_database.CreateParameter("Id", pref.UserId));
                                    query = "INSERT INTO usersettings VALUES ";
                                    query += "(:Id,'false','false', '')";

                                    put.ExecuteNonQuery();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetUserPreferences exception ", e);
                result = e.Message;
            }

            return true;
        }

        public bool UpdateUserPreferences(ref UserPreferences pref,  ref string result)
        {
            string query = string.Empty;

            query += "UPDATE usersettings SET ";
            query += "imviaemail=:ImViaEmail, ";
            query += "visible=:Visible, ";
            query += "email=:Email ";
            query += "WHERE useruuid=:uuid";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("ImViaEmail", pref.IMViaEmail));
                        cmd.Parameters.Add(m_database.CreateParameter("Visible", pref.Visible));
                        cmd.Parameters.Add(m_database.CreateParameter("EMail", pref.EMail.ToString().ToLower()));
                        cmd.Parameters.Add(m_database.CreateParameter("uuid", pref.UserId));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: UpdateUserPreferences exception ", e);
                result = e.Message;
                return false;
            }

            return true;
        }

        #endregion User Preferences

        #region Integration

        public bool GetUserAppData(ref UserAppData props, ref string result)
        {
            string query = string.Empty;

            query += "SELECT * FROM userdata WHERE ";
            query += "\"UserId\" = :Id AND ";
            query += "\"TagId\" = :TagId";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("Id", props.UserId));
                        cmd.Parameters.Add(m_database.CreateParameter("TagId", props.TagId));

                        using (NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if(reader.HasRows)
                            {
                                reader.Read();
                                props.DataKey = (string)reader["DataKey"];
                                props.DataVal = (string)reader["DataVal"];
                            }
                            else
                            {
                                query += "INSERT INTO userdata VALUES ( ";
                                query += ":UserId,";
                                query += ":TagId,";
                                query += ":DataKey,";
                                query +=  ":DataVal) ";

                                using (NpgsqlCommand put = new NpgsqlCommand(query, dbcon))
                                {
                                    put.Parameters.Add(m_database.CreateParameter("UserId", props.UserId));
                                    put.Parameters.Add(m_database.CreateParameter("TagId", props.TagId));
                                    put.Parameters.Add(m_database.CreateParameter("DataKey", props.DataKey.ToString()));
                                    put.Parameters.Add(m_database.CreateParameter("DataVal", props.DataVal.ToString()));

                                    put.ExecuteNonQuery();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: GetUserAppData exception ", e);
                result = e.Message;
                return false;
            }

            return true;
        }

        public bool SetUserAppData(UserAppData props, ref string result)
        {
            string query = string.Empty;

            query += "UPDATE userdata SET ";
            query += "\"TagId\" = :TagId, ";
            query += "\"DataKey\" = :DataKey, ";
            query += "\"DataVal\" = :DataVal WHERE ";
            query += "\"UserId\" = :UserId AND ";
            query += "\"TagId\" = :TagId";

            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("UserId", props.UserId.ToString()));
                        cmd.Parameters.Add(m_database.CreateParameter("TagId", props.TagId.ToString ()));
                        cmd.Parameters.Add(m_database.CreateParameter("DataKey", props.DataKey.ToString ()));
                        cmd.Parameters.Add(m_database.CreateParameter("DataVal", props.DataKey.ToString ()));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: SetUserData exception ", e);
                return false;
            }

            return true;
        }

        #endregion Integration
    }
}


text:
#ifndef GRAPHLAB_IMAGE_IMAGE_TYPE_HPP
#define GRAPHLAB_IMAGE_IMAGE_TYPE_HPP

#include <string>
#include <serialization/serialization_includes.hpp>
#include <boost/shared_ptr.hpp>

const char IMAGE_TYPE_CURRENT_VERSION = 0;

namespace graphlab {

/**
 * \ingroup group_gl_flexible_type
 * Possible image formats stored in the image type
 */
enum class Format: size_t {
  JPG = 0,  ///< JPEG Compressed
  PNG = 1,  ///< PNG Compressed
  RAW_ARRAY = 2,  ///< Not Compressed
  UNDEFINED = 3   ///< Unknown
};

/**
 * \ingroup group_gl_flexible_type
 * Image type, which is typedef'd to flex_image, part of the flexible_type union.
 * Holds image data and meta-data pertaining to image size and file type, but
 * does not hold meta-data like path or category.
 */
class image_type {

public:
  /// The image data, stored in the format indicated by m_format in a char array
  boost::shared_ptr<char[]> m_image_data;
  /// The height of the image
  size_t m_height = 0;
  /// The width of the image*
  size_t m_width = 0;
  /// The number of channels in the image: Grayscale = 1, RGB = 3, RGBA = 4.
  size_t m_channels = 0;
  /// Length of m_image_data char array
  size_t m_image_data_size = 0;
  /// Version of image_type object
  char m_version = IMAGE_TYPE_CURRENT_VERSION;
  /// Format of data, intitialized as UNDEFINED
  Format m_format = Format::UNDEFINED;
  /// Constructor
  image_type() = default;
  /// Construct from existing data
  image_type(char* image_data, size_t height, size_t width,
             size_t channels, size_t image_data_size, int version, int format);
  /// Check whether image is decoded
  inline bool is_decoded() const { return m_format == Format::RAW_ARRAY; }
  /// Serialization
  void save(oarchive& oarc) const;
  /// Deserialization
  void load(iarchive& iarc);
  /// Returns a char* pointer to the raw image data
  const unsigned char* get_image_data() const;
};

} // end of namespace graphlab

#endif

redpajama_set_name: RedPajamaGithub


text:
package internal

import (
	"math"
	"time"
)

type metricFuncType func() float64
type reportFuncType func(trigger string)

type ReportingStrategy struct {
	agent           *Agent
	delay           int
	interval        int
	metricFunc      metricFuncType
	reportFunc      reportFuncType
	reporting       bool
	anomalyReported bool
	measurements    []float64
}

func newReportingStrategy(agent *Agent, delay int, interval int, metricFunc metricFuncType, reportFunc reportFuncType) *ReportingStrategy {
	rs := &ReportingStrategy{
		agent:           agent,
		delay:           delay,
		interval:        interval,
		metricFunc:      metricFunc,
		reportFunc:      reportFunc,
		reporting:       false,
		anomalyReported: false,
		measurements:    make([]float64, 0),
	}

	return rs
}

func (rs *ReportingStrategy) start() {

	if rs.metricFunc != nil {
		anomalyTicker := time.NewTicker(1 * time.Second)
		go func() {
			ph := rs.agent.panicHandler()
			defer ph()

			for {
				select {
				case <-anomalyTicker.C:
					if rs.checkAnomaly() && !rs.anomalyReported {
						rs.executeReport(TriggerAnomaly)
						rs.anomalyReported = true
					}
				}
			}
		}()
	}

	delayTimer := time.NewTimer(time.Duration(rs.delay) * time.Second)
	go func() {
		ph := rs.agent.panicHandler()
		defer ph()

		<-delayTimer.C
		rs.executeReport(TriggerTimer)
		rs.anomalyReported = false

		intervalTicker := time.NewTicker(time.Duration(rs.interval) * time.Second)
		go func() {
			ph := rs.agent.panicHandler()
			defer ph()

			for {
				select {
				case <-intervalTicker.C:
					rs.executeReport(TriggerTimer)
					rs.anomalyReported = false
				}
			}
		}()
	}()
}

func (rs *ReportingStrategy) checkAnomaly() bool {
	m := rs.metricFunc()
	rs.measurements = append(rs.measurements, m)
	l := len(rs.measurements)

	if l < 60 {
		return false
	} else if l > 60 {
		rs.measurements = rs.measurements[l-60 : l]
	}

	mean, dev := stdev(rs.measurements[0:30])

	recent := rs.measurements[50:60]
	anomalies := 0
	for _, m := range recent {
		if m > mean+2*dev && m > mean+mean*0.5 {
			anomalies++
		}
	}

	return anomalies >= 5
}

func stdev(numbers []float64) (float64, float64) {
	mean := 0.0
	for _, number := range numbers {
		mean += number
	}
	mean = mean / float64(len(numbers))

	total := 0.0
	for _, number := range numbers {
		total += math.Pow(number-mean, 2)
	}

	variance := total / float64(len(numbers)-1)

	return mean, math.Sqrt(variance)
}

func (rs *ReportingStrategy) executeReport(trigger string) {
	if !rs.reporting {
		rs.agent.overheadLock.Lock()
		rs.reporting = true
		rs.reportFunc(trigger)
		rs.reporting = false
		rs.agent.overheadLock.Unlock()
	}
}


text:
package com.salesforce.dataloader.mapping;

import com.salesforce.dataloader.ConfigTestBase;
import com.salesforce.dataloader.client.PartnerClient;
import com.salesforce.dataloader.config.Config;
import com.salesforce.dataloader.exception.MappingInitializationException;
import com.sforce.ws.ConnectionException;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 *
 * @author Federico Recio
 * @since 27.0
 */
public class SOQLMapperTest extends ConfigTestBase {

    private SOQLMapper soqlMapper;

    @Before
    public void createSoqlMapper() throws Exception {
        PartnerClient partnerClient = new PartnerClient(getController());
        soqlMapper = new SOQLMapper(partnerClient, Collections.<String>emptyList(), null, "");
    }

    @Test
    public void testRelationshipQuery() throws Exception {
        getController().getConfig().setValue(Config.ENTITY, "User");
        soqlMapper.initSoqlMapping("select Id, Contact.Accountid from User");

        List<String> daoColumnsForSoql = soqlMapper.getDaoColumnsForSoql();
        assertEquals(2, daoColumnsForSoql.size());
        assertTrue(daoColumnsForSoql.contains("Id"));
        assertTrue(daoColumnsForSoql.contains("Contact.Accountid"));
    }

    /**
     * Verify that when the query does not match up to the column, an exception is thrown.
     *
     * @expectedResults Assert that an exception is thrown with the correct error message.
     *
     * @throws Exception
     */
    @Test
    public void testMapAutoMatchFail() throws Exception {
        try {
            doAutoMatchTest("Select id, account.parent.id, A.numberofemployees from account a");
        } catch (Mapper.InvalidMappingException e) {
            assertEquals(
                    "The following dao columns could not be mapped: [NAME]",
                    e.getMessage());
        }
    }

    /**
     * Verify that the correct columns are mapped from a query.
     *
     * @expectedResults Assert that the returned map of columns is the same as what the SOQLMapper returns.
     *
     * @throws Exception
     */
    @Test
    public void testMapAutoMatch() throws Exception {
        doAutoMatchTest("Select AccOUNT.NamE, id, account.parent.id, A.numberofemployees from account a");
    }

    private void doAutoMatchTest(String soql) throws ConnectionException,
            MappingInitializationException {
        getController().login();
        List<String> daoCols = Arrays.asList("NAME", "ID", "Parent.Id",
                "NumberOfEMPLOYEES");
        SOQLMapper mapper = new SOQLMapper(getController().getPartnerClient(),
                daoCols, null, null);
        mapper.initSoqlMapping(soql);
        List<String> actual = mapper.getDaoColumnsForSoql();
        assertEquals(daoCols, actual);
    }
}


text:
/**
 * @author	Binson Zhang <bin183cs@gmail.com>
 * @date		2013-1-5
 */

#include "brute_force_search.h"
#include <gtest/gtest.h>

namespace balgo {
namespace string {

TEST(BruteForce, BruteForceSearch) {
  const char* p = BruteForceSearch("abc", "abbbbc");
  EXPECT_EQ(NULL, p);

  p = BruteForceSearch("abc", "iamnotanabcohyeah");
  EXPECT_STREQ("abcohyeah", p);
}

TEST(BruteForce, BruteForceSearchFromRightToLeft) {
  const char* p = BruteForceSearchFromRightToLeft("abc", "abbbbc");
  EXPECT_EQ(NULL, p);

  p = BruteForceSearchFromRightToLeft("abc", "iamnotanabcohyeah");
  EXPECT_STREQ("abcohyeah", p);
}

} /* namespace string */
} /* namespace balgo */


text:
//
//  KHLeaveStreakServiceDelegate.h
//  Streak Club
//
//  Created by Kevin Hwang on 7/30/15.
//  Copyright © 2015 Kevin Hwang. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@protocol KHLeaveStreakServiceDelegate <NSObject>

/**
 @brief Indicates that the streak with this ID has been left.
 @param streakId the ID of the streak that the user has left.
 */
- (void)streakLeft:(NSNumber *)streakId;

@end

NS_ASSUME_NONNULL_END


text:
using System;
using System.Collections.Generic;
using System.Text;

namespace Geb.Image.Formats
{
    /// <summary>
    /// Memory managers are used to allocate memory for image processing operations.
    /// </summary>
    public abstract class MemoryManager
    {
        /// <summary>
        /// Allocates an <see cref="IBuffer{T}"/> of size <paramref name="length"/>, optionally
        /// clearing the buffer before it gets returned.
        /// </summary>
        /// <typeparam name="T">Type of the data stored in the buffer</typeparam>
        /// <param name="length">Size of the buffer to allocate</param>
        /// <param name="clear">True to clear the backing memory of the buffer</param>
        /// <returns>A buffer of values of type <typeparamref name="T"/>.</returns>
        internal abstract IBuffer<T> Allocate<T>(int length, bool clear)
            where T : struct;

        /// <summary>
        /// Allocates an <see cref="IManagedByteBuffer"/>
        /// </summary>
        /// <param name="length">The requested buffer length</param>
        /// <param name="clear">A value indicating whether to clean the buffer</param>
        /// <returns>The <see cref="IManagedByteBuffer"/></returns>
        internal abstract IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear);

        /// <summary>
        /// Temporal workaround. A method providing a "Buffer" based on a generic array without the 'Unsafe.As()' hackery.
        /// Should be replaced with 'Allocate()' as soon as SixLabors.Shapes has Span-based API-s!
        /// </summary>
        internal BasicArrayBuffer<T> AllocateFake<T>(int length, bool dummy = false)
            where T : struct
        {
            return new BasicArrayBuffer<T>(new T[length]);
        }

        /// <summary>
        /// Releases all retained resources not being in use.
        /// Eg: by resetting array pools and letting GC to free the arrays.
        /// </summary>
        public virtual void ReleaseRetainedResources()
        {
        }
    }
}


text:
ContactManager.module("HeaderApp", function(Header, ContactManager, Backbone, Marionette, $, _){
  var API = {
    listHeader: function(){
      Header.List.Controller.listHeader();
    }
  };

  ContactManager.commands.setHandler("set:active:header", function(name){
    ContactManager.HeaderApp.List.Controller.setActiveHeader(name);
  });

  Header.on("start", function(){
    API.listHeader();
  });
});


text:
// the server key and cert for the OSDP TLS server

#define OSDP_LCL_CA_KEYS        "/opt/osdp-conformance/etc/ca_keys.pem"
#define OSDP_LCL_SERVER_CERT    "/opt/osdp-conformance/etc/cert.pem"
#define OSDP_LCL_SERVER_KEY     "/opt/osdp-conformance/etc/key.pem"
#define OSDP_LCL_COMMAND_PATH   "/opt/osdp-conformance/run/%s/open_osdp_command.json"
#define OSDP_LCL_SERVER_RESULTS "/opt/osdp-conformance/run/%s"
#define OSDP_LCL_UNIX_SOCKET    "open-osdp-control"

#define OSDP_LCL_DEFAULT_PSK    "speakFriend&3ntr"

#define OSPD_LCL_SET_PID_TEMPLATE \
  "sudo -n /opt/osdp-conformance/bin/set-pid %s %d"


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
/*
 * This code was generated by https://github.com/googleapis/google-api-java-client-services/
 * Modify at your own risk.
 */

package com.google.api.services.healthcare.v1beta1.model;

/**
 * Documentation of a user's consent.
 *
 *
 This is the Java data model class that specifies how to parse/serialize into the JSON that is
 * transmitted over HTTP when working with the Cloud Healthcare API. For a detailed explanation see:
 * https://developers.google.com/api-client-library/java/google-http-java-client/json
 *

 *
 * @author Google, Inc.
 */
@SuppressWarnings("javadoc")
public final class ConsentArtifact extends com.google.api.client.json.GenericJson {

  /**
   * Optional. Screenshots, PDFs, or other binary information documenting the user's consent.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.util.List<Image> consentContentScreenshots;

  /**
   * Optional. An string indicating the version of the consent information shown to the user.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String consentContentVersion;

  /**
   * Optional. A signature from a guardian.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private Signature guardianSignature;

  /**
   * Optional. Metadata associated with the Consent artifact. For example, the consent locale or
   * user agent version.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.util.Map<String, java.lang.String> metadata;

  /**
   * Resource name of the Consent artifact, of the form `projects/{project_id}/locations/{location_i
   * d}/datasets/{dataset_id}/consentStores/{consent_store_id}/consentArtifacts/{consent_artifact_id
   * }`. Cannot be changed after creation.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String name;

  /**
   * Required. User's UUID provided by the client.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String userId;

  /**
   * Optional. User's signature.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private Signature userSignature;

  /**
   * Optional. A signature from a witness.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private Signature witnessSignature;

  /**
   * Optional. Screenshots, PDFs, or other binary information documenting the user's consent.
   * @return value or {@code null} for none
   */
  public java.util.List<Image> getConsentContentScreenshots() {
    return consentContentScreenshots;
  }

  /**
   * Optional. Screenshots, PDFs, or other binary information documenting the user's consent.
   * @param consentContentScreenshots consentContentScreenshots or {@code null} for none
   */
  public ConsentArtifact setConsentContentScreenshots(java.util.List<Image> consentContentScreenshots) {
    this.consentContentScreenshots = consentContentScreenshots;
    return this;
  }

  /**
   * Optional. An string indicating the version of the consent information shown to the user.
   * @return value or {@code null} for none
   */
  public java.lang.String getConsentContentVersion() {
    return consentContentVersion;
  }

  /**
   * Optional. An string indicating the version of the consent information shown to the user.
   * @param consentContentVersion consentContentVersion or {@code null} for none
   */
  public ConsentArtifact setConsentContentVersion(java.lang.String consentContentVersion) {
    this.consentContentVersion = consentContentVersion;
    return this;
  }

  /**
   * Optional. A signature from a guardian.
   * @return value or {@code null} for none
   */
  public Signature getGuardianSignature() {
    return guardianSignature;
  }

  /**
   * Optional. A signature from a guardian.
   * @param guardianSignature guardianSignature or {@code null} for none
   */
  public ConsentArtifact setGuardianSignature(Signature guardianSignature) {
    this.guardianSignature = guardianSignature;
    return this;
  }

  /**
   * Optional. Metadata associated with the Consent artifact. For example, the consent locale or
   * user agent version.
   * @return value or {@code null} for none
   */
  public java.util.Map<String, java.lang.String> getMetadata() {
    return metadata;
  }

  /**
   * Optional. Metadata associated with the Consent artifact. For example, the consent locale or
   * user agent version.
   * @param metadata metadata or {@code null} for none
   */
  public ConsentArtifact setMetadata(java.util.Map<String, java.lang.String> metadata) {
    this.metadata = metadata;
    return this;
  }

  /**
   * Resource name of the Consent artifact, of the form `projects/{project_id}/locations/{location_i
   * d}/datasets/{dataset_id}/consentStores/{consent_store_id}/consentArtifacts/{consent_artifact_id
   * }`. Cannot be changed after creation.
   * @return value or {@code null} for none
   */
  public java.lang.String getName() {
    return name;
  }

  /**
   * Resource name of the Consent artifact, of the form `projects/{project_id}/locations/{location_i
   * d}/datasets/{dataset_id}/consentStores/{consent_store_id}/consentArtifacts/{consent_artifact_id
   * }`. Cannot be changed after creation.
   * @param name name or {@code null} for none
   */
  public ConsentArtifact setName(java.lang.String name) {
    this.name = name;
    return this;
  }

  /**
   * Required. User's UUID provided by the client.
   * @return value or {@code null} for none
   */
  public java.lang.String getUserId() {
    return userId;
  }

  /**
   * Required. User's UUID provided by the client.
   * @param userId userId or {@code null} for none
   */
  public ConsentArtifact setUserId(java.lang.String userId) {
    this.userId = userId;
    return this;
  }

  /**
   * Optional. User's signature.
   * @return value or {@code null} for none
   */
  public Signature getUserSignature() {
    return userSignature;
  }

  /**
   * Optional. User's signature.
   * @param userSignature userSignature or {@code null} for none
   */
  public ConsentArtifact setUserSignature(Signature userSignature) {
    this.userSignature = userSignature;
    return this;
  }

  /**
   * Optional. A signature from a witness.
   * @return value or {@code null} for none
   */
  public Signature getWitnessSignature() {
    return witnessSignature;
  }

  /**
   * Optional. A signature from a witness.
   * @param witnessSignature witnessSignature or {@code null} for none
   */
  public ConsentArtifact setWitnessSignature(Signature witnessSignature) {
    this.witnessSignature = witnessSignature;
    return this;
  }

  @Override
  public ConsentArtifact set(String fieldName, Object value) {
    return (ConsentArtifact) super.set(fieldName, value);
  }

  @Override
  public ConsentArtifact clone() {
    return (ConsentArtifact) super.clone();
  }

}


text:
package unfurl

import (
	"sync"
	"time"

	lru "github.com/hashicorp/golang-lru"
	"github.com/keybase/client/go/protocol/gregor1"
	"github.com/keybase/clockwork"
)

const defaultCacheLifetime = 10 * time.Minute
const defaultCacheSize = 1000

type cacheItem struct {
	data  interface{}
	ctime gregor1.Time
}

type unfurlCache struct {
	sync.Mutex
	cache *lru.Cache
	clock clockwork.Clock
}

func newUnfurlCache() *unfurlCache {
	cache, err := lru.New(defaultCacheSize)
	if err != nil {
		panic(err)
	}
	return &unfurlCache{
		cache: cache,
		clock: clockwork.NewRealClock(),
	}
}

func (c *unfurlCache) setClock(clock clockwork.Clock) {
	c.clock = clock
}

// get determines if the item is in the cache and newer than 10
// minutes. We don't want to cache this value indefinitely in case the page
// content changes.
func (c *unfurlCache) get(key string) (res cacheItem, ok bool) {
	c.Lock()
	defer c.Unlock()

	item, ok := c.cache.Get(key)
	if !ok {
		return res, false
	}
	cacheItem, ok := item.(cacheItem)
	if !ok {
		return res, false
	}
	valid := c.clock.Now().Sub(cacheItem.ctime.Time()) <= defaultCacheLifetime
	if !valid {
		c.cache.Remove(key)
	}
	return cacheItem, valid
}

func (c *unfurlCache) put(key string, data interface{}) {
	c.Lock()
	defer c.Unlock()
	c.cache.Add(key, cacheItem{
		data:  data,
		ctime: gregor1.ToTime(c.clock.Now()),
	})
}


text:
package com.example.dell.learnaboutromania;

/**
 * Created by kinga on 11/27/2017.
 */

public class Questions {
    public String mQuestions[]={
            "How do you answer to the quiestion: Cum te cheama?",
            "How do you answer to the quiestion: De unde esti",
            "If it is 8:00 am, how do you great someone?",
            "How do you apologies?",
            "My train goes from ... at 10:00 pm",
            "Where do you go to buy a book?",
            "Translate this sentance: Voriti romaneste?",
            "Translate these words: newspaper, ticket, tissue",
            "I am hungry. I would like to eat ...",
            "How do you ask the price of something",
            "What did Popey eat to be stong?",
            "My mum is a nurse and my dad works in school, so he is a/an ...",
            "If you had a headacke what do you ask for?",
            "Wow, how many likes she has on her ... in Instagram",
            "what does the bear like to eat?",
            "Continue the dilogue: Salut, ce faci?"
    };

    private String mChoices[][]={
            {"Numele meu este Andrei.","Bine, multumesc.", "Nu stiu."},
            {"Imi lipsesti.","Buna seara.", "Sunt din Cluj-Napoca."},
            {"Buna ziua.","Buna seara.", "Buna dimineata."},
            {"Te rog.","Imi pare rau.", "Multumesc."},
            {"restaurant","scoala", "gara"},
            {"librarie","biserica", "banca"},
            {"Do you speak Romanian?","What is your name?", "How are you?"},
            {"creion, telefon, poza","ziar, bilet, servetel", "ziar, caiet, ceas"},
            {"paine","cutit", "sticla"},
            {"Cum te cheama?","Unde este toaleta?", "Cat costa?"},
            {"orez","spanac", "supa"},
            {"profesor","lamaie", "inghetata"},
            {"oglinga","chibrit", "calmant"},
            {"ruj","poza", "ceapa"},
            {"lapte","ciuperca", "miere"},
            {"Bine multumesc.","Numele meu este...", "Bine ai venit "},

    };

    private String mCorrectAnswer[] = {"Numele meu este Andrei.", "Sunt din Cluj-Napoca.",
            "Buna dimineata.", "Imi pare rau.", "gara","librarie", "Do you speak Romanian?",
            "ziar, bilet, servetel","paine","Cat costa?","spanac","profesor","calmant",
            "poza", "miere","Bine multumesc."};

    public String getQuestion(int a){
        String question= mQuestions[a];
        return question;
    }

    public String getChoice1(int a){
        String choice = mChoices[a][0];
        return choice;
    }

    public String getChoice2(int a){
        String choice = mChoices[a][1];
        return choice;
    }

    public String getChoice3(int a){
        String choice = mChoices[a][2];
        return choice;
    }

    public String getCorrectAnswer(int a){
        String answer= mCorrectAnswer[a];
        return answer;
    }

}


text:
package main

import (
	"fmt"
	"math/rand"
	"runtime"
	"time"
)

func from(connection chan int) {
	connection <- rand.Intn(100)
}

func to(connection chan int) {
	i := <-connection
	fmt.Printf("got %d\n", i)
}

func main() {
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)
	connection := make(chan int)
	go from(connection)
	go to(connection)
	time.Sleep(3000 * time.Millisecond)
}


text:
<!DOCTYPE html>

<link href="resources/flexbox.css" rel="stylesheet">


text:
In this tutorial, we demonstrate how combining (explicit) kernel methods with
linear models can drastically increase the latters' quality of predictions
without significantly increasing training and inference times. Unlike dual
kernel methods, explicit (primal) kernel methods scale well with the size of the
training dataset both in terms of training/inference times and in terms of
memory requirements.

Currently, explicit kernel mappings are supported for dense features. Support
for sparse features is in the works.

We will use [tf.contrib.learn](https://www.tensorflow.org/code/tensorflow/contrib/learn/python/learn) (TensorFlow's high-level Machine Learning API) Estimators for our ML models. The
tf.contrib.learn API reduces the boilerplate code one needs to write for
configuring, training and evaluating models and will let us focus on the core
ideas. If you are not familiar with this API, [tf.contrib.learn Quickstart](https://www.tensorflow.org/get_started/tflearn) is a good place to start. We
will use MNIST, a widely-used dataset containing images of handwritten digits
(between 0 and 9). The tutorial consists of the following steps:

* Load and prepare MNIST data for classification.
* Construct a simple linear model, train it and evaluate it on the eval data.
* Replace the linear model with a kernelized linear model, re-train and
re-evaluate.

## Load and prepare MNIST data for classification
The first step is to prepare the data to be fed to the ML models. The following
utility command from tf.contrib.learn loads the MNIST dataset:

```python
data = tf.contrib.learn.datasets.mnist.load_mnist()
```
This loads the entire MNIST dataset (containing 70K samples) and splits it into
train, validation and test data with 55K, 5K and 10K samples respectively. Each
split contains one numpy array for images (with shape [sample_size, 784]) and
one for labels (with shape [sample_size, 1]). In this tutorial, we only use the
train and validation splits (to train and evaluate our models respectively).

In order to feed data to a tf.contrib.learn Estimator, it is helpful to convert
it to Tensors. For this, we will use an `input function` which adds Ops to the
TensorFlow graph that, when executed, create mini-batches of Tensors to be used
downstream. For more background on input functions, check
[Building Input Functions with tf.contrib.learn](https://www.tensorflow.org/get_started/input_fn).
In this example, we will use the `tf.train.shuffle_batch` Op which, besides
converting numpy arrays to Tensors, allows us to specify the batch_size and
whether to randomize the input every time the input_fn Ops are executed
(randomization typically expedites convergence during training). The full code
for loading and preparing the data is shown in the snippet below. In this
example, we use mini-batches of size 256 for training and the entire sample (5K
entries) for evaluation. Feel free to experiment with different batch sizes.

```python
import numpy as np
import tensorflow as tf

def get_input_fn(dataset_split, batch_size, capacity=10000, min_after_dequeue=3000):

  def _input_fn():
    images_batch, labels_batch = tf.train.shuffle_batch(
        tensors=[dataset_split.images, dataset_split.labels.astype(np.int32)],
        batch_size=batch_size,
        capacity=capacity,
        min_after_dequeue=min_after_dequeue,
        enqueue_many=True,
        num_threads=4)
    features_map = {'images': images_batch}
    return features_map, labels_batch

  return _input_fn

data = tf.contrib.learn.datasets.mnist.load_mnist()

train_input_fn = get_input_fn(data.train, batch_size=256)
eval_input_fn = get_input_fn(data.validation, batch_size=5000)

```

## Training a simple linear model
We can now train a linear model over the MNIST dataset. We will use the
[tf.contrib.learn.LinearClassifier](https://www.tensorflow.org/code/tensorflow/contrib/learn/python/learn/estimators/linear.py) estimator with 10 classes (representing the 10 digits).
The input features form a 784-dimensional (dense) vector which can be specified
as follows:

```python
image_column = tf.contrib.layers.real_valued_column('images', dimension=784)
```

The full code for constructing, training and evaluating a LinearClassifier
estimator is shown below.

```python
import time

# Specify the feature(s) to be used by the estimator.
image_column = tf.contrib.layers.real_valued_column('images', dimension=784)
estimator = tf.contrib.learn.LinearClassifier(feature_columns=[image_column], n_classes=10)

# Train.
start = time.time()
estimator.fit(input_fn=train_input_fn, steps=2000)
end = time.time()
print('Elapsed time: {} seconds'.format(end - start))

# Evaluate and report metrics.
eval_metrics = estimator.evaluate(input_fn=eval_input_fn, steps=1)
print(eval_metrics)
```
On eval data, the loss (i.e., the value of the objective function being
minimized during training) lies between **0.25** and **0.30** (depending on the
parameters used) while the accuracy of the classifier is approximately **92.5%**
(training is randomized so the exact loss and accuracy will vary). Also, the
training time is around 25 seconds (this will also vary based on the machine you
run the code on).

In addition to experimenting with the (training) batch size and the number of
training steps, there are a couple other parameters that can be tuned as well.
For instance, you can change the optimization method used to minimize the loss
by explicitly selecting another optimizer from the collection of
[available optimizers](https://www.tensorflow.org/code/tensorflow/python/training).
As an example, the following code constructs a LinearClassifier estimator that
uses the Follow-The-Regularized-Leader (FTRL) optimization strategy with a
specific learning rate and L2-regularization.

```python
optimizer = tf.train.FtrlOptimizer(learning_rate=5.0, l2_regularization_strength=1.0)
estimator = tf.contrib.learn.LinearClassifier(
    feature_columns=[image_column], n_classes=10, optimizer=optimizer)
```

Regardless of the values of the parameters, the max accuracy a linear model can
achieve on this dataset caps at around **93%**.

## Using explicit kernel mappings with the linear model.
The relatively high error (~7%) of the linear model over MNIST indicates that
the input data is not linearly separable. We will use explicit kernel mappings
to reduce the classification error.

**Intuition:** The high-level idea is to use a non-linear map to transform the
input space to another feature space (of possibly higher dimension) where the
(transformed) features are (almost) linearly separable and then apply a linear
model on the mapped features. This is shown in the following figure:

![image](./kernel_mapping.png)

**Technical details overview:** In this example we will use **Random Fourier
Features** (introduced in the
["Random Features for Large-Scale Kernel Machines"](https://people.eecs.berkeley.edu/~brecht/papers/07.rah.rec.nips.pdf) paper by
Rahimi and Recht) to map the input data. Random Fourier Features map a vector
\\(\mathbf{x} \in \mathbb{R}^d\\) to \\(\mathbf{x'} \in \mathbb{R}^D\\) via the
following mapping:

$$
RFFM(\cdot): \mathbb{R}^d \to \mathbb{R}^D, \quad
RFFM(\mathbf{x}) =  \cos(\mathbf{\Omega} \cdot \mathbf{x}+ \mathbf{b})
$$

where \\(\mathbf{\Omega} \in \mathbb{R}^{D \times d}\\),
\\(\mathbf{x} \in \mathbb{R}^d,\\) \\(\mathbf{b} \in \mathbb{R}^D\\) and the
cosine is applied element-wise.

In this example, the entries of \\(\mathbf{\Omega}\\) and \\(\mathbf{b}\\) are
sampled from distributions such that the mapping satisfies the following
property:

$$
RFFM(\mathbf{x})^T \cdot RFFM(\mathbf{y}) \approx
e^{-\frac{\|\mathbf{x} - \mathbf{y}\|^2}{2 \sigma^2}}
$$

The right-hand-side quantity of the expression above is known as the RBF (or
Gaussian) kernel function. This function is one of the most-widely used kernel
functions in Machine Learning and measures (implicitly) similarity in a
different (much higher dimensional) space than the original one. See
[Radial basis function kernel](https://en.wikipedia.org/wiki/Radial_basis_function_kernel)
for more details.

**Kernel Classifier:** `tf.contrib.kernel_methods.KernelLinearClassifier` is a
pre-packaged `tf.contrib.learn` estimator that combines the power of explicit
kernel mappings with linear models. Its API is very similar to that of the
LinearClassifier with the additional ability to specify a list of explicit
kernel mappings to be applied to each feature used by the classifier. The
following code snippet demonstrates how to replace LinearClassifier with
KernelLinearClassifier.

```python
# Specify the feature(s) to be used by the estimator. This is identical to the
# code used for the LinearClassifier.
image_column = tf.contrib.layers.real_valued_column('images', dimension=784)
optimizer = tf.train.FtrlOptimizer(
   learning_rate=50.0, l2_regularization_strength=0.001)

kernel_mapper = tf.contrib.kernel_methods.RandomFourierFeatureMapper(
  input_dim=784, output_dim=2000, stddev=5.0, name='rffm')
kernel_mappers = {image_column: [kernel_mapper]}
estimator = tf.contrib.kernel_methods.KernelLinearClassifier(
   n_classes=10, optimizer=optimizer, kernel_mappers=kernel_mappers)

# Train.
start = time.time()
estimator.fit(input_fn=train_input_fn, steps=2000)
end = time.time()
print('Elapsed time: {} seconds'.format(end - start))

# Evaluate and report metrics.
eval_metrics = estimator.evaluate(input_fn=eval_input_fn, steps=1)
print(eval_metrics)
```
The only additional parameter passed to `KernelLinearClassifier` is a dictionary
from feature_columns to a list of kernel mappings to be applied to the
corresponding feature column. In this example, the lines

```python
kernel_mapper = tf.contrib.kernel_methods.RandomFourierFeatureMapper(
  input_dim=784, output_dim=2000, stddev=5.0, name='rffm')
kernel_mappers = {image_column: [kernel_mapper]}
estimator = tf.contrib.kernel_methods.KernelLinearClassifier(
   n_classes=10, optimizer=optimizer, kernel_mappers=kernel_mappers)
```
instruct the classifier to first map the initial 784-dimensional images to
2000-dimensional vectors using random Fourier features and then learn a linear
model on the transformed vectors. Note that, besides the output dimension, there
is one more parameter (stddev) involved. This parameter is the standard
deviation (\\(\sigma\\)) of the approximated RBF kernel and controls the
similarity measure used in classification. This parameter is typically
determined via hyperparameter tuning.

Running the code above yields a loss of approximately **0.10** while the
accuracy is increased to approximately **97%** on eval data (an increase of 4%
over the plain linear model). The training time hovers around 35 seconds. We can
increase the accuracy even more, by increasing the output dimension of the
mapping and tuning the standard deviation even more.

**On the role of stddev:** The classification quality is very sensitive to the
value of the stddev parameter used to define the similarity measure between the
pairs of input features. The following table shows the accuracy of the
classifier on the eval data for different values of stddev (for all experiments
the output dimension was fixed to 3000). The optimal value is stddev=5.0. Notice
how too small or too high stddev values can dramatically decrease the accuracy
of the classification.

stddev | eval accuracy
:----- | :------------
1.0    | 0.1362
2.0    | 0.4764
4.0    | 0.9654
5.0    | 0.9766
8.0    | 0.9714
16.0   | 0.8878

**On the role of the output dimension:** Intuitively, the larger the output
dimension of the mapping, the closer the inner product of two mapped vectors
approximates the kernel which typically translates to better classification
accuracy. Another way to think about this is that the output dimension equals
the number of weights of the linear model (the larger this dimension, the larger
the "degrees of freedom" of the model). However, after a certain threshold,
higher output dimensions increase the accuracy by very little (while still
increasing the training time). This is shown in the following 2 Figures which
depict the eval accuracy as a function of the output dimension and the training
time respectively.

![image](./acc_vs_outdim.png)  ![image](./acc-vs-trn_time.png)

## Explicit kernel mappings: summary and practical tips
* Explicit kernel mappings combine the predictive power of non-linear models
with the scalability of linear models.
* Unlike traditional dual kernel methods, they can scale to millions or hundreds
of millions of samples.
* Random Fourier Features can be particularly effective for datasets with dense
features.
* The parameters of the kernel mapping are often data-dependent. Model quality
can be very sensitive to these parameters. Use hyperparameter tuning to find the
optimal values.
* If you have multiple numerical features, concatinate them into a single
multi-dimensional feature and apply the kernel mapping to the concatenated
vector.