<commit_before>package DLocker

import (
	"github.com/nladuo/DLocker/modules"
	"github.com/samuel/go-zookeeper/zk"
	"log"
	"sync"
	"time"
)

type Dlocker struct {
	lockerPath string
	prefix     string
	basePath   string
	timeout    time.Duration
	innerLock  *sync.Mutex
}

func NewLocker(path string, prefix string, timeout time.Duration) *Dlocker {

	var locker Dlocker
	locker.basePath = path
	locker.prefix = prefix
	locker.timeout = timeout
	locker.innerLock = &sync.Mutex{}
	isExsit, _, err := getZkConn().Exists(path)
	if err != nil {
		panic(err.Error())
	}
	if !isExsit {
		log.Println("create the znode:" + path)
		getZkConn().Create(path, []byte(""), int32(0), zk.WorldACL(zk.PermAll))
	} else {
		log.Println("the znode " + path + " existed")
	}
	return &locker
}

func (this *Dlocker) Lock() (isSuccess bool) {
	isSuccess = false

	defer func() {
		e := recover()
		if e == zk.ErrConnectionClosed {
			//try reconnect the zk server
			log.Println("connection closed, reconnect to the zk server")
			reConnectZk()
		}
	}()
	this.innerLock.Lock()
	defer this.innerLock.Unlock()
	//create znode
	path := this.basePath + "/" + this.prefix
	var err error
	this.lockerPath, err = getZkConn().Create(path, []byte(""), zk.FlagSequence|zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
	//get children and check is the created locker is the minimum znode
	chidren, _, err := getZkConn().Children(this.basePath)
	if err != nil {
		panic(err)
	}
	minIndex := modules.GetMinIndex(chidren, this.prefix)
	minLockerPath := this.basePath + "/" + chidren[minIndex]
	// if the created znode is not the minimum znode,
	// listen for the pre-znode delete notification
	if minLockerPath != this.lockerPath {
		lastNodeName := modules.GetLastNodeName(this.lockerPath,
			this.basePath, this.prefix)
		watchPath := this.basePath + "/" + lastNodeName
		isExist, _, watch, err := getZkConn().ExistsW(watchPath)
		if err != nil {
			panic(err)
		}
		if isExist {
			select {
			case event := <-watch:
				if event.Type == zk.EventNodeDeleted {
					isSuccess = true
				} else {
					isSuccess = false<commit_msg>	modified:   dlocker.go<commit_after>package DLocker

import (
	"github.com/nladuo/DLocker/modules"
	"github.com/samuel/go-zookeeper/zk"
	"log"
	"sync"
	"time"
)

type Dlocker struct {
	lockerPath string
	prefix     string
	basePath   string
	timeout    time.Duration
	innerLock  *sync.Mutex
}

func NewLocker(path string, prefix string, timeout time.Duration) *Dlocker {

	var locker Dlocker
	locker.basePath = path
	locker.prefix = prefix
	locker.timeout = timeout
	locker.innerLock = &sync.Mutex{}
	isExsit, _, err := getZkConn().Exists(path)
	if err != nil {
		panic(err.Error())
	}
	if !isExsit {
		log.Println("create the znode:" + path)
		getZkConn().Create(path, []byte(""), int32(0), zk.WorldACL(zk.PermAll))
	} else {
		log.Println("the znode " + path + " existed")
	}
	return &locker
}

func (this *Dlocker) Lock() (isSuccess bool) {
	isSuccess = false

	defer func() {
		e := recover()
		if e == zk.ErrConnectionClosed {
			//try reconnect the zk server
			log.Println("connection closed, reconnect to the zk server")
			reConnectZk()
		}
	}()
	this.innerLock.Lock()
	defer this.innerLock.Unlock()
	//create znode
	path := this.basePath + "/" + this.prefix
	var err error
	this.lockerPath, err = getZkConn().Create(path, []byte(""), zk.FlagSequence|zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
	//get children and check is the created locker is the minimum znode
	chidren, _, err := getZkConn().Children(this.basePath)
	if err != nil {
		panic(err)
	}
	minIndex := modules.GetMinIndex(chidren, this.prefix)
	minLockerPath := this.basePath + "/" + chidren[minIndex]
	// if the created znode is not the minimum znode,
	// listen for the pre-znode delete notification
	if minLockerPath != this.lockerPath {
		lastNodeName := modules.GetLastNodeName(this.lockerPath,
			this.basePath, this.prefix)
		watchPath := this.basePath + "/" + lastNodeName
		isExist, _, watch, err := getZkConn().ExistsW(watchPath)
		if err != nil {
			panic(err)
		}
		if isExist {
			select {
			case event := <-watch:
				if event.Type == zk.EventNodeDeleted {
					isExist, _, err = getZkConn().Exists(this.lockerPath)
					if isExist && err == nil {
						isSuccess = true
					} else {
						isSuccess = false
					}
				} else {
					isSuccess = false
{'fraction_non_alphanumeric': 0.13438005079658277, 'fraction_numerical': 0.001385361348418379, 'mean_word_length': 3.6381156316916488, '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': '1135744', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1565, 'n_words': 373}