/*
 * Copyright (c) 2013-2015 Cinchapi, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cinchapi.concourse.server.storage.temp;

import org.cinchapi.concourse.server.storage.cache.BloomFilter;

/**
 * A special {@link Queue} that is used for {@link Transaction transactions}:
 * uses a local bloom filter to make verifies more efficient.
 * 
 * @author Jeff Nelson
 */
public class TransactionQueue extends Queue {

    /**
     * The bloom filter used to speed up verifies.
     */
    private final BloomFilter bloom = BloomFilter.create(500000);

    /**
     * Construct a new instance.
     * 
     * @param initialSize
     */
    public TransactionQueue(int initialSize) {
        super(initialSize);
    }

    @Override
    public boolean insert(Write write, boolean sync) {
        if(super.insert(write, sync)) {
            bloom.putCached(write.getKey(), write.getValue(), write.getRecord());
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public boolean verify(Write write, long timestamp, boolean exists) {
        if(bloom.mightContainCached(write.getKey(), write.getValue(),
                write.getRecord())) {
            return super.verify(write, timestamp, exists);
        }
        else {
            return exists;
        }
    }

}

@@ -40,17 +40,18 @@
  Inc.%0A *
+ 
 %0A
-
  * Licen
@@ -226,13 +226,12 @@
 t%0A *
+ 
 %0A *
-  
  htt
@@ -272,16 +272,17 @@
 E-2.0%0A *
+ 
 %0A * Unle
@@ -652,16 +652,77 @@
 .temp;%0A%0A
+import org.cinchapi.concourse.server.storage.PermanentStore;%0A
 import o
@@ -1000,16 +1000,231 @@
 ueue %7B%0A%0A
+    /**%0A     * An empty array of writes that is used to specify type conversion in the%0A     * %7B@link ArrayList#toArray(Object%5B%5D)%7D method.%0A     */%0A    private static final Write%5B%5D EMPTY_WRITES_ARRAY = new Write%5B0%5D;%0A%0A
     /**%0A
@@ -1747,32 +1747,32 @@
 %0A        else %7B%0A
-
             retu
@@ -1790,32 +1790,727 @@
        %7D%0A    %7D%0A%0A
+    @Override%0A    public void transport(PermanentStore destination, boolean sync) %7B%0A        // For transactions, this method will only be called once, so we can%0A        // optimize it by not using the services of an Iterator (e.g. hasNext(),%0A        // remove(), etc) and, if the number of writes in the Queue is large%0A        // enough, grabbing elements from the backing array directly.%0A        int length = writes.size();%0A        Write%5B%5D elts = length %3E 10000 ? writes.toArray(EMPTY_WRITES_ARRAY)%0A                : null;%0A        for (int i = 0; i %3C length; ++i) %7B%0A            Write write = elts == null ? writes.get(i) : elts%5Bi%5D;%0A            destination.accept(write, sync);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

optimize transporting within TransactionQueue