forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
executable file
·102 lines (72 loc) · 2.87 KB
/
entry.go
File metadata and controls
executable file
·102 lines (72 loc) · 2.87 KB
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
package ldb
import (
"encoding/binary"
"github.com/FactomProject/FactomCode/notaryapi"
"github.com/conformal/goleveldb/leveldb"
"log"
)
// InsertEntry inserts an entry and put it in queue
func (db *LevelDb) InsertEntryAndQueue(entrySha *notaryapi.Hash, binaryEntry *[]byte, entry *notaryapi.Entry, chainID *[]byte) (err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
var entryKey [] byte = []byte{byte(TBL_ENTRY)}
entryKey = append (entryKey, entrySha.Bytes ...)
db.lbatch.Put(entryKey, *binaryEntry)
//EntryQueue format: Table Name (1 bytes) + Chain Type (4 bytes) + Timestamp (8 bytes) + Entry Hash (32 bytes)
var key [] byte = []byte{byte(TBL_ENTRY_QUEUE)} // Table Name (1 bytes)
key = append(key, *chainID ...) // Chain id (32 bytes)
binaryTimestamp := make([]byte, 8)
binary.BigEndian.PutUint64(binaryTimestamp, uint64(entry.TimeStamp()))
key = append(key, binaryTimestamp ...) // Timestamp (8 bytes)
key = append(key, entrySha.Bytes ...) // Entry Hash (32 bytes)
db.lbatch.Put(key, []byte{byte(STATUS_IN_QUEUE)})
err = db.lDb.Write(db.lbatch, db.wo)
if err != nil {
log.Println("batch failed %v\n", err)
return err
}
return nil
}
// FetchEntry gets an entry by hash from the database.
func (db *LevelDb) FetchEntryByHash(entrySha *notaryapi.Hash) (entry *notaryapi.Entry, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key [] byte = []byte{byte(TBL_ENTRY)}
key = append (key, entrySha.Bytes ...)
data, err := db.lDb.Get(key, db.ro)
if data != nil{
entry = new (notaryapi.Entry)
entry.UnmarshalBinary(data)
}
return entry, nil
}
// FetchEntryInfoBranchByHash gets an EntryInfoBranch obj
func (db *LevelDb) FetchEntryInfoBranchByHash(entryHash *notaryapi.Hash) (entryInfoBranch *notaryapi.EntryInfoBranch, err error) {
entryInfoBranch = new (notaryapi.EntryInfoBranch)
entryInfoBranch.EntryHash = entryHash
entryInfoBranch.EntryInfo, _ = db.FetchEntryInfoByHash(entryHash)
if entryInfoBranch.EntryInfo != nil{
entryInfoBranch.EBInfo, _ = db.FetchEBInfoByHash(entryInfoBranch.EntryInfo.EBHash)
}
if entryInfoBranch.EBInfo != nil{
entryInfoBranch.DBBatch, _ = db.FetchDBBatchByHash(entryInfoBranch.EBInfo.DBHash)
}
return entryInfoBranch, nil
}
// FetchEntryInfoBranchByHash gets an EntryInfo obj
func (db *LevelDb) FetchEntryInfoByHash(entryHash *notaryapi.Hash) (entryInfo *notaryapi.EntryInfo, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key [] byte = []byte{byte(TBL_ENTRY_INFO)}
key = append (key, entryHash.Bytes ...)
data, err := db.lDb.Get(key, db.ro)
if data != nil{
entryInfo = new (notaryapi.EntryInfo)
entryInfo.UnmarshalBinary(data)
}
return entryInfo, nil
}