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
use std::collections::HashSet;
use std::cmp::Ordering;

use indexed_data_file::*;
use strings_set_file::*;

pub mod entity;
use entity::FieldEntity;

pub struct Field{
    index: IndexedDataFile<FieldEntity>
    ,strings:StringsSetFile
}
impl Field{
    pub fn new(path_prefix:&str) -> Result<Field,std::io::Error>{
        match IndexedDataFile::new(&(path_prefix.to_string()+".i")){
            Err(e)=>Err(e)
            ,Ok(index)=>{
                match StringsSetFile::new(&(path_prefix.to_string()+".d")){
                    Ok(strings)=>Ok(Field{
                        index
                        ,strings
                    })
                    ,Err(e)=>Err(e)
                }
            }
        }
    }
    pub fn entity<'a>(&self,id:u32)->Option<&'a FieldEntity>{
        if let Some(v)=self.index.triee().entity_value(id){
            Some(&v)
        }else{
            None
        }
    }
    pub fn string<'a>(&self,id:u32)->Option<&'a str>{
        if let Some(e)=self.entity(id){
            Some(unsafe{std::ffi::CStr::from_ptr(
                self.strings.offset(e.addr()) as *const libc::c_char
            )}.to_str().unwrap())
        }else{
            None
        }
    }
    pub fn num(&self,id:u32)->Option<f64>{
        if let Some(e)=self.entity(id){
            Some(e.num())
        }else{
            None
        }
    }
    pub fn search_match(&self,str:&str,and:Option<IdSet>)->IdSet{
        let mut r:IdSet=HashSet::default();
        let tree=self.index.triee();
        let (ord,found_id)=tree.search_cb(|data|->Ordering{
            let str2=unsafe{
                std::ffi::CStr::from_ptr(
                    self.strings.offset(data.addr()) as *const libc::c_char
                )
            }.to_str().unwrap();
            if str==str2{
                Ordering::Equal
            }else{
                natord::compare(str,str2)
            }
        });
        let found_id_i64=found_id as i64;
        if let Some(and)=and{
            if ord==Ordering::Equal{
                if and.contains(&found_id_i64){
                    r.insert(found_id_i64);
                }
                tree.sames_and(&mut r,&and, found_id);
            }
        }else{
            if ord==Ordering::Equal{
                r.insert(found_id_i64);
                tree.sames(&mut r, found_id);
            }
        }
        r
    }
    
    pub fn update(&mut self,id:u32,addr:*const i8) -> Option<u32>{
        //まずは消す(指定したidのデータが無い場合はスルーされる)
        if let RemoveResult::Unique(data)=self.index.delete(id){
            self.strings.remove(&data.string());    //削除対象がユニークの場合は対象文字列を完全削除
        }
        let cont=unsafe{
            std::ffi::CStr::from_ptr(addr)
        }.to_str().unwrap();
        let tree=self.index.triee();
        let (ord,found_id)=tree.search_cb(|data|->Ordering{
            let str2=unsafe{
                std::ffi::CStr::from_ptr(
                    self.strings.offset(data.addr()) as *const libc::c_char
                )
            }.to_str().unwrap();
            if cont==str2{
                Ordering::Equal
            }else{
                natord::compare(cont,str2)
            }
        });
        if ord==Ordering::Equal && found_id!=0{
            if let Some(_node)=self.index.triee().node(id){
                //すでにデータがある場合
                self.index.triee_mut().update_same(found_id,id);
                Some(id)
            }else{
                self.index.insert_same(found_id)
            }
        }else{
            //新しく作る
            if let Some(word)=self.strings.insert(cont){
                let e=FieldEntity::new(
                    word.address()
                    ,cont.parse().unwrap_or(0.0)
                );
                if let Some(_entity)=self.index.triee().node(id){
                    //既存データの更新処理
                    self.index.triee_mut().update_node(
                        found_id
                        ,id
                        ,e
                        ,ord
                    );
                    Some(id)
                }else{
                    //追加
                    self.index.insert_unique(e,found_id,ord)
                }
            }else{
                None
            }
        }
    }
    pub fn delete(&mut self,id:u32){
        self.index.delete(id);
    }
}