MySQL Create Or Update based on a Table definition

Question: I am trying to take a mysql table schema and then run it against other tables and either update or create. So for instance lets say I have table def like so
CREATE TABLE `mobileKeys` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `key` varchar(40) NOT NULL DEFAULT '',
  `expiration` datetime NOT NULL,
  `generated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

Now on another database I want the exact same schema table. In some cases the table might not exist at all (so I need to add it), in other cases it might have the table but might be missing a key, OR it could have everything but the values might be different (varchar length changed for instance).
I have about ~1000 DBs that need these updates and have data in the DB and maybe the tables already. I am trying to think of a way to do this, any info would be great.
Comment: If you run `CREATE TABLE IF NOT EXISTS` you'll accomplish table creation. Update is tough if you don't know exactly what to do with the table - I'm not sure how you can use `ALTER TABLE` to add columns if you don't know upfront what columns you need to add. Same applies to updating columns.
Answer: You will probably need a programming language to assist you with this process, but it may be a way to use this query
SELECT column_name
FROM information_schema.columns
WHERE table_schema='[your database name]'
    AND table_name='[your table name]'
    AND column_name NOT IN ('column A', 'column B')

With this you can get the missing columns and can create a "customized alter table".
And after fix the table, You can just sync cross databases
INSERT INTO databaseX.table_A(column1, column2, column3, ...)
  SELECT column1, column2, column3, ...
  FROM databaseY.table_A

Answer: You may want to look into querying the information_schema to see if tables or columns exist. The information_schema is easily query-able and has all of the information about your db schemas.
You can check if a table exists like this:
SELECT *
  FROM information_schema.tables
 WHERE table_name = 'your_table'
   AND table_schema = 'your_db';

You can get a list of all columns as well as their datatypes from the columns table:
SELECT *
  FROM information_schema.columns
 WHERE table_name = 'your_table'
   AND table_schema = 'your_db'

You can even find how much data is in each table in a database using the information_schema:
     SELECT CONCAT(table_schema, '.', table_name)                                          AS table_name,
            CONCAT(ROUND(table_rows / 1000000, 2), 'M')                                    AS rows_readable,
            CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G')                    AS total_data,
            CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G')                   AS idx,
            CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') AS total_size,
            ROUND(index_length / data_length, 2)                                           AS idxfrac
       FROM information_schema.tables
      WHERE table_schema = 'your_db'
-- order by data_length desc
-- order by table_rows desc
   ORDER BY data_length + index_length DESC;


How do I dynamically add a set of columns based on an input parameter?

Question: I need to add or remove roughly 20 columns based on variouse input parameters. I know I can wrap each line in the CASE statement, but when I need to add 10 columns for column set 10 that's a ton of extra code.  Is the answer to make a dynamic SQL monster?
Here's an example of what I'm trying to do - return 5 specific columns when the end user needs Column Set 1:
DECLARE @AddColumns AS NVARCHAR(20) = 'Column Set 1'

SELECT
    col1
    , col2
    , CASE WHEN @AddColumns = 'Column Set 1' OR @AddColumns = 'Include All Columns'
        THEN col3, col4, col5, col6, col7
    END
    , CASE WHEN @AddColumns = 'Column Set 2' OR @AddColumns = 'Include All Columns'
        THEN col8, col9, col10
    END
    , col11

FROM MyTable

Comment: Well if that's the case your two options are 1) a big IF statements that executes different queries based on input, or 2) calls to sp_executeSQL. The big downside with both these approaches is that the tooling around queries with dynamic columns isn't that great.
Comment: Any particular reason you don't just want to select all the columns and then deal with it on the client?
Comment: @Conrad, I'm consuming the dataset in VB, so that's a good idea, but the if..thens to remove the rows from the dataset get's messy because I have such long lists of column names to remove.
Comment: Thanks. I ended up going with a dynamically built SQL string (CDATA) in VB. So I have a SELECT block, IF logic to add column set 1, another IF for CS2, etc. and then the WHERE clause. That seems to be the easiest way to do it in this case.  I'll add notes to the original question.
Answer: Try this Dynamic SQL
DECLARE @AddColumns AS NVARCHAR(20) = 'Column Set 1'
DECLARE @SQL VARCHAR(5000)

SET @SQL = 'SELECT   col1, col2 '
IF @AddColumns = 'Column Set 1' OR @AddColumns = 'Include All Columns'
    SET @SQL = @SQL + ', col3, col4, col5, col6, col7 '
IF @AddColumns = 'Column Set 2' OR @AddColumns = 'Include All Columns'
    SET @SQL = @SQL + ', col8, col9, col10 '
SET @SQL = @SQL + ',col11 FROM MyTable'

EXEC (@SQL)

Comment: calling [sp_executesql](http://msdn.microsoft.com/en-us/library/ms188001.aspx) would be preferred, in the likely event that there's going to parameters in the where clause to avoid quoting, escaping and SQL injection headaches
Answer: After weighing the options of dynamic SQL, ExecuteSql, building the SQL string in VB, or editing the resulting VB dataset I went with dynamically building the SQL string in VB. I was looking for an 'easy' way to do this in SQL. Although it is possible to do in SQL, it is 'cleaner' for me to do it in VB. (Sorry I should have mentioned the query is being used by VB in the original question.)  Imagine many column sets with each set adding roughly 5-10 columns, multiple joins, parameterized filters, etc.  What I do here if build various SQL query parts and them append them together based on the input parameters.
' Build the various chunks of SQL as CDATA strings
Dim selectPrimaryColumns As String =
<![CDATA[
    /*
        My Report
    */
    USE DatabaseName

    SELECT col1
        , col2
]]>.Value

selectColumnSetA
<![CDATA[
        -- Column Set A
        , col3
        , col4
]]>.Value

selectColumnSetB
<![CDATA[
        -- Column Set B
        , col5
        , col6
]]>.Value

Dim selectFilters As String =
<![CDATA[
    FROM MyTable t
      -- with many joins

    WHERE t.Date BETWEEN @StartDate AND DATEADD(DAY, 1, @EndDAte)
      -- and many more parameterized filters.

]]>.Value

' Set the first part of the query
Dim fullQuery As String = selectPrimaryColumns

' Conditionally add the various column sets
SELECT CASE myParameter
 CASE "ColumnSetA"
    fullQuery = fullQuery & selectColumnSetA
 CASE "ColumnSetB"
    fullQuery = fullQuery & selectColumnSetB

' Add the last part of the query
fullQuery = fullQuery & selectFilters

I now have a SQL query string that I can pass to the SQL Command object.


Can I delete a file after it is read, or notify when all are processed?

Question: I have a list of files being read into Elasticsearch through Logstash. I wanted to know how to know when the files were all caught up.
I was thinking of deleting the file after it is fully read in.
I havent seen anything though in regards to notification or ack of file completion, or file deletion.
Id love some insight, as i figure it would be a part of the config file.  I just have no insight.
Ideally:  I would love delete the file after it is fully consumed.  That way, i can work my way through all of the file types, starting with txt files.
Answer: Using the file input in logstash you can do that, you need to change two config options, mode and file_completed_action.
You need to change the mode option to read, the default is tail, and add the file_completed_action with the value of delete.
file {
    mode => "read"
    path => "/path/to/your/files/*.log"
    file_completed_action => "delete"
}

Comment: Ah, ok.   Yeah, I was using tail originally which would read through the whole doc and (I think) then monitor the file for changes at the end.  So, This actually looks like it would make a lot of sense.


Get file depth in directory tree

Question: I'm using command find to recursively browse through directory tree, counting files, sizes, etc...
Now I need to get directory depth of each file.
Is there any portable way for both FreeBSD and CentOS?
I know that find is able to prinf actual directory depth but sadly this works only on CentOS, not FreeBSD.
Additionaly - I need to keep standard find output OR put directory depth on the beginning of output and cut it from there.
Answer: You can count the / in path :
$ find . -type f -exec bash -c 'echo '{}' | grep -o / | wc -l' \;

Or with file names :
$ mkdir -p one/two/three four/five && touch file one/two/file one/two/three/file
$ find . -type f -exec bash -c 'echo -n '{}' :; echo '{}' | grep -o / | wc -l' \;
./file :1
./one/two/file :3
./one/two/three/file :4

Answer: Try this:
find . -type d -exec bash -c 'echo $(tr -cd / <<< "$1"|wc -c):$1' -- {} \;  | sort -n | tail -n 1 | awk -F: '{print $1, $2}'

Get file depth in directory tree


React Native: How to make a push notification with custom content?

Question: I'm using OneSignal in my React Native project, and want to create something similar to this image, where there's a timer ticking down, and buttons that trigger events in the app itself:

I have no idea how to approach customizing the UI like this, and haven't had any luck looking for guides or tutorials. Can anyone point me in the right direction?
Answer: if you are talking about adding buttons inside your push notifications, you need to add Action Buttons with your notification. Please check this page for complete details


Index by more than one parameter?

Question: I'm creating an index for my user nodes called 'users', where the key is 'email' (with the users email as value).
So far so good - I can easily get users by email.
My user nodes also hold the user name (user_name) as a property and I am trying to query the db in way so I can tell if 'email' or 'user_name' are taken.
Of course, they can be used in different nodes.
I'm looking for a cypher query or a way to "index by more than one parameter" so I can tell if email or user_name were already used.
Thanks!
I'm talking about neo4j by the way, calling it from node.js using node-neo4j.
Answer: You can configure the auto-index to index both email and user_name and then run a query like this:
start n=node:node_auto_index("email:firstname.lastname@example.com user_name:"John Doe"')
return n

Answer: If we talk about Lucene, you could add to index documents, that contains two fields - user name and email. Your code could be something like this (very raw example in Java, I think Node.js, should have similar syntax, when workis with Lucene)
Document doc = new Document();
doc.add(new Field("userName", userName), Field.Store.YES,  Field.Index.ANALYZED);
doc.add(new Field("email", email, Field.Store.YES,  Field.Index.ANALYZED));

And after that, search easily for this two fields.
As reference - take a look at Lucene In Action (http://www.manning.com/hatcher3/) or similar books over Internet.
Comment: Hi, Actually I'm using neo4j for that so the answer I'm looking is more specific to it.

Index by more than one parameter?


Object deep locking in multithreading environment

Question: I have below class structures in my project for class A and class B:
class A {
    private String id;
    private List bs = new ArrayList();

    A(String id){
        this.id = id;
    }

    public List getBs(){
        return bs;
    }

    public void setBs(List bs){
        this.bs=bs;
    }

    public void addBs(List bs){
        this.bs.addAll(bs);
    }

    public void addB(B b){
        this.bs.add(b);
    }
}

class B {
    private String id;
    private List<String> tags;

    B(String id){
        this.id = id;
    }

    public List<String> getTags(){
        return tags;
    }

    public void setTags(List<String> tags){
        this.tags = tags;
    }

    public void addTags(List<String> tags){
        this.tags.addAll(tags);
    }

    public void addTag(String tag){
        this.tags.add(tag);
    }
}

Also we have a cache class:
class CacheService {
    private static final ConcurrentHashMap<String, Object> CACHE = new ConcurrentHashMap<String, Object>();

    public static Object get(String id){
        return CACHE.get(id);
    }

    public static void put(String id, Object obj){
        return CACHE.put(id, obj);
    }
}

Now objects for class A and B are created using unique IDs and put in this cache by <id, object> combination. For example:
A a1 = new A("10");
CacheService.put("10", a1);

A a2 = new A("11");
CacheService.put("11", a2);

B b1 = new B("1");
CacheService.put("1", b1);

B b2 = new B("2");
CacheService.put("2", b2);

B b3 = new B("3");
CacheService.put("3", b3);

B b4 = new B("4");
CacheService.put("4", b4);

Also I am putting class B objects in the List inside objects a1 and a2. It is important to note that a unique B object is only put once in any A object:
a1.add(b1);
a1.add(b2);

a2.add(b3);
a2.add(b4);

This way we can have multiple objects for class A and B in the CACHE.
Scenario: Now multiple threads access this CACHE but some of them end up to get class A objects and others class B objects depending upon ID specified by user. These threads actually want to read or update information on these objects.
Question: My requirement is when a thread has accessed an object of class A (for example a1) to update it then no other thread should be able to read or update a1 as well as all class B objects (b1 and b2 in this case) which are added to the List inside object a1 until I am finished with all updates on a1. Please tell me how can I acquire a lock in this scenario?
Comment: Question is updated. 1. `CacheService` class implementation is updated to only support get() and put(). 2. A unique B object can only be put once in any A object.
Answer: To have deep copying I suggest you to have a Lock in each instance of A and B and have them both implement some interface with lock() and unlock() methods, where class A will acquire its own lock, and all locks of B's. Then, lock an object before using it, and unlock after you're done.
EDIT: So your course of action would be:

create an interface, let's call it Lockable with two methods: lock() and unlock()
have both A and B implement that interface. Accordingly, your cache will now operate with Lockable instead of Object
add a private field to both A and B
private final Lock lock = new ReentrantLock();
now implementation of Lockable in B will be just to call same methods on the lock
in A, lock() will acquire local instance of lock, and iterate the list of b's and call their lock() method as well. Same for unlock()
now, every time you get an object from your cache, before doing anything with it, you call lock() on that object and then unlock() when you're done.
Comment: @sohanlal: what exactly do you have problems with?
Comment: How can I write a such type of LOCK which locks `a1` as well as all B object which are in `List` inside `a1`?
Comment: But when I get a lock on object `a1` and before take a lock on `b1` or `b2`, some other thread have a window to modify `b1` or `b2`. I want that as soon as I get a lock on `a1`, `b1` and `b2` are also locked.
Comment: @sohanlal: if another thread has a lock on b1 or b2, and you try to get a lock on a1, your thread will try to acquire same locks and thus wait for them to be released.
Comment: Thanks for the answer :) I think it solves my problem but again I have to be suspicious to look for any inconsistencies.
Answer: The synchronized keyword will help you with this.  You can either declare an entire method as synchronized or you can have synchronized() blocks, which lock on a specific key object which you define.  When a synchronized() block is entered, no other blocks which lock with that same key object can be accessed by another thread until the block is exited.
See the Java tutorial on synchronization here.
For your example, you could do either of the following:
public synchronized void addB(B b) {
    this.bs.add(b);
}

OR
... declare a lock object in your class ...
private final Object LOCK = new Object();

... and use it a synchronized() block:
public void addB(B b) {
    synchronized(LOCK) {
        this.bs.add(b);
    }
}

The advantages to using the second over the first is that you can completely, explicitly control which sections of code are locked (rather than just the entire method call).  When dealing with concurrency, you want to synchronize as little as possible for efficiency purposes, so using this you can perform synchronization on only the bare minimum.
Also, I'm sure someone will point out that you don't need to explicitly declare another lock object, because you are able to synchronize on the current object using the this keyword.  However, this other StackOverflow question and answer sum up the reasons I would not recommend doing so nicely.
Comment: Then it sounds like you simply need to move your level of synchronization higher (i.e., to encompass adding `b1` as well as `b2`).  Basically, just synchronize whatever parts of the program you want to happen atomically, without interference from other threads.  Note, though, it sounds like threading isn't actually a very useful technique then in this scenario, since it's difficult for your threads to work independently of one another.
Comment: I have tried this but the real problem is that some other thread can access `b1` or `b2` from `CACHE` (since lock is not acquired on `b1` or `b2`) and update them which I do not want. I want that no other thread can access `b1` and `b2` until I am finished with all the updates on `a1`. In other words when I take a lock on `a1` then all objects in `List` are also locked at the same time.
Answer: EDIT2: I was going to complete my answer after question was edited, but I just found the class java.util.concurrent.locks.ReentrantReadWriteLock, which I think fits exactly the needs of user sohanlal.
This class provides a pair of locks, one for reading operations that can be owned at the same time by several elements and is not-blocking by itself. The second one (the write part) avoids any read or write operation when acquired.
The solution is then as following:

Add lock to container class A. This class is the true owner of the read-write lock.
Add lock attribute to class B. This class DOES NOT own the lock, but only references it to read-lock it.
When adding a new B object to an A container, provide the B object with a reference to the ReentrantReadWriteLock on the container. When extracting a B object from A, dereference the lock "inherited" from the container.
In class A, write-lock on each operation that modifies the collection of B objects (add, removew...). Optionally (if underlying collection of B elements is not synchronized), read-lock operations that do not modify the collection but access it.
In class B, read-lock on every operation.

Code. This has not been tested, but will give an idea of how it should be implemented:
class A {
    private String id;
    private List bs = new ArrayList();
    private ReentrantReadWriteLock lk = new ReentrantReadWriteLock();

    A(String id){
        this.id = id;
    }

    public List getBs(){
        lk.readLock().lock(); // acquire the read lock, since this operation does not affect A contents
        return bs;
        lk.readLock().unlock();
    }

    public void setBs(List bs){
        lk.writeLock().lock(); // acquire the write lock, which automatically avoids further reading/writing operations
        this.bs=bs;
        for( B elem : bs )
        {
            // internal B elements need a reference to the reading part of the lock
            elem.setLock(lk.readLock());
        }
        lk.writeLock().unlock();
    }

    public void addBs(List bs){
        [...] // similar approach that in setBs
    }

    public void addB(B b){
        [...] // similar approach that in setBs
    }

    public void deleteB( B elem ) // Or whatever notation you want
    {
        lk.writeLock().lock(); // acquire the write lock
        B internalElem = bs.get(bs.indexOf(elem));
        if( internalElem != null )
        {
            bs.remove(internalElem);
            bs.unsetLock();
        }
        lk.writeLock().unlock();
    }
}

class B {
    private String id;
    private List<String> tags;
    private Lock lk;

    B(String id){
        this.id = id;
        lk = null;
    }

    public void setLock(Lock l){ lk = l; } // put additional controls if you want
    public void unsetLock()
    {
        lk = null;
    }

    private void lockInternal()
    {
        if(lk!=null){ lk.lock(); }
    }

    private void unlockInternal()
    {
        if(lk!=null){ lk.unlock(); }
    }

    public List<String> getTags(){
        List<String> ref = null;
        lockInternal();
            [...] //internal operations
        unlockInternal();
        return ref;
    }

    public void setTags(List<String> tags){
        [...] // similar approach that in getTags
    }

    public void addTags(List<String> tags){
        [...] // similar approach that in getTags
    }

    public void addTag(String tag){
        [...] // similar approach that in getTags
    }
}

ORIGINAL ANSWER:
Jeff's answer is a good starting point, since it appears to solve the problem of touching the A-type object, and modifying its composition. However, we still have a problem related with theB-type objects contained on A.
Suppose we have an A-type object with several B-type elements inside:
A a1 = new A("10");
B b1 = new B("myB1");
B b2 = new B("myB2");
B b3 = new B("myB3");
a1.add(b1);
a1.add(b2);
a1.add(b3);

The thing is that, if you are operating over a1, you want to lock any operation over b1, b2 and b3. How can you make sure of that?
The only solution I see is that all the B elements inside an A container share some common Lock variable. At the time of invoking write-related operations over a1, it acquires the lock, avoiding ANY operation over B elements. Note that no operation over a B element needs to actually acquire the lock... only to check if it has been acquired by A-type container.
A lot of drawbacks/considerations:

Performance: every operation over a B object will involve checking the lock (this can be very costly)
Operating constraints: if a single B object can be added to several A-type containers, this gets much more complicated
Implementation complexity: when you insert a B object inside a1, you have to provide B with the shared lock. When extracted, the lock must be dereferenced. What happens if container A is deleted? You have to take care of that kind of things.

EDIT: and I forgot to mention that this mechanism, when writing the A-type container, does not "stop" or take into account ongoing operations over the contained B-type elements.
Comment: Its a good idea to share some common Lock variable on B objects but how can I achieve it?

Regarding drawbacks:
1. I agree on performance part.
2. It is given that a single unique B object will never be added to several A containers. It will only be added to only one unique A object.
3. You can assume that no one can remove the object from CACHE. You can assume class `CacheService` having 'CACHE` object as private and methods to get and put ONLY in the `CACHE`.
Comment: I have changed the CacheService class implementation. Please help me now.
Comment: I liked your answer but sorry for not mentioning one small thing. Update on object `a1` can happen in multiple lines. Something like this: `A a1 = (A)CacheService.get("10"); a1.setBs(bsToSet); a1.addB(B b); a1.addBs(bsToAdd); a1.getBs().get(0).setTags(tagsToSet); a1.getBs().get(1).addTag(Strning tag);` So I think we have to now lock this block in some way to achieve locking on `a1` as well as `b1` and `b2`. So how can we edit the code to achieve this?
Comment: I can't see the problem here, so maybe I'm not understanding what you want to do there ("update on object `a1` can happen in multiple lines"... ??)... do you maybe want to lock the whole A container when you write-operate inside the contained B objects? This should not be a problem in the sense that the container A is actually not changing. Please, specify you problem a bit better.


