-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcurrentPersistentPermanentURLMap.go
114 lines (95 loc) · 4.08 KB
/
ConcurrentPersistentPermanentURLMap.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
// API:
// 1. PutEntry(long_url, expiry_date) -> (short_url, err)
// 2. GetEntry(short_url) -> (long_url, err)
// 3. CreateConcurrentPersistentPermanentURLMapFromDisk(expiration_check)
package util
import (
"log"
"sync"
"time"
)
type ConcurrentPersistentPermanentURLMap struct {
mut sync.Mutex
slice_map map[int]*RandomBag64
urlmap *ConcurrentPermanentMap
b53m *Base53IDManager
lsps *LogStructuredPermanentStorage
pbs *PermanentBucketStorage
generate_strings_up_to int
map_size_persister *MapSizeFileManager
}
func (manager *ConcurrentPersistentPermanentURLMap) PrintInternalState() {
manager.mut.Lock()
defer manager.mut.Unlock()
log.Println(" ============ Printing CPPUM internal state ===========")
log.Println("Printing slice_maps:")
for k, v := range manager.slice_map {
log.Println("k,v:", k, *v)
}
log.Println(manager.urlmap)
log.Println(" ------------------------------------------------------")
}
func (manager *ConcurrentPersistentPermanentURLMap) NumItems() int { //nolint:ireturn // is ok
manager.mut.Lock()
defer manager.mut.Unlock()
return manager.urlmap.NumItems()
}
func (manager *ConcurrentPersistentPermanentURLMap) NumPastes() int { //nolint:ireturn // is ok
// No need for lock here.
return manager.urlmap.NumPastes()
}
func (manager *ConcurrentPersistentPermanentURLMap) GetEntry(short_url string) (MapItem, error) { //nolint:ireturn //this is ok
manager.mut.Lock()
defer manager.mut.Unlock()
val, err := GetEntryCommon(manager.urlmap, short_url)
return val, err
}
// Shorten long URL into short URL and return the short URL and store the entry both in map and on disk
func (manager *ConcurrentPersistentPermanentURLMap) PutEntry(requested_length int, long_url string, _ int64, value_type MapItemValueType) (string, error) {
manager.mut.Lock()
defer manager.mut.Unlock()
cur_unix_timestamp := time.Now().Unix()
val, err := PutEntry_Common(requested_length, long_url, value_type, cur_unix_timestamp, manager.generate_strings_up_to, manager.slice_map, manager.urlmap, manager.b53m, manager.lsps, manager.pbs, manager.map_size_persister)
return val, err
}
type CPPUMParams struct {
Log_directory_path_absolute string
Bucket_directory_path_absolute string
B53m *Base53IDManager
Generate_strings_up_to int
Log_file_max_size_bytes int64
Size_file_rounded_multiple int64
Size_file_path_absolute string
}
// This is the one you want to use in production
func CreateConcurrentPersistentPermanentURLMapFromDisk(cppum_params *CPPUMParams) *ConcurrentPersistentPermanentURLMap {
slice_storage := make(map[int]*RandomBag64)
lsps := NewLogStructuredPermanentStorage(cppum_params.Log_file_max_size_bytes, cppum_params.Log_directory_path_absolute)
pbs := NewPermanentBucketStorage(cppum_params.Bucket_directory_path_absolute)
var nil_map_ptr *ConcurrentPermanentMap = nil
// Now load from each file into the map
params := LSRFD_Params{
B53m: cppum_params.B53m,
Log_directory_path_absolute: cppum_params.Log_directory_path_absolute,
Entry_should_be_deleted_fn: nil,
Lss: lsps,
Expiry_callback: nil,
Slice_storage: slice_storage,
Nil_ptr: nil_map_ptr,
Size_file_rounded_multiple: cppum_params.Size_file_rounded_multiple,
Generate_strings_up_to: cppum_params.Generate_strings_up_to,
Size_file_path_absolute: cppum_params.Size_file_path_absolute,
}
concurrent_map, map_size_persister := LoadStoredRecordsFromDisk(¶ms)
manager := ConcurrentPersistentPermanentURLMap{ //nolint:forcetypeassert // it's okay. Just let it crash.
mut: sync.Mutex{},
slice_map: slice_storage,
urlmap: concurrent_map.(*ConcurrentPermanentMap),
b53m: cppum_params.B53m,
lsps: lsps,
pbs: pbs,
generate_strings_up_to: cppum_params.Generate_strings_up_to,
map_size_persister: map_size_persister,
}
return &manager
}