dumping all of the database and utilites into the project, will work on the managers next

This commit is contained in:
2020-06-11 13:53:51 -04:00
parent 2cbdf21a81
commit 0ecb0b96ce
10 changed files with 741 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package database
import (
"io"
logger "github.com/apsdehal/go-logger"
"github.com/asdine/storm"
)
type DB struct {
*storm.DB
*logger.Logger
}
// NewDB returns a new database object,
// it configures the database for you.
func NewDB(dbPath, format string, logLevel int, logWriter io.WriteCloser) (*DB, error) {
//Note! Do not use logger as you have no idea if logWriter has been configured for output yet
var db DB
log, err := logger.New("db logger", 1, logWriter)
if err != nil {
return &db, err
}
log.SetLogLevel(logger.LogLevel(logLevel))
log.SetFormat(format)
db.Logger = log
if err := db.ConfigureDB(dbPath); err != nil {
log.ErrorF("Error configuring the database ", err)
return &db, err
}
return &db, nil
}

189
common/database/db-ops.go Normal file
View File

@@ -0,0 +1,189 @@
package database
import (
"fmt"
"github.com/asdine/storm"
)
// ConfigureDB sets up bolt and Storm according to the path of the database
// this is done here so that different databases can be configured in different scenarios
func (db *DB) ConfigureDB(dbPath string) error {
var err error
if db.DB, err = storm.Open(dbPath); err != nil {
return err
}
var file File
if err := db.One("Name", "-- All Files --", &file); err != nil {
if err.Error() != "not found" {
db.ErrorF("Error finding file by path %s", err)
return err
}
db.WarningF("No file found. initialising the database")
file.Name = "-- All Files --"
//file.Ignore = true //this is currently not used however could result in this file being ignored when file watching (etc) starts
if err := db.Save(&file); err != nil {
db.ErrorF("Error storing the diff %s", err)
return err
}
}
return nil
}
// CheckIfFileCurrentlyMonitored checks if the file is already being monitored. This is a read-only check
// to see whether the file was correctly initialised
// (BUG) The hash causes the same file to be in database multiple times!
func (db *DB) CheckIfFileCurrentlyMonitored(src string, hash [16]byte) (File, error) {
var file File
//TODO: check this actually works still (don't need hash passed to this anymore)
if err := db.One("Path", src, &file); err != nil {
if err.Error() != "not found" {
db.ErrorF("Error finding file by path %s", err)
return File{}, err
}
db.WarningF("no file found, %s", err)
return File{}, err
} else {
return file, nil
}
}
// RetrieveWatchedFiles all files that are in the database as "watched files"
// This can be used to trigger the same files to be watched again
func (db *DB) RetrieveWatchedFiles() ([]File, error) {
var files []File
if err := db.All(&files); err != nil {
db.ErrorF("Error retrieving all watched files %s", err)
return []File{}, err
} else {
return files, nil
}
}
// RetrieveAllDiffs retrieves all files that are in the database as "watched files"
// This can be used to trigger the same files to be watched again
// func (db *DB) RetrieveAllDiffs() ([]DiffObject, error) {
// var diffs []DiffObject
// if err := db.All(&diffs); err != nil {
// db.ErrorF("Error retrieving all diffs %s", err)
// return []DiffObject{}, err
// } else {
// return diffs, nil
// }
// }
// InitialiseFileInDatabase should be called before any file is copied/renamed/diff'd/patched,
// and this should be checked before any operation occurs on a file. Any loss of data is completely as a result
// of losing references
func (db *DB) InitialiseFileInDatabase(file File) (int, error) {
if err := db.Save(&file); err != nil {
db.ErrorF("Error initialising file in database %s", err)
return file.ID, err
}
return file.ID, nil
}
// FindFileByPath is a search function looking for file details
func (db *DB) FindFileByPath(filePath string) (File, error) {
var file File
if err := db.One("Path", filePath, &file); err != nil {
db.ErrorF("Error finding file by path %s", err)
return File{}, err
}
return file, nil
}
// FindFileByID is a search function looking for file details
func (db *DB) FindFileByID(ID int) (File, error) {
var file File
if err := db.One("ID", ID, &file); err != nil {
db.ErrorF("Error finding file by path %s", err)
return File{}, err
}
return file, nil
}
// UpdateFileData updates the current base file that diffs will compare to
func (db *DB) UpdateFileData(filePath, basePath string, hash [16]byte) error {
if file, err := db.FindFileByPath(filePath); err != nil {
db.ErrorF("Error updating the file base %s", err)
return err
} else {
err := db.Update(&File{ID: file.ID, CurrentBase: basePath, CurrentHash: hash})
return err
}
}
// RetrieveDiffsForFileByHash returns the diffs for a file. Diffs can be applied to a specific file (by its hash),
// so when looking for the diffs, the hash is a good place to start in terms of finding the diffs
func (db *DB) RetrieveDiffsForFileByHash(fileHash [16]byte, direction bool) ([]DiffObject, error) {
var diffs []DiffObject
var field string
if direction {
field = "ObjectHash"
} else {
field = "SubjectHash"
}
if err := db.Find(field, fileHash, &diffs); err != nil {
return []DiffObject{}, err
}
return diffs, nil
}
// RetrieveDiffsForFileByPath returns the diffs for a file base on the file path. Diffs are very specific to a file,
// so this may not be as robust as searching by diff, however we may not have the diff available
func (db *DB) RetrieveDiffsForFileByPath(filePath string) ([]DiffObject, error) {
var objDiffs []DiffObject
var subDiffs []DiffObject
if err := db.Find("Object", filePath, &objDiffs); err != nil && err.Error() != "not found" {
db.ErrorF("Error finding diff by object %s", err)
return []DiffObject{}, err
}
if err := db.Find("Subject", filePath, &subDiffs); err != nil && err.Error() != "not found" {
db.ErrorF("Error finding diff by subject %s", err)
return []DiffObject{}, err
}
return append(objDiffs, subDiffs...), nil
}
// StoreDiff just places the information about a diff in the database. Currently there is no protection
// to stop the entire diff entering the database (if fs is false), which may be very slow/bulky...
// TODO: decide what to do with diffs in memory
// func (db *DB) StoreDiff(diff DiffObject) error {
// if err := db.Save(&diff); err != nil {
// db.ErrorF("Error storing the diff %s", err)
// return err
// }
// return nil
// }
// FindDiffByPath is a search function looking for a diff
func (db *DB) FindDiffByPath(patchPath string) (DiffObject, error) {
var diff DiffObject
if err := db.One("DiffPath", patchPath, &diff); err != nil {
db.ErrorF("Error finding diff by path %s", err)
return DiffObject{}, err
}
return diff, nil
}
//RetrieveDiffsByID returns a diff based on the id it has in the database
func (db *DB) RetrieveDiffsByID(ID int) (DiffObject, error) {
var diff DiffObject
if err := db.One("ID", ID, &diff); err != nil {
db.ErrorF("Error finding diff by ID %s", err)
return DiffObject{}, err
}
return diff, nil
}
// UpdateDescription is a simple function to set the label on a patch
func (db *DB) UpdateDescription(patchID int, description string) error {
fmt.Println("attempting to path with id ", patchID, " description ", description)
if err := db.Update(&DiffObject{ID: patchID, Description: description}); err != nil {
db.ErrorF("Error changing diff label %s", err)
return err
}
return nil
}

