本篇内容介绍了“java中BitCaskLock的使用方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
高邑ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
本文主要研究一下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的使用方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!