<commit_before>// Copyright 2016 <NAME> <EMAIL>
// License: Apache2 - http://www.apache.org/licenses/LICENSE-2.0
package main

import (
	"bytes"
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"os"
	"sort"
	"sync"
	"time"
)

// Event stores the relevant information on events or updates to the storage layer.
type Event struct {
	Source        string
	Name          string
	Message       string
	Time          time.Time
	IsDirectory   bool
	NetworkSource string
}

var events = make([]Event, 0, 100)

// SendEvent gets events that have happened off to the peer servers so they can replicate the same change
func SendEvent(event Event, fullPath string) {
	// look back through the events for a similar event in the recent path.
	// Set the event source  (server name)
	event.Source = globalSettings.Name
	event.Time = time.Now()

	// Get the current owner of this entry if any
	ownershipLock.RLock()
	originalEntry, exists := ownership[event.Message]
	ownershipLock.RUnlock()

	if exists {
		log.Printf("Original ownership: %#v\n", originalEntry)
		timeDelta := time.Since(originalEntry.Time)
		if timeDelta > OWNERSHIP_EXPIRATION_TIMEOUT {
			log.Printf("Owndership expired. Delta is: %v\n", timeDelta)
		} else {
			// At this point, someone owns this item.
			// If we are not the owner, we are done.
			if originalEntry.Source != globalSettings.Name {
				log.Println("We do not own this. Do not send")
				return
			}
		}
	}

	// At this point, it is our change and our event. Store our ownership
	ownershipLock.Lock()
	ownership[event.Message] = event
	ownershipLock.Unlock()

	// sendEvent to manager
	sendEvent(&event, fullPath, globalSettings.ManagerAddress, globalSettings.ManagerCredentials)

	// SendEvent to all peers
	for k, v := range serverMap {
		fmt.Printf("Considering sending to: %s\n", k)
		if k != globalSettings.Name {
			fmt.Printf("sending to peer %s at %s\n", k, v.Address)
			sendEvent(&event, fullPath, v.Address, globalSettings.ManagerCredentials)
		}<commit_msg>adding more logging<commit_after>// Copyright 2016 <NAME> <EMAIL>
// License: Apache2 - http://www.apache.org/licenses/LICENSE-2.0
package main

import (
	"bytes"
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"os"
	"sort"
	"sync"
	"time"
)

// Event stores the relevant information on events or updates to the storage layer.
type Event struct {
	Source        string
	Name          string
	Message       string
	Time          time.Time
	IsDirectory   bool
	NetworkSource string
}

var events = make([]Event, 0, 100)

// SendEvent gets events that have happened off to the peer servers so they can replicate the same change
func SendEvent(event Event, fullPath string) {
	// look back through the events for a similar event in the recent path.
	// Set the event source  (server name)
	event.Source = globalSettings.Name
	event.Time = time.Now()

	// Get the current owner of this entry if any
	ownershipLock.RLock()
	originalEntry, exists := ownership[event.Message]
	ownershipLock.RUnlock()

	if exists {
		log.Printf("Original ownership: %#v\n", originalEntry)
		timeDelta := time.Since(originalEntry.Time)
		if timeDelta > OWNERSHIP_EXPIRATION_TIMEOUT {
			log.Printf("Owndership expired. Delta is: %v\n", timeDelta)
		} else {
			// At this point, someone owns this item.
			// If we are not the owner, we are done.
			if originalEntry.Source != globalSettings.Name {
				log.Println("We do not own this. Do not send")
				return
			}
		}
	} else {
		log.Printf("Send event called. No prior owndership: %s\n", fullPath)
	}

	// At this point, it is our change and our event. Store our ownership
	ownershipLock.Lock()
	ownership[event.Message] = event
	ownershipLock.Unlock()

	// sendEvent to manager
	sendEvent(&event, fullPath, globalSettings.ManagerAddress, globalSettings.ManagerCredentials)

	// SendEvent to all peers
	for k, v := range serverMap {
		fmt.Printf("Considering sending to: %s\n", k)
		if k != globalSettings.Name {
			fmt.Printf("sending to peer %s at %s\n", k, v.Address)
			sendEvent(&event, fullPath, v.Address, globalSettings.ManagerCredentials)
		}
{'fraction_non_alphanumeric': 0.10914245216158752, 'fraction_numerical': 0.006614694070399244, 'mean_word_length': 3.513859275053305, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6973543', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1357, 'n_words': 447}