<commit_before>package db

import (
	"bytes"
	"encoding/binary"

	wirefs "bazil.org/bazil/fs/wire"
	"bazil.org/fuse"
	"github.com/boltdb/bolt"
	"github.com/golang/protobuf/proto"
)

type Dirs struct {
	b *bolt.Bucket
}

func dirKey(parentInode uint64, name string) []byte {
	buf := make([]byte, 8+len(name))
	binary.BigEndian.PutUint64(buf, parentInode)
	copy(buf[8:], name)
	return buf
}

func basename(dirKey []byte) []byte {
	return dirKey[8:]
}

// Get the entry in parent directory with the given name.
//
// Returned value is valid after the transaction.
func (b *Dirs) Get(parentInode uint64, name string) (*wirefs.Dirent, error) {
	key := dirKey(parentInode, name)
	buf := b.b.Get(key)
	if buf == nil {
		return nil, fuse.ENOENT
	}

	var de wirefs.Dirent
	if err := proto.Unmarshal(buf, &de); err != nil {
		return nil, err
	}
	return &de, nil
}

// Put an entry in parent directory with the given name.
func (b *Dirs) Put(parentInode uint64, name string, de *wirefs.Dirent) error {
	buf, err := proto.Marshal(de)
	if err != nil {
		return err
	}
	key := dirKey(parentInode, name)
	if err := b.b.Put(key, buf); err != nil {
		return err
	}
	return nil
}

// Delete the entry in parent directory with the given name.
//
// Returns fuse.ENOENT if an entry does not exist.
func (b *Dirs) Delete(parentInode uint64, name string) error {
	key := dirKey(parentInode, name)
	if b.b.Get(key) == nil {
		return fuse.ENOENT
	}
	if err := b.b.Delete(key); err != nil {
		return err
	}
	return nil
}

// Rename renames an entry in the parent directory from oldName to
// newName.
//
// Returns the overwritten entry, or nil.
func (b *Dirs) Rename(parentInode uint64, oldName string, newName string) (*DirEntry, error) {
	kOld := dirKey(parentInode, oldName)
	kNew := dirKey(parentInode, newName)

	bufOld := b.b.Get(kOld)
	if bufOld == nil {
		return nil, fuse.ENOENT
	}

	// the file getting overwritten
	var loser *DirEntry
	if buf := b.b.Get(kNew); buf != nil {
		// overwriting
		loser = &DirEntry{
			name: basename(kNew),
			data: buf,
		}
	}

	if err := b.b.Put(kNew, bufOld); err != nil {
		return nil, err
	}
	if err := b.b.Delete(kOld); err != nil {
		return nil, err
	}

	return loser, nil
}

func (b *Dirs) List(inode uint64) *DirsCursor {
	c := b.b.Cursor()
	prefix := dirKey(inode, "")
	return &DirsCursor{
		inode:  inode,
		prefix: prefix,
		c:      c,
	}
}

type DirsCursor struct {
	inode  uint64
	prefix []byte
	c      *bolt.Cursor
}

func (c *DirsCursor) First() *DirEntry {
	return c.item(c.c.Seek(c.prefix))
}

func (c *DirsCursor) Next() *DirEntry {
	return c.item(c.c.Next())
}
<commit_msg>db: golint: Rename variables to avoid them looking like hungarian notation<commit_after>package db

import (
	"bytes"
	"encoding/binary"

	wirefs "bazil.org/bazil/fs/wire"
	"bazil.org/fuse"
	"github.com/boltdb/bolt"
	"github.com/golang/protobuf/proto"
)

type Dirs struct {
	b *bolt.Bucket
}

func dirKey(parentInode uint64, name string) []byte {
	buf := make([]byte, 8+len(name))
	binary.BigEndian.PutUint64(buf, parentInode)
	copy(buf[8:], name)
	return buf
}

func basename(dirKey []byte) []byte {
	return dirKey[8:]
}

// Get the entry in parent directory with the given name.
//
// Returned value is valid after the transaction.
func (b *Dirs) Get(parentInode uint64, name string) (*wirefs.Dirent, error) {
	key := dirKey(parentInode, name)
	buf := b.b.Get(key)
	if buf == nil {
		return nil, fuse.ENOENT
	}

	var de wirefs.Dirent
	if err := proto.Unmarshal(buf, &de); err != nil {
		return nil, err
	}
	return &de, nil
}

// Put an entry in parent directory with the given name.
func (b *Dirs) Put(parentInode uint64, name string, de *wirefs.Dirent) error {
	buf, err := proto.Marshal(de)
	if err != nil {
		return err
	}
	key := dirKey(parentInode, name)
	if err := b.b.Put(key, buf); err != nil {
		return err
	}
	return nil
}

// Delete the entry in parent directory with the given name.
//
// Returns fuse.ENOENT if an entry does not exist.
func (b *Dirs) Delete(parentInode uint64, name string) error {
	key := dirKey(parentInode, name)
	if b.b.Get(key) == nil {
		return fuse.ENOENT
	}
	if err := b.b.Delete(key); err != nil {
		return err
	}
	return nil
}

// Rename renames an entry in the parent directory from oldName to
// newName.
//
// Returns the overwritten entry, or nil.
func (b *Dirs) Rename(parentInode uint64, oldName string, newName string) (*DirEntry, error) {
	keyOld := dirKey(parentInode, oldName)
	keyNew := dirKey(parentInode, newName)

	bufOld := b.b.Get(keyOld)
	if bufOld == nil {
		return nil, fuse.ENOENT
	}

	// the file getting overwritten
	var loser *DirEntry
	if buf := b.b.Get(keyNew); buf != nil {
		// overwriting
		loser = &DirEntry{
			name: basename(keyNew),
			data: buf,
		}
	}

	if err := b.b.Put(keyNew, bufOld); err != nil {
		return nil, err
	}
	if err := b.b.Delete(keyOld); err != nil {
		return nil, err
	}

	return loser, nil
}

func (b *Dirs) List(inode uint64) *DirsCursor {
	c := b.b.Cursor()
	prefix := dirKey(inode, "")
	return &DirsCursor{
		inode:  inode,
		prefix: prefix,
		c:      c,
	}
}

type DirsCursor struct {
	inode  uint64
	prefix []byte
	c      *bolt.Cursor
}

func (c *DirsCursor) First() *DirEntry {
	return c.item(c.c.Seek(c.prefix))
}

func (c *DirsCursor) Next() *DirEntry {
	return c.item(c.c.Next())
}

{'fraction_non_alphanumeric': 0.14239848914069878, 'fraction_numerical': 0.007176581680830973, 'mean_word_length': 3.4466834592779176, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4176861', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2017, 'n_words': 600}