Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use BitCaskLock in java

Shulou Source: shulou.com Published: 2022-06-02 06:34:12 10月03日 Update

This article introduces the relevant knowledge of "how to use BitCaskLock in java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies 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;} / /.}

The acquire method of BitCaskLock first obtains BitCaskLock through lock_acquire, and if FileAlreadyExistsException occurs, executes delete_stale_lock; and its release method executes file.close () and filename.delete () against is_write_lock.

Lock_acquire

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);}

The lock_acquire method executes lockFilename.createNewFile () against is_write_lock, and throws FileAlreadyExistsException if it is false

Delete_stale_lock

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 ();}}

The delete_stale_lock method executes lock_acquire first, returns Stale.OK if FileNotFoundException occurs, returns Stale.NOT_STALE; if IOException occurs, otherwise gets pid through l.read_lock_data_pid (), returns Stale.NOT_STALE if OS.pid_exists (pid), executes lockFilename.delete () otherwise, returns Stale.OK; and finally executes l.release ()

Summary

The acquire method of BitCaskLock first obtains BitCaskLock through lock_acquire, and if FileAlreadyExistsException occurs, executes delete_stale_lock; and its release method executes file.close () and filename.delete () against is_write_lock.

This is the end of the content of "how to use BitCaskLock in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Method usage content more knowledge practicality achievement next dilemma practice summary situation article case editing website industry process high quality learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia MySQL Shulou Information macOS