Where does $\Lambda=P^{-1}AP$ come from?

Question: How do we derive the fact that if a matrix is diagonalizable then we can diagonalize it with the formula $\Lambda = P^{-1}AP$, where $P$ is a block matrix whose columns are the eigenvectors of $A$?
I can see that if you start with $A$ and multiply by $P=\begin{bmatrix} v_1 & v_2 & \cdots & v_k\end{bmatrix}$, then you end up with $AP = \begin{bmatrix} \lambda_1v_1 & \lambda_2v_2 & \cdots & \lambda_kv_k\end{bmatrix}$.  I don't see why the next step should be to left multiply by $P^{-1}$.
Also, even if the formula $\Lambda = P^{-1}AP$ holds, how do we know this is the only why to produce a diagonal matrix from $A$?  Perhaps $A$ is also similar to a diagonal matrix whose entries are not the eigenvalues of $A$ (but still the product of whose diagonal entries is the determinant of $A$).  How can we prove that this is not possible -- or if it is possible, why is it not ever mentioned?
Comment: It's not possible for $A$ to be similar to a diagonal matrix whose entries are not the eigenvalues because similarity transformations preserve eigenvalues (along with both their algebraic and geometric multiplicity).
Comment: It is change of variable. If $f:E\to E$ given by $y=f(x)=Ax$ and we change variables in the domain $x=Px'$ and in the target $y=Py'$ we get $Py'=APx'$, from where $y'=P^{-1}APx'$. Diagonalizable means that in some coordinates the matrix is diagonal. "Some coordinates" means that you are allowed, and only allowed, to change variables to new coordinates. One changes to new coordinates in linear algebra by applying an invertible linear transformation $x=Px'$.
Answer: Let $(e_1 \dots e_n)$ us the usual basis, and $X_k$ the $k$th column of the matrix $X$.
If $P^{-1}AP = D$ is diagonal, then taking the $k$th column of $P$ you get that:
$$
(P^{-1}AP)_k =
P^{-1}AP_k = D_k = \lambda_k e_k
\\ \implies AP_k = \lambda_k\times P e_k = \lambda_k P_k
$$
So the columns of $P$ are eigenvectors for $A$.
Comment: Interesting.  You proved it the other way around.
Answer: Here's the $2 \times 2$ case you can generalize:
Suppose $A$ is an arbitrary $2 \times 2$ matrix with two eigenvalues, $\lambda_1, \lambda_2$, and hence two corresponding eigenvectors, $v_1$ and $v_2$. Define a matrix $P$ whose columns are those two eigenvectors,
\begin{equation} P =\left( \begin{matrix} v_1 & v_2 \\ | & | \end{matrix} \right) \end{equation}
where those vertical bars indicate the column.
Then the action of $A$ on $P$ is the same as the action of $A$ on the individual columns. As those columns are also eigenvectors we may write
\begin{equation} AP =\left( \begin{matrix} Av_1 & Av_2 \\ | & | \end{matrix} \right) = \left( \begin{matrix} \lambda_1v_1 & \lambda_2v_2 \\ | & | \end{matrix} \right) =  \underbrace{\left( \begin{matrix} v_1 & v_2 \\ | & | \end{matrix} \right)}_{P} \underbrace{\left( \begin{matrix} \lambda_1 & 0 \\ 0 & \lambda_2 \end{matrix} \right)}_{\Lambda} \end{equation}
where $\Lambda$ is the diagonal matrix of eigenvalues. That is, $AP = P\Lambda$. As $P$ is invertible (why?) we may write
\begin{equation} A = P\Lambda P^{-1} \end{equation}
or
\begin{equation} \Lambda = P^{-1}A P \end{equation}

