Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The usage of BitCaskKeyDir in java

Shulou Source: shulou.com Published: 2022-06-02 06:34:02 09月13日 Update

This article mainly introduces "the usage of BitCaskKeyDir in java". In daily operation, I believe many people have doubts about the usage of BitCaskKeyDir in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the usage of BitCaskKeyDir in java! Next, please follow the editor to study!

Order

This paper mainly studies BitCaskKeyDir.

BitCaskKeyDir

Bitcask-java/src/main/java/com/trifork/bitcask/BitCaskKeyDir.java

Public class BitCaskKeyDir {Map map = new HashMap (); ReadWriteLock rwl = new ReentrantReadWriteLock (); private boolean is_ready; public boolean put (ByteString key, BitCaskEntry ent) {Lock writeLock = rwl.writeLock (); writeLock.lock (); try {BitCaskEntry old = map.get (key) If (old = = null) {map.put (key, ent); return true;} else if (ent.is_newer_than (old)) {map.put (key, ent) Return true;} else {return false;}} finally {writeLock.unlock () }} public BitCaskEntry get (ByteString key) {Lock readLock = rwl.readLock (); readLock.lock (); try {return map.get (key) } finally {readLock.unlock ();}} /.}

BitCaskKeyDir provides map to store BitCaskEntry; and its put method uses writeLock.lock (). If the null value is null or the new value is greater than the Old value, the put goes in, otherwise it returns false, and finally writeLock.unlock (); its get method uses readLock.lock () to read the value of the specified key from map, and finally readLock.unlock ()

Key_dirs

Bitcask-java/src/main/java/com/trifork/bitcask/BitCaskKeyDir.java

Public class BitCaskKeyDir {public static Map key_dirs = new HashMap (); public static Lock keydir_lock = new ReentrantLock (); public static BitCaskKeyDir keydir_new (File dirname, int openTimeoutSecs) throws IOException {File abs_name = dirname.getAbsoluteFile (); BitCaskKeyDir dir; keydir_lock.lock () Try {dir = key_dirs.get (abs_name); if (dir = = null) {dir = new BitCaskKeyDir (); key_dirs.put (abs_name, dir) Return dir;}} finally {keydir_lock.unlock () } if (dir.wait_for_ready (openTimeoutSecs)) {return dir;} else {throw new IOException ("timeout while waiting for keydir");}} public synchronized boolean is_ready () {return is_ready } public synchronized void mark_ready () {is_ready = true; this.notifyAll ();} public synchronized boolean wait_for_ready (int timeout_secs) {long now = System.currentTimeMillis (); long abs_timeout = now + (timeout_secs * 1000) While (! is_ready & & now < abs_timeout) {try {wait () } catch (InterruptedException e) {/ / ignore} now = System.currentTimeMillis ();} return is_ready;}}

BitCaskKeyDir defines the key_dirs of the static, which is used to store the BitCaskKeyDir; of the specified File. The keydir_new is created for the BitCaskKeyDir that does not exist, and finally waits for the ready via dir.wait_for_ready (openTimeoutSecs).

Summary

BitCaskKeyDir provides map to store BitCaskEntry; and its put method uses writeLock.lock (). If the null value is null or the new value is greater than the Old value, the put goes in, otherwise it returns false, and finally writeLock.unlock (); its get method uses readLock.lock () to read the value of the specified key from map, and finally readLock.unlock ()

At this point, the study of "the use of BitCaskKeyDir in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Methods learning more help practical next summary articles theories knowledge articles websites materials follow questions easy to use practice research solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Xiaomi Redmi Linux