View File

@@ -0,0 +1,63 @@
package database
import "time"
// Commit stores all the necessary information for a commit
type Commit struct {
CommitHash string // The hash of the commit (generated by hashing commit author name, time, the previous commit, and more? TODO: Not sure what else)
TrackedFiles []File // All of the tracked files for this commit
Date string
Version string //User can tag a commit with a version number
}
// CommitMeta stores the meta information about the commit
type CommitMeta struct {
Tag string
Flavour string
PersistLogs bool
Production bool
Virtual bool
}
// File represents a tracked file
type File struct {
ID int `storm:"id,increment"`
Path string `storm:"index"`
Name string
BkpLocation string //TODO: Needed?
CurrentBase string
CurrentHash [16]byte `storm:"index,unique"`
CreatedAt time.Time
Unique string
Version float64
NoCompress bool // Whether or not to compress this file
}
type FileIndex struct {
ID int `storm:"id,increment"`
FileID int `storm:"index"`
FileHash [16]byte `storm:"index,unique"`
Index []byte
Length int64
}
// DiffObject store the information for each diff that is made
type DiffObject struct {
ID int `storm:"id,increment"`
Subject string `storm:"index"`
Object string `storm:"index"`
SubjectHash [16]byte `storm:"index"`
ObjectHash [16]byte `storm:"index"`
Watching string //name of the file being watched
DiffPath string //path of the diff/patch
//Label string //store a comment if the user wants to (user written)
//Screenshot string //path to the screen shot when the diff was made
Fs bool //whether it was written to the directly
Description string //record of forward or backward (just a quick helper)
E error //a record of the error when it was created. Maybe able to optimize out later
//Diff *[]byte //the diff itself (incase we want to store in memory) - unused as of now
DiffSize int64 //the size of the diff in bytes
StartTime time.Time //when was the diff created (can take a while to create)
Message string //any message we want to store against the diff while its created
}