/*
 * Copyright 2012-2015 Aerospike, Inc.
 *
 * Portions may be licensed to Aerospike, Inc. under one or more contributor
 * license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
 *
 * 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 com.aerospike.examples;

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.ScanCallback;
import com.aerospike.client.policy.ScanPolicy;

public class ScanParallel extends Example implements ScanCallback {

	private int recordCount = 0;

	public ScanParallel(Console console) {
		super(console);
	}

	/**
	 * Scan all nodes in parallel and read all records in a set.
	 */
	@Override
	public void runExample(AerospikeClient client, Parameters params) throws Exception {
		console.info("Scan parallel: namespace=" + params.namespace + " set=" + params.set);
		recordCount = 0;
		long begin = System.currentTimeMillis();
		ScanPolicy policy = new ScanPolicy();
		client.scanAll(policy, params.namespace, params.set, this);

		long end = System.currentTimeMillis();
		double seconds =  (double)(end - begin) / 1000.0;
		console.info("Total records returned: " + recordCount);
		console.info("Elapsed time: " + seconds + " seconds");
		double performance = Math.round((double)recordCount / seconds);
		console.info("Records/second: " + performance);
	}

	@Override
	public void scanCallback(Key key, Record record) {
		recordCount++;

		if ((recordCount % 10000) == 0) {
			console.info("Records " + recordCount);
		}
	}
}

@@ -781,16 +781,67 @@
 mples;%0A%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 import c
@@ -1118,11 +1118,21 @@
 ate 
-int
+AtomicInteger
  rec
@@ -1139,20 +1139,16 @@
 ordCount
- = 0
 ;%0A%0A%09publ
@@ -1476,17 +1476,35 @@
 Count = 
-0
+new AtomicInteger()
 ;%0A%09%09long
@@ -1736,16 +1736,49 @@
 1000.0;%0A
+%09%09int count = recordCount.get();%0A
 %09%09consol
@@ -1809,31 +1809,25 @@
 turned: %22 + 
-recordC
+c
 ount);%0A%09%09con
@@ -1920,23 +1920,17 @@
 (double)
-recordC
+c
 ount / s
@@ -2061,21 +2061,49 @@
 %7B%0A%09%09
-recordCount++
+int count = recordCount.incrementAndGet()
 ;%0A%0A%09
@@ -2108,23 +2108,17 @@
 %0A%09%09if ((
-recordC
+c
 ount %25 1
@@ -2160,23 +2160,17 @@
 rds %22 + 
-recordC
+c
 ount);%0A%09

Increment recordCount in a thread-safe manner in ScanParallel example.