On the uniqueness of the diagonal matrix $\Lambda$, see Ian's comment above.
Answer: This has nothing to see with eigenvectors: it is the general change of basis formula for linear maps.
Let me give some details: suppose a linear map $f$ has matrix $A$ in some basis $\mathcal B$. Let $mathcal B'$ another basis; denote as $P$ the matrix with column vector $C_i$ equal to the coordinates (in basis $\mathcal B$) of the $i$-th vector of $\mathcal B'$. $P$ is called the change of basis matrix from $\mathcal B$ to $\mathcal B'$.
Now for any vector $x$ with column matrix $X$ in basis $\mathcal B$, $X'$ in $\mathcal B'$, one has:
$$X=PX'$$
Let $Y$ the column vector of $f(x)$ in basis $\mathcal B$. $Y$  and $X$  are related by $Y=AX$. Similarly if  $Y'$ is the column vector of $f(x)$ in basis $\mathcal B'$, one has $Y=PY'$.
Thus we can write:
$$Y=PY'=AX=A(PX'),\enspace\text{whence}\quad Y'=(P^{-1}AP)X$$
This proves the matrix of $f$  in basis $\mathcal B'$ is:
$$A'=P^{-1}AP.$$


HXmap_get not returning the right data with the key

Question: int main()
{
       struct HXmap *limitMap;
        limitMap = HXmap_init(HXMAPT_DEFAULT,HXMAP_CKEY);
        int a[]={1,2,3};
        HXmap_add(limitMap,a,limitMap);
        HXmap_add(limitMap,(a+1),BuyTree);
        HXmap_add(limitMap,&a[2],main);
          int price =1;
          Limit* add = (Limit*)HXmap_get(limitMap,&price);
   printf("Price: %d, Address %p LimitMap %p \n",price,add,limitMap);
        return 0;
}

The output of this piece of code is here:
Price: 1, Address (nil) LimitMap 0x8ff5008
Why isnt HXmap_get() returning the right address?
Would appreciate some help.
Answer: I don't have libHX installed, so this is just guessing based off of the documentation…
Because you did not specify that the keys are C strings, nor provided a key size, nor gave a custom HXmap_ops, it's using a direct value comparison instead of following the pointer to int.
You can either use price directly as the key (without using pointer-to-int), or specify the key size.
HXmap_init5(HXMAPT_DEFAULT, HXMAP_CKEY, NULL, sizeof(int), 0);

Comment: Now I am cursing myself. Damn! I went through the doc 4 times to check the validity of HXmap_init() function but didnt strike me strange not to use it.Thanks it worked.


How do I display a contour plot for my eigenvalue function

Question: Code for image is below:
P <- function(v){
      x <- v[1]
      y <- v[2]
      return(
      sum(eigen(matrix(c(
        5,y,x,
        y,2,1,
        x,1,x),3,3,byrow=TRUE))$values))
    }

    xgrid <- seq(from=1, to=6, len=6)
    ygrid <- seq(from=1, to=6, len=6)
    jj <- expand.grid(xgrid,ygrid)

    M <- apply(jj,1,P) # apply the function f() to jj
    M <- matrix(M,6,6)
    str(M)
    M

    contour(xgrid,ygrid,M,asp=1,xlim=c(1,6),ylim=c(1,6))
