about summary refs log tree commit diff stats
Commit message (Expand)AuthorAgeFilesLines
...
* fizz-buzz exerciseKartik K. Agaram2021-06-061-3/+11
* make fake screens more realisticKartik K. Agaram2021-06-061-9/+33
* shell: concept of palettesKartik K. Agaram2021-06-061-13/+8
* growing disenamored with up and downKartik K. Agaram2021-06-061-8/+5
* .Kartik K. Agaram2021-06-061-14/+24
* handle navigating to a non-existent functionKartik K. Agaram2021-06-061-14/+23
* .Kartik Agaram2021-06-061-0/+250
* .Kartik Agaram2021-06-061-0/+18
* .Kartik Agaram2021-06-062-3/+1
* shell: more foundations from LispKartik K. Agaram2021-06-061-0/+44
* .Kartik K. Agaram2021-06-061-1/+3
* .Kartik K. Agaram2021-06-061-4/+4
* .Kartik K. Agaram2021-06-061-4/+6
* nicer onboarding experience with VimKartik K. Agaram2021-06-062-14/+20
* hacky Vim syntax highlighting for Mu LispKartik K. Agaram2021-06-062-1/+34
* shell: fleshing out the 'standard library'Kartik K. Agaram2021-06-062-1/+11
* .Kartik K. Agaram2021-06-061-1/+1
* shell: support negative integer literalsKartik K. Agaram2021-06-063-1/+133
* shell: remainder operationKartik K. Agaram2021-06-061-1/+65
* .Kartik K. Agaram2021-06-061-3/+3
* more space for definitionsKartik K. Agaram2021-06-063-11/+16
* shell: cool new stress-testKartik K. Agaram2021-06-051-2/+10
* .Kartik K. Agaram2021-06-051-36/+84
* .Kartik Agaram2021-06-0515-9176/+10291
* .Kartik K. Agaram2021-06-051-0/+10
* clean up a large memory leakKartik K. Agaram2021-06-052-16/+7
* .Kartik K. Agaram2021-06-051-16/+12
* shell: moar macrosKartik K. Agaram2021-06-051-41/+42
* .Kartik K. Agaram2021-06-051-6/+2
* refresh edited definitions on ctrl-sKartik K. Agaram2021-06-052-2/+57
* .Kartik K. Agaram2021-06-051-2/+12
* go-to dialog starts with word at cursor by defaultKartik K. Agaram2021-06-051-37/+39
* working on jumping to word at cursorKartik K. Agaram2021-06-053-1/+284
* start editing function definitionsKartik K. Agaram2021-06-042-2/+12
* run sandbox even when cursor in functionKartik K. Agaram2021-06-041-0/+7
* bugfix: show functions after ctrl-g, enterKartik K. Agaram2021-06-041-1/+8
* .Kartik K. Agaram2021-06-041-0/+24
* select function to render firstKartik K. Agaram2021-06-042-1/+11
* always render functions starting at the cursorKartik K. Agaram2021-06-041-2/+4
* record the definition the cursor is currently atKartik K. Agaram2021-06-041-1/+10
* conditionally display cursor on function sideKartik K. Agaram2021-06-042-4/+5
* conditionally display cursor in sandboxKartik K. Agaram2021-06-042-35/+45
* modal dialog for function name to jump toKartik K. Agaram2021-06-042-2/+115
* hacky colors for registersKartik K. Agaram2021-06-041-1/+16
* .Kartik K. Agaram2021-06-042-3/+3
* .Kartik K. Agaram2021-06-041-4/+4
* .Kartik K. Agaram2021-06-041-0/+54
* .Kartik K. Agaram2021-06-042-0/+6
* .Kartik K. Agaram2021-06-042-11/+11
* .Kartik K. Agaram2021-06-042-5/+5
refactored some initialization.' href='/gbmor/getwtxt/commit/svc/db.go?h=v0.4.11&id=fdd9bd02f9ec37d25104dce8c1279a007aa980fc'>fdd9bd0 ^
1a15258 ^
df8fa27 ^
c8fddff ^
c8fddff ^
e9d4a6b ^
df8fa27 ^
c8fddff ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98


















                                                                    
                                                      

        


              
                                             
                               

 








                                                




                      

                                          

                     
                          


                                


                               
                                                         
                                 


                                      
                                 
 
         
 
                    
                

 








                                             

 

                                        
                     
                      
                        

                    

                   
                  

 
                                            
               
                           
                      
                 
                    
                                                                 
 
/*
Copyright (c) 2019 Ben Morrison (gbmor)

This file is part of Getwtxt.

Getwtxt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Getwtxt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Getwtxt.  If not, see <https://www.gnu.org/licenses/>.
*/

package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"log"
	"time"

	"github.com/syndtr/goleveldb/leveldb"
	"golang.org/x/sys/unix"
)

// Everything in this file is database-agnostic.
// Functions and types related to specific kinds
// of databases will be in their own respective
// files, such as:
//  - leveldb.go
//  - sqlite.go

// Abstraction to allow several different
// databases to be used interchangeably.
type dbase interface {
	push() error
	pull()
}

// Opens a new connection to the specified
// database, then reads it into memory.
func initDatabase() {
	var db dbase
	confObj.Mu.RLock()
	dbpath := confObj.DBPath
	confObj.Mu.RUnlock()

	switch confObj.DBType {

	case "leveldb":
		lvl, err := leveldb.OpenFile(dbpath, nil)
		errFatal("", err)
		db = &dbLevel{db: lvl}

	case "sqlite":
		db = initSqlite()

	}

	dbChan <- db
	pullDB()
}

// Close the database connection.
func killDB() {
	db := <-dbChan
	switch dbType := db.(type) {
	case *dbLevel:
		errLog("", dbType.db.Close())
	case *dbSqlite:
		errLog("", dbType.db.Close())
	}
}

// Pushes the registry's cache data
// to a local database for safe keeping.
func pushDB() error {
	db := <-dbChan
	err := db.push()
	dbChan <- db

	unix.Sync()

	return err
}

// Reads the database from disk into memory.
func pullDB() {
	start := time.Now()
	db := <-dbChan
	db.pull()
	dbChan <- db
	log.Printf("Database pull took: %v\n", time.Since(start))
}