// add and remove client and create neccessary data struct public void add(LogEntry entry) { while (size.get() >= capacity || bufferElementPersist.get(writePosition)) { synchronized (full) { try { if (size.get() >= capacity) { // print1("full: waiting thread:" + "size.get()>=capacity " + (size.get()>=capacity) // + " bufferElementPersist.get(writePosition) // "+(bufferElementPersist.get(writePosition)) ); full.wait(); // print1("full: release:" + "size.get()>=capacity " + (size.get()>=capacity) + " // bufferElementPersist.get(writePosition) "+(bufferElementPersist.get(writePosition)) // ); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } String clientId = entry.getEntryId().getClientId(); Long msgId = Long.valueOf(entry.getEntryId().getMessageId()); Integer intWritePosition = Integer.valueOf(writePosition); if (!logIndex.containsKey(clientId)) { // move to add a client method later HashMap<Long, Integer> messages = new HashMap<Long, Integer>(); messages.put(msgId, intWritePosition); logIndex.put(clientId, messages); } else logIndex.get(clientId).put(msgId, intWritePosition); bufferElementRead.set(writePosition, true); bufferElementPersist.set(writePosition, true); if (persistClient.contains(clientId)) { persistIndexQueue.offer(Integer.valueOf(writePosition)); synchronized (persistLock) { persistLock.notifyAll(); } } buffer[writePosition++] = entry; writePosition = writePosition % capacity; size.incrementAndGet(); synchronized (empty) { empty.notifyAll(); } synchronized (readLock) { readLock.notifyAll(); } // print("Buffered " + entry.getEntryId().getMessageId()); }
/** @return */ public LogEntry nextToPersist() { // TODO Auto-generated method stub // if element is not read can not be persisted while (persistIndexQueue.size() <= 0 || bufferElementRead.get(persistIndexQueue.peek()) || !bufferElementPersist.get(persistIndexQueue.peek())) { synchronized (persistLock) { try { if (persistIndexQueue.size() > 0) print1( "persistlock: waiting thread:" + "persistIndexQueue.size()<=0 " + String.valueOf(persistIndexQueue.size() <= 0) + " bufferElementRead.get(persistIndexQueue.peek()) " + String.valueOf(bufferElementRead.get(persistIndexQueue.peek())) + " !bufferElementPersist.get(persistIndexQueue.peek()) " + String.valueOf(!bufferElementPersist.get(persistIndexQueue.peek()))); persisterWaitForReader = true; persistLock.wait(); if (persistIndexQueue.size() > 0) print1( "persistlock: release thread:" + "persistIndexQueue.size()<=0 " + String.valueOf(persistIndexQueue.size() <= 0) + " bufferElementRead.get(persistIndexQueue.peek()) " + String.valueOf(bufferElementRead.get(persistIndexQueue.peek())) + " !bufferElementPersist.get(persistIndexQueue.peek()) " + String.valueOf(!bufferElementPersist.get(persistIndexQueue.peek()))); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } persisterWaitForReader = false; int index = persistIndexQueue.poll().intValue(); LogEntry entry = buffer[index]; // bufferElementPersist.set(index, false); print("PersistNext From Buffer " + entry.getEntryId().getMessageId()); return entry; }