Showing Posts From
Hugo
Shubham Kumar- 02 Mar, 2025
Working on mulitple braches at once using git worktree
Working on multiple branches at once Git worktree enables you to have multiple working directories linked to the same repository. Each directory can have its own branch checked out. And you can edit the code for each feature without changing or stashing the changes you did on each branch. Worktree creation The following command will create a new directory, debug with branch1 checked out. You can browse the ../debug directory and change the code as per your development for branch1. git worktree add ../debug branch1pwd echo somenewfeature on branch 1 > somenewfeature cat somenewfeature/home/cold/Projects/Personal/gitworktreedemo/debug somenewfeature on branch 1In the meanwhile you can keep editing the root directory with the main branch. pwd echo somenewfeature on main 1 > somenewfeatureonmain cat somenewfeatureonmain/home/cold/Projects/Personal/gitworktreedemo/main somenewfeature on main 1You can run the git commands going inside each directory. pwd git add . git status/home/cold/Projects/Personal/gitworktreedemo/main On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: somenewfeatureonmainpwd git add . git status/home/cold/Projects/Personal/gitworktreedemo/debug On branch branch1 Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: somenewfeatureList existing worktrees You can list your worktrees from the main as well as the debug directories. git worktree list/home/cold/Projects/Personal/gitworktreedemo/main e0ccbca [main] /home/cold/Projects/Personal/gitworktreedemo/debug 42ad28a [branch1]Remove a worktree When you are finally done with the feature branch, you can delete the worktree using remove command. git worktree remove ../debug ls .. git worktree listmain /home/cold/Projects/Personal/gitworktreedemo/main e0ccbca [main]Any commit you did on the feature branch will automatically be reflected in the main codebase. git checkout branch1 git log --onelineA somenewfeatureonmain 82cad2d done 42ad28a A commit on branch1 e0ccbca A commit on mainAdvice on working with worktreesAs the new directory will not be ignored by default, it is best to create your worktree directories out of git rootThe way I create my worktrees is I create a new directory with project name. Then I clone my actual repository inside this directory and keep my worktrees on the same level as project. For example, my main project is cloned inside main directory and I created debug directory in the same level as main. tree .. ├── debug │ └── somenewfeature └── main └── somenewfeatureonmain2 directories, 2 files
Shubham Kumar- 23 Feb, 2025
Upgrading Hugo to latest version on Debian based systems
There is a bad news for Ubuntu (or any Debian based OS) user if you have installed Hugo using apt. The official apt repository is not being updated continuously. Today, I wanted to experiment with the Tailwind support introduced in Hugo 0.128.0. But the apt package manager does not has this version. The latest version is 0.92.2 as of now. apt list -a hugo: Listing... : hugo/jammy-updates,jammy-security,now 0.92.2-1ubuntu0.1 amd64 [installed] : hugo/jammy 0.92.2-1 amd64In such scenarios, you can install the .deb file directly from the published releases. The disadvantage of this method is, you will lose the ability to upgrade the version automatically. In the future, you will be restricted to download the deb file and install the updated version. Anyways, here are the steps to do so. First look for the latest (or the version you want) to install at the GoHugoIO release page - https://github.com/gohugoio/hugo/releases At the time of writing this post, the latest version is 0.144.2. Any my current version is 0.92.2. hugo version: hugo v0.92.2+extended linux/amd64 BuildDate=2023-01-31T11:11:57Z VendorInfo=ubuntu:0.92.2-1ubuntu0.1From the Downloads directory, I can run the below command to download the deb file that I want to install. You can use any directory to download this file. Generally people use /tmp, but I like all my downloads in my Downloads directory. wget https://github.com/gohugoio/hugo/releases/download/v0.144.2/hugo_0.144.2_linux-amd64.debNext you can install the deb file as follows. ls | grep hugo: hugo_0.144.2_linux-amd64.debdpkg -i hugo_0.144.2_linux-amd64.debThis should upgrade your installation to latest. hugo version: hugo v0.144.2-098c68fd18f48031a7145bedab30cbaede48858f linux/amd64 BuildDate=2025-02-19T12:17:04Z VendorInfo=gohugoioHope this will help you upgrading Hugo to latest version on Debian based systems.
Shubham Kumar- 17 Feb, 2025
Designing my own theme in Hugo - II
What we did and what we will do? This is a part blog in which I am documenting my learning process of creating my own theme in Hugo. Previously, we went through the basic flow of creating a theme. My current theme is poison theme and we replaced the theme with our new theme which we named, "awesometheme". But unfortunately, after applying the new theme, the rendered page was blank. And in this post, we will expand our knowledge of Hugo and lean to render single pages. Layout and content separation Hugo architecture keeps content separate from the layout. This allows me to keep my blog post contents as it is and apply a new theme on the top of it. There are 3 general pages and for each page there is a layout. These pages are home page, single page and list pages. The home page is the landing page of your site. For a blog site, it can be the introductory page with list of recent blogs. The single page defines a page with content. For example, the blogs itself. And list pages are the pages that displays the list of items. For example, a list of blogs ordered in some preference. You can define and customize more pages based on your preference but these are the basic ones. As I already have the content ready, let's explore the layout designs first. Homepage The home page layout is defined under layouts/index.html. Currently, there isn't anything present in my index.html. Let's write something arbitrary in it and check if something happens. <html> <head> <title>Shubham's Blog</title> </head> <body> <div>Hi all,</div> <div>How are you guys</div> </body> </html>curl localhost:1313<html> <head> <meta name="generator" content="Hugo 0.92.2" /><script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant defer></script> <title>Shubham's Blog</title> </head> <body> <div>Hi all,</div> <div>How are you guys</div> </body> </html>Now, I can see the title and body as I defined. List Layout Let's try to render a list of blogs. The _default/list.html defines a default layout for all the list items. I can enter any HTML code and it will be displayed. But I want to do something more. I want to iterate through all the blog contents which I keep in posts directory and create a link for the blog page. This is where Hugo uses GoLang's templating library. Anything beginning with a . refers to the current context. I will explore more on contexts later. The reference, .Pages contains a list of all the pages for this section. So if, we are referring to posts directory, then .Pages will contain all the blogs inside the posts directory. The for-loop is done using range keyword. The range keyword iterates through a list and changes the context to the current item. So calling .Title inside the range block will refer to the current blog's title. We can do something as follows. <html> <head> <title>Shubham's Blog</title> </head> <body> <div>List of my recent posts ...</div> <ul>{{range .Pages}} <li>{{.Title}}</li> {{end}} </ul> </body> </html>curl http://localhost:1313/posts/<html> <head><script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant defer></script> <title>Shubham's Blog</title> </head> <body> <div>List of my recent posts ...</div> <ul> <li>Designing my own theme in Hugo - II</li> <li>Designing my own theme in Hugo - I</li> <li>Julia and basic matrix operations</li> <li>AOP in pure Java, keeping logging simple and aside</li> <li>Object pool design pattern in Java</li> <li>Downloading a single file from 2 independent apps</li> <li>Syncing org roam files across devices in WSL2 environment with better performance</li> <li>Reflection API in Java</li> </ul> </body> </html>Single page layout The single pages like the actual blog post layout can be defined inside, _default/single.html Same as what we did in the list layout, we can call .Content to display the content of the current page. <html> <head> <title>Shubham's Blog</title> </head> <body> {{.Content}} </body> </html>And awesome, I can see my contents as well. curl http://localhost:1313/posts/designing-my-own-theme-in-hugo-i/ | head -n 10<html> <head><script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant defer></script> <title>Shubham's Blog</title> </head> <body> <h2 id="hugo-is-all-about-themes">Hugo is all about themes</h2> <p>Hugo is a developers dream of static sites. Whether you are planning on a new blog site or maybe a documentation for a product, you can easily launch a static site using Hugo.</p> <p>The most powerful feature of Hugo is its simplicity. It is designed to separate your content from styling.Summary This post allowed us to display the contents of our blogs. We saw, how to design our layout for 3 basic types of pages, homepage, list page and single page. We explored some of the GoLang's templating features and got a shallow understanding of contexts. Next, let's make our blog more dynamic and good looking.
Shubham Kumar- 15 Feb, 2025
Designing my own theme in Hugo - I
Hugo is all about themes Hugo is a developers dream of static sites. Whether you are planning on a new blog site or maybe a documentation for a product, you can easily launch a static site using Hugo. The most powerful feature of Hugo is its simplicity. It is designed to separate your content from styling. This means you can write your content in normal markdown files independent of all the website pages and layouts. The actual design of the website can be stored as a separate theme. And as it is independent of the content, you can easily switch themes without ever thinking about your content. Creating a new theme To create a new theme, you can just use the below hugo command. It will create a new directory under theme with your theme name. hugo new theme <theme-name>The below files will be created on executing the above command. Let's get an idea on what are these files in this blog. . └── awesometheme ├── LICENSE ├── archetypes │ └── default.md ├── layouts │ ├── 404.html │ ├── _default │ │ ├── baseof.html │ │ ├── list.html │ │ └── single.html │ ├── index.html │ └── partials │ ├── footer.html │ ├── head.html │ └── header.html ├── static │ ├── css │ └── js └── theme.toml8 directories, 11 filesThe interesting part are the archetypes and layouts directory. The layout directory contains the template for your pages. A single page template is defined inside single.html. A page containing a list of items like blog posts are defined inside list.html. The archetypes directory provides the defaults for a type of content. When you run hugo new content post/learninghugo, the new file is created using the default template as described in archetypes/default.md, given you don't have any overrides. Applying the theme My blogs are written in Hugo so I can just start experimenting with my blogs theme. This way, I can forget about generating any dummy content for my theme. To tell Hugo to use a theme, you can just update the theme entry in your config. My blog site uses toml, so below are the changes. - theme = "poison" + theme = "awesometheme"Now let's run the dev server using hugo server --buildDrafts. And I got the below error. 2025-02-15 06:00:39.535 +0530 ERROR 2025/02/15 06:00:39 render of "section" failed: "/home/cold/Projects/Personal/blog.shubham.codes/layouts/_default/baseof.html:1:3": execute of template failed: template: cv/cv.html:1:3: executing "cv/cv.html" at <partial "head/head.html" .>: error calling partial: partial "head/head.html" not found ERROR 2025/02/15 06:00:39 failed to render pages: render of "home" failed: "/home/cold/Projects/Personal/blog.shubham.codes/layouts/_default/baseof.html:1:3": execute of template failed: template: index.html:1:3: executing "index.html" at <partial "head/head.html" .>: error calling partial: partial "head/head.html" not foundSeems like, I have overridden the poison theme for customization. But now, I am using my own theme which does not contains the poison partials. And this is causing the issue. Both the error corresponds to my layout's directory. So let's rename layouts to layouts_. And we will need to refactor these layouts as well.And this is why you should always keep your code loosely coupledmv layouts layouts_And it worked. Start building sites … hugo v0.92.2+extended linux/amd64 BuildDate=2023-01-31T11:11:57Z VendorInfo=ubuntu:0.92.2-1ubuntu0.1 | EN -------------------+------- Pages | 13 Paginator pages | 0 Non-page files | 0 Static files | 1014 Processed images | 0 Aliases | 0 Sitemaps | 1 Cleaned | 0Built in 88 ms Watching for changes in /home/cold/Projects/Personal/blog.shubham.codes/{archetypes,assets,content,data,static,themes} Watching for config changes in /home/cold/Projects/Personal/blog.shubham.codes/config.toml Environment: "development" Serving pages from memory Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) Press Ctrl+C to stopBut my screen is blank. Summary In this post, we went thorough the basic flow of creating a new theme in Hugo. Next, we will learn about layouts and implement the single and list layout for the blog.
Shubham Kumar- 07 Dec, 2024
Object pool design pattern in Java
What is it? The object pool design pattern exposes a manager to manage a pool of reusable objects. The idea is to keep a know number of reusable objects (with a hard limit to initialize some more lazily). Whenever someone need the object from the pool, it will ask the pool manager. If there are free objects, the manager will engage one for your. If there aren't any free objects but the hard limit is not breached, then the manager will initialize a new object and provide you. Else if the hard limit is breached then you will return empty handed. Why is this? This is used when we have an resource object which takes some time to initialize. And once initialized, it can be reused over and over again without a performance hit. Basically, creation is expensive and hence we want to reuse already created instances. Generally, we put a soft limit on the number of resources initially initialized. And we want to create more resources lazily if required. To prevent a huge number of resources from being created, we also put a hard limit. Example At GreyOrange, we use something called as IDC files. These are huge binary files (sometimes 100-200 GBs). They provide the time it takes to travel b/w 2 coordinates. We created an IDC Manager to parse these files and provide us the required information. The initialization takes a huge amount of time (sometimes 10s). Once initialized, it takes less than 1ms to provide the information. Right now, we are good with just once instance of this manager, so it is a singleton class with one buffer linked to an IDC file. But if the demands for parallel calls increases, we might want to implement the manager as a Object Pool. How to implement this? There are 3 requirements.An Object Pool Manager The initial number of objects - m The maximum number of objects - nThe Object Pool Manager will be a singleton class. We cannot allow multiple Object Pool Manager objects as they will create max, n objects each. We will create 2 lists, availableResources and enagaedResources. Initially, we will populate the availableResources with m new resource objects. Each getter call will check the availableResources list for available objects. If the objects are available then it will move the last object to engagedObjects. If the objects are not available then there are 2 choices. Check the hard limit, if not reached then create more objects and add to availableResources. Else return null. A pseudo code for the manager is as follows. class PoolManager { private static PoolManager instance; private List<Object> availableResources; private List<Object> engagedResources; private Integer initialLimit; private Integer hardLimit; private PoolManager() { // Get these properties from already defined config // Assume this is defined as per standard or equivalent configurations initialLimit = Properties.getInstance().getIntegerValue("POOL_INITIAL_LIMIT"); hardLimit = Properties.getInstance().getIntegerValue("POOL_HARD_LIMIT"); availableResources = new ArrayList<>(); engagedResources = new ArrayList<>(); // Initialize the initial number of resources in the pool for (int i=0; i<initialLimit; i++) { availableResources.add(new ResourceObject()); } } public static PoolManager getInstance() { if (instance == null) { synchronized(PoolManager.class) { if (instance == null) { instance = new PoolManager(); } } } return instance; } public Object getObject() { if (!availableResources.isEmpty()) { // A sync is required as 2 thread may want to get a free object at the same time synchronized(availableResources) { Object freeObject = availableResources.remove(availableResources.size()-1); engagedResources.add(freeObject); return freeObject; } } else if (engagedResources.size() < hardLimit) { Object freeObject = new ResourceObject(); availableResources.add(freeObject); return getObject(); } else { return null; } } public void releaseObject(Object engagedObject) { if (engagedObject != null) { try { synchronized(engagedResources) { Object freeObject = engagedResources.remove(engagedObject); availableResources.add(freeObject); } } catch (Exception e){} } } }
Shubham Kumar- 31 Aug, 2024
Downloading a single file from 2 independent apps
Understanding the problem Let's say you have a very large log file. And you want to create an app that can analyze this file and generate insights. Also, let's say you want to create an another app that can simulate the work by reading the logs one-by-one. Both these apps are dependent on the same log file. Now, there are 2 scenarios.App1 starts, downloads the file and then App2 starts. App1 starts, downloading the file and App2 starts while the download is incomlete.The first scenario is easy to deal with. We can check the md5sum of the local file and the file on the server. If they match, nothing to worrry about. If they don't then we can have a complex logic to determine the life of the old log file and decide accordingly. The second scenario is conflicting one and this we can solve in code. The second scenario can also happen when the same app is ran twice simultaneously. Both the instances will start downloading the same file and this will create a havoc. Solution The idea is to have an identifier that an app has already started the download and is still downloading the resouce. If the first app has started the download, then wait for the first app to complete the download and then only start the application. For accomplishing this, we generally use file locking mechanism. Download with file locking The process is modified to first create a lock file with extension .lock. This lock file signifies that a download is already in progress. If this lock file exists then wait for the download to complete by the second app. The lock file will have processid_threadid as identifier. This is useful in checking the race condition that can happen while writing the file. public static void downloadFileWithLock(String filePath) { File lockFile = new File(filePath + ".lock"); // Check if the file is being downloaded by another app // If it is being downloaded by an another app then wait for the download to finish // Else proceed with the download if (lockFile.exists()) { waitForDownloadToFinish(lockFile); } else { int processID = (int) ProcessHandle.current().pid(); String identifier = thread + "_" + processID; String contents = String.valueOf(identifier); writeToFile(lockFile, contents); // May be due to race condition, the file is already downloaded by another app // Check if this process started the download String savedIdentifier = readFromFile(lockFile); if (identifier.equals(savedIdentifier)) { // Download the file System.out.println(thread + " - Downloading file..."); File downloadFile = new File(filePath); try { RandomAccessFile randomAccessFile = new RandomAccessFile(downloadFile, "rw"); randomAccessFile.write("Very important works".getBytes()); Thread.sleep(5000); } catch (IOException e) {} catch (InterruptedException e) {} System.out.println(thread + " - File downloaded successflly."); } else { waitForDownloadToFinish(lockFile); } if (lockFile.exists()) { lockFile.delete(); } } }The Utilities method - waitFoDownloadToFinish, readFromFile and writeToFile are as follows. private static void writeToFile(File file, String contents) { try { Files.write(file.toPath(), contents.getBytes()); } catch (IOException e) { e.printStackTrace(); } } private static String readFromFile(File file) { try { return new String(Files.readAllBytes(file.toPath())); } catch (IOException e) { e.printStackTrace(); } return null; } private static void waitForDownloadToFinish(File lockFile) { System.out.println(thread + " - File is already being downloaded by another app. Wait for it to finish."); while (lockFile.exists()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(thread + " - File download completed."); }Now you can create 2 new apps that will call this method and we will run the apps simultaneously. public class App1 { public static void main(String[] args) { String filePath = "downloaded_file.txt"; FileDownloadUtil.downloadFileWithLock(filePath); System.out.println(Thread.currentThread().getName() + " - App1 starting operation..."); } }public class App2 { public static void main(String[] args) { String filePath = "downloaded_file.txt"; FileDownloadUtil.downloadFileWithLock(filePath); System.out.println(Thread.currentThread().getName() + " - App2 starting operation..."); } }Outputs # For App1 main - Downloading file... main - File downloaded successfully. main - App1 starting operation...# For App2 main - File is already being downloaded by another app. Wait for it to finish. main - File download completed. main - App2 starting operation...App1 started downloading the file and thus App2 waited for the download to complete. After the download completes, both the apps resumed its operations. Conclusion and improvements This is just a basic code that lays the foundation of file locking mechanism for downloading a file simultaneously by multiple apps. This code is not a production ready code. A more complete solution should handle scenarios like downloads in chunks, resume functionality with unexpected shutdowns and other edge cases.