GithubHelp home page GithubHelp logo

wal's Introduction

Kuentra WAL

Kuentra's Wal has a structure that is easy to understand and can be used for highly available databases.

architecture

Understanding WAL (Write-Ahead Logging) Easily

WAL is not complex. It is simply a way to efficiently store files and read only the necessary data. Understanding the following four principles will enable you to build your own WAL system:

  1. SegSerialID
  2. BlockNumber
  3. ChunkOffset
  4. ChunkSize

Think of these components as similar to the structural principles of MySQL.

1. Segment File Structure

Segment files can exist in multiples and may merge depending on the situation. They internally contain what are known as Blocks.

  • Think of a segment as a Shard Table.
  • Think of a block as just one of the many tables in a shard.
+-----------------------------------------------------+
| Segment File (SegmentID = 1)                        |
+-----------------------------------------------------+
| Block 0               | Block 1               | ... |
+-----------------------+-----------------------+-----+

2. Concept of Chunks in a Block

A block is composed of one or more chunks, akin to how a table contains one or more rows.

+-----------------------+
| Block 0               |
+-----------------------+
| Chunk 0 | Chunk 1 | ...|
+---------+---------+----+

Each chunk is distinguished by ChunkOffset and ChunkSize, which you should not see as a single row but rather as limit and offset.

+-----------------------+
| Block 0               |
+-----------------------+
| C0      | C1      | C2 |
+---------+---------+----+
| Offset 0| Offset X| ...|
+---------+---------+----+
| Size Y  | Size Z  | ...|
+---------+---------+----+

Consider this concept as inserting many documents into each shard table. The ChunkOffset represents the starting position of the document you are looking for, and ChunkSize represents the number of pages to the end. For instance, if a document starts at 0 and has 100 pages, it would be similar to the following SQL query:

SELECT * FROM SHARD_TABLE1 WHERE BLOCK_ID=0 OFFSET 0 LIMIT 100

The same concept applies when writing data:

+-----------------------+
| Block 0               |
+-----------------------+
| C0      | C1      | C2 |
+---------+---------+----+
| Data    | Data    | Data|
+---------+---------+----+

By knowing which shard table (SegSerialID) to insert into, the location, and the length, WAL can efficiently manage and retrieve data quickly and securely.

3. Getting Started

package main

import (
	"io"
	"log"

	"github.com/kuentra-official/wal"
)

func main() {
	walOpts := wal.DefaultOptions
	walOpts.DirPath = "tmp"
	kwal, err := wal.Open(walOpts)
	if err != nil {
		panic(err)
	}

	pos1, err := kwal.Write([]byte("first chunk"))
	if err != nil {
		panic(err)
	}
	rval1, err := kwal.Read(pos1)
	if err != nil {
		panic(err)
	}
	log.Println("Is this the expected value? Expect: [first chunk] return Value : [", string(rval1), "]")
	_, err = kwal.Write([]byte("second chunk"))
	if err != nil {
		panic(err)
	}
	_, err = kwal.Write([]byte("third chunk"))
	if err != nil {
		panic(err)
	}
	reader := kwal.NewReader()
	for {
		rv, pos, err := reader.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}
		log.Println("[pos] : ", pos, " [return value] : ", string(rv))
		//must print first, second, third chunk
	}
}

wal's People

Contributors

sjy-dv avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

vdt-khj

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.