```

The above code creates the image below:
enter image description here
Why does the image show the line in the upper right not completely straight?
Thank
Answer: The function is already working.  The plot looks bad because both x and y go from -3 to 3, but you have plot limits set to run from 1 to 6.  Remove those, and you get the following:
P <- function(v){
  x <- v[1]
  y <- v[2]
  return(
    sum(eigen(matrix(c(
      5,y,x,
      y,2,1,
      x,1,x),3,3,byrow=TRUE))$values))
}

xgrid <- seq(from=-3, to=3, len=6)
ygrid <- seq(from=-3, to=3, len=6)
jj <- expand.grid(xgrid,ygrid)

M <- apply(jj,1,P) # apply the function P() to jj
M <- matrix(M, 6,6)

contour(xgrid,ygrid,M,asp=1)

Created on 2022-09-03 with reprex v2.0.2
It's not obvious from the function definition, but in fact your function doesn't depend on v[2], so the contours are vertical lines.  This is a property of the sum of eigenvalues:  it is the trace of the matrix, and y doesn't appear in the matrix diagonal.
Comment: when I change v[2] the sum of the eigenvalues stats the same. Is that why the contour plot looks funny?
Comment: Yes, that's what I said.
Comment: I fixed the margins with:
contour(xgrid,ygrid,M,asp=1,xlim=c(1,6),ylim=c(1,6)). however my contour plot shows a slight kink in 13 line. Why is that? is it not completely independent. I've updated the graph in the post to show you
Comment: You've added a plot to the question, but I don't think the code in the question produces that plot, so I can't really suggest what happened.
Comment: edited the post to have the code that creates the image. I've tried manually testing Y when x=6 and it always = 13 to 10+dp so it does not appear to be slight rounding error.
Comment: It's rounding error in the contouring.  The smallest and largest x values are exactly on contours, so sometimes rounding leads to the contour being dropped.  Use `xgrid <- ygrid <- seq(from=0.9, to=6.1, len=6)` and things will be more stable.
Comment: You can see where the points are by running `points(jj)`.


What open source and free wireframing tools are there?

Question: I am writing a tutorial on how to do wireframing: https://github.com/dwyl/learn-wireframing/issues/1 and I would like to be able to recommend an online, free and open source wireframing tool to others. However I am struggling to find one that is open source, free and online.
I have come across this SO question: https://stackoverflow.com/questions/3432665/open-source-or-free-wireframe-tools
and this article https://www.briefside.com/free-open-source-wireframe-tools/
However neither of them detail currently free and open source tools aside from Pencil which is desktop only and was last updated in 2017.
Comment: is open source a dealbreaker for you?
Comment: I'm voting to close this question as off-topic because it is seeking advice on specific tools, not Usability.
Comment: I've already got some non open source options (see the github link), but I'd prefer open source which is why I wanted to check if any existed...
Comment: Asking for tool recommendations is off topic: https://ux.meta.stackexchange.com/questions/1286/update-faq-to-state-that-tool-recommendation-questions-are-out-of-scope
Answer: MyDraft
- https://mydraft.cc/
- https://github.com/mydraft-cc/ui
- Can be used online, written in typescript, react and redux, uses font awesome library for icons.
I found it by searching on github for wireframe tool repos and then looking at those with lots of stars.
Answer: I use Draw.io a lot. I use Balsamiq or Omnigraffle that are paid as well, and have some premade templates on Sketch, but if I don't have to share wireframes with clients that use those programs, or if they're private projects, I tend to use Draw.io, it's way faster for me, and for a free program, incredibly resourceful


C - Comparing floats with if statements

Question: I am currently writing a greedy algorithm, but I have stumbled upon a problem when comparing floats.
I would use code like this:
float f = 0;

if (f == 0) {
   // code
}

I have tested this on a seperate program and it worked fine, but not on the program I am working on.
Here is an extract from my own program.
float chf2 = fmod(chf, 0.1);
float ch3 = chf - chf2;

if (chf2 == 0) {

    /* Divide user's number by 0.1 */

    float ch3 = chf / 0.1;

    /* Round the number */

    int ch4 = round(ch3);

    /* Print the amount of coins and end */

    printf("%d\n", ch4 + coin2);
    return 0;
}

Oddly, this seems to work with a previous if statement that checks when a fmod of 0.25 from the user's input.
Is there a better way of checking if a float is equal to another float?
Comment: You cannot compare floating point numbers for equality because they can easily be rounded by some arithmetic operations, because they cannot have infinite precision. You can set a positive reasonably small tolerance and then something like `if (fabs(x - y) < tolerance))`.
Comment: @iharob: Yes, you certainly *can* compare floating-point numbers for equality. It's just tricky. It can be done reliably *if* the values are restricted to those that are exactly representable. If you're dealing with money in dollars and cents, though, you're very likely to get values that *can't* be represented exactly -- and neither the compiler nor the run-time system is likely to warn you if you stray from the set of "safe" floating-point numbers into "unsafe" floating-point numbers.
Comment: Your program's comment `Print the amount of coins` tips me off that this is about money, and `float` or `double` is *not* the way to go about it. Use integer values for cents/pennies, as numerous previous **SO** answers have advised.
Comment: @Weather Vane Do not agree with advice about coding money.  The solution is to understand and deal with the issues should code uses `double`, `int` or `decima128`.  Successful and poor financially code can be written with binary FP, decimal FP or integers.  It is not the underlying type that is the problem.  OTOH, do agree not to use `float` as its precision/range is far too often too small.
Comment: @Weather Vane Agree that if the extent of "money" is counting/adding/subtracting coins/bills, `int` is fine.  I deal with interest rates, cost per product in fractions of a cents and internationalizations issues - `int` is insufficient.
Comment: To see the significant digits of some `float` like `ch3`, use `#include   printf("%.*e\n", FLT_DECIMAL_DIG -1, ch3);`.  This will aid in your debugging.
Comment: @chux *"to be or not to be"* perhaps the question has the wrong title. I stand by my comment: problems involving coins and change are invariably best solved with `int` type. For example, if answering *"how do I cut a pea properly with an axe?"* you could learn a lot about an axe but the correct answer is not to use an axe.
Comment: @WeatherVane I have to use floats if I want to use numbers like 0.65 unless I multiply the number by 100. That could work I suppose.
Comment: @Argarak 0.65 squalids in `float` is 65 sequins in `int`. I suggest you research previously similar questions.
Comment: @chux in your world I guess you would probably be using `>=` and `<=` and not trying for an equality.
Comment: This question isn't a duplicate of the one people are marking it as a dupe of.  "Why is floating-point math broken" is a closer dupe, but what's going on here is actually a tiny bit subtle.  `fmod` always returns an exact result, so you really do get the remainder after division by the `double` you get when you write `0.1`.  The trick is that you don't get 1/10 when you write `0.1`.
Answer: Your code works correctly.  Your expectations might need a little bit of adjusting, however.
The fmod function always returns an exact result---when you write c = fmod(a, b), c is a number such that c + k*b (evaluated in infinite precision) exactly equals a for some integer k.  So your code is actually sound---if (c == 0) will trigger exactly when a is exactly a multiple of b.
You took b to be the double 0.1, which is actually the fraction 3602879701896397/36028797018963968, not the fraction 1/10.  So if I compute fmod(1, 0.1), I should expect to get the fraction (36028797018963968 % 3602879701896397) / 36028797018963968, which is 3602879701896395 36028797018963968.  That's very slightly smaller than the double called 0.1.
This also explains why your code meets your expectations when you use 0.25 instead of 0.1; 0.25 gives you 1/4 exactly.
You might read this floating-point guide to learn about the representation of floating-point numbers.
Comment: Thanks for your anwser! After looking at the guide, I got an idea to find out how to round something to two decimal places. I found this post: http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c . I managed to fix it by adding a new line: 'float roundedValue = ceilf(valueToRound * 100) / 100;'  This made the code work perfectly.

C - Comparing floats with if statements


How does CodeCommit discriminate between identical repos in different accounts?

Question: I ran into a weird deployment situation involving codecommit under cloudformation.
AWS CodeCommit allows me to create the same repository name in the same region in two separate AWS accounts. Both Repositories are given the SSH same endpoint (ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/[reponame])
I was also able to add the same SSH key to both of my IAM accounts to grant access to these two identically named repositories.
Aside from deleting the duplicate, and recreating it with a unique name, how would one discriminate which repository you are pushing to or pulling from with git?
Answer: I got it to work not long after finally asking here. What I had to do was set up different Public Keys for each AWS account, and attach the unique keys to the separate AWS IAM users. I was then able to update my .ssh config file as follows:
<code>Host codecommit-acct1
  Hostname git-codecommit.us-west-2.amazonaws.com
  User [ssh_key_id_1]
  IdentityFile ~/.ssh/[ssh_private_key_1]

Host codecommit-acct2
  Hostname git-codecommit.us-west-2.amazonaws.com
  User [ssh_key_id_2]
  IdentityFile ~/.ssh/[ssh_private_key_2]
</code>
My Git repo config files were able to use 'codecommit-acct1' as a hostname, and adjusted to use the unique SSH keys, which gave AWS enough information to identify which IAM account to authenticate with.
<code>[remote "origin1"]
    url = ssh://codecommit-acct1/v1/repos/[repo_name]
    fetch = +refs/heads/*:refs/remotes/origin/*

[remote "origin2"]
    url = ssh://codecommit-acct2/v1/repos/[repo_name]
    fetch = +refs/heads/*:refs/remotes/origin/*
</code>
I found the solution in the AWS forums here: https://forums.aws.amazon.com/thread.jspa?threadID=228206


What does "passive mode" actually do?

Question: Grand Theft Auto Online has a passive mode which seems to make you semi transparent to other players and prevent you from arming a weapon but doesn't prevent you from driving over people and seems to have conflicting guidance about it's functionality on various sites.
What exactly does "passive mode" actually do?
Answer: This confusion is seemingly caused by the difference in the way passive mode behaves between the original PS3/Xbox 360 release and the so called "enhanced" version released on PC, PS4 and Xbox One.
In both versions, passive mode is only available in "Free Mode". Entering a mission will disable passive mode for the duration of that mission and it will be re-enabled once the mission has completed. Passive mode will prevent you from arming a weapon (except in vehicles, in the original release as discussed below) or otherwise attacking another player or NPC directly. You're still able to drive over NPCs.
In the original version, passive mode is only enabled while on foot - it is automatically disabled while in a vehicle. Additionally, passive players can still be run over or otherwise killed by a player using a vehicle, in that vehicles still hit players in passive mode.
In the enhanced version, passive mode puts the player in a ghost mode, in which they appear translucent to other players and other players cannot physically interact with you while you're in passive mode. When you get into a vehicle that vehicle will also enter the same ghost mode, preventing other players from damaging your vehicle (your vehicle will still take damage from the environment).
Even in the enhanced version, it is still possible for another player to kill you using the environment - for example, blowing up a car that you're standing next to (as the car is "real" for both players) or igniting a gas canister.

What does "passive mode" actually do?


Flash: BitmapData.draw(Video) ignores video height

Question: I'm pretty much the rookiest rookie when it comes to Flash.
Here's the actionscript (3):
// Here's the dumb-dumb:
/*****************************************************************/
/*****************************************************************/
function captureImage(e:MouseEvent):void {
    // The video still-image is captured and drawn...but at 320x240? Oh, and the real kicker is that it gets squeezed (resized), not cropped.
    bitmapData.draw(video);
}
/*****************************************************************/
/*****************************************************************/

// Here's the other relevant code...
/*****************************************************************/
var bandwidth:int = 0;
var quality:int = 100;

var myWidth:int = 320; // the width for camera, video, and bitmaps.
var myHeight:int = 320; // the height for camera, video, and bitmaps.

var cam:Camera = Camera.getCamera();
cam.setQuality(bandwidth, quality);
cam.setMode(myWidth,myHeight,30,false); // (width, height, FPS, favorSize)

var video:Video = new Video();
    video.attachCamera(cam);
    video.x = 20;
    video.y = 20;
    // setting the video.width and video.height here makes the video appear in the desired aspect-ratio (1:1). If this is not set it defaults to 320x240.
    video.width = myWidth;
    video.height = myHeight;
addChild(video);

// 0xff0000 sets a red background just so I can see that the BitmapData "element" is 320x320 like it should be.
var bitmapData:BitmapData = new BitmapData(myWidth, myHeight, false, 0xff0000);

var bitmap:Bitmap = new Bitmap(bitmapData);
    bitmap.x = 360;
    bitmap.y = 20;
    bitmap.width=myWidth;
    bitmap.height=myHeight;
addChild(bitmap);

// capture_mc is my take-a-picture button. :-)
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

So, what am I missing here. I know the makers of Flash don't insist that all images should be displayed in a 4:3 aspect ratio, right? :o)
Anyway, thanks for helping out a "n00b".
p.s. The fact that Flash uses Ctrl+Y to "redo" instead of Ctrl+Shift+Z (like Photoshop) makes me want to flash.events.destroy(flash), or something.
UPDATE:
I figured out how to stretch the video from 240 to 320. But there is a significant reduction in quality in doing that. Here is the code with the updated parts in BOLD:
var bitmapData:BitmapData = new BitmapData(myWidth,240, false, 0xff0000);

var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 360;
bitmap.y = 20;
bitmap.width=myWidth;
bitmap.height=240;
bitmap.scaleY=1.333; // ADDED scaleY
addChild(bitmap);
So I'd still like to find a solution that maximizes the quality.
Answer: I was asked if I had found a solution to the problem and the answer is yes. I'm just copy+pasting the actionscript from my .fla so I'm not going to explain the code below (I actually can't remember what everything does). And obviously there are objects on the stage so you won't be able to plug and play...sorry. Feel free to explain whats going on in the comments.  :-)
import flash.display.Bitmap;
import flash.display.BitmapData;
import com.adobe.images.PNGEncoder;
// for javascript
import flash.external.ExternalInterface;

