Changeset 29484 in osm for applications/editors/josm/plugins/imagerycache/src/org/mapdb/Utils.java
- Timestamp:
- 2013-04-07T17:07:27+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagerycache/src/org/mapdb/Utils.java
r29363 r29484 21 21 import java.util.*; 22 22 import java.util.concurrent.atomic.AtomicLong; 23 import java.util.concurrent.locks.LockSupport; 24 import java.util.concurrent.locks.ReentrantLock; 25 import java.util.concurrent.locks.ReentrantReadWriteLock; 23 26 import java.util.logging.Logger; 24 27 … … 188 191 File index = File.createTempFile("mapdb","db"); 189 192 index.deleteOnExit(); 190 new File(index.getPath()+ StorageDirect.DATA_FILE_EXT).deleteOnExit();191 new File(index.getPath()+ Stor ageJournaled.TRANS_LOG_FILE_EXT).deleteOnExit();193 new File(index.getPath()+ StoreDirect.DATA_FILE_EXT).deleteOnExit(); 194 new File(index.getPath()+ StoreWAL.TRANS_LOG_FILE_EXT).deleteOnExit(); 192 195 193 196 return index; … … 233 236 } 234 237 235 public static void print er(final AtomicLong value){236 new Thread("print er"){238 public static void printProgress(final AtomicLong value){ 239 new Thread("printProgress"){ 237 240 { 238 241 setDaemon(true); 239 242 } 240 241 243 242 244 @Override 243 245 public void run() { 244 246 long startValue = value.get(); 245 long startTime = System.currentTimeMillis(); 247 long startTime, time = System.currentTimeMillis(); 248 startTime = time; 246 249 long old = value.get(); 247 250 while(true){ 248 249 try { 250 Thread.sleep(1000); 251 } catch (InterruptedException e) { 251 time+=1000; 252 while(time>System.currentTimeMillis()){ 253 LockSupport.parkNanos(1000*1000); //1ms 254 } 255 256 long current = value.get(); 257 if(current<0){ 258 System.out.println("Finished, total time: "+(time-startTime)+", aprox items: "+old); 252 259 return; 253 260 } 254 255 long current = value.get(); 256 long totalSpeed = 1000*(current-startValue)/(System.currentTimeMillis()-startTime); 261 long totalSpeed = 1000*(current-startValue)/(time-startTime); 257 262 System.out.print("total: "+current+" - items per last second: "+(current-old)+" - avg items per second: "+totalSpeed+"\r"); 258 263 old = current; … … 263 268 } 264 269 270 public static <A> DataOutput2 serializer(Serializer<A> serializer, A value) { 271 try{ 272 DataOutput2 out = new DataOutput2(); 273 serializer.serialize(out,value); 274 return out; 275 }catch(IOException e){ 276 throw new IOError(e); 277 } 278 279 } 280 281 public static String randomString(int size) { 282 String chars = "0123456789abcdefghijklmnopqrstuvwxyz !@#$%^&*()_+=-{}[]:\",./<>?|\\"; 283 StringBuilder b = new StringBuilder(size); 284 for(int i=0;i<size;i++){ 285 b.append(chars.charAt(RANDOM.nextInt(chars.length()))); 286 } 287 return b.toString(); 288 } 289 290 public static ReentrantReadWriteLock[] newReadWriteLocks(int size) { 291 ReentrantReadWriteLock[] locks = new ReentrantReadWriteLock[size]; 292 for(int i=0;i<locks.length;i++) locks[i] = new ReentrantReadWriteLock(); 293 return locks; 294 } 295 296 public static ReentrantLock[] newLocks(int size) { 297 ReentrantLock[] locks = new ReentrantLock[size]; 298 for(int i=0;i<locks.length;i++) locks[i] = new ReentrantLock(); 299 return locks; 300 } 301 302 public static void lock(ReentrantLock[] locks, long recid) { 303 locks[Utils.longHash(recid)%locks.length].lock(); 304 } 305 306 public static void lockAll(ReentrantLock[] locks) { 307 for(ReentrantLock lock:locks)lock.lock(); 308 } 309 310 public static void unlockAll(ReentrantLock[] locks) { 311 for(ReentrantLock lock:locks)lock.unlock(); 312 } 313 314 315 public static void unlock(ReentrantLock[] locks, long recid) { 316 locks[Utils.longHash(recid)%locks.length].unlock(); 317 } 318 319 320 public static void readLock(ReentrantReadWriteLock[] locks, long recid) { 321 locks[Utils.longHash(recid)%locks.length].readLock().lock(); 322 } 323 324 public static void readUnlock(ReentrantReadWriteLock[] locks, long recid) { 325 locks[Utils.longHash(recid)%locks.length].readLock().unlock(); 326 } 327 328 public static void writeLock(ReentrantReadWriteLock[] locks, long recid) { 329 locks[Utils.longHash(recid)%locks.length].writeLock().lock(); 330 } 331 332 public static void writeUnlock(ReentrantReadWriteLock[] locks, long recid) { 333 locks[Utils.longHash(recid)%locks.length].writeLock().unlock(); 334 } 335 336 public static void writeLockAll(ReentrantReadWriteLock[] locks) { 337 for(ReentrantReadWriteLock l:locks) l.writeLock().lock(); 338 } 339 340 public static void writeUnlockAll(ReentrantReadWriteLock[] locks) { 341 for(ReentrantReadWriteLock l:locks) l.writeLock().unlock(); 342 } 343 344 345 public static void lock(LongConcurrentHashMap<Thread> locks, long recid){ 346 //feel free to rewrite, if you know better (more efficient) way 347 if(locks.get(recid)==Thread.currentThread()){ 348 //check node is not already locked by this thread 349 throw new InternalError("node already locked by current thread: "+recid); 350 } 351 352 while(locks.putIfAbsent(recid, Thread.currentThread()) != null){ 353 LockSupport.parkNanos(10); 354 } 355 } 356 357 358 359 public static void unlock(LongConcurrentHashMap<Thread> locks,final long recid) { 360 final Thread t = locks.remove(recid); 361 if(t!=Thread.currentThread()) 362 throw new InternalError("unlocked wrong thread"); 363 364 } 365 366 public static void assertNoLocks(LongConcurrentHashMap<Thread> locks){ 367 if(CC.PARANOID){ 368 LongMap.LongMapIterator<Thread> i = locks.longMapIterator(); 369 while(i.moveToNext()){ 370 if(i.value()==Thread.currentThread()){ 371 throw new InternalError("Node "+i.key()+" is still locked"); 372 } 373 } 374 } 375 } 265 376 }
Note:
See TracChangeset
for help on using the changeset viewer.