<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest topics for the forum "Docs"]]></title>
		<link>http://forum.space4j.org/forums/show/2.page</link>
		<description><![CDATA[The newest discussed topics in the forum "Docs"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Yale Football Coach Resigns After Faulty Rhodes Scholarship Claim</title>
				<description><![CDATA[ <br /> <br /> <br /> NEW HAVEN, Conn. -- Yale football coach Tom Williams resigned Wednesday amid a probe by the university into whether he lied on his resume about being a candidate for a prestigious Rhodes scholarship, the New Haven Register reported.<br /> Williams said last month that when he was a senior linebacker at Stanford in 1992 he was a candidate for a Rhodes scholarship, but passed up the opportunity in favor of an NFL tryout.<br /> He spoke on the issue while Yale quarterback Patrick Witt, a Rhodes finalist, debated whether to play in the season finale against archrival Harvard or miss the game for an interview that would determine if he obtained the scholarship.<br /> <br /> Witt opted to play in the Nov. 19 game, which Yale lost 45-7.<br /> Williams, who turns 42 Thursday, listed on his resume submitted to Yale that he had been a Rhodes finalist.<br /> The university launched an inquiry after The New York Times reported that the Rhodes Trust, which finances the scholarship, had no record of Williams ever applying for the scholarship.<br /> The result of the inquiry has not been made public. Williams, in a statement announcing his decision, sought to explain the discrepancy.<br /> <br /> <br /> <br /> <br /> ]]></description>
				<guid isPermaLink="true">http://forum.space4j.org/posts/preList/94/138.page</guid>
				<link>http://forum.space4j.org/posts/preList/94/138.page</link>
				<pubDate><![CDATA[Fri, 23 Dec 2011 21:57:18]]> GMT</pubDate>
				<author><![CDATA[ shoud7]]></author>
			</item>
			<item>
				<title>Database Replication</title>
				<description><![CDATA[ We know that it is impossible to abandon your good and old relational database. You will probably need to do offline reports and heavy queries and a relational database is perfect for that. So what you need is to replicate all your Space4J commands to a relational database. To do this you can implement the executeSQL method for your commands so that you can later read the logs and execute them against a relational database.<br /> <br /> Check the method below from the [i]Command [/i]interface:<br /> <br /> [code]<br />     /**<br />      * This method will let you replicate your data in a relational database,<br />      * so you can continue creating your reports and using data data warehousing tools with SQL.<br />      * Doing this with Space4J and Commands is possible, but not recommended at all.<br />      * Note that this method is not abstract, therefore it is not required. <br />      * It is up to the system to replicate its data in a relational database.<br />      * <i>Rodolfo de Paula was the first one to raise this issue! Why not have the best of both worlds?</i><br />      * @param conn A connection to the database where this command will be executed through SQL.<br />      * @throws SQLException If there was an error executing the SQL.<br />      */<br />     public void executeSQL(Connection conn) throws SQLException {}<br /> [/code]<br /> <br /> [u]ToDo:[/u] Create a standalone program that read the logs and execute them against a JDBC connection.<br /> ]]></description>
				<guid isPermaLink="true">http://forum.space4j.org/posts/preList/7/7.page</guid>
				<link>http://forum.space4j.org/posts/preList/7/7.page</link>
				<pubDate><![CDATA[Fri, 5 Sep 2008 16:28:02]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
			<item>
				<title>Clustering</title>
				<description><![CDATA[ Clustering can be useful for load-balance and fault-tolerance. It also allows a snapshot to be taken without placing the whole system in read-only mode. That's because you can use a slave to take snapshot while the rest of the nodes continue to run like nothing as happened, in other words, only the slave taking the snapshot needs to enter read-only mode.<br /> <br /> To create a Space4J cluster is very easy and simple. Refer to the [url="http://www.space4j.org/phonebookcluster.jsp"]PhoneBookCluster.java[/url] program for an example. We also have a [url="http://s4j.mentaframework.org/posts/list/4.page"]stress program[/url] to test the cluster feature.<br /> <br /> The source code for PhoneBookCluster.java is below:<br /> [code]<br /> package org.space4j.examples.phonebook;<br /> <br /> <br /> import org.space4j.Space4J;<br /> import org.space4j.implementation.MasterSpace4J;<br /> import org.space4j.implementation.SlaveSpace4J;<br /> <br /> <br /> /**<br />  * This is to show you how simple it is to make a Space4J cluster!<br />  * <br />  * To make a cluster, open up two shells (or DOS) and execute in the first:<br />  * <br />  * java org.space4j.demos.phonebook.PhoneBookCluster master<br />  * <br />  * and in the second:<br />  * <br />  * java org.space4j.demos.PhoneBookCluster slave 127.0.0.1<br />  * <br />  * You can also use two different machines. Just pass the master IP address to the slave instead of 127.0.0.1.<br />  * IMPORTANT: If you use two different machines, you must have the main Space4J dir (space4j_db) mounted, so <br />  * both machines can have access to it.<br />  */<br /> public class PhoneBookCluster extends PhoneBookIndexing {<br />     <br />     public PhoneBookCluster(Space4J space4j) throws Exception {<br />     	<br />         super(space4j);<br />     	<br />     }<br />     <br />     static Space4J createSpace4J(String master, String master_ip) throws Exception {<br />     	<br />         Space4J space4j = null;<br />     	<br />         if (master.equals("-master")) {<br />         	<br />             space4j = new MasterSpace4J("PhoneBook");<br />         	<br />         } else if (master.equals("-slave")) {<br />         	<br />             if (master_ip != null) {<br />             	<br />                 space4j = new SlaveSpace4J("PhoneBook", master_ip);<br />             	<br />             } else {<br />             	<br />                 space4j = new SlaveSpace4J("PhoneBook", "127.0.0.1");<br />             	<br />             }<br />         }<br />         <br />         return space4j;<br />     }<br />     <br />     public static void main(String[] args) throws Exception {<br />     	<br />         if (args.length &lt; 1 || args.length &gt; 2) {<br />     		<br />             System.out.println(<br />                     "format: java PhoneBookCluster [-master|-slave] &lt;IP def: 127.0.0.1&gt;");<br />     		<br />             return;<br />         }<br />     	<br />         Space4J space4j = createSpace4J(args[0],<br />                 args.length &gt; 1 ? args[1] : null);<br />     	<br />         PhoneBookCluster book = new PhoneBookCluster(space4j);<br />         <br />         run(book);<br />     }<br /> }<br /> [/code]<br /> <br /> <br /> <br /> ]]></description>
				<guid isPermaLink="true">http://forum.space4j.org/posts/preList/6/6.page</guid>
				<link>http://forum.space4j.org/posts/preList/6/6.page</link>
				<pubDate><![CDATA[Fri, 5 Sep 2008 16:24:35]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
			<item>
				<title>Indexing</title>
				<description><![CDATA[ When your collection becomes too big, even in-memory searches can become expensive. Imagine a Java collection with 1 million users and you need to find all users with the last name equals to "Smith". Iterating through all elements is clearly not an option. What you need is INDEXES.<br /> <br /> Space4J supports 4 different types of indexes through maps, in other words, when you create an index a new Java Map is created to store the index.<br /> <br /> The four types of index are:<br /> <br /> :arrow: Index.TYPE.REGULAR = Unique index non-sorted. This is used to get an user by its id for example, where each user has an unique id.<br /> [code]<br /> User u = usersById.get(2345);<br /> [/code]<br /> <br /> :arrow: Index.TYPE.SORTED = Unique index sorted. This is used to get a list of users in a range of ids (ex: 4000 &lt; id &lt; 5000). Also used when you need to fetch users sorted by the indexed field.<br /> [code]<br /> Map&lt;Integer, User&gt; map = usersById.subMap(4000, 5000);<br /> <br /> // or to get all the values<br /> <br /> Collection&lt;User&gt; users = usersById.subMap(4000, 5000).values();<br /> [/code]<br /> <br /> :arrow: Index.TYPE.MULTI = Non-unique index non-sorted. This is used to get all users with age equal to 23.<br /> [code]<br /> Set&lt;User&gt; users = usersByAge.get(23);<br /> [/code]<br /> <br /> :arrow: Index.TYPE.MULT_SORTED = Non-unique index sorted. This is used to get all users with age &gt;= 23 and age &lt;= 40. Also used when you need to fech users sorted by the indexed field.<br /> [code]<br /> Map&lt;Integer, Set&gt;&lt;User&gt;&gt; map = usersByAge.subMap(23, 40);<br /> <br /> // or to get a collection with all the values<br /> <br /> Collection&lt;User&gt; users = usersByAge.subMapValues(23, 40);<br /> [/code]<br /> <br /> The [url="http://www.space4j.org/phonebookindex.jsp"]PhoneBookIndexing.java[/url] program provides examples of how to create and user indexes for your collections in memory. Below is the source code:<br /> [code]<br /> package org.space4j.examples.phonebook;<br /> <br /> <br /> import java.util.Collection;<br /> <br /> import org.space4j.Space4J;<br /> import org.space4j.implementation.SimpleSpace4J;<br /> import org.space4j.indexing.Index;<br /> import org.space4j.indexing.IndexManager;<br /> import org.space4j.indexing.Key;<br /> import org.space4j.indexing.MultiMap;<br /> import org.space4j.indexing.MultiSortedMap;<br /> <br /> public class PhoneBookIndexing extends PhoneBook {<br /> 	<br />     private static final String INDX_LASTNAME = "indx_lastname";<br />     <br />     private static final String INDX_AGE = "indx_age";<br /> 	<br />     private final Index&lt;User&gt; indexByLastName;<br />     <br />     private final Index&lt;User&gt; indexByAge;<br /> 	<br />     public PhoneBookIndexing(Space4J space4j) throws Exception {<br />     	<br />         super(space4j);<br />     	<br />         IndexManager im = space.getIndexManager();<br />     	<br />         // If index does not exist, let's create it...<br />         if (!im.checkIndex(INDX_LASTNAME)) {<br />             <br />             /*<br />              * Index name = INDX_LASTNAME<br />              * Map in the Space that will be indexed = MAP_NAME<br />              * Type of index = MULTI = non-unique index<br />              * Object that will be indexed = User.class<br />              * Atributes of the object that will be indexed = "lastName"<br />              * <br />              * We want to create this index because we will be searching<br />              * for Users by lastName. We don't want to do a full table<br />              * scan anymore like in the example without indexes.<br />              */<br />         	<br />             Index&lt;User&gt; indx = new Index&lt;User&gt;(INDX_LASTNAME, MAP_NAME, Index.TYPE.MULTI, User.class, "lastName");<br />         	<br />             boolean ok = im.createIndex(indx, space4j);<br />             <br />             if (ok) {<br />             	<br />                 System.out.println(indx + " created!");<br />             	<br />             } else {<br />             	<br />                 System.out.println(indx + " could not be created!");<br />             }<br />         }<br />         <br />         // If index does not exist, let's create it...<br />         if (!im.checkIndex(INDX_AGE)) {<br />             <br />             /*<br />              * Index name = INDX_AGE<br />              * Map in the Space that will be indexed = MAP_NAME<br />              * Type of index = MULTI = non-unique sorted index<br />              * Object that will be indexed = User.class<br />              * Atributes of the object that will be indexed = "age"<br />              * <br />              * We want to create this index because we will be searching<br />              * for Users by age. The index will be SORTED because we want<br />              * to search for users in a age range. (age &gt; min and age &lt; max)<br />              */<br />          <br />             Index&lt;User&gt; indx = new Index&lt;User&gt;(INDX_AGE, MAP_NAME, Index.TYPE.MULTI_SORTED, User.class, "age");<br />          <br />             boolean ok = im.createIndex(indx, space4j);<br />             <br />             if (ok) {<br />                <br />                 System.out.println(indx + " created!");<br />                <br />             } else {<br />                <br />                 System.out.println(indx + " could not be created!");<br />             }<br />         }<br /> <br />         <br />         indexByLastName = im.getIndex(INDX_LASTNAME);<br />         indexByAge = im.getIndex(INDX_AGE);<br />     }<br />     <br />     /*<br />      * Let's override our search method to use our<br />      * index instead of doing a full table scan.<br />      */<br />     @Override<br />     public Collection&lt;User&gt; findUsers(String last) {<br /> 	<br />     	// Remember that our index is of the type MULTI.<br />     	<br />         MultiMap&lt;User&gt; map = indexByLastName.getMap();<br />         <br />         // A multi-map will store a Set for each key.<br />         // That's why our index is NON-UNIQUE meaning the same<br />         // key (lastName) can be associated with different entries in the map.<br />         // That makes sense as some users can have the same last name.<br />     	<br />        return map.get(last);<br />     }<br />     <br />     @Override<br />     public Collection&lt;User&gt; findUsers(int minAge, int maxAge) {<br />        <br />        // Remember that our index is of the type MULTI_SORTED.<br />        <br />        MultiSortedMap&lt;User&gt; map = indexByAge.getMap();<br />        <br />        // A multi-sorted-map will store a Set for each key.<br />        // That's why our index is NON-UNIQUE meaning the same<br />        // key (minAge, maxAge) can be associated with different entries in the map.<br />        // The map is also sorted by its key, meaning we can<br />        // perform range searches efficiently like below.<br />        <br />        return map.subMapValues(new Key(minAge), true, new Key(maxAge), true);<br />     }<br />     <br />     public static void main(String[] args) throws Exception {<br />     	<br />         PhoneBookIndexing book = new PhoneBookIndexing(<br />                 new SimpleSpace4J("PhoneBook"));<br /> <br />         run(book);<br />     	<br />     }<br /> }<br /> [/code]<br /> ]]></description>
				<guid isPermaLink="true">http://forum.space4j.org/posts/preList/5/5.page</guid>
				<link>http://forum.space4j.org/posts/preList/5/5.page</link>
				<pubDate><![CDATA[Fri, 5 Sep 2008 16:13:04]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
			<item>
				<title>Cluster Stress Test</title>
				<description><![CDATA[ To stress test Space4J's cluster implementation, let's place a master node and a slave node under stress doing many reads and updates. Each node will have a predefined number of threads doing insert, delete, search and iteration at the same time in the Java collection in memory. A snapshot will also be taken from time to time by the slave node.<br /> <br /> You can download the [i]PhoneBookClusterStress.java[/i] program by [url="http://www.space4j.org/examples/PhoneBookClusterStress.java"]clicking here[/url]. The source code is also listed below. (Note that to compile and run this problem you must include the space4.jar in the classpath) <br /> <br /> To run this example you need to open two shell/dos windows, one for the master node and one for the slave node. You can also run each node in a separate machine over the network.<br /> <br /> The arguments you must pass to the each program are:<br /> <br /> :arrow: -master or -slave = to indicate if this node is the master or the slave<br /> :arrow: IP address of the master<br /> :arrow: number of insert threads<br /> :arrow: number of delete threads<br /> :arrow: number of select threads<br /> :arrow: number of search threads<br /> :arrow: thread sleep time = how long each thread will sleep after each action<br /> :arrow: collection initial size = how many elements should the collection has before the threads start<br /> :arrow: snapshot time = how often should we take a snapshot <br /> <br /> After executing the programs for some time, hit CTRL+C to print the statistics. Here is an example: <br /> <br /> [u]Master Program:[/u]<br /> [code]<br /> java -cp .;space4j.jar org.space4j.examples.phonebook.PhoneBookClusterStress -master 127.0.0.1 5 4 10 5 50 2000 1000<br /> ...<br /> ...<br /> ...<br /> Selected 302 records!<br /> Found 14 recods!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Found 24 recods!<br /> Found 21 recods!<br /> Found 23 recods!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Inserted record number 5004<br /> Inserted record number 5005<br /> Deleted record number 2489<br /> Inserted record number 5006<br /> Deleted record number 4974<br /> Deleted record number 655<br /> Deleted record number 4926<br /> Inserted record number 5007<br /> <br /> <br /> ========== Results:<br /> Inserted = 2487<br /> Deleted  = 2041<br /> Selected = 2334460<br /> Searched = 86698<br /> Snapshots= 0<br /> Time: 39265 ms<br /> [/code]<br /> [u]Slave Program:[/u]<br /> [code]<br /> java -cp .;space4j.jar org.space4j.examples.phonebook.PhoneBookClusterStress -slave 127.0.0.1 5 4 10 5 50 2000 1000<br /> ...<br /> ...<br /> Inserted record number 13273<br /> Deleted record number 3267<br /> Deleted record number 6578<br /> Found 31 recods!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Found 28 recods!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Found 41 recods!<br /> Selected 302 records!<br /> Found 24 recods!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Found 29 recods!<br /> Selected 302 records!<br /> Executing snapshot...<br /> =============================&gt; Snapshot Complete!<br /> <br /> <br /> ========== Results:<br /> Inserted = 30<br /> Deleted  = 32<br /> Selected = 45300<br /> Searched = 2558<br /> Snapshots= 3<br /> Time: 781 ms<br /> <br /> <br /> [/code]<br /> The source code of the [i]PhoneBookClusterStress.java[/i] program:<br /> [code]<br /> package org.space4j.examples.phonebook;<br /> <br /> <br /> import java.util.Collection;<br /> import java.util.Iterator;<br /> import java.util.Random;<br /> <br /> import org.space4j.Space4J;<br /> <br /> <br /> public class PhoneBookClusterStress extends PhoneBookCluster {<br /> 	<br />     private final Thread[] insertThread;<br /> 	<br />     private final Thread[] deleteThread;<br /> 	<br />     private final Thread[] selectThread;<br /> 	<br />     private final Thread[] searchThread;<br /> 	<br />     private Thread snapshotThread;<br />     <br />     private Random rand = new Random();<br />     <br />     private volatile long totalInserted = 0;<br />     private volatile long totalDeleted = 0;<br />     private volatile long totalSelected = 0;<br />     private volatile long totalSearched = 0;<br />     private volatile long snapshotsTaken = 0;<br />     <br />     private long started;<br />     <br />     private volatile boolean running = true;<br /> 	<br />     public PhoneBookClusterStress(Space4J space4j, final int nInsert, final int nDelete, final int nSelect, final int nSearch,<br />             final int sleepTime, final int tableSize, final int snapshotTime) throws Exception {<br /> 		<br />         super(space4j);<br /> 		<br />         // populate the table if needed...<br /> 		<br />         int size = users.size();<br /> 		<br />         if (size &lt; tableSize) {<br /> 			<br />             int diff = tableSize - size;<br /> 			<br />             for (int i = 0; i &lt; diff; i++) {<br /> 				<br />                 insertRandom();<br /> 				<br />                 System.out.print("\rInserting " + i + "...                 ");<br />             }<br />         }<br /> 		<br />         System.out.println();<br /> 		<br />         insertThread = new Thread[nInsert];<br /> 		<br />         deleteThread = new Thread[nDelete];<br /> 		<br />         selectThread = new Thread[nSelect];<br /> 		<br />         searchThread = new Thread[nSearch];<br /> 		                         <br />         for (int i = 0; i &lt; nInsert; i++) {<br />             insertThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleInsert();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         for (int i = 0; i &lt; nDelete; i++) {<br />             deleteThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleDelete();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         for (int i = 0; i &lt; nSelect; i++) {<br />             selectThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleSelect();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         for (int i = 0; i &lt; nSearch; i++) {<br />             searchThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleSearch();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br />         <br />         if (snapshotTime &gt; 0) {<br /> 		<br /> 	        snapshotThread = new Thread(<br /> 	                new Runnable() {<br /> 				<br /> 	            public void run() {<br /> 					<br /> 	                while (running) {<br /> 						<br /> 	                    try {<br /> 							<br /> 	                        Thread.sleep(snapshotTime);<br /> 							<br /> 	                        System.out.println("Executing snapshot...");<br /> 							<br /> 	                        PhoneBookClusterStress.this.executeSnapshot();<br /> 							<br /> 	                        System.out.println(<br /> 	                                &quot;=============================&gt; Snapshot Complete!&quot;);<br /> 	                        <br /> 	                        snapshotsTaken++;<br /> 							<br /> 	                    } catch (Exception e) {<br /> 							<br /> 	                        return;<br /> 	                    }<br /> 	                }<br /> 	            }<br /> 				<br /> 	        });<br />         }<br />     }<br /> 	<br />     public void start() {<br /> 		<br />         for (int i = 0; i &lt; insertThread.length; i++) {<br />             insertThread[i].start();<br />         }<br /> 		<br />         for (int i = 0; i &lt; deleteThread.length; i++) {<br />             deleteThread[i].start();<br />         }<br /> 		<br />         for (int i = 0; i &lt; selectThread.length; i++) {<br />             selectThread[i].start();<br />         }<br /> 		<br />         for (int i = 0; i &lt; searchThread.length; i++) {<br />             searchThread[i].start();<br />         }<br /> 		<br />         snapshotThread.start();<br />     }<br /> 	<br />     protected int insertRandom() throws Exception {<br />     	<br />     	// insert a random phone number...<br /> 		<br />         StringBuilder sb = new StringBuilder();<br />         <br />         sb.append("Oliveira").append(rand.nextInt(100) + 1);<br /> 		<br />         return this.addNumber("Sergio", sb.toString(), "123-4321", rand.nextInt(100) + 1);<br />     }<br /> 	<br />     protected int deleteRandom() throws Exception {<br />     	<br />     	// delete a random phone number<br />     	// (guess a number, iterate to choose a phone and delete it) <br /> <br />         Iterator iter = users.keySet().iterator();<br /> 		<br />         int count = rand.nextInt(users.size() / 10) + 1;<br /> 		<br />         int index = 0;<br /> 		<br />         int id = -1;<br /> 		<br />         while (iter.hasNext() && index++ &lt;= count) {<br /> 			<br />             id = (Integer) iter.next();<br /> 			<br />         }<br /> 		<br />         if (id &gt; 0) {<br /> 			<br />             this.delUser(id);<br /> 			<br />         }<br /> 		<br />         return id;<br />     }<br /> 	<br />     private void handleInsert() throws Exception {<br /> 		<br />         int id = insertRandom();<br /> 		<br />         System.out.println("Inserted record number " + id);<br />         <br />         totalInserted++;<br />     }<br /> 	<br />     private void handleDelete() throws Exception {<br /> 		<br />         int id = deleteRandom();<br /> 		<br />         if (id == -1) {<br /> 			<br />             System.out.println("Could not delete anything!");<br /> 			<br />         } else {<br /> 			<br />             System.out.println("Deleted record number " + id);<br />             <br />             totalDeleted++;<br />         }<br />     }<br /> 	<br />     private void handleSelect() {<br />     	<br />     	// select any 300 phone numbers and loop through them...<br /> 		<br />         Iterator iter = users.values().iterator();<br /> 		<br />         int count = 0;<br /> 		<br />         while (iter.hasNext() && count++ &lt;= 300) {<br /> 			<br />             iter.next();<br />         }<br /> 		<br />         System.out.println("Selected " + count + " records!");<br />         <br />         totalSelected += count;<br />     }<br /> <br />     private void handleSearch() {<br />     	<br />     	// choose a user and try to find his phone number...<br />     	<br />     	StringBuilder sb= new StringBuilder();<br /> 		<br />         sb.append("Oliveira").append(rand.nextInt(100) + 1);<br /> 		<br />         Collection coll = this.findUsers(sb.toString());<br /> 		<br />         int count = 0;<br /> 		<br />         Iterator iter = coll.iterator();<br /> 		<br />         while (iter.hasNext()) {<br /> 			<br />             count++;<br /> 			<br />             iter.next();<br />         }<br /> 		<br />         System.out.println("Found " + count + " recods!");<br />         <br />         totalSearched += count;<br /> 		<br />     }<br />     <br />     private void interruptAll() {<br />     	<br />     	for(int i=0;i&lt;insertThread.length;i++) insertThread[i].interrupt();&gt;<br />     	<br />     	for(int i=0;i&lt;deleteThread.length;i++) deleteThread[i].interrupt();&gt;<br />     	<br />     	for(int i=0;i&lt;selectThread.length;i++) selectThread[i].interrupt();&gt;<br />     	<br />     	for(int i=0;i&lt;searchThread.length;i++) searchThread[i].interrupt();&gt;<br />     }<br /> 	<br />     public static void main(String[] args) throws Exception {<br /> 		<br />         if (args.length &gt;= 3) {<br /> 			<br />             if (args[2].equals("-nostress")) {<br /> 				<br />                 Space4J space4j = createSpace4J(args[0], args[1]);<br /> 					<br />                 PhoneBookCluster phone = new PhoneBookCluster(space4j);<br /> 				<br />                 run(phone);<br /> 				<br />                 return;<br />             }<br /> 			<br />         }<br /> 		<br />         if (args.length != 9) {<br /> 			<br />             System.out.println(<br />                     "format: java PhoneBookClusterStress [-master|-slave] &lt;ip&gt; [-nostress] &lt;number of insert threads&gt; &lt;number of delete threads&gt; "<br />                             + "&lt;number of select threads&gt; &lt;number of search threads&gt; &lt;thread sleep time&gt; &lt;table size&gt; &lt;snapshot time&gt;");<br /> 			<br />             return;<br />         }<br /> 		<br />         int nInsert, nDelete, nSelect, nSearch, sleepTime, tableSize, snapTime;<br /> 		<br />         nInsert = Integer.parseInt(args[2]);<br /> 		<br />         nDelete = Integer.parseInt(args[3]);<br /> 		<br />         nSelect = Integer.parseInt(args[4]);<br /> 		<br />         nSearch = Integer.parseInt(args[5]);<br /> 		<br />         sleepTime = Integer.parseInt(args[6]);<br /> 		<br />         tableSize = Integer.parseInt(args[7]);<br /> 		<br />         snapTime = Integer.parseInt(args[8]);<br />         <br />         // let the slaves take the snapshot<br />         if (args[0].equals("-master")) {<br />         	snapTime = 0;<br />         }<br /> 		<br />         Space4J space4j = createSpace4J(args[0], args[1]);<br /> 		<br />         final PhoneBookClusterStress stress = new PhoneBookClusterStress(space4j,<br />                 nInsert, nDelete, nSelect, nSearch, sleepTime, tableSize,<br />                 snapTime);<br />         <br />         stress.started = System.currentTimeMillis();<br />         <br />         Runtime.getRuntime().addShutdownHook(new Thread() {<br /> <br />             @Override<br />             public void run() {<br />             	<br />             	long totalInserted = stress.totalInserted;<br />             	long totalDeleted = stress.totalDeleted;<br />             	long totalSelected = stress.totalSelected;<br />             	long totalSearched = stress.totalSearched;<br />             	long snapshotsTaken = stress.snapshotsTaken;<br />             	long now = System.currentTimeMillis();<br />             	<br />             	stress.running = false;<br />             	<br />             	stress.interruptAll();<br />             	<br />             	try { Thread.sleep(1500); } catch(Exception e) { }<br />             	<br />             	System.out.println("\n\n========== Results:");<br />             	System.out.println("Inserted = " + totalInserted);<br />             	System.out.println("Deleted  = " + totalDeleted);<br />             	System.out.println("Selected = " + totalSelected);<br />             	System.out.println("Searched = " + totalSearched);<br />             	System.out.println("Snashopts= " + snapshotsTaken);<br />             	System.out.println("Time: " + (now - stress.started) + " ms");<br />             	<br />             }<br />         });<br /> 		<br />         stress.start();<br />     }<br /> 	<br /> }<br /> [/code]<br /> <br /> ]]></description>
				<guid isPermaLink="true">http://forum.space4j.org/posts/preList/4/4.page</guid>
				<link>http://forum.space4j.org/posts/preList/4/4.page</link>
				<pubDate><![CDATA[Fri, 5 Sep 2008 15:33:08]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
			<item>
				<title>Stress Test</title>
				<description><![CDATA[ To prove how fast and highly multithreaded Space4J is, we implemented a stress test program that creates many threads to perform insertion, deletion, search and iteration at the same time in the same indexed Java collection in memory.<br /> <br /> You can download the [i]PhoneBookStress.java[/i] program by [url="http://www.space4j.org/examples/PhoneBookStress.java"]clicking here[/url]. The source code is also listed below. (Note that to compile and run this problem you must include the space4.jar in the classpath)<br /> <br /> The arguments you must pass to the program are:<br /> <br /> :arrow: number of insert threads<br /> :arrow: number of delete threads<br /> :arrow: number of select threads<br /> :arrow: number of search threads<br /> :arrow: thread sleep time = how long each thread will sleep after each action<br /> :arrow: collection initial size = how many elements should the collection has before the threads start<br /> :arrow: snapshot time = how often should we take a snapshot<br /> <br /> After executing the program for some time, hit CTRL+C to print the statistics. Here is an example:<br /> [code]<br /> java -cp .;space4j.jar org.space4j.examples.phonebook.PhoneBookStress 5 4 10 5 50 2000 1000<br /> ...<br /> ...<br /> ...<br /> Found 29 recods!<br /> Found 28 recods!<br /> Found 27 recods!<br /> Selected 302 records!<br /> Selected 302 records!<br /> Found 20 recods!<br /> Selected 302 records!<br /> Found 22 recods!<br /> =============================&gt; Snapshot Complete!<br /> Deleted record number 575<br /> Deleted record number 1224<br /> Deleted record number 675<br /> Inserted record number 3020<br /> Inserted record number 3019<br /> Inserted record number 3018<br /> Deleted record number 575<br /> Inserted record number 3017<br /> Inserted record number 3021<br /> <br /> <br /> ========== Results:<br /> Inserted = 60<br /> Deleted  = 54<br /> Selected = 79426<br /> Searched = 2867<br /> Snapshots=6<br /> Time: 1375 ms<br /> [/code]<br /> <br /> The source code of the [i]PhoneBookStress.java[/i] program:<br /> [code]<br /> package org.space4j.examples.phonebook;<br /> <br /> <br /> import java.util.Collection;<br /> import java.util.Iterator;<br /> import java.util.Random;<br /> <br /> import org.space4j.Space4J;<br /> import org.space4j.implementation.SimpleSpace4J;<br /> <br /> <br /> public class PhoneBookStress extends PhoneBookIndexing {<br /> 	<br />     private final Thread[] insertThread;<br /> 	<br />     private final Thread[] deleteThread;<br /> 	<br />     private final Thread[] selectThread;<br /> 	<br />     private final Thread[] searchThread;<br /> 	<br />     private final Thread snapshotThread;<br />     <br />     private Random rand = new Random();<br />     <br />     private volatile long totalInserted = 0;<br />     private volatile long totalDeleted = 0;<br />     private volatile long totalSelected = 0;<br />     private volatile long totalSearched = 0;<br />     private volatile long snapshotsTaken = 0;<br />     <br />     private long started;<br />     <br />     private volatile boolean running = true;<br /> 	<br />     public PhoneBookStress(Space4J space4j, final int nInsert, final int nDelete, final int nSelect, final int nSearch,<br />             final int sleepTime, final int tableSize, final int snapshotTime) throws Exception {<br /> 		<br />         super(space4j);<br /> 		<br />         // populate the table if needed...<br /> 		<br />         int size = users.size();<br /> 		<br />         if (size &lt; tableSize) {<br /> 			<br />             int diff = tableSize - size;<br /> 			<br />             for (int i = 0; i &lt; diff; i++) {<br /> 				<br />                 insertRandom();<br /> 				<br />                 System.out.print("\rInserting " + i + "...                 ");<br />             }<br />         }<br /> 		<br />         System.out.println();<br /> 		<br />         insertThread = new Thread[nInsert];<br /> 		<br />         deleteThread = new Thread[nDelete];<br /> 		<br />         selectThread = new Thread[nSelect];<br /> 		<br />         searchThread = new Thread[nSearch];<br /> 		                         <br />         for (int i = 0; i &lt; nInsert; i++) {<br />             insertThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleInsert();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         for (int i = 0; i &lt; nDelete; i++) {<br />             deleteThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleDelete();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         for (int i = 0; i &lt; nSelect; i++) {<br />             selectThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleSelect();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         for (int i = 0; i &lt; nSearch; i++) {<br />             searchThread[i] = new Thread(new Runnable() {<br /> 			<br />                 public void run() {<br /> 				<br />                     while (running) {<br /> 					<br />                         try {<br /> 						<br />                             Thread.sleep(sleepTime);<br /> 						<br />                             handleSearch();<br /> 						<br />                         } catch (Exception e) {<br /> 						<br />                             return;<br />                         }<br />                     }<br />                 }<br /> 			<br />             });<br />         }<br /> 		<br />         snapshotThread = new Thread(<br />                 new Runnable() {<br /> 			<br />             public void run() {<br /> 				<br />                 while (running) {<br /> 					<br />                     try {<br /> 						<br />                         Thread.sleep(snapshotTime);<br /> 						<br />                         System.out.println("Executing snapshot...");<br /> 						<br />                         PhoneBookStress.this.executeSnapshot();<br /> 						<br />                         System.out.println(<br />                                 &quot;=============================&gt; Snapshot Complete!&quot;);<br />                         <br />                         snapshotsTaken++;<br /> 						<br />                     } catch (Exception e) {<br /> 						<br />                         return;<br />                     }<br />                 }<br />             }<br /> 			<br />         });		<br />     }<br /> 	<br />     public void start() {<br /> 		<br />         for (int i = 0; i &lt; insertThread.length; i++) {<br />             insertThread[i].start();<br />         }<br /> 		<br />         for (int i = 0; i &lt; deleteThread.length; i++) {<br />             deleteThread[i].start();<br />         }<br /> 		<br />         for (int i = 0; i &lt; selectThread.length; i++) {<br />             selectThread[i].start();<br />         }<br /> 		<br />         for (int i = 0; i &lt; searchThread.length; i++) {<br />             searchThread[i].start();<br />         }<br /> 		<br />         snapshotThread.start();<br />     }<br /> 	<br />     protected int insertRandom() throws Exception {<br />     	<br />     	// insert a random phone number...<br /> 		<br />         StringBuilder sb = new StringBuilder();<br />         <br />         sb.append("Oliveira").append(rand.nextInt(100) + 1);<br /> 		<br />         return this.addNumber("Sergio", sb.toString(), "123-4321", rand.nextInt(100) + 1);<br />     }<br /> 	<br />     protected int deleteRandom() throws Exception {<br />     	<br />     	// delete a random phone number<br />     	// (guess a number, iterate to choose a phone and delete it) <br /> <br />         Iterator iter = users.keySet().iterator();<br /> 		<br />         int count = rand.nextInt(users.size() / 10) + 1;<br /> 		<br />         int index = 0;<br /> 		<br />         int id = -1;<br /> 		<br />         while (iter.hasNext() && index++ &lt;= count) {<br /> 			<br />             id = (Integer) iter.next();<br /> 			<br />         }<br /> 		<br />         if (id &gt; 0) {<br /> 			<br />             this.delUser(id);<br /> 			<br />         }<br /> 		<br />         return id;<br />     }<br /> 	<br />     private void handleInsert() throws Exception {<br /> 		<br />         int id = insertRandom();<br /> 		<br />         System.out.println("Inserted record number " + id);<br />         <br />         totalInserted++;<br />     }<br /> 	<br />     private void handleDelete() throws Exception {<br /> 		<br />         int id = deleteRandom();<br /> 		<br />         if (id == -1) {<br /> 			<br />             System.out.println("Could not delete anything!");<br /> 			<br />         } else {<br /> 			<br />             System.out.println("Deleted record number " + id);<br />             <br />             totalDeleted++;<br />         }<br />     }<br /> 	<br />     private void handleSelect() {<br />     	<br />     	// select any 300 phone numbers and loop through them...<br /> 		<br />         Iterator iter = users.values().iterator();<br /> 		<br />         int count = 0;<br /> 		<br />         while (iter.hasNext() && count++ &lt;= 300) {<br /> 			<br />             iter.next();<br />         }<br /> 		<br />         System.out.println("Selected " + count + " records!");<br />         <br />         totalSelected += count;<br />     }<br /> <br />     private void handleSearch() {<br />     	<br />     	// choose a user and try to find his phone number...<br />     	<br />     	StringBuilder sb= new StringBuilder();<br /> 		<br />         sb.append("Oliveira").append(rand.nextInt(100) + 1);<br /> 		<br />         Collection coll = this.findUsers(sb.toString());<br /> 		<br />         int count = 0;<br /> 		<br />         Iterator iter = coll.iterator();<br /> 		<br />         while (iter.hasNext()) {<br /> 			<br />             count++;<br /> 			<br />             iter.next();<br />         }<br /> 		<br />         System.out.println("Found " + count + " recods!");<br />         <br />         totalSearched += count;<br /> 		<br />     }<br />     <br />     private void interruptAll() {<br />     	<br />     	for(int i=0;i&lt;insertThread.length;i++) insertThread[i].interrupt();&gt;<br />     	<br />     	for(int i=0;i&lt;deleteThread.length;i++) deleteThread[i].interrupt();&gt;<br />     	<br />     	for(int i=0;i&lt;selectThread.length;i++) selectThread[i].interrupt();&gt;<br />     	<br />     	for(int i=0;i&lt;searchThread.length;i++) searchThread[i].interrupt();&gt;<br />     }<br /> 	<br />     public static void main(String[] args) throws Exception {<br /> 		<br />         if (args.length &gt;= 1) {<br /> 			<br />             if (args[0].equals("-nostress")) {<br /> 				<br />                 PhoneBook book = new PhoneBook(new SimpleSpace4J("PhoneBook"));<br /> 				<br />                 run(book);<br /> 				<br />                 return;<br />             }<br /> 				<br />         }<br /> 		<br />         if (args.length != 7) {<br /> 			<br />             System.out.println(<br />                     "format: java PhoneBookStress [-nostress] &lt;number of insert threads&gt; &lt;number of delete threads&gt; "<br />                             + "&lt;number of select threads&gt; &lt;number of search threads&gt; &lt;thread sleep time&gt; &lt;collection initial size&gt; &lt;snapshot time&gt;");<br /> 			<br />             return;<br />         }<br /> 		<br />         int nInsert, nDelete, nSelect, nSearch, sleepTime, tableSize, snapTime;<br /> 		<br />         nInsert = Integer.parseInt(args[0]);<br /> 		<br />         nDelete = Integer.parseInt(args[1]);<br /> 		<br />         nSelect = Integer.parseInt(args[2]);<br /> 		<br />         nSearch = Integer.parseInt(args[3]);<br /> 		<br />         sleepTime = Integer.parseInt(args[4]);<br /> 		<br />         tableSize = Integer.parseInt(args[5]);<br /> 		<br />         snapTime = Integer.parseInt(args[6]);<br /> 		<br />         final PhoneBookStress stress = new PhoneBookStress(<br />                 new SimpleSpace4J("PhoneBook"), nInsert, nDelete, nSelect,<br />                 nSearch, sleepTime, tableSize, snapTime);<br /> 		<br />         stress.started = System.currentTimeMillis();<br />         <br />         Runtime.getRuntime().addShutdownHook(new Thread() {<br /> <br />             @Override<br />             public void run() {<br />             	<br />             	long totalInserted = stress.totalInserted;<br />             	long totalDeleted = stress.totalDeleted;<br />             	long totalSelected = stress.totalSelected;<br />             	long totalSearched = stress.totalSearched;<br />             	long snapshotsTaken = stress.snapshotsTaken;<br />             	long now = System.currentTimeMillis();<br />             	<br />             	stress.running = false;<br />             	<br />             	stress.interruptAll();<br />             	<br />             	try { Thread.sleep(1500); } catch(Exception e) { }<br />             	<br />             	System.out.println("\n\n========== Results:");<br />             	System.out.println("Inserted = " + totalInserted);<br />             	System.out.println("Deleted  = " + totalDeleted);<br />             	System.out.println("Selected = " + totalSelected);<br />             	System.out.println("Searched = " + totalSearched);<br />             	System.out.println("Snashopts= " + snapshotsTaken);<br />             	System.out.println("Time: " + (now - stress.started) + " ms");<br />             	<br />             }<br />         });<br />         <br />         stress.start();<br />     }<br /> 	<br /> }<br /> <br /> [/code]<br /> <br /> ]]></description>
				<guid isPermaLink="true">http://forum.space4j.org/posts/preList/3/3.page</guid>
				<link>http://forum.space4j.org/posts/preList/3/3.page</link>
				<pubDate><![CDATA[Fri, 5 Sep 2008 14:44:15]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>
