Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.mapdb;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Test;
  
  import java.io.*;
  import java.util.Iterator;
  import java.util.UUID;
https://github.com/jankotek/MapDB/issues/41

Author(s):
Laurent Pellegrino TODO fully investigate this concurrent issue.
 
 public class Issue41Test {
 
     private static int NB_OPERATIONS = 1000;
 
     private File DB_PATH = UtilsTest.tempDbFile();
 
     private static String MAP_NAME = "mymap";
 
     private DB db;
 
     private HTreeMap<KeyValuemap;
 
     private ExecutorService threadPool;
 
     private CountDownLatch doneSignal;
 
     @Before
     public void setUp() {
          =
                 DBMaker.newFileDB()
                         .cacheSoftRefEnable()
                         .closeOnJvmShutdown()
                         .deleteFilesAfterClose()
                         .transactionDisable()
                         .make();
 
          =
                 .createHashMap()
                         .keySerializer(new Key.Serializer())
                         .valueSerializer(new Value.Serializer())
                         .make();
 
          = Executors.newFixedThreadPool(16);
 
          = new CountDownLatch();
 
 
     }
 
     @Test
     public void test1() throws InterruptedException {
         final Value value = new Value();
         final Key key = new Key(value"http://www.mapdb.org/");
 
         for (int i = 0; i < i++) {
             final int j = i;
 
             .execute(new Runnable() {
 
                 @Override
                 public void run() {
                     try {
                         .put(keyvalue);
                     } finally {
                         .countDown();
 //                        System.out.println("OP " + j);
                     }
                 }
             });
         }
     }
 
     @Test
     public void test2() throws InterruptedException {
         final ConcurrentMap<KeyValuealreadyAdded =
                 new ConcurrentHashMap<KeyValue>();
 
         for (int i = 0; i < i++) {
             final int j = i;
 
             .execute(new Runnable() {
 
                 @Override
                 public void run() {
                     try {
                         if (j % 2 == 0) {
                             Value value = new Value();
                             Key key = new Key(value, Integer.toString(j));
 
                             alreadyAdded.putIfAbsent(keyvalue);
                             .putIfAbsent(keyvalue);
                         } else {
                            Iterator<Keyit = alreadyAdded.keySet().iterator();
                            if (it.hasNext()) {
                                .get(it.next());
                            }
                        }
                    } finally {
                        .countDown();
//                        System.out.println("OP " + j);
                    }
                }
            });
        }
    }
    @After
    public void tearDown() throws InterruptedException {
        .await();
        .shutdown();
        .close();
    }
    public static class Value implements Serializable {
        private static final long serialVersionUID = 1L;
        public static final Serializer SERIALIZER = new Serializer();
        protected final UUID value;
        public Value() {
            this. = UUID.randomUUID();
        }
        private Value(UUID uuid) {
            this. = uuid;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((this. == null)
                    ? 0 : this..hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Value)) {
                return false;
            }
            Value other = (Valueobj;
            if (this. == null) {
                if (other.value != null) {
                    return false;
                }
            } else if (!this..equals(other.value)) {
                return false;
            }
            return true;
        }
        public static final class Serializer implements
                org.mapdb.Serializer<Value>, Serializable {
            private static final long serialVersionUID = 140L;
            @Override
            public void serialize(DataOutput outValue value)
                    throws IOException {
                out.writeLong(value.value.getMostSignificantBits());
                out.writeLong(value.value.getLeastSignificantBits());
            }
            @Override
            public Value deserialize(DataInput inint available)
                    throws IOException {
                return new Value(new UUID(in.readLong(), in.readLong()));
            }
            @Override
            public int fixedSize() {
                return -1;
            }
        }
    }
    public static class Key implements Serializable {
        private static final long serialVersionUID = 1L;
        protected final Value subscriptionId;
        protected final String eventId;
        public Key(Value subscriptionIdString eventId) {
            this. = subscriptionId;
            this. = eventId;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((this. == null)
                    ? 0 : this..hashCode());
            result = prime * result + ((this. == null)
                    ? 0 : this..hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Key)) {
                return false;
            }
            Key other = (Keyobj;
            if (this. == null) {
                if (other.eventId != null) {
                    return false;
                }
            } else if (!this..equals(other.eventId)) {
                return false;
            }
            if (this. == null) {
                if (other.subscriptionId != null) {
                    return false;
                }
            } else if (!this..equals(other.subscriptionId)) {
                return false;
            }
            return true;
        }
        public static final class Serializer implements
                org.mapdb.Serializer<Key>, Serializable {
            private static final long serialVersionUID = 1L;
            @Override
            public void serialize(DataOutput outKey notificationId)
                    throws IOException {
                out.writeUTF(notificationId.eventId);
                ..serialize(outnotificationId.subscriptionId);
            }
            @Override
            public Key deserialize(DataInput inint available)
                    throws IOException {
                String eventId = in.readUTF();
                Value subscriptionId =
                        ..deserialize(inavailable);
                return new Key(subscriptionIdeventId);
            }
            @Override
            public int fixedSize() {
                return -1;
            }
        }
    }
New to GrepCode? Check out our FAQ X