var snd:Sound = new camerasound(); //new sound instance for the "capture" button click

var bandwidth:int = 0; // Maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second.
var quality:int = 100; // This value is 0-100 with 1 being the lowest quality.

var myWidth:int = 240;
var myHeight:int = 240;

var hiderD:BitmapData = new BitmapData(myWidth+4,myHeight+4,false,0xffffff);
var borderBox1:Bitmap = new Bitmap(hiderD);
borderBox1.x = 22;
borderBox1.y = 52;
borderBox1.width=hiderD.width;
borderBox1.height=hiderD.height;

var borderBox2:Bitmap = new Bitmap(hiderD);
borderBox2.x = 287;
borderBox2.y = 52;
borderBox2.width=hiderD.width;
borderBox2.height=hiderD.height;

addChild(borderBox1);
addChild(borderBox2);

var cam:Camera = Camera.getCamera();
cam.setQuality(bandwidth, quality);
cam.setMode(320, myHeight, 30, true); // setMode(videoWidth, videoHeight, video fps, favor area)

var video:Video = new Video();
video.attachCamera(cam);
video.x = -500;
video.y = -500;
addChild(video);

// display mode ONLY
var video2:Video = new Video();
video2.attachCamera(cam);
video2.x = -16;
video2.y = 54;
video2.width=320;
video2.height=myHeight;
var m:Shape = new Shape();
m.graphics.beginFill(0x0);
m.graphics.drawRect( 24, 54, 240, 240 ); // draw the mask
m.graphics.endFill(); // end the fill
video2.mask = m;

addChild(video2);

// used for sending
var bitmapData:BitmapData = new BitmapData(320,myHeight,false,0xFF0000);

// used for display
var bitmapData2:BitmapData = new BitmapData(320,myHeight,false,0xAAAAAA);
var bitmap2:Bitmap = new Bitmap(bitmapData2);
bitmap2.x = 249;
bitmap2.y = 54;
bitmap2.width=320;
bitmap2.height=myHeight;
var m2:Shape = new Shape();
m2.graphics.beginFill(0x0);
m2.graphics.drawRect( 289, 54, 240, 240 ); // draw the mask
m2.graphics.endFill(); // end the fill
bitmap2.mask=m2;
addChild(bitmap2);

capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

function captureImage(e:MouseEvent):void {
    snd.play();

    bitmapData2.draw(video);

    bitmapData.draw(video);

    warn.visible = false;

    save_mc.buttonMode = true;
    save_mc.addEventListener(MouseEvent.CLICK, onSaveJPG);
    save_mc.alpha = 1;
}

save_mc.alpha = .5;

function onSaveJPG(e:Event):void{

    saving.visible = true;

    var byteArray:ByteArray = PNGEncoder.encode(bitmapData);

    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");

    var saveJPG:URLRequest = new URLRequest("/utilities/saveImage.aspx");
    saveJPG.requestHeaders.push(header);
    saveJPG.method = URLRequestMethod.POST;
    saveJPG.data = byteArray;

    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE, sendComplete);
    urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    urlLoader.load(saveJPG);

    function sendComplete(event:Event):void{
        var loader:URLLoader = URLLoader(event.target);
        ExternalInterface.call("updateImageInputWithFileName",loader.data.filename);
        warn.visible = true;
        saving.visible = false;

        setTimeout(function(){warn.visible=false},10000);
    }
    function onIOError(event:Event):void{
        saving.visible = false;
        ExternalInterface.call("error", "Error", "Could not save image." ,"There was a problem saving the image. You can try again or contact an administrator for assistance.");
    }
}

warn.visible = false;
saving.visible = false;

Answer: For AS2 Apply a  matrix Transform:
import flash.display.BitmapData;
import flash.geom.Matrix;

capture_btn.onPress = function() {
   var cameraBMP= new BitmapData(video.width, video.height);
   var myMatrix:Matrix = new Matrix();
   myMatrix.scale(1.5, 1.5);
   cameraBMP.draw(video,myMatrix);
}

Answer: First off, Ctrl+Shift+Z simply "undoes" the actions in Photoshop. Ctrl+Y redoes :)
Secondly, the height and width of your video depend on the camera it is attached to. In other words, if your camera is set to 320 by 240, then it will stay that way, unless you change the camera's height and width. Plus you made a mistake: it's
video.videoHeight = myHeight;
video.videoWidth = myWidth;

Thus, this means that you have to change your camera's height and width.
Comment: Thanks. It looks like `videoHeight` and `videoWidth` are read-only. Also, the video DisplayObject displays the live webcam video at the correct aspect ratio (320x320 or 1:1]). Its just when I capture the still that it squeezes the image into 320x240.
Comment: Technically Ctrl+Shift+Z undoes an undo.  :-)


How to merge two or more columns into one?

Question: I have a streaming Dataframe that I want to calculate min and avg over some columns.
Instead of getting separate resulting columns of min and avg after applying the operations, I want to merge the min and average output into a single column.
The dataframe look like this:
+-----+-----+
|  1  |  2  |
+-----+-----+-
|24   |  55 |
+-----+-----+
|20   |  51 |
+-----+-----+

I thought I'd use a Scala tuple for it, but that does not seem to work:
val res = List("1","2").map(name => (min(col(name)), avg(col(name))).as(s"result($name)"))

All code used:
val res = List("1","2").map(name => (min(col(name)),avg(col(name))).as(s"result($name)"))

val groupedByTimeWindowDF1 = processedDf.groupBy($"xyz", window($"timestamp", "60 seconds"))
  .agg(res.head, res.tail: _*)

I'm expecting the output after applying the min and avg mathematical opearations to be:
+-----------+-----------+
|  result(1)|  result(2)|
+-----------+-----------+
|20 ,22     |  51,53    |
+-----------+-----------+

How I should write the expression?
Answer: Use struct standard function:

struct(colName: String, colNames: String*): Column
struct(cols: Column*): Column
Creates a new struct column that composes multiple input columns.

That gives you the values as well as the names (of the columns).
val res = List("1","2").map(name =>
  struct(min(col(name)), avg(col(name))) as s"result($name)")
  ^^^^^^ HERE

The power of struct can be seen when you want to reference one field in the struct and you can use the name (not index).
q.select("structCol.name")

Answer: What you want to do is to merge the values of multiple columns together in a single column. For this you can use the array function. In this case it would be:
val res = List("1","2").map(name => array(min(col(name)),avg(col(name))).as(s"result($name)"))

Which will give you :
+------------+------------+
|   result(1)|   result(2)|
+------------+------------+
|[20.0, 22.0]|[51.0, 53.0]|
+------------+------------+

Comment: Thanks , In addition how to merge column name with  DF  row. I want result [20.0,22.0,result(1)] in this format. how should be expr ?
val res = List("1","2").map(name => array(min(col(name)),avg(col(name))).as(s"result($name)"))
Comment: @shrikrishna: If I understand correctly you want to add the column name to the array? That seems very inconvenient to be honest, but you can add it with `lit(s"result$name")`.
Comment: @ Shaido its work for me. I need column name  for ForeachWriter  for further operation and DB sink class. is any better design?
Comment: @shrikrishna: I suspect there is but it depends on your actual situation, I would recommend asking a new question about it with more details.


How do I change the minecraft menu song?

Question: I was making a new Soundtrack for Minecraft using a resourcepack I have the .OGG file I want to play and I have it in .minecraft\resourcepacks\(My Resourcepack Name)\assets\minecraft\sounds\menu the song's file name is got.OGG
I have changed the records but I dont know how to change the menu's song.
Answer: Rename the file to menu1.ogg. You may also need to overwrite the other menu music files by duplicating your song and calling it menu2.ogg, menu3.ogg and menu4.ogg.
Comment: I have tied that and when I go to the main menu it just plays the normal C418 music, do you have anything else that might work?
Answer: Do what colorfusion said but change the files to read-only, Minecraft will just replace the music :/ You can do this by right-clicking the files and then clicking 'Properties'. Next, tick 'Read-Only' and then hit apply and ok.
-The reason I didn't comment is because I don't have enough reputation as I created my account literally 43 seconds ago.
Comment: That's no reason to post a comment as an answer. However, in this case it should be rather easy to expand this to a proper, self sufficent answer. You can even include details from *colorfusion*'s answer, if you attribute them properly.
Answer: This can be done using Resource Packs.
Comment: Could you explain how to accomplish it with Resource Packs?


How do you stop squid from despawning?

Question: In Minecraft how do you stop squids from despawning without name tags!
Comment: @BillyMailman Not a dupe because I don't have any name tags
Comment: Then see all the other answers on that question. There's more than one.
Answer: Without nametags, you cannot stop squids despawning in vanilla Minecraft.
Comment: Except for going around it in circles and staring at it from less that 24 meters - if that suits you.

How do you stop squid from despawning?


Do multiple land claim blocks still prevent zombie spawning?

Question: The Land Claim Block (LCB) is now used in both multiplayer and singleplayer. Most notably, user-crafted workstations (workbenches, cement mixers, forges, etc) need to be within the realm of a placed LCB in order to be removed. Hitting it with a wrench or other tool will just destroy it.
The question: If I put multiple LCBs down (multiple bases, etc), I know that only the most recent LCB placed affects the workstations (I've already experienced this and had to put down a new LCB in order to move my forges to another location) but if I leave LCBs in place in an old base, placing a new one elsewhere, will the original LCB prevent zombie spawns in that area?
TL;DR:
I'm building an underground tunnel system between parts of my base, which spans more than the 41x41 space that a single LCB affects, so if I put multiple LCBs down in various areas, can I effectively prevent all zombie spawning inside my bases?
Comment: In regards to LCB they are suppose to be active simultaneously, however, there is a bug a few of us experience in SP where a LCB randomly stops working and we have to pick it up and re-place it. Also in general, while player blocks in custom bases do prevent spawns, screamers can still spawn zeds inside your base if it is huge and if you walk away and the chunk unloads I have occasionally found Zombies in my base when going back, loading the chunk back in. TFP has to fix that so we can build mega defensive bases IMHO :)
Answer: Research shows that LCB doesn't stop spawning.  There isn't anything super conclusive, but it looks like the following stops spawning:

Your bedroll (41x41 area by default)
Player made "building blocks" (specific types of things classify)

