Yuta HIGUCHI

MapDBLog: configure LogMap parameters

Change-Id: I6d69b0b3824554c46e8aae466c3a03cf2b318eac
...@@ -18,6 +18,7 @@ import org.mapdb.Atomic; ...@@ -18,6 +18,7 @@ import org.mapdb.Atomic;
18 import org.mapdb.BTreeMap; 18 import org.mapdb.BTreeMap;
19 import org.mapdb.DB; 19 import org.mapdb.DB;
20 import org.mapdb.DBMaker; 20 import org.mapdb.DBMaker;
21 +import org.mapdb.Serializer;
21 import org.mapdb.TxBlock; 22 import org.mapdb.TxBlock;
22 import org.mapdb.TxMaker; 23 import org.mapdb.TxMaker;
23 import org.onlab.onos.store.serializers.StoreSerializer; 24 import org.onlab.onos.store.serializers.StoreSerializer;
...@@ -84,7 +85,7 @@ public class MapDBLog implements Log { ...@@ -84,7 +85,7 @@ public class MapDBLog implements Log {
84 txMaker.execute(new TxBlock() { 85 txMaker.execute(new TxBlock() {
85 @Override 86 @Override
86 public void tx(DB db) { 87 public void tx(DB db) {
87 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 88 + BTreeMap<Long, byte[]> log = getLogMap(db);
88 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME); 89 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME);
89 long nextIndex = log.isEmpty() ? 1 : log.lastKey() + 1; 90 long nextIndex = log.isEmpty() ? 1 : log.lastKey() + 1;
90 for (Entry entry : entries) { 91 for (Entry entry : entries) {
...@@ -105,7 +106,7 @@ public class MapDBLog implements Log { ...@@ -105,7 +106,7 @@ public class MapDBLog implements Log {
105 assertIsOpen(); 106 assertIsOpen();
106 DB db = txMaker.makeTx(); 107 DB db = txMaker.makeTx();
107 try { 108 try {
108 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 109 + BTreeMap<Long, byte[]> log = getLogMap(db);
109 return log.containsKey(index); 110 return log.containsKey(index);
110 } finally { 111 } finally {
111 db.close(); 112 db.close();
...@@ -118,7 +119,7 @@ public class MapDBLog implements Log { ...@@ -118,7 +119,7 @@ public class MapDBLog implements Log {
118 txMaker.execute(new TxBlock() { 119 txMaker.execute(new TxBlock() {
119 @Override 120 @Override
120 public void tx(DB db) { 121 public void tx(DB db) {
121 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 122 + BTreeMap<Long, byte[]> log = getLogMap(db);
122 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME); 123 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME);
123 log.clear(); 124 log.clear();
124 size.set(0); 125 size.set(0);
...@@ -131,7 +132,7 @@ public class MapDBLog implements Log { ...@@ -131,7 +132,7 @@ public class MapDBLog implements Log {
131 assertIsOpen(); 132 assertIsOpen();
132 DB db = txMaker.makeTx(); 133 DB db = txMaker.makeTx();
133 try { 134 try {
134 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 135 + BTreeMap<Long, byte[]> log = getLogMap(db);
135 return log.isEmpty() ? null : serializer.decode(log.firstEntry().getValue()); 136 return log.isEmpty() ? null : serializer.decode(log.firstEntry().getValue());
136 } finally { 137 } finally {
137 db.close(); 138 db.close();
...@@ -143,7 +144,7 @@ public class MapDBLog implements Log { ...@@ -143,7 +144,7 @@ public class MapDBLog implements Log {
143 assertIsOpen(); 144 assertIsOpen();
144 DB db = txMaker.makeTx(); 145 DB db = txMaker.makeTx();
145 try { 146 try {
146 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 147 + BTreeMap<Long, byte[]> log = getLogMap(db);
147 return log.isEmpty() ? 0 : log.firstKey(); 148 return log.isEmpty() ? 0 : log.firstKey();
148 } finally { 149 } finally {
149 db.close(); 150 db.close();
...@@ -155,7 +156,7 @@ public class MapDBLog implements Log { ...@@ -155,7 +156,7 @@ public class MapDBLog implements Log {
155 assertIsOpen(); 156 assertIsOpen();
156 DB db = txMaker.makeTx(); 157 DB db = txMaker.makeTx();
157 try { 158 try {
158 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 159 + BTreeMap<Long, byte[]> log = getLogMap(db);
159 if (log.isEmpty()) { 160 if (log.isEmpty()) {
160 throw new LogIndexOutOfBoundsException("Log is empty"); 161 throw new LogIndexOutOfBoundsException("Log is empty");
161 } else if (from < log.firstKey()) { 162 } else if (from < log.firstKey()) {
...@@ -179,7 +180,7 @@ public class MapDBLog implements Log { ...@@ -179,7 +180,7 @@ public class MapDBLog implements Log {
179 assertIsOpen(); 180 assertIsOpen();
180 DB db = txMaker.makeTx(); 181 DB db = txMaker.makeTx();
181 try { 182 try {
182 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 183 + BTreeMap<Long, byte[]> log = getLogMap(db);
183 byte[] entryBytes = log.get(index); 184 byte[] entryBytes = log.get(index);
184 return entryBytes == null ? null : serializer.decode(entryBytes); 185 return entryBytes == null ? null : serializer.decode(entryBytes);
185 } finally { 186 } finally {
...@@ -192,7 +193,7 @@ public class MapDBLog implements Log { ...@@ -192,7 +193,7 @@ public class MapDBLog implements Log {
192 assertIsOpen(); 193 assertIsOpen();
193 DB db = txMaker.makeTx(); 194 DB db = txMaker.makeTx();
194 try { 195 try {
195 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 196 + BTreeMap<Long, byte[]> log = getLogMap(db);
196 return log.isEmpty(); 197 return log.isEmpty();
197 } finally { 198 } finally {
198 db.close(); 199 db.close();
...@@ -204,7 +205,7 @@ public class MapDBLog implements Log { ...@@ -204,7 +205,7 @@ public class MapDBLog implements Log {
204 assertIsOpen(); 205 assertIsOpen();
205 DB db = txMaker.makeTx(); 206 DB db = txMaker.makeTx();
206 try { 207 try {
207 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 208 + BTreeMap<Long, byte[]> log = getLogMap(db);
208 return log.isEmpty() ? null : serializer.decode(log.lastEntry().getValue()); 209 return log.isEmpty() ? null : serializer.decode(log.lastEntry().getValue());
209 } finally { 210 } finally {
210 db.close(); 211 db.close();
...@@ -216,7 +217,7 @@ public class MapDBLog implements Log { ...@@ -216,7 +217,7 @@ public class MapDBLog implements Log {
216 assertIsOpen(); 217 assertIsOpen();
217 DB db = txMaker.makeTx(); 218 DB db = txMaker.makeTx();
218 try { 219 try {
219 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 220 + BTreeMap<Long, byte[]> log = getLogMap(db);
220 return log.isEmpty() ? 0 : log.lastKey(); 221 return log.isEmpty() ? 0 : log.lastKey();
221 } finally { 222 } finally {
222 db.close(); 223 db.close();
...@@ -229,7 +230,7 @@ public class MapDBLog implements Log { ...@@ -229,7 +230,7 @@ public class MapDBLog implements Log {
229 txMaker.execute(new TxBlock() { 230 txMaker.execute(new TxBlock() {
230 @Override 231 @Override
231 public void tx(DB db) { 232 public void tx(DB db) {
232 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 233 + BTreeMap<Long, byte[]> log = getLogMap(db);
233 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME); 234 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME);
234 long startIndex = index + 1; 235 long startIndex = index + 1;
235 long endIndex = log.lastKey(); 236 long endIndex = log.lastKey();
...@@ -265,7 +266,7 @@ public class MapDBLog implements Log { ...@@ -265,7 +266,7 @@ public class MapDBLog implements Log {
265 txMaker.execute(new TxBlock() { 266 txMaker.execute(new TxBlock() {
266 @Override 267 @Override
267 public void tx(DB db) { 268 public void tx(DB db) {
268 - BTreeMap<Long, byte[]> log = db.getTreeMap(LOG_NAME); 269 + BTreeMap<Long, byte[]> log = getLogMap(db);
269 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME); 270 Atomic.Long size = db.getAtomicLong(SIZE_FIELD_NAME);
270 ConcurrentNavigableMap<Long, byte[]> headMap = log.headMap(index); 271 ConcurrentNavigableMap<Long, byte[]> headMap = log.headMap(index);
271 long deletedBytes = headMap.keySet().stream().mapToLong(i -> log.remove(i).length).sum(); 272 long deletedBytes = headMap.keySet().stream().mapToLong(i -> log.remove(i).length).sum();
...@@ -277,4 +278,12 @@ public class MapDBLog implements Log { ...@@ -277,4 +278,12 @@ public class MapDBLog implements Log {
277 } 278 }
278 }); 279 });
279 } 280 }
281 +
282 + private BTreeMap<Long, byte[]> getLogMap(DB db) {
283 + return db.createTreeMap(LOG_NAME)
284 + .valuesOutsideNodesEnable()
285 + .keySerializerWrap(Serializer.LONG)
286 + .valueSerializer(Serializer.BYTE_ARRAY)
287 + .makeOrGet();
288 + }
280 } 289 }
......