Created
February 16, 2013 18:37
-
-
Save simbo1905/4968112 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.simbo1905.chronicle; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.higherfrequencytrading.chronicle.Chronicle; | |
import com.higherfrequencytrading.chronicle.datamodel.DataStore; | |
import com.higherfrequencytrading.chronicle.datamodel.ListWrapper; | |
import com.higherfrequencytrading.chronicle.datamodel.ModelMode; | |
import com.higherfrequencytrading.chronicle.impl.IndexedChronicle; | |
public class ProcessMaster { | |
static final String TMP = System.getProperty("java.io.tmpdir"); | |
public static void main(String[] args) throws Exception { | |
String name = TMP + "/chronicle"; | |
Chronicle chronicle = new IndexedChronicle(name); | |
DataStore dataStore = new DataStore(chronicle, ModelMode.MASTER); | |
List<String> underlying = new ArrayList<String>(); | |
int maxMessageSize = 128; | |
ListWrapper<String> list = new ListWrapper<String>(dataStore, | |
"testlist", String.class, underlying, maxMessageSize); | |
dataStore.start(); | |
for( int i = 0; i < 1000000; i++ ){ | |
list.add(0, "hello"+i); | |
list.add(1, "world"+i); | |
list.remove(0); | |
list.remove(1); // does nothing until second loop and leaves one elements | |
} | |
System.out.println("list.size(): "+list.size()); | |
System.out.println("list: "+list); | |
chronicle.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.simbo1905.chronicle; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import com.higherfrequencytrading.chronicle.Chronicle; | |
import com.higherfrequencytrading.chronicle.datamodel.DataStore; | |
import com.higherfrequencytrading.chronicle.datamodel.ListListener; | |
import com.higherfrequencytrading.chronicle.datamodel.ListWrapper; | |
import com.higherfrequencytrading.chronicle.datamodel.ModelMode; | |
import com.higherfrequencytrading.chronicle.impl.IndexedChronicle; | |
public class ProcessSlave { | |
static final String TMP = System.getProperty("java.io.tmpdir"); | |
public static void main(String[] args) throws Exception { | |
String name = TMP + "/chronicle"; | |
System.out.println("file is "+name); | |
Chronicle chronicle = new IndexedChronicle(name); | |
DataStore dataStore = new DataStore(chronicle, ModelMode.READ_ONLY); | |
List<String> underlying = new ArrayList<String>(); | |
int maxMessageSize = 128; | |
ListWrapper<String> list = new ListWrapper<String>(dataStore, | |
"testlist", String.class, underlying, maxMessageSize); | |
final AtomicInteger addCounter = new AtomicInteger(); | |
final AtomicInteger removeCounter = new AtomicInteger(); | |
list.addListener(new ListListener<String>() { | |
@Override | |
public void eventStart(long eventId, String name) { | |
} | |
@Override | |
public void eventEnd(boolean lastEvent) { | |
} | |
@Override | |
public void remove(String e) { | |
removeCounter.getAndIncrement(); | |
} | |
@Override | |
public void add(String e) { | |
addCounter.getAndIncrement(); | |
} | |
@Override | |
public void set(int index, String oldElement, String element) { | |
} | |
@Override | |
public void remove(int index, String element) { | |
removeCounter.getAndIncrement(); | |
if( removeCounter.get() == 2*1000000-1){ | |
System.out.println("notifying..."); | |
synchronized (ProcessSlave.class) { | |
ProcessSlave.class.notifyAll(); | |
} | |
} | |
} | |
@Override | |
public void add(int index, String element) { | |
addCounter.getAndIncrement(); | |
} | |
}); | |
System.out.println("starting..."); | |
dataStore.start(); | |
System.out.println("waiting..."); | |
synchronized (ProcessSlave.class) { | |
ProcessSlave.class.wait(60000); | |
} | |
System.out.println("addCounter:"+addCounter.get()); | |
System.out.println("removeCounter:"+removeCounter.get()); | |
System.out.println("list.size(): "+list.size()); | |
System.out.println("list: "+list); | |
System.out.println("The End."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demonstration of interprocess communication using peter-lawrey / Java-Chronicle. Output of ProcessMaster is:
Feb 16, 2013 6:33:25 PM com.higherfrequencytrading.chronicle.impl.IndexedChronicle
INFO: /var/folders/w4/zw1k9pbj3f34nb914b36l5gm0000gn/T//chronicle created.
list.size(): 1
list: [world999999]
Output of ProcessSlave is:
Feb 16, 2013 6:33:27 PM com.higherfrequencytrading.chronicle.impl.IndexedChronicle
INFO: /var/folders/w4/zw1k9pbj3f34nb914b36l5gm0000gn/T//chronicle, size=1189402
starting...
waiting...
notifying...
addCounter:2000000
removeCounter:1999999
list.size(): 1
list: [world999999]
The End.
Resulting filesystem residue is:
simbo$ find . -name chronicle* -exec ls -al {} ;
-rw-r--r-- 1 simbo staff 134217728 16 Feb 18:33 ./chronicle.data
-rw-r--r-- 1 simbo staff 33554432 16 Feb 18:33 ./chronicle.index
simbo$