prometheus-client-mmap 0.7.0.beta27 → 0.7.0.beta28
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37b02cfd13e165809e2772ab5af3d8e53ad59fa0
|
4
|
+
data.tar.gz: 2b9778993828942aef818d0ba62e690c99a7c456
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43fc00711b83898298d7d60e811636bca584289e39f81ee5a82c94041596bc214265b7fefb0b152adfa14ed5176db1fd0e009069eb2ca5c626e6fa84a5e3e95b
|
7
|
+
data.tar.gz: d2019871acee358cf7d6f23f6514dbae33578e55a7e58237f698e334a64c5d9c519b8a2a83ff037aec435b72854a92a435cac3e72fb8a61158ee96049a62d934
|
@@ -1,8 +1,12 @@
|
|
1
1
|
#include <ruby.h>
|
2
|
+
#include <ruby/intern.h>
|
2
3
|
#include "mmap.h"
|
3
4
|
|
4
5
|
VALUE MMAPED_FILE = Qnil;
|
5
6
|
|
7
|
+
#define START_POSITION 8
|
8
|
+
|
9
|
+
VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
6
10
|
|
7
11
|
VALUE method_get_double(VALUE self, VALUE index) {
|
8
12
|
mm_ipc *i_mm;
|
@@ -21,7 +25,71 @@ VALUE method_get_double(VALUE self, VALUE index) {
|
|
21
25
|
return DBL2NUM(tmp);
|
22
26
|
}
|
23
27
|
|
28
|
+
VALUE method_add_entry(VALUE self, VALUE positions, VALUE key, VALUE value) {
|
29
|
+
Check_Type(positions, T_HASH);
|
30
|
+
Check_Type(key, T_STRING);
|
31
|
+
VALUE position = rb_hash_lookup(positions, key);
|
32
|
+
if (position != Qnil){
|
33
|
+
return position;
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
// Check_Type(value, T_FLOAT); //TODO: will float catch DOUBLE ?
|
38
|
+
|
39
|
+
mm_ipc *i_mm;
|
40
|
+
GetMmap(self, i_mm, MM_MODIFY);
|
41
|
+
|
42
|
+
if ((i_mm->t->real) < sizeof(int32_t)) {
|
43
|
+
rb_raise(rb_eRuntimeError, "mmaped file too small %ld", i_mm->t->real);
|
44
|
+
}
|
45
|
+
|
46
|
+
uint32_t used = 0;
|
47
|
+
|
48
|
+
memcpy(&used, (char *)i_mm->t->addr, sizeof(uint32_t));
|
49
|
+
|
50
|
+
if (used == 0){
|
51
|
+
used = START_POSITION;
|
52
|
+
}
|
53
|
+
|
54
|
+
long slen = RSTRING_LEN(key);
|
55
|
+
uint32_t padding_length = 8 - (slen + 4) % 8; // padding | 8 byte aligned
|
56
|
+
//TODO: check slen if its less than uint32 throw exception otherwise
|
57
|
+
uint32_t entry_length = (uint32_t)slen;
|
58
|
+
uint32_t new_used = used + sizeof(uint32_t) + entry_length + padding_length + sizeof(double);
|
59
|
+
|
60
|
+
if (i_mm->t->real < new_used) {
|
61
|
+
rb_raise(rb_eRuntimeError, "mmaped file too small %ld", i_mm->t->real);
|
62
|
+
}
|
63
|
+
|
64
|
+
//TODO: add checks if mmap can be written to
|
65
|
+
|
66
|
+
char *pos = (char *)i_mm->t->addr + used;
|
67
|
+
// rb_raise(rb_eRuntimeError, "ugh %d %ld %u ", used, i_mm->t->real, entry_length);
|
68
|
+
|
69
|
+
memcpy(pos, &entry_length, sizeof(uint32_t));
|
70
|
+
pos += sizeof(uint32_t);
|
71
|
+
|
72
|
+
memmove(pos, StringValuePtr(key), slen);
|
73
|
+
pos += slen;
|
74
|
+
|
75
|
+
memset(pos, ' ', padding_length);
|
76
|
+
pos += padding_length;
|
77
|
+
|
78
|
+
double val = NUM2DBL(value);
|
79
|
+
memcpy(pos, &val, sizeof(double));
|
80
|
+
|
81
|
+
|
82
|
+
position = INT2NUM(new_used - 8);
|
83
|
+
rb_hash_aset(positions, key, position);
|
84
|
+
|
85
|
+
memcpy((char *)i_mm->t->addr, &new_used, sizeof(uint32_t));
|
86
|
+
|
87
|
+
return position;
|
88
|
+
}
|
89
|
+
|
90
|
+
|
24
91
|
void Init_fast_mmaped_file() {
|
25
92
|
MMAPED_FILE = rb_define_module("FastMmapedFile");
|
26
93
|
rb_define_method(MMAPED_FILE, "get_double", method_get_double, 1);
|
94
|
+
rb_define_method(MMAPED_FILE, "add_entry", method_add_entry, 3);
|
27
95
|
}
|
data/lib/fast_mmaped_file.bundle
CHANGED
Binary file
|
@@ -30,8 +30,6 @@ module Prometheus
|
|
30
30
|
@m = m
|
31
31
|
# @m.mlock # TODO: Ensure memory is locked to RAM
|
32
32
|
|
33
|
-
@used = @m.used
|
34
|
-
|
35
33
|
@positions = {}
|
36
34
|
read_all_positions.each do |key, pos|
|
37
35
|
@positions[key] = pos
|
@@ -66,23 +64,21 @@ module Prometheus
|
|
66
64
|
|
67
65
|
private
|
68
66
|
|
69
|
-
def
|
70
|
-
@
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
size = @m.size
|
76
|
-
@m.close
|
77
|
-
|
78
|
-
File.truncate(filepath, size * 2)
|
79
|
-
@m = Helper::MmapedFile.open(filepath)
|
80
|
-
end
|
67
|
+
def ok(key)
|
68
|
+
@m.add_entry(@positions, key, 0.0)
|
69
|
+
rescue RuntimeError => e
|
70
|
+
raise e if e.message.match /.*ugh.*/
|
71
|
+
nil
|
72
|
+
end
|
81
73
|
|
82
|
-
|
83
|
-
|
74
|
+
def init_value(key)
|
75
|
+
until ok(key)
|
76
|
+
filepath = @m.filepath
|
77
|
+
size = @m.size
|
78
|
+
@m.close
|
84
79
|
|
85
|
-
|
80
|
+
File.truncate(filepath, size * 2)
|
81
|
+
@m = Helper::MmapedFile.open(filepath)
|
86
82
|
end
|
87
83
|
end
|
88
84
|
|