Class: Mmapper::File
- Inherits:
-
Object
- Object
- Mmapper::File
- Defined in:
- lib/mmapper.rb,
ext/mmapper.c
Instance Method Summary collapse
- #find_matching_line(prefix) ⇒ Object
- #initialize(path) ⇒ Object constructor
- #read(offset_val, length_val) ⇒ Object
- #size ⇒ Object
- #write(offset_val, str_val) ⇒ Object
Constructor Details
#initialize(path) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'ext/mmapper.c', line 34
static VALUE mmap_file_initialize(VALUE self, VALUE path) {
mmap_file *m;
const char *c_path = StringValueCStr(path);
Data_Get_Struct(self, mmap_file, m);
m->fd = open(c_path, O_RDWR);
if (m->fd < 0)
rb_sys_fail("open");
m->size = lseek(m->fd, 0, SEEK_END);
lseek(m->fd, 0, SEEK_SET);
m->map = mmap(NULL, m->size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, 0);
if (m->map == MAP_FAILED)
rb_sys_fail("mmap");
return self;
}
|
Instance Method Details
#find_matching_line(prefix) ⇒ Object
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 |
# File 'lib/mmapper.rb', line 5 def find_matching_line(prefix) low = 0 high = size while low < high mid = (low + high) / 2 line_start = find_line_start(mid) line = read_line_at(line_start) return nil if line.nil? if line < prefix low = mid + 1 else high = mid end end final_line_start = find_line_start(low) line = read_line_at(final_line_start) if line&.start_with?(prefix) line else nil end end |
#read(offset_val, length_val) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'ext/mmapper.c', line 53
static VALUE mmap_file_read(VALUE self, VALUE offset_val, VALUE length_val) {
mmap_file *m;
Data_Get_Struct(self, mmap_file, m);
long offset = NUM2LONG(offset_val);
long length = NUM2LONG(length_val);
if (offset < 0 || offset + length > (long)m->size)
rb_raise(rb_eArgError, "read out of bounds");
return rb_str_new((char *)m->map + offset, length);
}
|
#size ⇒ Object
83 84 85 86 87 |
# File 'ext/mmapper.c', line 83
static VALUE mmap_file_size(VALUE self) {
mmap_file *m;
Data_Get_Struct(self, mmap_file, m);
return LONG2NUM(m->size);
}
|
#write(offset_val, str_val) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'ext/mmapper.c', line 66
static VALUE mmap_file_write(VALUE self, VALUE offset_val, VALUE str_val) {
mmap_file *m;
Data_Get_Struct(self, mmap_file, m);
long offset = NUM2LONG(offset_val);
StringValue(str_val);
long len = RSTRING_LEN(str_val);
if (offset < 0 || offset + len > (long)m->size)
rb_raise(rb_eArgError, "write out of bounds");
memcpy((char *)m->map + offset, RSTRING_PTR(str_val), len);
msync(m->map, m->size, MS_SYNC);
return Qtrue;
}
|