There isn't any mention of LCB stopping spawning.
On top of that, certain types of spawning are not affected, like Sleeper in POI.  If you build an existing POI into your base, some respawns may not be preventable.
Sources:
March 2016 reddit post
A16 release notes
Comment: This is correct, TS - accept this answer. I took over POI and even after full upgrade or destruction of original blocks, I got sleepers every time when there were no one at home (i.e when we all left for raid).
Comment: That's while using a LCB at your POI base?
Comment: [This Post](https://steamcommunity.com/app/251570/discussions/0/1354868867720493635/) contradicts what you said. I've seen arguments on both sides, although I admit that the LCB wiki page says nothing about blocking spawns, but it doesn't say that it doesn't block spawns.
Comment: But that post is just some guy saying LCB stops spawns in a radius, but there isn't any actual information based on that, and nobody reported whether it works or not.

What usually happens is if you got enough resources to start dropping LCB, you got resources to reinforce, and that's actually what typically stops spawns.  When you start digging up all the dirt and putting in cobblestones.
Answer: No, there is no prohibition on spawning on player-placed blocks. There is a property on blocks, player-placed or not, that allows non-POI, non-screamer spawns called canMobsSpawnOn. I don't know specifically which blocks have this property set or not but you can look in the items.xml file to check (I think). Further, if you remove this property (or set it to false) for those blocks where it is true, mobs will no longer spawn on that block type. Note that, as I understand it, screamers are not bound by this rule (nor are the mobs they call), and POI sleeper spawning, as it works with a different mechanism, is also not bound by this rule.
So if you added < property name='canMobsSpawnOn' value='false' /> to the entry for flagstoneBlock, then zombies would not spawn on those blocks. I've also read that zombies only spawn on the 'highest' block for a specific coordinate (outside a POI, of course). So, placing your base underground would keep you safe from general mobs spawns. I expect that will change pretty soon.
Answer: LCB does prevent zombie respawn. Multiple LCBs do not change this. It is now possible, in A18, to build a larger defensible position, including building with an existing POI because the LCB will prevent all respawns (including loot) within the boundary of that block.


Spring Batch AbstractPagingItemReader first reads all records and then process and writes

Question: I am having a use case where in I am using AbstractPagingItemReader to make paginated calls.The use case is that reader should make an api call process and write immediately and this should continue until no more records are fetched by reader.
What currently is happening is that it accumulates all records first and then starts to process and write.
Elaborated Further :-
Say I want a total of 100 users in my destination.
For this I make 10 calls with page size of 10 (10*10).
The reader will first accumulate all 100 users calling api 10 times and then call the writer.
Instead i wanted to first send the response of first request to writer and then make second call and so on.
To make the 10 calls I intentionally added a for loop to iterate 10 times as else the code would have just made 1 fetch request and called writer and would have exited.
Comment: Please share your code. It could be that the chunk size is bigger than the page size, hence the reader will call make multiple calls (request multiple pages) to accumulate a chunk.
Comment: Hi @MahmoudBenHassine ,i kept chunk size as 1.
Answer: This got solved when I implemented ItemReader .This lead to overriding of read method :-
public class DataReader implements ItemReader{


Will c# compiler optimize empty if blocks

Question: Is there a chance that C# will optimize the following code block?
if (specField == null || AddSystemType(specField, layout)
                      || AddEnumType(specField, layout)
                      || AddUserType(specField, layout))
{
}

Comment: Build the code and then find out using ILDASM :-P
Comment: Frankly I have no idea what you mean. Can you please be more specific?
Comment: I would have thought not. The methods could have side effects.
Comment: Intersting question, bu how should it be optimized?
Answer: Well you can use ildasm to see what the compiler has optimized for yourself. But if you were expecting it to remove the code entirely, it can't - because those three method calls could throw exceptions or modify state. So the best it could do would be to emit the equivalent of:
if (specField != null)
{
    if (!AddSystemType(specField, layout))
    {
        if (!AddEnumType(specField, layout))
        {
            AddUserType(specField, layout);
        }
    }
}

Comment: Your example is not equal to  cond || cond.
You could use && and the short circuit evalution will prevent the execution of the second condition
Answer: The AddSystemType will execute and could change some states, so everything in the IF will be executed.
When using AND
if(specField == null && AddSystemType()) {

}

Then if specField is (always) null,  AddSystemType() isn't executed and skipped (short circuit evaluation)


Do You Have To Kill EVERY SINGLE MONSTERS In Undertale Or Can You Leave Some Behind?

Question: I Don't Know What To Put ;^; SCREEEEEE
Comment: The whole point of *Undertale* is that you don't have to kill *any* single monster, let alone every last one of them.
Comment: You can elaborate on why you feel there is a choice in the matter. What happens in the game to give you the idea there is or isn't a choice? you can [edit](https://gaming.stackexchange.com/posts/348744/edit) the question to add this.
Answer: You don't have to kill anything, but the number of monsters killed will affect the ending. Generally speaking, you have three different endings:
Neutral ending
It occurs if you've killed at least one monster and it will change depending who lives and who dies. Asgore will die, you can escape the Underground and new person (or a dog) will be the new ruler. You will receive a phone call from Sans describing what is happening and Flowey will appear suggesting that you should try playing again without killing anyone
Pacifist ending
Don't kill anyone, at the end Asgore will be knocked out and a new boss - Asriel - will appear. After the fight you can console and forgive him and your friends will be able to go through the barrier.
Genocide ending
Achieved by killing EVERY creature. After killing Sans, you will meet Asgore and Flowey, the latter will beg you not to kill him. After their death, You will then come face to face with Chara, the original human who is also you in a weird way. Chara will praise you for your determination and power and will say the two of you can go on to destroy more worlds and erase this one. Regardless the game will end with a slash animation which implies the game itself is permanently damaged. This is somewhat true as your save files for future playthroughs will always be tainted after this ending.
Starting the game up again after this at any point will result in a blank screen with wind sounds. After 10 minutes Chara will appear asking if you think you are above consequences.
If you want to play again you will have to agree to give her your soul and it will alter your future play thoughs.


Rails 3: create input field for each element of a collection (MongoID)

Question: I have two models, Item and Bid
class Item
  include Mongoid::Document
  field :name, type: String
  has_many :bids
end

class Bid
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  field :bid, type: Float
  field :bidder, type: String
  belongs_to :item
end

In views/prices/index I would like to list all items in a table and for each item put a field next it in which people can enter a number. At the bottom of the form should be a submit button for all records.
How is this best achieved?
There's plenty of code out there explaining how to add, for instance, several questions to one survey (http://railscasts.com/episodes/196-nested-model-form-part-1) but I couldn't find an example that shows how to add one new record for each of an existing element of a collection.
Answer: I don't think you need to dynamically add a form for each item if you know you are going to use all of the items there to begin with.
Quick and Dirty:
form_for(@items) do |f|
  @items.each do |item|
    f.label item.name
    f.text_field item.name.to_sym, :value => "1"
  end
  f.submit "Submit"
end

I haven't tried this and that code isn't tested, but I would assume if you do some work in the controller to build each bid it should be fine. Also amount would be a better variable name for the bid class than using Bid.bid.
The proper way to do this (which would also allow updates) would be to make a nested form and then use fields_for method to actually look at a live bid object: http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
Comment: Worked very well after I changed the first line to `form_for :items do |f|`. No idea why that was necessary though...
Comment: They just point at different route paths. Most likely it was trying to point at updating the @items. http://stackoverflow.com/questions/957204/instance-variable-vs-symbol-in-ruby-on-rails-form-for


What determines the success of Axii / Persuasion / Intimidate?

Question: Sometimes in dialog of The Witcher 2, Geralt gets the option to try to alter the course of the conversation with persuasion, intimidate, or the axii sign.
In practice, I've had intimidation fail to work, so I'm curious as to how the chance of success is determined in the first place. Is it related to the level of the abilities gained via use of the related mechanic? E.g., Axii Sign Hex increases when you use the Axii Sign to resolve a conversation.
Answer: According to info listed on IGN Vault, you start with a 50/50 random chance of success for each one.
Each time you use one (either succeed or fail) it improves the skill (you'll see a message pop up about acquiring a new skill) thus improving your chance next time.
My person experience was that I succeeded the first/only three times I tried Axii and the first/only time I tried Persuasion, so either I'm very lucky, or it's not really 50/50...
Comment: Or you have a small sample size.
Comment: I looked through the article you linked; parts of it are flat out wrong. Specifically, the section regarding abilities. Strong Back is for letting Arayan LaVallette live, and Backstabber, actually Assassin, is for finding the Altair corpse in the courtyard. "Cover" can be gained even if you stray outside of the shield in Act 1. The parts regarding talented abilities may be right, but the legitimacy of the rest of it is now in question.
Comment: Indeed, 1/16 chance you're right and I was just lucky.  But I'm afraid I agree about the unreliable source.  My guess is they (IGN) had early/privileged access and started documenting a bunch of things that later changed before proper release.  Maybe try their procedure though - "C" for character screen and check the descriptions of the acquired abilities?


Redirect https://example to https://www.example.com Vhost

Question: I am trying to implement a redirect which takes my site from https://example.com to https://www.example.com via redirect and not from the .htaccess file. Our site already redirects http requests to https://www.example.com. I posted what we have below.
<Virtualhost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
RewriteEngine on
RewriteRule ^/?(.*) https://www.example.com/$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
ServerName www.example.com:443
DocumentRoot /var/www/html
SSL information here

Comment: Welcome to StackOverflow! How far along are you? (What happens if you try this now?) Have you configured your DNS so that the bare domain (`example.com`) points to your web server, or is that outside the bounds of what you're asking here?
Comment: Hi Jeff. Thanks for the response. - yes both example.com and www.example.com point to the web server. Everything works from http > https and  https://www.. The only issue is getting https://example.com to work with https://www.example.com. Hopefully this gives you more clarification.
Comment: Have you consulted the [documentation on canonical hostnames](https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost)?
Comment: "The only issue is getting `https://example.com` to work with `https://www.example.com`" - From the code you've included in your question, presumably `http://example.com` would also be an issue currently?
Comment: Nope that currently works as we have all http requests redirecting to https. Only issue is https://example.com not redirecting.
Comment: @MikeScott So how did you get on with ReynaldoAceve's answer?
Answer: Adjust just HTTPS/443 virtual hosts:
<VirtualHost *:443>
ServerName www.example.com:443
DocumentRoot /var/www/html
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
SSL information here


Why won't my command block chain work?

Question: I've been working on a large multiplayer world. In it, I have set up a "P.A. System" using a command block chain, with all the blocks set to use the <code>/say</code> command to send messages in chat.
When I send a Redstone signal through the chain, the first block (set to impulse, not chain, and unconditional) activates. Then the second block (set to chain and conditional) activates. The signal seems to end there, as the third block (set to chain and conditional) does not activate.
Why is this setup not working?
I am playing Bedrock Edition, on my phone.
Comment: Oh, they need to be set to always active? Didnt know that. I'll change that when I get back on later and see if that works.
Comment: @Fabian Röling that worked, thank you! Can you please make it an answer so I can accept it?
Comment: Ummm… Alright… That's an extremely basic thing, but if you say so, I'll write an answer.
Comment: All command blocks contain `/say` commands? Those should always succeed. Have you checked whether they are all rotated the right way and set to "always active" instead of "needs redstone"?
Answer: When a command block says "needs redstone", it needs redstone. Otherwise it doesn't work. Either give it redstone (for example a lever) or set it to "always active".

Why won't my command block chain work?


Unit testing a file reading function

Question: The code I currently have is as follows:
public class SerializeFileHandler
{
    public void WriteListToFile(MyProject myProject, string filePath)
    {
        FileStream outFile;
        BinaryFormatter bFormatter = new BinaryFormatter();

        outFile = new FileStream(filePath, FileMode.Create, FileAccess.Write);

        bFormatter.Serialize(outFile, myProject);

        // Close file
        outFile.Close();
    }

    public MyProject ReadListFromFile(String filePath)
    {
        FileStream inFile;
        BinaryFormatter bFormatter = new BinaryFormatter();
        MyProject myProject = new MyProject();

        // Open file for input
        inFile = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        // Obtain objects from file via serialization
        myProject = (MyProject)bFormatter.Deserialize(inFile);

        inFile.Close();
        return myProject;
     }
}

To save the data file I use public const string myProjectFile = "myproject.dat"; and then to save any new data to the file when the user clicks the Save button I do serializeFileHandler.WriteListToFile(); with parameters in the parentheses of course.
What I want to do is write a unit test or multiple tests if needed to test the functionality in terms of reading the data file into the application. Currently the data file is automatically loaded in when the program loads (Just does a ReadListFromFile on the serializeFileHandler.
I have created the test using the test wizard within Visual Studio but I am not sure how I'd construct the test to automate the testing of the file reading functionality. Any help would be appreciated.
Comment: It's a bit unclear what you have tried and what your problem is, please try to fix that.
Answer: If it was me I'd have a ReadListFromStream method and then I'd inject test streams to chcek that myProject was loaded up correctly.
To Test from file
I'd stub ReadListFromStream, and simply make sure ReadListFromFile called it.
Comment: Do you have an example of what the code might look like for something like that? I'm relatively new in terms of dealing with serialized files within C# so a lot of it is completely new to me.
Comment: It would look like the the file when you open it. Looking at it, might be better off with an integration test. Create a thingy, call WriteToFile. Create another with ReadFromFile from that file and then compare the two instances. What you are testing here is the file system, which is low value and serialisation markup which is high value and barely looked at.
Comment: I'm just a bit muddled as I get quite confused thinking how to actually implement the tests into my code.
Answer: The issue that SerializeFileHandler has at the moment is that it has a dependency on FileStream which in turn ties it to reading from the file system; in order to write a test for it currently, we actually need to interact with the file system.
We can break the dependency on the file system by introducing a test seam that will allow us to mock/stub/fake the behaviour of the FileStream. In the production system, we'll pass SerializeFileHandler a FileStream to work with, but in unit tests, we'll pass it something that adheres to the interface of a FileStream that SerializeFileHandler is using but will allow us to control the behaviour for test purposes.
First, we can introduce an interface for a stream that interacts with the file system
public interface IFileStreamSource
{
   Stream Open(string path, FileMode mode, FileAccess access, FileShare share);
}

and let SerializeFileHandler take a dependency on this
public class SerializeFileHandler
{
    private readonly IFileStreamSource _source;

    public SerializeFileHandler(IFileStreamSource source)
    {
        _source = source;
    }

    public void WriteListToFile(MyProject myProject, string filePath)
    {
        using (var stream = _source.Open(filePath, FileMode.Create, FileAccess.Write)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, myProject);
        }
    }

    public MyProject ReadListFromFile(String filePath)
    {
        MyProject myProject = null;

        using (var stream = _source.Open(filePath, FileMode.Open, FileAccess.Read))
        {
            BinaryFormatter bFormatter = new BinaryFormatter();

            // Obtain objects from file via serialization
            myProject = (MyProject)bFormatter.Deserialize(stream);
        }

        return myProject;
     }
}

Now, you can use a mocking library such as Moq to create a Mock<IFileStreamSource>(), likely returning a MemoryStream, in your unit tests and set expectations for when Open() is called.
In your production code, you may decide to create a parameterless constructor for SerializeFileHandler that passes an instance of an IFileStreamSource to the constructor that takes an IFileStreamSource, which will return an instance of FileStream when Open() is called (a la "Poor Man's Dependency Injection"), or you may already be using an IoC/Dependency Injection container already, in which case you would simply register the component for the IFileStreamSource service.
Looking at the code currently, I can see a few cases that are not handled in ReadListFromFile, such as if the file does not exist or the file does exist but cannot be deserialized to an instance of MyProject. You may want to write tests for the cases and update the code accordingly :)
Answer: If you need your tests to interact with the file system you can create a mock file system wrapper as other suggested, or create an isolated file system your test can use. I prefer the second approach, as with it the test is more similar to reality.
Prior to the test being run, set up a folder with all the files the test needs. Then have the code in the test access that folder, instead of the folder accessed by the actual code. That way, you can read the file, test how your code works if the file is missing, and also test file creation.

Unit testing a file reading function


MySQL query performance?

Question: I have MySQL database and 5 tables called tribes(groups), posts, posts_to_groups, post_comments and posts_votes.
Relationship between groups and posts is MANY_2_MANY so each post can belong to many groups and each group can contain 0-* posts. This is what table posts_to_groups does.
I'm searching for the 3 most popular posts that were posted into the groups that this user follows(associated via posts_to_tribes - table for MANY_2_MANY relationship) for the last 24 hours from this moment and ordered by sum of (comments_count + votes_count) DESC
This is my current query:
SELECT DISTINCT
    p.post_id,
    p.description,
    p.link,
    p.user_id,
    p.total_comments,
    p.total_votes,
    (SELECT
            COUNT(*)
        FROM
            comments
        WHERE
            last_edited > DATE_SUB(NOW(), INTERVAL 24 HOUR)
                AND post_id = p.post_id) AS comments_count,
    (SELECT
            COUNT(*)
        FROM
            posts_votes
        WHERE
            date_voted > DATE_SUB(NOW(), INTERVAL 24 HOUR)
                AND post_id = p.post_id) AS votes_count
FROM
    posts p
        JOIN
    posts_to_tribes pt ON pt.post_id = p.post_id
WHERE
    pt.tribe_id IN (3 , 38, 107)
ORDER BY (comments_count + votes_count) DESC , p.last_edited DESC
LIMIT 3;

This query is extremely slow and took right now ~500ms.
Is any way to rewrite this query in order to improve performance ?
UPDATED:
EXPLAIN output:

Query suggested by Tim3880:
SELECT
    p.post_id,
    p.description,
    p.link,
    p.user_id,
    p.total_comments,
    p.total_votes,
    t.comments_count,
    t.votes_count
FROM posts p
JOIN (
    SELECT
        p.post_id,
        (SELECT
                COUNT(*)
            FROM
                comments
            WHERE
                last_edited > DATE_SUB(NOW(), INTERVAL 24 HOUR)
                    AND post_id = p.post_id) AS comments_count,
        (SELECT
                COUNT(*)
            FROM
                posts_votes
            WHERE
                date_voted > DATE_SUB(NOW(), INTERVAL 24 HOUR)
                    AND post_id = p.post_id) AS votes_count
    FROM
        posts p
            JOIN
        posts_to_tribes pt ON pt.post_id = p.post_id
    WHERE
        pt.tribe_id IN (3 , 38, 107)
    ORDER BY (comments_count + votes_count) DESC , p.last_edited DESC
    LIMIT 3
) t
ON p.post_id = t.post_id
ORDER BY (t.comments_count + t.votes_count) DESC , p.last_edited DESC

It took now ~280ms.
EXPLAIN output:
Comment: Please add the result of a `EXPLAIN SELECT...` from your query together with your table definition.
Comment: What is the `EXPLAIN` output? Do you have indexes on the columns you are using in the `WHERE` conditions?
Comment: Thanks for your comments ! Unfortunately I have no access to this db right now, I'll provide this information tomorrow.
Comment: Without execution plan, we can only guess. You can try rewrite your sub queries using group by post id and then join them for the outer query.
Comment: 500 ms does not seem particularly slow, particularly if the underlying tables have a lot of data.  What is your expectation.
Comment: we have a lot of different queries(inside of REST endpoints) and only this one under performance testing shows ~16 sec(in concurrent environment) while the other endpoints show ~2 or 3 sec
Comment: I have added EXPLAIN output
Answer: If you post_id is a primary key (or unique), try get the 3 post_id first:
SELECT
    p.post_id,
    p.description,
    p.link,
    p.user_id,
    p.total_comments,
    p.total_votes,
    t.comments_count,
    t.votes_count
FROM posts p
JOIN (
    SELECT
        p.post_id,
        (SELECT
                COUNT(*)
            FROM
                comments
            WHERE
                last_edited > DATE_SUB(NOW(), INTERVAL 24 HOUR)
                    AND post_id = p.post_id) AS comments_count,
        (SELECT
                COUNT(*)
            FROM
                posts_votes
            WHERE
                date_voted > DATE_SUB(NOW(), INTERVAL 24 HOUR)
                    AND post_id = p.post_id) AS votes_count
    FROM
        posts p
            JOIN
        posts_to_tribes pt ON pt.post_id = p.post_id
    WHERE
        pt.tribe_id IN (3 , 38, 107)
        AND p.last_edited >  DATE_SUB(NOW(), INTERVAL 24 HOUR)
    ORDER BY (comments_count + votes_count) DESC , p.last_edited DESC
    LIMIT 3
) t
ON p.post_id = t.post_id
ORDER BY (t.comments_count + t.votes_count) DESC , p.last_edited DESC

Edit:  This is the join version:
SELECT
    p.post_id,
    p.description,
    p.link,
    p.user_id,
    p.total_comments,
    p.total_votes,
    t.comments_count,
    t.votes_count
FROM posts p
JOIN (
    SELECT
        p.post_id,Comments_Count, Votes_Count
    FROM
        posts p
        JOIN
        posts_to_tribes pt ON pt.post_id = p.post_id
        LEFT JOIN (SELECT
                post_id, COUNT(*) Comments_Count
            FROM
                comments
            WHERE
                last_edited > DATE_SUB(NOW(), INTERVAL 24 HOUR)
            GROUP BY post_id) cc
        ON p.post_id = cc.post_id
        LEFT JOIN
        (
            SELECT
                post_id, COUNT(*) Votes_Count
            FROM
                posts_votes
            WHERE
                date_voted > DATE_SUB(NOW(), INTERVAL 24 HOUR)
            GROUP BY post_id
        ) vc
        ON p.post_id = vc.post_id
        WHERE pt.tribe_id IN (3 , 38, 107)
    ORDER BY (comments_count + votes_count) DESC , p.last_edited DESC
    LIMIT 3
) t
ON p.post_id = t.post_id
ORDER BY (t.comments_count + t.votes_count) DESC , p.last_edited DESC

If the performance is still not acceptable, you may have to think about updating the total_comments, total_votes directly or using trigger or scheduled job.
Comment: I added  the line " AND p.last_edited >  DATE_SUB(NOW(), INTERVAL 24 HOUR)". On my testing environment, it makes huge difference.
Comment: Thanks, this is the fastest query.. but it shows the other output results than the original one..
Comment: It works 2 times faster than the original query.. but the speed is still not perfect. May be correlated subqueries here can also be optimized ?
Comment: Your explain output didn't indicate any sub query issues. Please post your new explain result.
Comment: Added explain output for your query
Comment: As I said, the plan didn't show any sub query issues. group by may make it worse.
Comment: Also, I already use  total_comments and  total_votes but for total comments/votes number for all time.. but inside of this query I need to find posts based on some sliding interval (now - x). This is why I need to calculate this on the fly with subselects or something similar..
Comment: I don't have the data to play around. If I have to dig further, I would try 1) add the time filter to the posts 2) try different composite indexes.
Comment: Thanks Tim3880! Right now this query runs ~156ms. Tomorrow I'll run my performance tests and report a results here.
Comment: The last query shows the best results. Thanks Tim3880!
Answer: You have used 2 correlated subqueries. Each one from the correlated subqueries will be executed once for every row from the outer query. Hence, if you can avoid them it's likely to get faster query.

[..] they are inefficient and likely to be slow. Rewriting the query as a join might improve performance.

You have to avoid them by using join. This may help you: MySQL - can I avoid these correlated / dependant subqueries?
Comment: Thanks, could you please help me with this query ?

MySQL query performance?


How to refinish indoor window molding wood?

Question: The window molding in my house feels very "rough," for lack of a better word. The house is 25 years old.
I assume this is from the sun beating down on the finish and wearing it down over the years, since the trim that is not in direct sunlight (like the top of the window molding) has held up much better.
The damage ranges, depending on which room you're in. Some moldings appear as if the stain has evaporated away.  Some moldings have black discoloration in the corners (What is this?). Only one molding is in good shape.
The unfinished wood attracts a lot of dust. The moldings all have a perpetual layer of dust. When you dust it, more dust comes out and I can never really seem to get all of it out. As someone with allergies, this is terrible, and this is actually the main reason I am trying to repair it.
Here are photos (it's kind of hard to see what I'm talking about just by looking, which is why I took a bunch of photos):

(Notice how the vertical wood (on the right hand side) is much smoother and shinier than the horizontal wood in the following photo):

Kitchen window (it has a lighter stain, like butterscotch):

Here's the black stuff I mentioned earlier:

Here is the only window molding in the house that feels totally smooth and looks great. I don't know why this one hasn't deteriorated like the others just a few feet away from it. Maybe it's the figurines...

My first question: Can a newbie handle this project?
If it is easy enough for a beginner, how should I go about doing this? Please be as specific as possible...for example, instead of saying "sandpaper" please tell me what grit sandpaper to use. Assume I know nothing. Thanks in advance.
Comment: Those pics take forever to load and paint, and I'm on a fast connection. Could someone who has the time help Kyle reduce them to a reasonable size?
Comment: @keshlam -- I can reduce them but currently they are all only around 1MB a piece. What size should I reduce them to?
Answer: Refinishing it by stripping / sanding off the existing finish, restaining and resealing, would look great but a lot of work.
If it was older, rubbing it with fine steel wool and some finishing oil can make a beat finish look pretty good, though way short of refinishing.  At 25 years old, it was probably sealed with polyurethane, which doesn't rub up as well.  Still, it's worth a try.  I doubt it will make it worse, but still try it on a limited area first to be safe.
For the oil, there are many to choose from, one that no purist would approve of but works pretty good is Scott's Liquid Gold.
Since this method doesn't add any sealer or color, it won't make refinishing any harder if you have to bite the bullet.
Comment: I'd like to try the "proper" way at first, but good to know about the Scott's Liquid Gold.
Answer: You have three choices: Refinish them in place, remove them from the wall and refinish on the workbench, or do part one way and part tHe other.
You can do a much better job with less effort if you dismount the trim and refinish the pieces elsewhere. That does require learning how toto carefully take the trim off the wAll. Not hard if you're careful and patient, but easier after you've done it the first few times. For me, the trick was learning to use wedges to pull the pieces free relatively gently.
Doing it in place means sanding into corners and working at awkward angles, but is also quite doable. Try not to let the finish bridge gaps and adhere pieces to each other, or they'll be a bit harder to remove if/when that becomes necessary... but unlike finishing a frame-and-panel door, nothing's likely to split if finish gets where it shouldn't be.
Remember to have fun.
Comment: I don't understand this sentence: "Try not to let the finish bridge gaps and adhere pieces to each other, or they'll be a bit harder to remove if/when that becomes necessary." Could you please explain?
Answer: The dark discoloration in the corners looks like water came through at some point, check the outside of the window for damaged or missing glazing putty or weatherstrip material and repair as necessary (seal with exterior caulking, maybe?).
For the wood, I would start with 220 grit sandpaper and give the entire frame and trim a quick sanding. Then get a small can of wood stain and stain the whole thing, this will blend and improve the appearance of the discolored areas. Then use a high quality water-based gloss varnish and coat using a good paintbrush. It will look much better when you are done and the gloss finish will make it easy to clean and dust in the future.
Comment: It doesn't look like mold, It just looks discolored. If you have concern just put a few drops of liquid bleach in some water and wipe the area with a rag, that will kill it. Let it dry and finish as explained above. The gloss varnish will protect the wood from future condensation drips and help it resist any mold growth too.
Comment: before sanding, just a bit to make it damp then let dry before sanding
Comment: Finish alone is all that is needed, however if you want the color to match you need to carefully choose the new stain or sand and stain the existing.
Comment: Dry sand. Graduating grits from coarse to fine (220) is right if you intend to take it down to "natural" per your link, which is a lot of work and time but will result in perfection if done right. My answer (quick sand then stain to blend color then varnish) is a method to get reasonably nice results with less requirement for expertise/time.
Comment: Now that you mention it, I think the discoloration might be from condensation that forms on the inside of the window on cold days in the winter, and then pools to the edges. Since the wood is unfinished, that is a breeding ground for mold, right?
Comment: Should the liquid bleach be applied before or after sanding?
Comment: The topside of the trim is slightly rough, but to the eye looks unscathed (that is, the stain is still strong, not faded). Should I sand, stain, and finish these marginally impacted areas, or is simply applying finish to these areas (without sanding and staining) appropriate?
Comment: I have little experience sanding. I'm guessing I should sand the trim until it looks natural, something like [this](https://d3ui957tjb5bqd.cloudfront.net/images/screenshots/products/10/100/100236/lmissenda-textures-002-f.jpg?1398129225)? I have heard that using a gradation of grits is recommended when sanding; does that apply here, or is using only 220 grit sandpaper appropriate? Should I wet sand or dry sand? Do you have any tips for sanding evenly, particularly the corners, short of taking the trim off of the wall (as keshlam recommended)?
Comment: Ah, okay. I will do the easier way then. So since I am not aiming for the "natural" appearance, how do I know when to stop sanding? Should I only do one pass with the 220 grit sandpaper and then begin staining? I thought that in order to re-stain a piece of wood and have it look good, you must first get all of the old stain out.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/31925/discussion-between-jimmy-fix-it-and-kyle).


why my code shows segmentation fault.. for n = 1000000000

Question: #include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define mod 1000000007

int main()
{

    unsigned long long int n,i,j,k;
    unsigned long long int *power = (unsigned long long int *)malloc(sizeof(unsigned long long int) * (1000000000LL));
    power[0] = 1;
    for(i = 1;i <= 1000000000LL;i++){
        power[i] = (power[i-1] * 2 ) % mod;
    }

    int t;
    scanf("%d",&t);
    while(t--){

        scanf("%lld",&n);
        unsigned long long int f = 2;
        for(i=2; i<=n;i++){
            f = (f + power[i/2]) % mod;
        }

        printf("%llu\n",f);

    }
    return 0;
}

Comment: When `malloc` is unable to allocate the memory for you, it may return a null pointer. Dereferencing one would cause undefined behaviour. You should always check the return value before using is.
Comment: There's no need for the `LL`. An integral literal always has a type in which it fits, if such a type exists.
Comment: @KerrekSB I believe that's a pretty recent behavior though, else why do we have the type suffixes?
Comment: @unwind: Well, you can say `printf("%lu", 1UL)`, but you cannot say `printf("%lu", 1)`...
Comment: [Do not cast the return of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)
Comment: @unwind If you call 13 years recent, then yes.
Comment: Does your computer even have 8GB of ram?
Comment: Welcome to StackOverflow. Please provide a simple explanation of what you're trying to achieve with your code. Also, try reducing your code to an [SSCCE](http://sscce.org/) - often you will discover the problem during this process. See the [FAQ](http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites) for more details.
Answer: Surely power[1000000000] is out of bounds! Your array doesn't have that many elements. You need <, not <= in the loop.
Answer: You should check if malloc properly allocated power.
Answer: Because you did not test the return value of malloc. It fails by giving NULL (e.g. when out of memory).
Answer: This is not a good idea
for(i = 1;i <= 1000000000LL;i++){

indices are zero based anyway but you should replace the <= with a < to prevent from reading past the end of the array
Also given the size of the array you are trying to allocate you should check your power pointer is valid.