-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.go
546 lines (490 loc) · 13.5 KB
/
database.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package rrh
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
)
/*
Remote represents remote of the git repository.
*/
type Remote struct {
Name string `json:"name"`
URL string `json:"url"`
}
func (r *Remote) String() string {
return fmt.Sprintf("%s:%s", r.Name, r.URL)
}
/*
Repository represents a Git repository.
*/
type Repository struct {
ID string `json:"repository_id"`
Path string `json:"repository_path"`
Description string `json:"repository_desc"`
Remotes []*Remote `json:"remotes"`
}
/*
Group represents the groups of the Git repositories.
*/
type Group struct {
Name string `json:"group_name"`
Description string `json:"group_desc"`
OmitList bool `json:"omit_list"`
}
func (r *Repository) Identifier() string {
return r.ID
}
func (g *Group) Identifier() string {
return g.Name
}
/*
Relation represents the relation between group and the repository.
*/
type Relation struct {
RepositoryID string `json:"repository_id"`
GroupName string `json:"group_name"`
}
func (relation *Relation) String() string {
return relation.GroupName + "/" + relation.RepositoryID
}
/*
Database represents the whole database of RRH.
*/
type Database struct {
Timestamp RrhTime `json:"last_modified"`
Repositories []*Repository `json:"repositories"`
Groups []*Group `json:"groups"`
Relations []*Relation `json:"relations"`
Config *Config `json:"-"`
}
func groupFrequencies(db *Database) map[string]int {
var groupMap = map[string]int{}
for _, group := range db.Groups {
groupMap[group.Name] = 0
}
for _, relation := range db.Relations {
groupMap[relation.GroupName] = groupMap[relation.GroupName] + 1
}
return groupMap
}
func pruneGroups(db *Database) []*Group {
var groupMap = groupFrequencies(db)
var prunedGroups = []*Group{}
for _, group := range db.Groups {
if groupMap[group.Name] == 0 {
prunedGroups = append(prunedGroups, group)
}
}
return prunedGroups
}
func repositoryFrequencies(db *Database) map[string]int {
var repoFlags = map[string]int{}
for _, repo := range db.Repositories {
repoFlags[repo.ID] = 0
}
for _, relation := range db.Relations {
repoFlags[relation.RepositoryID] = repoFlags[relation.RepositoryID] + 1
}
return repoFlags
}
func pruneRepositories(db *Database) []*Repository {
var repoFlags = repositoryFrequencies(db)
var repos = []*Repository{}
for _, repo := range db.Repositories {
if repoFlags[repo.ID] == 0 {
repos = append(repos, repo)
}
}
return repos
}
// Prune eliminates unnecessary repositories, and groups from db.
func (db *Database) Prune() ([]*Repository, []*Group) {
var repos, groups = pruneRepositories(db), pruneGroups(db)
for _, repo := range repos {
db.DeleteRepository(repo.ID)
}
for _, group := range groups {
db.DeleteGroup(group.Name)
}
return repos, groups
}
/*
FindRepository returns the repository which ID is given `repoID.`
*/
func (db *Database) FindRepository(repoID string) *Repository {
for _, repo := range db.Repositories {
if repo.ID == repoID {
return repo
}
}
return nil
}
/*
FindGroup returns the group which name is given `groupID.`
*/
func (db *Database) FindGroup(groupID string) *Group {
for _, group := range db.Groups {
if group.Name == groupID {
return group
}
}
return nil
}
func sortIfNeeded(db *Database) {
if !db.Config.IsSet(SortOnUpdating) {
return
}
sort.Slice(db.Repositories, func(i, j int) bool {
return db.Repositories[i].ID < db.Repositories[j].ID
})
sort.Slice(db.Groups, func(i, j int) bool {
return db.Groups[i].Name < db.Groups[j].Name
})
sort.Slice(db.Relations, func(i, j int) bool {
if db.Relations[i].GroupName == db.Relations[j].GroupName {
return db.Relations[i].RepositoryID < db.Relations[j].RepositoryID
}
return db.Relations[i].GroupName < db.Relations[j].GroupName
})
}
/*
CreateRepository returns the repository by creating the given parameters and store it to database.
*/
func (db *Database) CreateRepository(repoID string, path string, desc string, remotes []*Remote) (*Repository, error) {
if db.HasRepository(repoID) {
return nil, fmt.Errorf("%s: already registered repository", repoID)
}
var absPath, err = filepath.Abs(path)
if err != nil {
return nil, err
}
var repo = &Repository{repoID, absPath, desc, remotes}
db.Repositories = append(db.Repositories, repo)
sortIfNeeded(db)
return repo, nil
}
/*
AutoCreateGroup returns the group by creating the given parameters and store it to the database
by satisfying the config value of RrhAutoCreateGroup is true.
*/
func (db *Database) AutoCreateGroup(groupID string, description string, omitList bool) (*Group, error) {
if db.HasGroup(groupID) {
return db.FindGroup(groupID), nil
}
if db.Config.IsSet(AutoCreateGroup) {
return db.CreateGroup(groupID, description, omitList)
}
return nil, fmt.Errorf("%s: could not create group", groupID)
}
/*
CreateGroup returns the group by creating the given parameters and store it to database.
If db has the group with the given groupID, this method returns error.
*/
func (db *Database) CreateGroup(groupID string, description string, omitList bool) (*Group, error) {
if db.HasGroup(groupID) {
return nil, fmt.Errorf("%s: already registered group", groupID)
}
var group = &Group{groupID, description, omitList}
db.Groups = append(db.Groups, group)
sortIfNeeded(db)
return group, nil
}
/*
UpdateGroup updates found group with `newGroupID` and `newDescription`.
The return value is that the update is success or not.
*/
func (db *Database) UpdateGroup(groupID string, newGroup *Group) bool {
if !db.HasGroup(groupID) {
return false
}
for i, group := range db.Groups {
if group.Name == groupID {
db.Groups[i] = newGroup
break
}
}
sortIfNeeded(db)
return true
}
/*
UpdateRepository updates the repository information.
*/
func (db *Database) UpdateRepository(repositoryID string, newRepo Repository) bool {
if !db.HasRepository(repositoryID) {
return false
}
for i, repo := range db.Repositories {
if repo.ID == repositoryID {
updateRepositoryImpl(db, i, newRepo)
}
}
sortIfNeeded(db)
return true
}
func updateRepositoryImpl(db *Database, index int, newRepo Repository) {
var oldID = db.Repositories[index].ID
db.Repositories[index].ID = newRepo.ID
db.Repositories[index].Description = newRepo.Description
db.Repositories[index].Path = newRepo.Path
for i, rel := range db.Relations {
if rel.RepositoryID == oldID {
db.Relations[i].RepositoryID = newRepo.ID
}
}
}
/*
Relate create the relation between the group and the repository.
The group and the repository are specified by the given parameters.
If the group and the repository have the relation, this function returns `nil` (successfully create relation).
*/
func (db *Database) Relate(groupID string, repoID string) error {
if db.HasRelation(groupID, repoID) {
return nil
}
db.Relations = append(db.Relations, &Relation{repoID, groupID})
sortIfNeeded(db)
return nil
}
/*
BelongingCount returns the number of groups belonging given repoID.
*/
func (db *Database) BelongingCount(repoID string) int {
var repos = repositoryFrequencies(db)
return repos[repoID]
}
/*
ContainsCount returns the number of repositories in the given groupID.
*/
func (db *Database) ContainsCount(groupID string) int {
var groups = groupFrequencies(db)
return groups[groupID]
}
/*
FindRelationsOfGroup returns the repository ids belonging to the specified group.
*/
func (db *Database) FindRelationsOfGroup(groupID string) []string {
var repositories = []string{}
for _, relation := range db.Relations {
if relation.GroupName == groupID {
repositories = append(repositories, relation.RepositoryID)
}
}
return repositories
}
/*
FindRelationsOfRepository returns the group names of the specified repository.
*/
func (db *Database) FindRelationsOfRepository(repositoryID string) []string {
var groups = []string{}
for _, relation := range db.Relations {
if relation.RepositoryID == repositoryID {
groups = append(groups, relation.GroupName)
}
}
return groups
}
/*
HasRelation returns true if the group and the repository has relation.
The group and the repository are specified by the given parameters.
*/
func (db *Database) HasRelation(groupID string, repoID string) bool {
for _, relation := range db.Relations {
if relation.GroupName == groupID && relation.RepositoryID == repoID {
return true
}
}
return false
}
/*
Unrelate deletes the relation between the group and the repository.
The group and the repository are specified by the given parameters.
If the group and the repository do not have the relation, this function returns `nil` (successfully delete relation).
*/
func (db *Database) Unrelate(groupID string, repoID string) {
if !db.HasRelation(groupID, repoID) {
return
}
var newRelations = []*Relation{}
for _, relation := range db.Relations {
if !(relation.GroupName == groupID && relation.RepositoryID == repoID) {
newRelations = append(newRelations, relation)
}
}
db.Relations = newRelations
}
/*
UnrelateRepository deletes all relations of the specified repository.
*/
func (db *Database) UnrelateRepository(repoID string) {
var newRelations = []*Relation{}
for _, relation := range db.Relations {
if relation.RepositoryID != repoID {
newRelations = append(newRelations, relation)
}
}
db.Relations = newRelations
}
/*
UnrelateFromGroup deletes all relations about the specified group.
*/
func (db *Database) UnrelateFromGroup(groupID string) {
var newRelations = []*Relation{}
for _, relation := range db.Relations {
if relation.GroupName != groupID {
newRelations = append(newRelations, relation)
}
}
db.Relations = newRelations
}
/*
HasRepository returns true if the db has the repository of repoID.
*/
func (db *Database) HasRepository(repoID string) bool {
for _, repo := range db.Repositories {
if repo.ID == repoID {
return true
}
}
return false
}
/*
HasGroup returns true if the db has the group of groupID.
*/
func (db *Database) HasGroup(groupID string) bool {
for _, group := range db.Groups {
if group.Name == groupID {
return true
}
}
return false
}
/*
DeleteRepository function removes the repository of given repoID from DB.
Also, the relation between the repository and groups are removed.
*/
func (db *Database) DeleteRepository(repoID string) error {
if !db.HasRepository(repoID) {
return fmt.Errorf("%s: repository not found", repoID)
}
db.UnrelateRepository(repoID)
var newRepositories = []*Repository{}
for _, repo := range db.Repositories {
if repo.ID != repoID {
newRepositories = append(newRepositories, repo)
}
}
db.Repositories = newRepositories
return nil
}
func deleteGroup(db *Database, groupID string) error {
var groups = []*Group{}
for _, group := range db.Groups {
if group.Name != groupID {
groups = append(groups, group)
}
}
db.Groups = groups
return nil
}
/*
DeleteGroup removes the group of the given groupId from DB.
If the group has some repositories, the function fails to remove.
*/
func (db *Database) DeleteGroup(groupID string) error {
if !db.HasGroup(groupID) {
return fmt.Errorf("%s: group not found", groupID)
}
var groups = groupFrequencies(db)
if groups[groupID] != 0 {
return fmt.Errorf("%s: group has %d relatins", groupID, groups[groupID])
}
return deleteGroup(db, groupID)
}
/*
ForceDeleteGroup removes the group of the given groupID from DB.
Even if the group has some repositories, the function forcely remove the group.
*/
func (db *Database) ForceDeleteGroup(groupID string) error {
if !db.HasGroup(groupID) {
return fmt.Errorf("%s: group not found", groupID)
}
db.UnrelateFromGroup(groupID)
return deleteGroup(db, groupID)
}
func databasePath(config *Config) string {
return config.GetValue(DatabasePath)
}
/*
StoreAndClose stores the database to file and close the database.
The database path is defined in RRH_DATABASE_PATH of config.
*/
func (db *Database) StoreAndClose() error {
db.Timestamp = Now()
var bytes, err = json.Marshal(db)
if err != nil {
return err
}
var databasePath = databasePath(db.Config)
var err1 = CreateParentDir(databasePath)
if err1 != nil {
return err1
}
return ioutil.WriteFile(databasePath, bytes, 0644)
}
/*
Open function is to read rrh database from a certain path.
How to call this function
var db *Database
db = common.Open()
*/
func Open(config *Config) (*Database, error) {
bytes, err := ioutil.ReadFile(databasePath(config))
var db = Database{Unix(0, 0), []*Repository{}, []*Group{}, []*Relation{}, config}
if err != nil {
return &db, nil
}
if err := json.Unmarshal(bytes, &db); err != nil {
return nil, err
}
db.Config = config
return &db, nil
}
/*
FindTargets returns instances of Relation objects with given groupNames.
*/
func FindTargets(db *Database, groupNames []string) []Relation {
var result = []Relation{}
for _, groupName := range groupNames {
var repos = db.FindRelationsOfGroup(groupName)
var relations = toRelations(groupName, repos)
result = append(result, relations...)
}
return eliminateDuplication(result)
}
func foundIn(relation Relation, list []Relation) bool {
for _, r := range list {
if r.GroupName == relation.GroupName &&
r.RepositoryID == relation.RepositoryID {
return true
}
}
return false
}
func eliminateDuplication(relations []Relation) []Relation {
var result = []Relation{}
for _, relation := range relations {
if !foundIn(relation, result) {
result = append(result, relation)
}
}
return result
}
func toRelations(groupName string, repoNames []string) []Relation {
var result = []Relation{}
for _, repo := range repoNames {
result = append(result, Relation{RepositoryID: repo, GroupName: groupName})
}
return result
}