您好,登錄后才能下訂單哦!
本篇內容介紹了“java中BitCaskLock的使用方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
本文主要研究一下BitCaskLock
bitcask-java/src/main/java/com/trifork/bitcask/BitCaskLock.java
public class BitCaskLock { public static enum Stale { OK, NOT_STALE } public static enum Type { WRITE, MERGE; private String type_name() { if (this == MERGE) return "merge"; if (this == WRITE) return "write"; throw new RuntimeException(); } } private RandomAccessFile file; private boolean is_write_lock; private File filename; private BitCaskLock(RandomAccessFile file, File filename, boolean isWriteLock) { this.file = file; this.filename = filename; this.is_write_lock = isWriteLock; } public static BitCaskLock acquire(Type type, File dirname) throws IOException { File lock_filename = lock_filename(type, dirname); foo: do { try { BitCaskLock lock = lock_acquire(lock_filename, true); String lock_contents = Integer.toString(OS.getpid()) + " \n"; lock.write_data(ByteString.copyFromUtf8(lock_contents)); return lock; } catch (FileAlreadyExistsException e) { delete_stale_lock(lock_filename); continue foo; } } while (false); return null; } public void release() throws IOException { if (file != null) { if (is_write_lock) { file.close(); filename.delete(); } } file = null; } //...... }
BitCaskLock的acquire方法,先通過lock_acquire獲取BitCaskLock,如果出現FileAlreadyExistsException則執行delete_stale_lock;其release方法針對is_write_lock執行file.close()及filename.delete()
bitcask-java/src/main/java/com/trifork/bitcask/BitCaskLock.java
private static BitCaskLock lock_acquire(File lockFilename, boolean is_write_lock) throws IOException { if (is_write_lock) { if (lockFilename.createNewFile() == false) { // file already exists, so we fail! throw new FileAlreadyExistsException(lockFilename); } } RandomAccessFile f = new RandomAccessFile(lockFilename, is_write_lock ? "rws" : "r"); return new BitCaskLock(f, lockFilename, is_write_lock); }
lock_acquire方法針對is_write_lock,執行lockFilename.createNewFile(),若為false則拋出FileAlreadyExistsException
bitcask-java/src/main/java/com/trifork/bitcask/BitCaskLock.java
public static Stale delete_stale_lock(Type type, File dirname) throws IOException { return delete_stale_lock(lock_filename(type, dirname)); } private static Stale delete_stale_lock(File lockFilename) throws IOException { BitCaskLock l = null; try { l = lock_acquire(lockFilename, false); } catch (FileNotFoundException e) { return Stale.OK; } catch (IOException e) { return Stale.NOT_STALE; } try { int pid = l.read_lock_data_pid(); if (OS.pid_exists(pid)) { return Stale.NOT_STALE; } else { lockFilename.delete(); return Stale.OK; } } catch (IOException e) { return Stale.NOT_STALE; } finally { l.release(); } }
delete_stale_lock方法先執行lock_acquire,若出現FileNotFoundException,則返回Stale.OK,若出現IOException則返回Stale.NOT_STALE;否則通過l.read_lock_data_pid()獲取pid,若OS.pid_exists(pid)則返回Stale.NOT_STALE,否則執行lockFilename.delete(),返回Stale.OK;最后執行l.release()
BitCaskLock的acquire方法,先通過lock_acquire獲取BitCaskLock,如果出現FileAlreadyExistsException則執行delete_stale_lock;其release方法針對is_write_lock執行file.close()及filename.delete()
“java中BitCaskLock的使用方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。