using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; namespace SoyText { public enum FoundHandlerAction { End, Accept, Ignore }; public delegate FoundHandlerAction FoundHandler(Document d); public class DocumentSet { public int maxHandle = -1; internal Workspace workspace; DocumentList cache=null; public String stateDir(DocumentState state) { return workspace.home + "/document/" + state; } public String getFileName(int handle, DocumentState state) { return stateDir(state) + "/" + handle + ".txt"; } public DocumentSet(Workspace w) { workspace = w; } public static int cnt=0; public Document createDocumentWithNewHandle() { return createDocument(createHandle()); } public Document createDocument() { var d = new Document(this); cnt++; //if (cnt >= 2) throw new Exception("cnt over " + cnt); return addToCache(d); } public Document createDocument(int handle) { var d=new Document(this, handle); return addToCache(d); } private Document addToCache(Document d) { if (cache == null) cache = new DocumentList(); lock (cache) { cache.Add(-d.uniqueLastUpdate, d); } return d; } public int byRecency(Document d1, Document d2) { long d = d1.lastUpdate - d2.lastUpdate; return (d == 0 ? 0 : (d < 0 ? 1 : -1)); } public DocumentList getRecents() { if (cache != null) return cache; Debug.print("文書一覧取得中..."); //var res = new DocumentList(); searchMax(DocumentState.active, true); searchMax(DocumentState.archived, false); searchMax(DocumentState.indexed,true); if (cache == null) cache = new DocumentList(); Debug.print("文書一覧並べ替え中..."); //res.Sort(byRecency); Debug.print("END: 文書一覧並べ替え中..."); return cache; } Dictionary docCount; private void searchMax(DocumentState s, bool add) { foreach (string fileName in Mkdir.getFiles(stateDir(s))) { var m = Regex.Match(fileName, @"(\d+).txt$"); if (m.Success) { var r = m.Groups[1].Value; var handle = Int32.Parse(r); if (handle > maxHandle) maxHandle = handle; if (add) createDocument(handle); } } } public int createHandle() { if (maxHandle < 0) getRecents(); maxHandle++; return maxHandle; } public DocumentSercher createDocumentSearcher(String kw, FoundHandler f) { var s = new DocumentSercher(); s.searchCondition = SearchCondition.fromExpression(kw); s.foundHandler = f; s.documentSet = this; return s; } public SortedList search(String kw, FoundHandler f) { var s = createDocumentSearcher(kw, f); return s.search(); } private DocumentList exactSearch(Token t) { var res = new DocumentList(); if (!File.Exists(t.documentPath)) return res; foreach (var ln in File.ReadAllLines(t.documentPath)) { var flds = ln.Split('\t'); if (flds[0].Equals("")) continue; var handle = Int32.Parse(flds[0]); if (File.Exists(getFileName(handle, DocumentState.indexed))) { var d = new Document(this, handle); res.Add(d.uniqueLastUpdate, d); //res.Add(createDocument(handle)); } } return res; } public Document getOldestActiveDocument() { var r = getRecents(); return r[0]; } } }