[{"data":1,"prerenderedAt":976},["ShallowReactive",2],{"content:\u002Fen\u002Fprojects\u002Fmydb\u002Fmydb2":3,"series:content_en":266},{"id":4,"title":5,"authorship":6,"body":7,"categories":237,"date":239,"description":240,"draft":241,"extension":242,"image":243,"meta":244,"navigation":246,"path":247,"permalink":248,"published":248,"readingTime":249,"recommend":248,"references":248,"seo":254,"seoDescription":255,"seoTitle":248,"sitemap":256,"stem":257,"tags":258,"type":264,"__hash__":265},"content_en\u002Fposts\u002Fprojects\u002Fmydb\u002Fmydb2.md","MYDB 2. A Reference-Counted Cache Framework and Shared Byte Arrays","human-only",{"type":8,"value":9,"toc":227},"minimark",[10,22,27,30,36,39,42,46,51,54,57,65,78,81,86,89,96,99,103,109,119,122,128,135,141,144,150,153,159,162,168,171,177,180,184,187,190,195,198,206,209,212,215,218,224],[11,12,13,14,21],"p",{},"All the code in this chapter is in ",[15,16,20],"a",{"href":17,"rel":18},"https:\u002F\u002Fgithub.com\u002FCN-GuoZiyang\u002FMYDB\u002Ftree\u002Fmaster\u002Fsrc\u002Fmain\u002Fjava\u002Ftop\u002Fguoziyang\u002Fmydb\u002Fbackend\u002Fcommon",[19],"nofollow","backend\u002Fcommon",".",[23,24,26],"h3",{"id":25},"introduction","Introduction",[11,28,29],{},"In this chapter, we begin looking at MYDB’s lowest-level module, the Data Manager:",[31,32,33],"blockquote",{},[11,34,35],{},"DM directly manages the database’s DB file and log file. Its main responsibilities are: 1) managing and caching the pages of the DB file; 2) managing the log file so the database can recover from errors using the log; and 3) exposing the DB file as DataItems to higher-level modules and providing a cache for them.",[11,37,38],{},"DM’s work really boils down to two things: it is an abstraction layer between higher-level modules and the filesystem, reading and writing files below and providing data wrappers above; it also handles logging.",[11,40,41],{},"Notice that DM provides caching in both directions, using in-memory operations to keep things efficient.",[23,43,45],{"id":44},"a-reference-counted-cache-framework","A Reference-Counted Cache Framework",[47,48,50],"h4",{"id":49},"why-not-lru","Why Not LRU?",[11,52,53],{},"Both page management and DataItem management need caches, so we will design a more general-purpose cache framework here.",[11,55,56],{},"At this point, you may be wondering why we are using reference counting instead of the supposedly “far more advanced” LRU policy.",[11,58,59,60,64],{},"Let us start with the cache interface. An LRU cache needs only a ",[61,62,63],"code",{"code":63},"get(key)"," method: entries can be evicted automatically when the cache fills up. Now imagine the cache fills up and evicts a resource. A higher-level module then wants to force a resource back to its backing store, and it happens to be the one that was just evicted. The module discovers that its data has disappeared from the cache. This leaves an awkward question: should it write the resource back to the backing store?",[66,67,68,72,75],"ol",{},[69,70,71],"li",{},"Do not write it back. We cannot tell when it was evicted, much less whether the DataItem has changed since eviction. This is extremely unsafe.",[69,73,74],{},"Write it back. If the data is still the same as it was at eviction, this is an unnecessary write.",[69,76,77],{},"Put it back in the cache and write it back on the next eviction. That seems to solve the problem, but the cache is already full, so we need to evict another resource to make room. This can cause cache thrashing.",[11,79,80],{},"We could, of course, record the resource’s last-modified time and have the cache record its eviction time. But…",[31,82,83],{},[11,84,85],{},"Entities should not be multiplied beyond necessity. — Occam’s razor",[11,87,88],{},"The root of the problem is that LRU eviction is outside the higher-level module’s control, and the module is unaware of it. Reference counting solves exactly this problem: a resource is evicted only after higher-level modules explicitly release their references and the cache knows no module is using it anymore.",[11,90,91,92,95],{},"That is reference counting. We add a ",[61,93,94],{"code":94},"release(key)"," method for a higher-level module to release its reference when it no longer needs a resource. When the reference count reaches zero, the cache evicts the resource.",[11,97,98],{},"Conversely, when the cache is full, reference counting cannot automatically free space. We should simply report an error at that point, much like the JVM throwing an OOM.",[47,100,102],{"id":101},"implementation","Implementation",[11,104,105,108],{},[61,106,107],{"code":107},"AbstractCache\u003CT>"," is an abstract class with two abstract methods that subclasses implement for their specific operations:",[110,111,117],"pre",{"className":112,"code":114,"language":115,"meta":116},[113],"language-java","\u002F**\n * 当资源不在缓存时的获取行为\n *\u002F\nprotected abstract T getForCache(long key) throws Exception;\n\u002F**\n * 当资源被驱逐时的写回行为\n *\u002F\nprotected abstract void releaseForCache(T obj);\n","java","",[61,118,114],{"__ignoreMap":116},[11,120,121],{},"Beyond ordinary caching, reference counting needs a count for each resource. To support multiple threads, we also need to record which resources are currently being fetched from the backing store, since fetching is relatively time-consuming. That gives us these three Maps:",[110,123,126],{"className":124,"code":125,"language":115,"meta":116},[113],"private HashMap\u003CLong, T> cache;                     \u002F\u002F 实际缓存的数据\nprivate HashMap\u003CLong, Integer> references;          \u002F\u002F 资源的引用个数\nprivate HashMap\u003CLong, Boolean> getting;             \u002F\u002F 正在被获取的资源\n",[61,127,125],{"__ignoreMap":116},[11,129,130,131,134],{},"When ",[61,132,133],{"code":133},"get()"," retrieves a resource, it first enters an infinite loop to keep trying the cache. It checks whether another thread is currently fetching this resource from the backing store. If so, it waits a bit and checks again later…",[110,136,139],{"className":137,"code":138,"language":115,"meta":116},[113],"while(true) {\n    lock.lock();\n    if(getting.containsKey(key)) {\n        \u002F\u002F 请求的资源正在被其他线程获取\n        lock.unlock();\n        try {\n            Thread.sleep(1);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n            continue;\n        }\n        continue;\n    }\n    ...\n}\n",[61,140,138],{"__ignoreMap":116},[11,142,143],{},"If the resource is already cached, we can return it directly, remembering to increment its reference count. Otherwise, if the cache is not full, register the key in getting to indicate that this thread is about to fetch the resource from the backing store.",[110,145,148],{"className":146,"code":147,"language":115,"meta":116},[113],"while(true) {\n    if(cache.containsKey(key)) {\n        \u002F\u002F 资源在缓存中，直接返回\n        T obj = cache.get(key);\n        references.put(key, references.get(key) + 1);\n        lock.unlock();\n        return obj;\n    }\n\n    \u002F\u002F 尝试获取该资源\n    if(maxResource > 0 && count == maxResource) {\n        lock.unlock();\n        throw Error.CacheFullException;\n    }\n    count ++;\n    getting.put(key, true);\n    lock.unlock();\n    break;\n}\n",[61,149,147],{"__ignoreMap":116},[11,151,152],{},"Fetching the resource is straightforward: call the abstract method. Once it finishes, remember to remove the key from getting.",[110,154,157],{"className":155,"code":156,"language":115,"meta":116},[113],"T obj = null;\ntry {\n    obj = getForCache(key);\n} catch(Exception e) {\n    lock.lock();\n    count --;\n    getting.remove(key);\n    lock.unlock();\n    throw e;\n}\n\nlock.lock();\ngetting.remove(key);\ncache.put(key, obj);\nreferences.put(key, 1);\nlock.unlock();\n",[61,158,156],{"__ignoreMap":116},[11,160,161],{},"Releasing a cached resource is much simpler. Decrement its count in references. If the count reaches zero, write the resource back and remove all its associated cache entries:",[110,163,166],{"className":164,"code":165,"language":115,"meta":116},[113],"\u002F**\n * 强行释放一个缓存\n *\u002F\nprotected void release(long key) {\n    lock.lock();\n    try {\n        int ref = references.get(key)-1;\n        if(ref == 0) {\n            T obj = cache.get(key);\n            releaseForCache(obj);\n            references.remove(key);\n            cache.remove(key);\n            count --;\n        } else {\n            references.put(key, ref);\n        }\n    } finally {\n        lock.unlock();\n    }\n}\n",[61,167,165],{"__ignoreMap":116},[11,169,170],{},"The cache should also support a safe shutdown, forcing every cached resource back to its backing store when it closes.",[110,172,175],{"className":173,"code":174,"language":115,"meta":116},[113],"lock.lock();\ntry {\n    Set\u003CLong> keys = cache.keySet();\n    for (long key : keys) {\n        release(key);\n        references.remove(key);\n        cache.remove(key);\n    }\n} finally {\n    lock.unlock();\n}\n",[61,176,174],{"__ignoreMap":116},[11,178,179],{},"That completes a simple cache framework. Other caches only need to extend this class and implement its two abstract methods.",[23,181,183],{"id":182},"shared-byte-arrays","Shared Byte Arrays",[11,185,186],{},"Here is one rather irritating thing about Java.",[11,188,189],{},"Java treats arrays as objects and stores them as objects in memory. In languages such as C, C++, and Go, arrays are implemented using pointers. This is why you sometimes hear:",[31,191,192],{},[11,193,194],{},"Only Java has real arrays.",[11,196,197],{},"For this project, though, that does not seem to be good news. In Go, for example, you can write:",[110,199,204],{"className":200,"code":202,"language":203,"meta":116},[201],"language-go","var array1 [10]int64\narray2 := array1[5:]\n","go",[61,205,202],{"__ignoreMap":116},[11,207,208],{},"Here, array2 shares the same memory as array1 from its fifth element through its last, even though the two arrays have different lengths.",[11,210,211],{},"You cannot do this in Java. So much for being a high-level language~",[11,213,214],{},"In Java, an operation like subArray merely copies the data underneath; it cannot share the same memory.",[11,216,217],{},"So I wrote a SubArray class to specify, rather loosely, which part of an array may be used:",[110,219,222],{"className":220,"code":221,"language":115,"meta":116},[113],"public class SubArray {\n    public byte[] raw;\n    public int start;\n    public int end;\n\n    public SubArray(byte[] raw, int start, int end) {\n        this.raw = raw;\n        this.start = start;\n        this.end = end;\n    }\n}\n",[61,223,221],{"__ignoreMap":116},[11,225,226],{},"Honestly, this is an ugly solution, but it is what I have for now. If you know another way, please leave a comment below. I do not want the code to look this ugly either \u002F(ㄒo ㄒ)\u002F~~",{"title":116,"searchDepth":228,"depth":228,"links":229},4,[230,232,236],{"id":25,"depth":231,"text":26},3,{"id":44,"depth":231,"text":45,"children":233},[234,235],{"id":49,"depth":228,"text":50},{"id":101,"depth":228,"text":102},{"id":182,"depth":231,"text":183},[238],"projects","2021-11-30 23:18:00","The Data Manager (DM) bridges higher-level modules and the filesystem, handling paging and caching while ensuring data safety and recovery. Its cache uses reference counting rather than traditional LRU, aiming for a reusable, efficient foundation for subsequent data operations.",false,"md","https:\u002F\u002Fblog-img.774352199.xyz\u002FWdIGoG.webp",{"slots":245},{},true,"\u002Fprojects\u002Fmydb\u002Fmydb2",null,{"text":250,"minutes":251,"time":252,"words":253},"6 min read",5.725,343500,1145,{"title":5,"description":240},"Build MYDB’s reference-counted Java cache with coordinated loading and explicit release, then use SubArray to share byte-array regions across data objects.",{"loc":247},"posts\u002Fprojects\u002Fmydb\u002Fmydb2",[259,260,261,262,263],"MYDB","Java","Reference counting","Cache design","Shared memory","tech","1tkO0Zy2wAaQYAc0TND_Nxs43nmIFHpSzHBty7bgQto",[267,285,302,320,337,356,374,392,409,425,441,456,472,488,505,523,538,555,569,585,602,619,635,652,668,687,703,719,737,751,769,786,802,818,832,847,861,876,880,894,908,922,937,951,962],{"categories":268,"date":270,"description":271,"image":272,"path":273,"readingTime":274,"recommend":248,"tags":279,"title":284,"type":264},[269],"daily","2024-09-01 22:03:10","Chronic gastritis has been a long, uneven journey: stomach trouble throughout childhood, a surprising reprieve at university, then a return of symptoms after years of late nights and drinking. Frequent nausea and reflux eventually became too much to live with. After repeated examinations and some reflection, I finally began taking the recovery process seriously.","https:\u002F\u002Fblog-img.774352199.xyz\u002F0Hr9l5.webp","\u002Fdaily\u002Fanti-chronic-gastritis",{"text":275,"minutes":276,"time":277,"words":278},"4 min read",3.08,184800,616,[280,281,282,283],"Chronic gastritis","Medical care experiences","Gastroscopy","Health journal","My Journey with Chronic Gastritis",{"categories":286,"date":287,"description":288,"image":289,"path":290,"readingTime":291,"recommend":248,"tags":296,"title":301,"type":264},[269],"2022-04-11 00:13:13","In The Three-Body Problem, Liu Cixin uses an immense historical canvas to explore the relationship between individuals and the collective. His doubts about Western democracy and the majority’s ability to determine its own fate run through the trilogy. Heroes struggle and sacrifice, only to see their work swept aside by history. Beginning with the Cultural Revolution, the story sets up a conflict between exceptional individuals and the ordinary masses, raising uncomfortable questions about humanity and society.","https:\u002F\u002Fblog-img.774352199.xyz\u002FLTzbFP.webp","\u002Fdaily\u002Fpeople-in-three-body",{"text":292,"minutes":293,"time":294,"words":295},"9 min read",8.635,518100,1727,[297,298,299,300],"The Three-Body Problem","Liu Cixin","Science fiction","Reading notes","The “Majority” in The Three-Body Problem",{"categories":303,"date":304,"description":305,"image":306,"path":307,"readingTime":308,"recommend":248,"tags":313,"title":319,"type":264},[269],"2024-01-04 02:06:51","My learning goals for 2024: reach N2 in Japanese, finish SICP and TAPL, build a kernel with full POSIX support, and update the blog theme. A challenging list, but a clear direction for the year.","https:\u002F\u002Fblog-img.774352199.xyz\u002FV3rSQC.webp","\u002Fdaily\u002Fplan2024",{"text":309,"minutes":310,"time":311,"words":312},"1 min read",0.15,9000,30,[314,315,316,317,318],"Annual planning","Learning Japanese","SICP","TAPL","Operating systems","My Learning Plan for 2024",{"categories":321,"date":322,"description":323,"image":324,"path":325,"readingTime":326,"recommend":248,"tags":331,"title":336,"type":264},[269],"2024-09-28 16:26:00","Three years at ByteDance have made time feel strangely accelerated. Amid the changes, I have still managed to hold on to some mental balance. Looking back at the new graduate gazing at distant mountains from the balcony of our Hangzhou office, I can trace how curiosity, workplace challenges, and shifting expectations taught me to find a rhythm of my own.","https:\u002F\u002Fblog-img.774352199.xyz\u002FhnCaht.webp","\u002Fdaily\u002Fwork-for-3-years",{"text":327,"minutes":328,"time":329,"words":330},"10 min read",9.74,584400,1948,[332,333,334,335],"ByteDance","Software engineering careers","Career retrospective","Mental health","Working at ByteDance for Three Years and Staying Somewhat Sane Is Not Entirely Impossible",{"categories":338,"date":340,"description":341,"image":342,"path":343,"readingTime":344,"recommend":248,"tags":349,"title":355,"type":264},[339],"fiddling","2025-05-28 22:09:00","After moving my blog to Astro, the usual Google Analytics integration no longer fit its performance goals. Adding event-reporting JavaScript directly to the head works, but hurts page performance. I used partytown to move the scripts off the main thread so they would not interfere with loading. With a few adjustments to the example code, Google Analytics finally worked, balancing performance with analytics.","https:\u002F\u002Fblog-img.774352199.xyz\u002FQ0w4RN.webp","\u002Ffiddling\u002Fastro-google-tag-manager",{"text":345,"minutes":346,"time":347,"words":348},"2 min read",1.785,107100,357,[350,351,352,353,354],"Astro","Google Analytics","Google Tag Manager","Partytown","Web analytics","Adding Google Analytics to Astro with Tag Manager",{"categories":357,"date":358,"description":359,"image":360,"path":361,"readingTime":362,"recommend":367,"tags":368,"title":373,"type":264},[339],"2023-04-08 13:16:36","Designing a new programming language is challenging and fun. Setting aside complicated compiler theory and implementation details to focus on where code runs helps clarify how languages are built. Starting from the RISC-VI instruction set, this discussion explores the underlying architecture, layered computer systems, and virtual-machine model, reflecting on the nature of programming languages as well as their implementation.","https:\u002F\u002Fblog-img.774352199.xyz\u002FuO420B.webp","\u002Ffiddling\u002Fchitchat-about-programming-language",{"text":363,"minutes":364,"time":365,"words":366},"15 min read",14.275,856500,2855,2,[369,370,371,372],"Programming language design","Type systems","Compiler design","RISC-V","Some Thoughts on Programming Languages",{"categories":375,"date":376,"description":377,"image":378,"path":379,"readingTime":380,"recommend":228,"tags":385,"title":391,"type":264},[339],"2025-04-18 16:43:12","My girlfriend moved from Beijing to Shanghai for work, and I helped arrange broadband too. Shanghai Telecom’s 500M connection costs more than a 1000M line in Hangzhou, frustratingly. I set out to connect the two cities’ networks: transparent proxying in Shanghai, selected traffic exiting through Hangzhou, and access between both LANs. Hangzhou already had a simple setup with a software router and an AP, configured to route my everyday traffic home and ready for the next networking adventure.","https:\u002F\u002Fblog-img.774352199.xyz\u002FO6cAGh.webp","\u002Ffiddling\u002Fcross-city-network-setup",{"text":381,"minutes":382,"time":383,"words":384},"5 min read",4.865,291900,973,[386,387,388,389,390],"Tailscale","mihomo","Site-to-site networking","Transparent proxy","Home networking","Connecting My Shanghai and Hangzhou Networks",{"categories":393,"date":394,"description":395,"image":396,"path":397,"readingTime":398,"recommend":248,"tags":402,"title":408,"type":264},[339],"2021-12-27 00:09:00","The labs are an essential part of learning CSAPP, but setting up Linux can be discouraging. Virtual machines bring installation errors, compatibility problems, and broken networking. WSL (Windows Subsystem for Linux), especially on Windows 10 version 2004 and later, provides a simpler, more direct Linux environment without the complexity and performance bottlenecks of a traditional VM.","https:\u002F\u002Fblog-img.774352199.xyz\u002FvqOC7N.webp","\u002Ffiddling\u002Fcsapplab0",{"text":250,"minutes":399,"time":400,"words":401},5.295,317700,1059,[403,404,405,406,407],"CSAPP","WSL2","Linux","GDB","Lab environment","Setting Up the CSAPP Lab Environment",{"categories":410,"date":411,"description":412,"image":413,"path":414,"readingTime":415,"recommend":248,"tags":420,"title":424,"type":264},[339],"2024-07-13 17:49:00","Using Debian as a side router offers a more stable and flexible alternative without depending on OpenWrt and LuCI. Configuring Debian directly gives you greater control over the system and avoids the limitations and instability of a GUI. Compared with common side-router setups, this approach makes transparent proxying more reliable and offers another option for those who value performance and efficiency.","https:\u002F\u002Fblog-img.774352199.xyz\u002FpPRU5x.webp","\u002Ffiddling\u002Fdebian-as-bypass-router",{"text":416,"minutes":417,"time":418,"words":419},"12 min read",11.79,707400,2358,[421,422,387,423,389],"Debian","Side router","AdGuard Home","Using Debian as a Side Router",{"categories":426,"date":427,"description":428,"image":429,"path":430,"readingTime":431,"recommend":248,"tags":435,"title":440,"type":264},[339],"2024-08-16 23:53:00","This FakeIP-based transparent proxy design addresses the single point of failure, poor performance, and awkward port forwarding of a traditional side router. Switching to the sing-box proxy core improves forwarding performance and simplifies configuration, with broader protocol support and better optimization than the previous Clash setup. Clash can implement the same design, but sing-box provides a flexible alternative.","https:\u002F\u002Fblog-img.774352199.xyz\u002FS2HHD5.webp","\u002Ffiddling\u002Ffake-ip-based-transparent-proxy",{"text":327,"minutes":432,"time":433,"words":434},9.395,563700,1879,[436,437,389,438,439],"FakeIP","sing-box","Policy-based routing","Traffic routing","Routing Transparent Proxy Traffic with FakeIP",{"categories":442,"date":443,"description":444,"image":445,"path":446,"readingTime":447,"recommend":248,"tags":451,"title":455,"type":264},[339],"2024-08-15 23:50:00","Port forwarding on the main router often stops working when a side router is introduced. Setting the side router as the gateway changes the forwarding path, breaking mappings that previously relied on the main router. A gateway translates addresses and forwards traffic from the internal network to the outside, and each internal device needs one to communicate externally. Understanding this mechanism helps explain how to fix the forwarding problem.","https:\u002F\u002Fblog-img.774352199.xyz\u002FmRqws9.webp","\u002Ffiddling\u002Ffix-port-forward-in-bypass-router",{"text":275,"minutes":448,"time":449,"words":450},3.03,181800,606,[422,452,453,454],"Port forwarding","NAT","Network troubleshooting","Fixing Port Forwarding with a Side Router",{"categories":457,"date":458,"description":459,"image":460,"path":461,"readingTime":462,"recommend":248,"tags":467,"title":471,"type":264},[339],"2023-02-02 23:24:55","Inspiration struck during Chinese New Year: an article read on the train about running Go on bare metal sparked an interest in low-level system interfaces. Its successful implementation suggested exciting possibilities for combining a high-level language with an OS. Further research revealed earlier work on the idea, and that growing enthusiasm became a project full of anticipation that ultimately did not work out.","https:\u002F\u002Fblog-img.774352199.xyz\u002FxB1Ni5.webp","\u002Ffiddling\u002Fgo-os",{"text":463,"minutes":464,"time":465,"words":466},"8 min read",7.29,437400,1458,[468,372,318,469,470],"Go","Bare-metal programming","Runtime","A Project That Failed: Seven Not-So-Happy Days over Chinese New Year",{"categories":473,"date":474,"description":475,"image":476,"path":477,"readingTime":478,"recommend":248,"tags":483,"title":487,"type":264},[339],"2022-08-15 01:05:01","While refactoring a system, converting entities between layers made deep copying surprisingly awkward. A product VO in the view layer, an entity in the domain layer, and a PO in the persistence layer can look nearly identical, yet small type differences complicate direct conversion. I used reflection to build a general conversion method, reducing repetitive assembler methods and making the code more maintainable and flexible.","https:\u002F\u002Fblog-img.774352199.xyz\u002FBlhm0I.webp","\u002Ffiddling\u002Fgolang-deepcopy-between-different-type",{"text":479,"minutes":480,"time":481,"words":482},"3 min read",2.855,171300,571,[468,484,485,486],"Reflection","Deep copy","Struct conversion","Deep Copying Between Different Struct Types in Go",{"categories":489,"date":490,"description":491,"image":492,"path":493,"readingTime":494,"recommend":248,"tags":498,"title":504,"type":264},[339],"2025-03-31 23:51:00","Periodically syncing heart-rate data from Apple Health to a server and visualizing it in Grafana provides an intuitive way to monitor it. Health Auto Export sends the data to an HTTP endpoint through its REST API automation, the server stores it in InfluxDB, and Grafana presents a clear dashboard for tracking and analyzing personal heart-rate changes.","https:\u002F\u002Fblog-img.774352199.xyz\u002FF4qD2T.webp","\u002Ffiddling\u002Fheart-rate-to-grafana",{"text":479,"minutes":495,"time":496,"words":497},2.45,147000,490,[499,500,501,502,503],"Apple Watch","Health Auto Export","InfluxDB","Grafana","Heart rate monitoring","My Heart Beats for U: Visualizing Heart Rate in Grafana",{"categories":506,"date":507,"description":508,"image":509,"path":510,"readingTime":511,"recommend":515,"tags":516,"title":522,"type":264},[339],"2025-06-10 20:18:00","macOS and iPadOS 26 introduce the Liquid Glass design language, refreshing icons and windows with a more modern look. Windowed apps on iPad are a step toward making it a productivity device. The transparent Control Center and the integration of Launchpad have been controversial, however, and the experience still needs work. Despite its shortcomings, this update lays groundwork for a promising future.","https:\u002F\u002Fblog-img.774352199.xyz\u002FTPSaLE.webp","\u002Ffiddling\u002Fmacos-26-trial",{"text":275,"minutes":512,"time":513,"words":514},3.305,198300,661,5,[517,518,519,520,521],"macOS 26","iPadOS 26","Liquid Glass","Apple Intelligence","Operating system impressions","Trying Out macOS 26 and iPadOS 26",{"categories":524,"date":525,"description":526,"image":527,"path":528,"readingTime":529,"recommend":248,"tags":533,"title":537,"type":264},[339],"2026-06-11 10:00:00","Another WWDC brings new Apple Intelligence features in macOS 27. The workarounds from macOS 26 no longer get through, so the battle of wits with Apple continues.","https:\u002F\u002Fblog-img.774352199.xyz\u002FfOFucm.webp","\u002Ffiddling\u002Fmacos-27-apple-intelligence-chatgpt",{"text":250,"minutes":530,"time":531,"words":532},5.1,306000,1020,[534,520,535,536],"macOS 27","ChatGPT","Mainland China Mac","Getting Apple Intelligence Working on a China-Market Mac: Region Changes and Extracting ChatGPT",{"categories":539,"date":540,"description":541,"image":542,"path":543,"readingTime":544,"recommend":248,"tags":548,"title":554,"type":264},[339],"2025-07-20 23:32:00","MoonTV is a new video aggregation platform built with Next.js and React to make following shows convenient. It began as an attempt to improve LibreTV and has attracted substantial attention and usage over several months of development. Cursor made development efficient, although multi-platform support and complex data dependencies posed challenges. As its user base grows, MoonTV continues improving in response to feedback.","https:\u002F\u002Fblog-img.774352199.xyz\u002FnIeONi.webp","\u002Ffiddling\u002Fmoontv-vibe-coding",{"text":250,"minutes":545,"time":546,"words":547},5.76,345600,1152,[549,550,551,552,553],"MoonTV","LunaTV","Vibe Coding","Cursor","AI-assisted development","MoonTV: An Experiment in Vibe Coding",{"categories":556,"date":557,"description":558,"image":559,"path":560,"readingTime":561,"recommend":248,"tags":565,"title":568,"type":264},[339],"2024-10-07 16:51:00","BGP-based routing for Chinese and overseas IPs makes transparent proxying more efficient and accurate. Marking overseas destinations with FakeIP lets the main router route traffic more intelligently for smoother connectivity. The sing-box DNS configuration is also refined to handle queries more flexibly and efficiently, improving the overall network experience.","https:\u002F\u002Fblog-img.774352199.xyz\u002FMOmM1s.webp","\u002Ffiddling\u002Fmore-accurate-chnroute",{"text":275,"minutes":562,"time":563,"words":564},3.805,228300,761,[566,567,438,439],"BGP","IP address database","More Accurate Routing for Chinese and Overseas IPs with BGP",{"categories":570,"date":571,"description":572,"image":573,"path":574,"readingTime":575,"recommend":248,"tags":579,"title":584,"type":264},[339],"2025-12-14 14:31:00","A friend’s comment got me interested in simulation games. After buying Microsoft Flight Simulator, I discovered streamed maps and models, another account login, oddly hidden tutorials, and awkward keyboard controls. A flight stick and some tinkering with Pico VR finally rounded out the experience.","https:\u002F\u002Fblog-img.774352199.xyz\u002FlfhEuE.webp","\u002Ffiddling\u002Fmsfs2024-joystick-and-pico",{"text":381,"minutes":576,"time":577,"words":578},4.68,280800,936,[580,581,582,583],"Microsoft Flight Simulator 2024","Thrustmaster TCA","PICO VR","Flight simulation","Adventures with Microsoft Flight Simulator 2024",{"categories":586,"date":587,"description":588,"image":589,"path":590,"readingTime":591,"recommend":248,"tags":595,"title":601,"type":264},[339],"2025-06-05 23:26:00","My frequent phone changes took me from OnePlus to iPhone, and from enjoying tinkering to relying on an ecosystem. At my girlfriend’s suggestion, I recently bought an OPPO Find X8 Ultra to improve my photos. Migrating apps out of Apple’s ecosystem reminded me how uneven Android’s app selection remains and how difficult finding replacements can be. A month of migration has been an exercise in friction and adaptation between platforms.","https:\u002F\u002Fblog-img.774352199.xyz\u002F19NIhZ.webp","\u002Ffiddling\u002Fone-month-using-android",{"text":463,"minutes":592,"time":593,"words":594},7.61,456600,1522,[596,597,598,599,600],"Android","OPPO","iOS","Smartphone impressions","Ecosystem migration","One Month After Switching to Android",{"categories":603,"date":604,"description":605,"image":606,"path":607,"readingTime":608,"recommend":248,"tags":612,"title":618,"type":264},[339],"2026-05-11 22:43:00","I found a trip-planning tool on GitHub Trending and wanted to host my own instance. Since I was getting a new VPS anyway, I might as well install a little extra. Well, quite a lot extra.","https:\u002F\u002Fblog-img.774352199.xyz\u002FBBdDWW.webp","\u002Ffiddling\u002Fone-trek-twenty-stacks",{"text":250,"minutes":609,"time":610,"words":611},5.735,344100,1147,[613,614,615,616,617],"TREK","Docker Compose","Dockge","VPS","Self-hosting","One TREK, Twenty Stacks",{"categories":620,"date":621,"description":622,"image":623,"path":624,"readingTime":625,"recommend":248,"tags":630,"title":634,"type":264},[339],"2025-01-16 23:09:00","OPNsense is an open-source firewall and router with an attractive interface and a comprehensive feature set. After trying several routing setups, I came to appreciate its potential for transparent proxying and traffic routing. Combining it with BGP-based routing offers better security and stability, while its automatically updated IP lists make network management more convenient.","https:\u002F\u002Fblog-img.774352199.xyz\u002FxA8C1E.webp","\u002Ffiddling\u002Fopnsense-transparent-proxy",{"text":626,"minutes":627,"time":628,"words":629},"7 min read",6.47,388200,1294,[631,632,387,633,389,439],"OPNsense","FreeBSD","tun2socks","Transparent Proxying and Traffic Routing with OPNsense",{"categories":636,"date":637,"description":638,"image":639,"path":640,"readingTime":641,"recommend":231,"tags":645,"title":651,"type":264},[339],"2025-03-15 20:35:00","Distinguishing user-defined type names from ordinary variables is a challenge during parsing. A statement such as `a*b;` can be either an arithmetic expression or a declaration. Grammar rules, especially those involving type specifiers, can misidentify variables as types, affecting correctness and readability. The prevalence of declarations without initializers makes this ambiguity especially common.","https:\u002F\u002Fblog-img.774352199.xyz\u002F2VKHK9.webp","\u002Ffiddling\u002Fparser-type-variable-ambiguity",{"text":463,"minutes":642,"time":643,"words":644},7.475,448500,1495,[646,647,648,649,650],"Parsing","GLR","Symbol table","Scope","Disambiguation","Resolving Type Name and Variable Name Ambiguity in Parsing",{"categories":653,"date":654,"description":655,"image":656,"path":657,"readingTime":658,"recommend":248,"tags":662,"title":667,"type":264},[339],"2023-05-24 17:51:09","To install the RISC-V toolchain, first obtain the riscv-gnu-toolchain source. Using `--depth=1` when cloning reduces the download size. Check the README’s Prerequisites section and install the required dependencies. On Debian, a simple package installation command prepares the environment for building the toolchain.","https:\u002F\u002Fblog-img.774352199.xyz\u002FrWNOKx.webp","\u002Ffiddling\u002Fspike-install",{"text":345,"minutes":659,"time":660,"words":661},1.92,115200,384,[372,663,664,665,666],"Spike","riscv-pk","Cross-compilation","Toolchain","Installing the RISC-V Toolchain and Emulator",{"categories":669,"date":670,"description":671,"image":672,"path":673,"readingTime":674,"recommend":678,"tags":679,"title":686,"type":264},[339],"2026-09-09 23:59:00","This counts as NTR too, surely.","https:\u002F\u002Fblog-img.774352199.xyz\u002FNDPUwc.webp","\u002Ffiddling\u002Fsteamdeck-switch-60fps",{"text":463,"minutes":675,"time":676,"words":677},7.345,440700,1469,7,[680,681,682,683,684,685],"Steam Deck","Nintendo Switch","EmuDeck","Eden","Lossless Scaling","Frame generation","A Second Life for the Steam Deck: Switch Emulation and Frame Generation at 60 FPS",{"categories":688,"date":689,"description":690,"image":691,"path":692,"readingTime":693,"recommend":248,"tags":697,"title":702,"type":264},[339],"2024-06-23 15:31:32","The GFW does more than monitor an exit gateway: it inspects international traffic through passive taps, copying inbound and outbound IP packets to a cluster for analysis and filtering. Understanding where and how this happens matters when studying censorship circumvention. Examining the GFW’s network topology helps explain its blocking mechanisms and how to work around them.","https:\u002F\u002Fblog-img.774352199.xyz\u002FsOpJuL.webp","\u002Ffiddling\u002Ftech-about-gfw",{"text":416,"minutes":694,"time":695,"words":696},11.885,713100,2377,[698,699,700,701],"GFW","DNS poisoning","TCP","Internet censorship","How the Great Firewall Works",{"categories":704,"date":705,"description":706,"image":707,"path":708,"readingTime":709,"recommend":248,"tags":713,"title":718,"type":264},[339],"2022-04-16 00:01:28","In Java, using `this` can prevent the compiler from optimizing constants. Although `ab1` and `ab2` in this example appear to refer to the same static final variable `s`, comparing them produces different results. `ab1` concatenates a direct reference to the static variable, while `ab2` accesses it through `this`, preventing the same constant propagation optimization. This illustrates how a small syntactic difference can change compilation behavior.","https:\u002F\u002Fblog-img.774352199.xyz\u002FgKtkYe.webp","\u002Ffiddling\u002Fthis-in-javac-string-concat",{"text":479,"minutes":710,"time":711,"words":712},2.305,138300,461,[260,714,715,716,717],"javac","String concatenation","Constant propagation","Bytecode","How Java’s this Keyword Can Prevent Compile-Time Constant Propagation",{"categories":720,"date":721,"description":722,"image":723,"path":724,"readingTime":725,"recommend":248,"tags":730,"title":736,"type":264},[339],"2025-01-29 21:58:00","A microblog lets you share short thoughts whenever they occur, without the overhead of publishing a full static-blog post. This implementation uses Cloudflare Workers for the backend and KV for storage and management. A Vue component embedded in VitePress displays the updates, adding a lively, interactive element to the blog.","https:\u002F\u002Fblog-img.774352199.xyz\u002FhZX6hr.webp","\u002Ffiddling\u002Fvitepress-memos-component",{"text":726,"minutes":727,"time":728,"words":729},"19 min read",18.96,1137600,3792,[731,732,733,734,735],"VitePress","Vue","Cloudflare Workers","Cloudflare KV","Microblogging","Adding a Microblog to VitePress",{"categories":738,"date":739,"description":740,"image":741,"path":742,"readingTime":743,"recommend":248,"tags":747,"title":750,"type":264},[339],"2025-03-15 16:24:00","A Telegram notification on an ordinary afternoon introduced a tempting VPS deal: a direct China Telecom CN2 route and 2.5G bandwidth. The plan includes IPv6, useful for unlocking streaming services, but not every connection needs to go through WARP. My previous script was convenient, yet its effects on speed and traffic routing called for a more flexible solution.","https:\u002F\u002Fblog-img.774352199.xyz\u002FMcjrrF.webp","\u002Ffiddling\u002Fvps-warp-ipv6",{"text":479,"minutes":744,"time":745,"words":746},2.34,140400,468,[748,749,616,438,439],"Cloudflare WARP","IPv6","Routing Selected VPS Traffic Through WARP over IPv6",{"categories":752,"date":753,"description":754,"image":755,"path":756,"readingTime":757,"recommend":761,"tags":762,"title":768,"type":264},[339],"2026-08-17 23:59:21","The more I tinker, the more I want to tinker. Still going strong in my old age, still going strong!","https:\u002F\u002Fblog-img.774352199.xyz\u002FqfxB0h.webp","\u002Ffiddling\u002Fxiaomi17-root-and-hide-root",{"text":381,"minutes":758,"time":759,"words":760},4.545,272700,909,6,[763,764,765,766,767],"Xiaomi 17","Bootloader","Android Root","Firmware flashing","Root hiding","Making the Xiaomi 17 My Daily Driver: Unlocking, Flashing, Rooting, and Hiding Root",{"categories":770,"date":772,"description":773,"image":774,"path":775,"readingTime":776,"recommend":248,"tags":780,"title":785,"type":264},[771],"notes","2022-01-20 22:29:00","Lab 1 asks us to implement a MapReduce system with two core components: a master and workers. This requires a good command of Go RPC and concurrent programming, along with a thorough understanding of the MapReduce workflow. I built two versions, starting with mutex locks and then moving to a more elegant channel-based implementation without explicit locks, whose design is simpler and clearer. The key to understanding the lab is to read the relevant documentation carefully, especially the flowcharts and explanations.","https:\u002F\u002Fblog-img.774352199.xyz\u002FibVwPJ.webp","\u002Fnotes\u002F65840\u002Fmapreducelab",{"text":463,"minutes":777,"time":778,"words":779},7.21,432600,1442,[781,782,468,783,784],"MIT 6.5840","MapReduce","RPC","Concurrent programming","6.5840 Lab 1: MapReduce",{"categories":787,"date":788,"description":789,"image":790,"path":791,"readingTime":792,"recommend":248,"tags":796,"title":801,"type":264},[771],"2022-01-16 17:32:00","MapReduce is an efficient parallel computing model designed to simplify processing large datasets. By defining the two key functions, Map and Reduce, users can break complex tasks into simple operations. The framework automatically handles data distribution and task scheduling, allowing developers to focus on the algorithm rather than low-level details. Its widespread use in distributed systems demonstrates its flexibility and practical value.","https:\u002F\u002Fblog-img.774352199.xyz\u002FApIDdC.webp","\u002Fnotes\u002F65840\u002Fmapreducepaper",{"text":381,"minutes":793,"time":794,"words":795},4.12,247200,824,[782,797,798,799,800],"Distributed systems","Paper notes","Parallel computing","Fault tolerance","Reading the MapReduce Paper",{"categories":803,"date":804,"description":805,"image":806,"path":807,"readingTime":808,"recommend":248,"tags":813,"title":817,"type":264},[771],"2022-12-16 02:06:10","Lab 2A focuses on implementing Raft leader election and heartbeats so that elections and term changes work correctly even under extreme conditions. The lab has four stages and lays the foundation for the distributed key-value store that follows. A design without explicit locks simplifies the Raft struct. The lab instructions provide the necessary background, but compared with the previous lab, this one relies on almost no reference material and places greater emphasis on implementing the system independently.","https:\u002F\u002Fblog-img.774352199.xyz\u002Fc11Uk4.webp","\u002Fnotes\u002F65840\u002Fraftlab2a",{"text":809,"minutes":810,"time":811,"words":812},"13 min read",12.235,734100,2447,[781,814,468,815,816],"Raft","Leader election","Distributed consensus","6.5840 Lab 2A: Leader Election",{"categories":819,"date":820,"description":821,"image":822,"path":823,"readingTime":824,"recommend":248,"tags":829,"title":831,"type":264},[771],"2022-12-03 21:40:09","Raft is a consensus algorithm designed to improve the efficiency of log replication. It is particularly suited to clusters of machines, allowing them to keep providing service even when some machines fail. It uses the replicated state machine model: logs record the order of commands so that every machine in the cluster can reach the same state. In Search of an Understandable Consensus Algorithm explores Raft’s design and compares it with Paxos, highlighting its understandability and providing a foundation for building reliable large-scale software systems. These reading notes aim to help explain the paper’s core concepts and their applications.","https:\u002F\u002Fblog-img.774352199.xyz\u002F7mmvIZ.webp","\u002Fnotes\u002F65840\u002Freftextendedpaper",{"text":825,"minutes":826,"time":827,"words":828},"16 min read",15.98,958800,3196,[814,816,830,798],"Log replication","Reading the Raft Paper",{"categories":833,"date":834,"description":835,"image":836,"path":837,"readingTime":838,"recommend":842,"tags":843,"title":846,"type":264},[238],"2021-11-27 14:43:00","MYDB is a personal project exploring and implementing the fundamentals of databases, built in my spare time over a little more than half a month. I picked up some basic knowledge in my university database systems course, though during my internship I mostly used the classes as an excuse to slack off. My candid answers in an interview did not cause too much trouble, but they did make me reconsider what I knew about databases and decide to learn through hands-on practice. That was how this project began.","https:\u002F\u002Fblog-img.774352199.xyz\u002Fxfci2J.webp","\u002Fprojects\u002Fmydb\u002Fmydb0",{"text":381,"minutes":839,"time":840,"words":841},4.15,249000,830,1,[259,260,844,845],"Database implementation","Database architecture","MYDB 0. Project Structure and a Few Things I Had to Say",{"categories":848,"date":849,"description":850,"image":851,"path":852,"readingTime":853,"recommend":248,"tags":857,"title":860,"type":264},[238],"2021-11-28 16:10:00","MYDB manages transactions through an XID file. Each transaction has a unique XID, incrementing from 1; XID 0 denotes a super transaction whose state is always committed. TransactionManager maintains this file and records three states: active, committed, and aborted. This mechanism supports accurate transaction state queries and management, providing a foundation for system stability and reliability.","https:\u002F\u002Fblog-img.774352199.xyz\u002FH4zZAK.webp","\u002Fprojects\u002Fmydb\u002Fmydb1",{"text":381,"minutes":854,"time":855,"words":856},4.755,285300,951,[259,260,858,859],"Transaction management","XID","MYDB 1. Starting with the Transaction Manager",{"categories":862,"date":863,"description":864,"image":865,"path":866,"readingTime":867,"recommend":248,"tags":871,"title":875,"type":264},[238],"2021-12-25 18:26:00","MYDB uses a client\u002Fserver architecture similar to MySQL, allowing multiple clients to connect to a server over sockets, execute SQL queries, and receive results. Communication uses a special binary format, though plain text would also be an option for a simpler implementation. The basic transport structure supports effective communication and processing between client and server.","https:\u002F\u002Fblog-img.774352199.xyz\u002FPAHrUZ.webp","\u002Fprojects\u002Fmydb\u002Fmydb10",{"text":381,"minutes":868,"time":869,"words":870},4.305,258300,861,[259,260,872,873,874],"Socket","Client-server architecture","Communication protocol","MYDB 10. Implementing the Server, Client, and Wire Protocol",{"categories":877,"date":239,"description":240,"image":243,"path":247,"readingTime":878,"recommend":248,"tags":879,"title":5,"type":264},[238],{"text":250,"minutes":251,"time":252,"words":253},[259,260,261,262,263],{"categories":881,"date":882,"description":883,"image":884,"path":885,"readingTime":886,"recommend":248,"tags":890,"title":893,"type":264},[238],"2021-12-05 15:28:00","DM abstracts the filesystem into pages and uses them as the unit of reading, writing, and caching. The default page size is 8K, with larger pages available to improve write performance under heavy loads. With the general-purpose cache framework already in place, we now define the page structure and implement efficient page caching.","https:\u002F\u002Fblog-img.774352199.xyz\u002FjlFC4E.webp","\u002Fprojects\u002Fmydb\u002Fmydb3",{"text":381,"minutes":887,"time":888,"words":889},4.7,282000,940,[259,260,891,892],"Data pages","Cache management","MYDB 3. Caching and Managing Data Pages",{"categories":895,"date":896,"description":897,"image":898,"path":899,"readingTime":900,"recommend":248,"tags":904,"title":907,"type":264},[238],"2021-12-08 22:55:00","Log files are essential to MYDB’s design, allowing data to be recovered after a crash. DM logs every operation on underlying data, forming a continuous sequence of records. Stored in a specific binary format with checksums and individual operation records, these logs let the database accurately reconstruct its data on restart and maintain consistency and integrity.","https:\u002F\u002Fblog-img.774352199.xyz\u002FTRcbsj.webp","\u002Fprojects\u002Fmydb\u002Fmydb4",{"text":463,"minutes":901,"time":902,"words":903},7.885,473100,1577,[259,260,905,906],"Database logging","Crash recovery","MYDB 4. Log Files and Recovery Strategies",{"categories":909,"date":910,"description":911,"image":912,"path":913,"readingTime":914,"recommend":248,"tags":918,"title":921,"type":264},[238],"2021-12-11 15:16:00","The page index is an important part of DM, optimizing insertions by caching the free space available on each page. It lets higher-level modules quickly locate a suitable page without a lengthy search, making data operations more efficient. Its implementation works closely with the DataItem abstraction to support efficient database operation.","https:\u002F\u002Fblog-img.774352199.xyz\u002F22PSG1.webp","\u002Fprojects\u002Fmydb\u002Fmydb5",{"text":626,"minutes":915,"time":916,"words":917},6.37,382200,1274,[259,260,919,920],"Page index","Data management","MYDB 5. The Page Index and the Data Manager",{"categories":923,"date":924,"description":925,"image":926,"path":927,"readingTime":928,"recommend":248,"tags":932,"title":936,"type":264},[238],"2021-12-18 14:58:00","VM uses two-phase locking to ensure serializable schedules and introduces multiversion concurrency control (MVCC) to eliminate blocking between reads and writes. This chapter also defines conflicts between database operations, focusing on the interaction between updates and reads as a foundation for understanding transaction isolation levels.","https:\u002F\u002Fblog-img.774352199.xyz\u002F8YzotA.webp","\u002Fprojects\u002Fmydb\u002Fmydb6",{"text":292,"minutes":929,"time":930,"words":931},8.64,518400,1728,[259,260,933,934,935],"MVCC","Transaction isolation","Two-phase locking","MYDB 6. Record Versions and Transaction Isolation",{"categories":938,"date":939,"description":940,"image":941,"path":942,"readingTime":943,"recommend":248,"tags":947,"title":950,"type":264},[238],"2021-12-23 21:20:00","VM must handle version skipping introduced by MVCC as well as deadlocks. By simply marking a transaction, MYDB can cancel or roll it back and keep data from aborted transactions from affecting others. This design makes concurrent transaction handling more efficient and reliable, avoids the deadlock risks common with traditional 2PL, and improves overall stability and performance.","https:\u002F\u002Fblog-img.774352199.xyz\u002FBF3yDW.webp","\u002Fprojects\u002Fmydb\u002Fmydb7",{"text":463,"minutes":944,"time":945,"words":946},7.265,435900,1453,[259,260,948,949],"Deadlock detection","Version management","MYDB 7. Deadlock Detection and the Version Manager",{"categories":952,"date":953,"description":954,"image":955,"path":956,"readingTime":957,"recommend":248,"tags":958,"title":961,"type":264},[238],"2021-12-24 21:01:00","MYDB implements a clustered index using a B+ tree. IM interacts directly with the Data Manager (DM), bypassing the Version Manager (VM), so index data is written directly to the database file. This chapter details the binary-tree index structure and its basic node fields, including the leaf flag, key count, and sibling identifier, establishing the framework for indexed lookups.","https:\u002F\u002Fblog-img.774352199.xyz\u002Ff92X4o.webp","\u002Fprojects\u002Fmydb\u002Fmydb8",{"text":381,"minutes":839,"time":840,"words":841},[259,260,959,960],"B+ tree","Database indexing","MYDB 8. Index Management",{"categories":963,"date":964,"description":965,"image":966,"path":967,"readingTime":968,"recommend":248,"tags":972,"title":975,"type":264},[238],"2021-12-25 15:44:00","The Table Manager (TBM) manages field and table structures. Parser turns SQL-like statements into structured representations, wrapping their information in the corresponding classes to simplify subsequent operations. This chapter also covers MYDB’s SQL syntax as a foundation for understanding the management process.","https:\u002F\u002Fblog-img.774352199.xyz\u002FzOMyv5.webp","\u002Fprojects\u002Fmydb\u002Fmydb9",{"text":250,"minutes":969,"time":970,"words":971},5.035,302100,1007,[259,260,973,974],"SQL parsing","Table management","MYDB 9. Field and Table Management",1789914053654]