/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.index.translog.memory;

import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.shard.AbstractIndexShardComponent;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.translog.Translog;
import org.elasticsearch.index.translog.TranslogException;
import org.elasticsearch.util.SizeUnit;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.concurrent.ThreadSafe;
import org.elasticsearch.util.concurrent.jsr166y.LinkedTransferQueue;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;

import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author kimchy (Shay Banon)
 */
@ThreadSafe
public class MemoryTranslog extends AbstractIndexShardComponent implements Translog {

    private final Object mutex = new Object();

    private final AtomicLong idGenerator = new AtomicLong();

    private final AtomicLong estimatedMemorySize = new AtomicLong();

    private volatile long id;

    private final Queue<Operation> operations = new LinkedTransferQueue<Operation>();

    @Inject public MemoryTranslog(ShardId shardId, @IndexSettings Settings indexSettings) {
        super(shardId, indexSettings);
        newTranslog();
    }

    @Override public long currentId() {
        return this.id;
    }

    @Override public int size() {
        return operations.size();
    }

    @Override public SizeValue estimateMemorySize() {
        return new SizeValue(estimatedMemorySize.get(), SizeUnit.BYTES);
    }

    @Override public void newTranslog() {
        synchronized (mutex) {
            estimatedMemorySize.set(0);
            operations.clear();
            id = idGenerator.getAndIncrement();
        }
    }

    @Override public void add(Operation operation) throws TranslogException {
        operations.add(operation);
        estimatedMemorySize.addAndGet(operation.estimateSize() + 20);
    }

    @Override public Snapshot snapshot() {
        synchronized (mutex) {
            return new MemorySnapshot(currentId(), operations.toArray(new Operation[0]));
        }
    }

    @Override public Snapshot snapshot(Snapshot snapshot) {
        synchronized (mutex) {
            MemorySnapshot memorySnapshot = (MemorySnapshot) snapshot;
            if (currentId() != snapshot.translogId()) {
                return snapshot();
            }
            ArrayList<Operation> retVal = new ArrayList<Operation>();
            int counter = 0;
            int snapshotSize = memorySnapshot.operations.length;
            for (Operation operation : operations) {
                if (++counter > snapshotSize) {
                    retVal.add(operation);
                }
            }
            return new MemorySnapshot(currentId(), retVal.toArray(new Operation[retVal.size()]));
        }
    }

    @Override public void close() {
    }

}

@@ -1266,78 +1266,8 @@
 fe;%0A
-import org.elasticsearch.util.concurrent.jsr166y.LinkedTransferQueue;%0A
 impo
@@ -1313,16 +1313,16 @@
 Inject;%0A
+
 import o
@@ -1411,24 +1411,73 @@
 util.Queue;%0A
+import java.util.concurrent.LinkedBlockingQueue;%0A
 import java.
@@ -1861,16 +1861,293 @@
 ng id;%0A%0A
+    // we use LinkedBlockingQueue and not LinkedTransferQueue since we clear it on #newTranslog%0A    // and with LinkedTransferQueue, nodes are not really cleared, just marked causing for memory%0A    // not to be cleaned properly (besides, clear is  heavy..., %22while ... poll%22).%0A
     priv
@@ -2192,32 +2192,32 @@
 = new Linked
-Transfer
+Blocking
 Queue%3COperat

move to use LinkedBlockingQueue instead of LinkedTransferQueue so we eagerly clear memory when a new translog is created