<commit_before>package storage

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
	"sync"

	"utils"

	"github.com/golang/protobuf/proto"
)

// AOF ...
type AOF struct {
	file   *os.File
	buffer *bufio.ReadWriter
	lock   sync.RWMutex
	in     chan *Entry
}

// NewAOF ...
func NewAOF(path string) *AOF {
	file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
	utils.PanicOnError(err)
	rdr := bufio.NewReader(file)
	wtr := bufio.NewWriter(file)
	in := make(chan *Entry)
	return &AOF{file, bufio.NewReadWriter(rdr, wtr), sync.RWMutex{}, in}
}

// Run ...
func (aof *AOF) Run() {
	go func() {
		for {
			select {
			case e := <-aof.in:
				aof.write(e)
			}
		}
	}()
}

func (aof *AOF) write(e *Entry) {
	line := fmt.Sprintf("%d|%s/", e.op, string(e.raw))
	if _, err := aof.buffer.WriteString(line); err != nil {
		fmt.Println(err)
	}
	if err := aof.buffer.Flush(); err != nil {
		fmt.Println(err)
	}
}

// Append ...
func (aof *AOF) Append(op uint8, msg proto.Message) error {<commit_msg>Make the AOF in: channel sized to 100<commit_after>package storage

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
	"sync"

	"utils"

	"github.com/golang/protobuf/proto"
)

// AOF ...
type AOF struct {
	file   *os.File
	buffer *bufio.ReadWriter
	lock   sync.RWMutex
	in     chan *Entry
}

// NewAOF ...
func NewAOF(path string) *AOF {
	file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
	utils.PanicOnError(err)
	rdr := bufio.NewReader(file)
	wtr := bufio.NewWriter(file)
	in := make(chan *Entry, 100)
	return &AOF{file, bufio.NewReadWriter(rdr, wtr), sync.RWMutex{}, in}
}

// Run ...
func (aof *AOF) Run() {
	go func() {
		for {
			select {
			case e := <-aof.in:
				aof.write(e)
			}
		}
	}()
}

func (aof *AOF) write(e *Entry) {
	line := fmt.Sprintf("%d|%s/", e.op, string(e.raw))
	if _, err := aof.buffer.WriteString(line); err != nil {
		fmt.Println(err)
	}
	if err := aof.buffer.Flush(); err != nil {
		fmt.Println(err)
	}
}

// Append ...
func (aof *AOF) Append(op uint8, msg proto.Message) error {
{'fraction_non_alphanumeric': 0.18734673859735165, 'fraction_numerical': 0.00784698381559588, 'mean_word_length': 3.1379310344827585, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3782712', 'n_tokens_mistral': 943, 'n_tokens_neox': 903, 'n_words': 190}