Table of Contents
Question content
Workaround
Home Backend Development Golang Issues with LevelDB database size reduction in Go (levigo)

Issues with LevelDB database size reduction in Go (levigo)

Feb 11, 2024 am 11:06 AM
key value pair overflow

Go 中 LevelDB 数据库大小缩减的问题 (levigo)

php editor Youzi will introduce you to the size reduction problems and solutions that may be encountered when using the LevelDB database in Go. LevelDB is a high-performance key-value database, but when processing large amounts of data, the size of the database may grow rapidly and occupy a large amount of storage space. The article will discuss in detail how to solve this problem by using the levigo library and using compression algorithms to reduce the size of the database, thereby improving performance and saving storage space. Whether you are a beginner or an experienced developer, this article will help you.

Question content

Hello Stack Overflow community,

I am currently developing a Go program that utilizes LevelDB for data storage using the levigo package. My goal is to manage the database size efficiently, specifically deleting old records when available storage is low. However, I observed an unexpected behavior: the LevelDB database folder size did not decrease proportionally after deleting records.

Here is a simplified version of the code that reproduces the problem:

Save data code:

package main

import (
    "crypto/rand"
    "fmt"
    "log"

    "github.com/jmhodges/levigo"
)

func main() {
    // Specify the LevelDB options
    options := levigo.NewOptions()
    cache := levigo.NewLRUCache(5 << 20)
    options.SetCache(cache)
    options.SetCreateIfMissing(true)
    options.SetMaxOpenFiles(100)

    // Open or create the LevelDB database
    db, _ := levigo.Open("/tmp/mydatabase", options)
    defer db.Close()

    dataSize := 1024 * 1024 * 5 // 5MB
    randomData := make([]byte, dataSize)
    rand.Read(randomData)

    // Enqueue 5 pieces of data
    for i := 1; i <= 5; i++ {
        key := []byte(fmt.Sprintf("key%d", i))

        // Write the batch to the database
        if err := db.Put(levigo.NewWriteOptions(), key, randomData); err != nil {
            log.Fatal(err)
        }

        fmt.Printf("Enqueued: %s \n", key)
    }

    fmt.Println("Enqueue completed.")
}
Copy after login

Delete data code:

package main

import (
    "fmt"
    "log"

    "github.com/jmhodges/levigo"
)

func main() {
    // Specify the LevelDB options
    options := levigo.NewOptions()
    cache := levigo.NewLRUCache(5 << 20)
    options.SetCache(cache)
    options.SetCreateIfMissing(true)
    options.SetMaxOpenFiles(100)

    // Open or create the LevelDB database
    db, _ := levigo.Open("/tmp/mydatabase", options)
    defer db.Close()

    // Dequeue (remove) the 3 pieces of data
    for i := 1; i <= 3; i++ {
        key := []byte(fmt.Sprintf("key%d", i))

        // Create a WriteOptions for deleting from the database
        wo := levigo.NewWriteOptions()
        defer wo.Close()

        // Delete the key from the database
        if err := db.Delete(wo, key); err != nil {
            log.Fatal(err)
        }

        fmt.Printf("Dequeued: %s\n", key)
    }

    fmt.Println("Dequeue completed.")
}
Copy after login

After running the code and saving 5 items, the database folder size is 30MB. Later, when I ran the code to delete 3 items, the folder size was reduced to 26MB. Considering the amount of data removed, I expected the size to be reduced more significantly.

I have set LevelDB options such as cache size and file limits, but it seems I may have missed something during configuration or removal.

question:

  1. What could cause the LevelDB database folder size not to decrease proportionally after deleting records?
  2. Are there any other configurations or optimizations I should consider in order to manage the database size more efficiently?
  3. Is there a specific way in levigo to compact the database to free up unused space?

Any insight or guidance on resolving this issue would be greatly appreciated. Thanks!

Workaround

By reading the question on this level DB repository I realized that I could add this to delete the row at the end of the loop db.CompactRange (levigo.Range{})
So the database will delete unused data and the total size of the database folder will be reduced.

The above is the detailed content of Issues with LevelDB database size reduction in Go (levigo). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the method of converting Vue.js strings into objects? What is the method of converting Vue.js strings into objects? Apr 07, 2025 pm 09:18 PM

Using JSON.parse() string to object is the safest and most efficient: make sure that strings comply with JSON specifications and avoid common errors. Use try...catch to handle exceptions to improve code robustness. Avoid using the eval() method, which has security risks. For huge JSON strings, chunked parsing or asynchronous parsing can be considered for optimizing performance.

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

How to control the top and end of pages in browser printing settings through JavaScript or CSS? How to control the top and end of pages in browser printing settings through JavaScript or CSS? Apr 05, 2025 pm 10:39 PM

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

What method is used to convert strings into objects in Vue.js? What method is used to convert strings into objects in Vue.js? Apr 07, 2025 pm 09:39 PM

When converting strings to objects in Vue.js, JSON.parse() is preferred for standard JSON strings. For non-standard JSON strings, the string can be processed by using regular expressions and reduce methods according to the format or decoded URL-encoded. Select the appropriate method according to the string format and pay attention to security and encoding issues to avoid bugs.

How to use foreach loop in vue How to use foreach loop in vue Apr 08, 2025 am 06:33 AM

The foreach loop in Vue.js uses the v-for directive, which allows developers to iterate through each element in an array or object and perform specific operations on each element. The syntax is as follows: &lt;template&gt; &lt;ul&gt; &lt;li v-for=&quot;item in items&gt;&gt;{{ item }}&lt;/li&gt; &lt;/ul&gt; &lt;/template&gt;&am

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

The text under Flex layout is omitted but the container is opened? How to solve it? The text under Flex layout is omitted but the container is opened? How to solve it? Apr 05, 2025 pm 11:00 PM

The problem of container opening due to excessive omission of text under Flex layout and solutions are used...

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

See all articles