<commit_before>// Copyright (c) 2012, <NAME> <<EMAIL>>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package leveldb

import (
	"time"

	"github.com/syndtr/goleveldb/leveldb/memdb"
	"github.com/syndtr/goleveldb/leveldb/opt"
)

func (d *DB) doWriteJournal(b *Batch) error {
	err := d.journal.journal.Append(b.encode())
	if err == nil && b.sync {
		err = d.journal.writer.Sync()
	}
	return err
}

func (d *DB) writeJournal() {
	for b := range d.jch {
		if b == nil {
			break
		}

		// write journal
		d.jack <- d.doWriteJournal(b)
	}

	close(d.jch)
	close(d.jack)
	d.ewg.Done()
}

func (d *DB) flush() (m *memdb.DB, err error) {
	s := d.s

	delayed, cwait := false, false
	for {
		v := s.version()
		mem := d.getMem()
		switch {
		case v.tLen(0) >= kL0_SlowdownWritesTrigger && !delayed:
			delayed = true
			time.Sleep(time.Millisecond)
		case mem.cur.Size() <= s.o.GetWriteBuffer():
			// still room
			return mem.cur, nil
		case mem.froze != nil:
			if cwait {
				err = d.geterr()
				if err != nil {
					return
				}
				d.cch <- cSched
			} else {
				cwait = true
				d.cch <- cWait
			}
		case v.tLen(0) >= kL0_StopWritesTrigger:
			d.cch <- cSched
		default:
			// create new memdb and journal
			m, err = d.newMem()
			if err != nil {
				return<commit_msg>leveldb: version: Fix missing v.release() call on DB.flush()<commit_after>// Copyright (c) 2012, <NAME> <<EMAIL>>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package leveldb

import (
	"time"

	"github.com/syndtr/goleveldb/leveldb/memdb"
	"github.com/syndtr/goleveldb/leveldb/opt"
)

func (d *DB) doWriteJournal(b *Batch) error {
	err := d.journal.journal.Append(b.encode())
	if err == nil && b.sync {
		err = d.journal.writer.Sync()
	}
	return err
}

func (d *DB) writeJournal() {
	for b := range d.jch {
		if b == nil {
			break
		}

		// write journal
		d.jack <- d.doWriteJournal(b)
	}

	close(d.jch)
	close(d.jack)
	d.ewg.Done()
}

func (d *DB) flush() (m *memdb.DB, err error) {
	s := d.s

	delayed, cwait := false, false
	for {
		v := s.version()
		mem := d.getMem()
		switch {
		case v.tLen(0) >= kL0_SlowdownWritesTrigger && !delayed:
			delayed = true
			time.Sleep(time.Millisecond)
		case mem.cur.Size() <= s.o.GetWriteBuffer():
			// still room
			v.release()
			return mem.cur, nil
		case mem.froze != nil:
			if cwait {
				err = d.geterr()
				if err != nil {
					return
				}
				d.cch <- cSched
			} else {
				cwait = true
				d.cch <- cWait
			}
		case v.tLen(0) >= kL0_StopWritesTrigger:
			d.cch <- cSched
		default:
			// create new memdb and journal
			m, err = d.newMem()
			if err != nil {
				return
{'fraction_non_alphanumeric': 0.15570809248554912, 'fraction_numerical': 0.005780346820809248, 'mean_word_length': 2.5821474773609316, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '7015474', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1098, 'n_words': 287}