<commit_before>package store

import "errors"

func (zk *ZKStore) CreateInstance(ins *Instance) error {
	op := &AtomicOp{
		Op:      OP_ADD,
		Entity:  ENTITY_INSTANCE,
		Param1:  ins.ID,
		Payload: ins,
	}

	return zk.Apply(op, true)
}

func (zk *ZKStore) DeleteInstance(idOrName string) error {
	i, _ := zk.GetInstance(idOrName)
	if i == nil {
		return nil
	}

	op := &AtomicOp{
		Op:     OP_REMOVE,
		Entity: ENTITY_INSTANCE,
		Param1: i.ID,
	}

	return zk.Apply(op, true)
}

func (zk *ZKStore) UpdateInstance(ins *Instance) error {
	i, _ := zk.GetInstance(ins.ID)
	if i == nil {
		return ErrInstanceNotFound
	}

	op := &AtomicOp{
		Op:      OP_UPDATE,
		Entity:  ENTITY_INSTANCE,
		Param1:  ins.ID,
		Payload: ins,
	}

	return zk.Apply(op, true)
}

func (zk *ZKStore) GetInstance(idOrName string) (*Instance, error) {
	zk.mu.RLock()
	defer zk.mu.RUnlock()

	// by id
	ins, ok := zk.Storage.Instances[idOrName]
	if ok {
		return ins, nil
	}

	// by name
	var inss []*Instance
	inss, err := zk.ListInstances()
	if err != nil {
		return nil, err
	}
	for _, ins := range inss {
		if ins.Name == idOrName {
			return ins, nil
		}
	}

	return nil, errors.New("no such compose instance")
}

func (zk *ZKStore) ListInstances() ([]*Instance, error) {
	zk.mu.RLock()
	defer zk.mu.RUnlock()

	is := make([]*Instance, 0, 0)
	for _, i := range zk.Storage.Instances {<commit_msg>fix another potential dead lock on zk store<commit_after>package store

import "errors"

func (zk *ZKStore) CreateInstance(ins *Instance) error {
	op := &AtomicOp{
		Op:      OP_ADD,
		Entity:  ENTITY_INSTANCE,
		Param1:  ins.ID,
		Payload: ins,
	}

	return zk.Apply(op, true)
}

func (zk *ZKStore) DeleteInstance(idOrName string) error {
	i, _ := zk.GetInstance(idOrName)
	if i == nil {
		return nil
	}

	op := &AtomicOp{
		Op:     OP_REMOVE,
		Entity: ENTITY_INSTANCE,
		Param1: i.ID,
	}

	return zk.Apply(op, true)
}

func (zk *ZKStore) UpdateInstance(ins *Instance) error {
	i, _ := zk.GetInstance(ins.ID)
	if i == nil {
		return ErrInstanceNotFound
	}

	op := &AtomicOp{
		Op:      OP_UPDATE,
		Entity:  ENTITY_INSTANCE,
		Param1:  ins.ID,
		Payload: ins,
	}

	return zk.Apply(op, true)
}

func (zk *ZKStore) GetInstance(idOrName string) (*Instance, error) {
	zk.mu.RLock()
	defer zk.mu.RUnlock()

	// by id
	ins, ok := zk.Storage.Instances[idOrName]
	if ok {
		return ins, nil
	}

	// by name
	for _, ins := range zk.Storage.Instances {
		if ins.Name == idOrName {
			return ins, nil
		}
	}

	return nil, errors.New("no such compose instance")
}

func (zk *ZKStore) ListInstances() ([]*Instance, error) {
	zk.mu.RLock()
	defer zk.mu.RUnlock()

	is := make([]*Instance, 0, 0)
	for _, i := range zk.Storage.Instances {
{'fraction_non_alphanumeric': 0.14418431809736157, 'fraction_numerical': 0.0037160906726124115, 'mean_word_length': 3.3630470016207457, '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': '898597', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1091, 'n_words': 275}